Skip to contentSkip to conversation

lib: utils/i2c: update i2c interface

PATCH Superseded Xiang W 3 participants 5 messages Dec 30, 2021 – Jan 3, 2022 SiFive drivers

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Xiang W

author
#0 lore raw

PATCH v2 1/1 lib: utils/i2c: update i2c interface

1. Remove i2c register related operations
2. Simplify the low-level interface
3. Add 10bit address support
4. Add combination operation
5. Update fdt_i2c_sifive.c
6. Update sifive_fu740.c

---
Changes since v1:

* Simplify fdt_i2c_sifive.c
* Add struct i2c_msg, modify the interface

 include/sbi_utils/i2c/i2c.h     |  65 ++++++---------
 lib/utils/i2c/fdt_i2c_sifive.c  | 136 +++++++-------------------------
 lib/utils/i2c/i2c.c             |  70 +++++++++++-----
 platform/generic/sifive_fu740.c |  52 ++++++++++--
 4 files changed, 148 insertions(+), 175 deletions(-)
include/sbi_utils/i2c/i2c.h +25 -40
index 5a70364..307fa55 100644
@@ -13,6 +13,21 @@
 #include <sbi/sbi_types.h>
 #include <sbi/sbi_list.h>
 
+struct i2c_msg {
+	uint16_t addr;
+	uint16_t flags;
+#define I2C_M_RD		0x0001
+#define I2C_M_TEN		0x0010
+#define I2C_M_RECV_LEN		0x0400 /* Fixup: unclear meaning, currently unused */
+#define I2C_M_NO_RD_ACK		0x0800 /* Fixup: unclear meaning, currently unused */
+#define I2C_M_IGNORE_NAK	0x1000 /* Fixup: unclear meaning, currently unused */
+#define I2C_M_REV_DIR_ADDR	0x2000 /* Fixup: unclear meaning, currently unused */
+#define I2C_M_NOSTART		0x4000
+#define I2C_M_STOP		0x8000
+	uint16_t len;
+	uint8_t *buf;
+};
+
 /** Representation of a I2C adapter */
 struct i2c_adapter {
 	/** Pointer to I2C driver owning this I2C adapter */
@@ -21,21 +36,11 @@ struct i2c_adapter {
 	/** Unique ID of the I2C adapter assigned by the driver */
 	int id;
 
-	/**
-	 * Send buffer to given address, register
-	 *
-	 * @return 0 on success and negative error code on failure
-	 */
-	int (*write)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		    uint8_t *buffer, int len);
+	int (*write_byte)(struct i2c_adapter *ia, uint8_t byte,
+			bool send_start, bool send_stop);
 
-	/**
-	 * Read buffer from given address, register
-	 *
-	 * @return 0 on success and negative error code on failure
-	 */
-	int (*read)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		    uint8_t *buffer, int len);
+	int (*read_byte)(struct i2c_adapter *ia, uint8_t *byte,
+			bool nack, bool send_stop);
 
 	/** List */
 	struct sbi_dlist node;
@@ -55,31 +60,11 @@ int i2c_adapter_add(struct i2c_adapter *ia);
 /** Un-register I2C adapter */
 void i2c_adapter_remove(struct i2c_adapter *ia);
 
-/** Send to device on I2C adapter bus */
-int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		      uint8_t *buffer, int len);
-
-/** Read from device on I2C adapter bus */
-int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		     uint8_t *buffer, int len);
-
-static inline int i2c_adapter_reg_write(struct i2c_adapter *ia, uint8_t addr,
-				 uint8_t reg, uint8_t val)
-{
-	return i2c_adapter_write(ia, addr, reg, &val, 1);
-}
-
-static inline int i2c_adapter_reg_read(struct i2c_adapter *ia, uint8_t addr,
-				       uint8_t reg, uint8_t *val)
-{
-	uint8_t buf;
-	int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
-
-	if (ret)
-		return ret;
-
-	*val = buf;
-	return 0;
-}
+/**
+ * once  message transmission
+ * It can be a complete read or write
+ * can also be used in combination to form a combined transmission
+ */
+int i2c_adapter_trans(struct i2c_adapter *ia, struct i2c_msg *msg);
 
 #endif
lib/utils/i2c/fdt_i2c_sifive.c +28 -108

175 lines not on this page · raw message

lib/utils/i2c/i2c.c +51 -19

83 lines not on this page · raw message

platform/generic/sifive_fu740.c +44 -8

102 lines not on this page · raw message

Signature · 1 line
2.30.2
author
#1 lore raw

PATCH v2 1/1 lib: utils/i2c: update i2c interface

? 2021-12-30???? 22:25 +0800?Xiang W???

Quoted 8 lines

1. Remove i2c register related operations
2. Simplify the low-level interface
3. Add 10bit address support
4. Add combination operation
5. Update fdt_i2c_sifive.c
6. Update sifive_fu740.c

I have performed 10 reboot on my unmatched, and it succeeded five times and failed five times. Are we missing some timing issues?

Regards,
Xiang W

Quoted 532 lines

---
Changes since v1:

* Simplify fdt_i2c_sifive.c
* Add struct i2c_msg, modify the interface

?include/sbi_utils/i2c/i2c.h???? |? 65 ++++++--------- ?lib/utils/i2c/fdt_i2c_sifive.c? | 136 +++++++------------------------ -
?lib/utils/i2c/i2c.c???????????? |? 70 +++++++++++----- ?platform/generic/sifive_fu740.c |? 52 ++++++++++--
?4 files changed, 148 insertions(+), 175 deletions(-)

include/sbi_utils/i2c/i2c.h +0 -0
index 5a70364..307fa55 100644
@@ -13,6 +13,21 @@

?#include <sbi/sbi_types.h>
?#include <sbi/sbi_list.h>
?

include/sbi_utils/i2c/i2c.h +6 -0
+struct i2c_msg {
+???????uint16_t addr;
+???????uint16_t flags;
+#define I2C_M_RD???????????????0x0001
+#define I2C_M_TEN??????????????0x0010
+#define I2C_M_RECV_LEN?????????0x0400 /* Fixup: unclear meaning,

currently unused */
+#define I2C_M_NO_RD_ACK????????????????0x0800 /* Fixup: unclear meaning, currently unused */
+#define I2C_M_IGNORE_NAK???????0x1000 /* Fixup: unclear meaning, currently unused */
+#define I2C_M_REV_DIR_ADDR?????0x2000 /* Fixup: unclear meaning, currently unused */

include/sbi_utils/i2c/i2c.h +6 -0
+#define I2C_M_NOSTART??????????0x4000
+#define I2C_M_STOP?????????????0x8000
+???????uint16_t len;
+???????uint8_t *buf;
+};
+

?/** Representation of a I2C adapter */
?struct i2c_adapter {
????????/** Pointer to I2C driver owning this I2C adapter */

include/sbi_utils/i2c/i2c.h +0 -0
@@ -21,21 +36,11 @@ struct i2c_adapter {

????????/** Unique ID of the I2C adapter assigned by the driver */ ????????int id;
?
-???????/**
-??????? * Send buffer to given address, register
-??????? *
-??????? * @return 0 on success and negative error code on failure -??????? */
-???????int (*write)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-?????????????????? uint8_t *buffer, int len);
+???????int (*write_byte)(struct i2c_adapter *ia, uint8_t byte, +???????????????????????bool send_start, bool send_stop);
?
-???????/**
-??????? * Read buffer from given address, register -??????? *
-??????? * @return 0 on success and negative error code on failure -??????? */
-???????int (*read)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg, -?????????????????? uint8_t *buffer, int len);
+???????int (*read_byte)(struct i2c_adapter *ia, uint8_t *byte, +???????????????????????bool nack, bool send_stop);
?
????????/** List */
????????struct sbi_dlist node;

include/sbi_utils/i2c/i2c.h +0 -0
@@ -55,31 +60,11 @@ int i2c_adapter_add(struct i2c_adapter *ia);

?/** Un-register I2C adapter */
?void i2c_adapter_remove(struct i2c_adapter *ia); ?

include/sbi_utils/i2c/i2c.h +0 -2
-/** Send to device on I2C adapter bus */
-int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr, uint8_t

reg,
-???????????????????? uint8_t *buffer, int len);

include/sbi_utils/i2c/i2c.h +0 -3
-
-/** Read from device on I2C adapter bus */
-int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t

reg,
-??????????????????? uint8_t *buffer, int len);

include/sbi_utils/i2c/i2c.h +0 -2
-
-static inline int i2c_adapter_reg_write(struct i2c_adapter *ia,

uint8_t addr,
-??????????????????????????????? uint8_t reg, uint8_t val)

include/sbi_utils/i2c/i2c.h +0 -5
-{
-???????return i2c_adapter_write(ia, addr, reg, &val, 1);
-}
-
-static inline int i2c_adapter_reg_read(struct i2c_adapter *ia,

uint8_t addr,
-????????????????????????????????????? uint8_t reg, uint8_t *val)

include/sbi_utils/i2c/i2c.h +6 -10
-{
-???????uint8_t buf;
-???????int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
-
-???????if (ret)
-???????????????return ret;
-
-???????*val = buf;
-???????return 0;
-}
+/**
+ * once? message transmission
+ * It can be a complete read or write
+ * can also be used in combination to form a combined transmission
+ */
+int i2c_adapter_trans(struct i2c_adapter *ia, struct i2c_msg *msg);

?
?#endif

lib/utils/i2c/fdt_i2c_sifive.c +0 -0
index 871241a..47d85f3 100644
@@ -101,139 +101,59 @@ static int sifive_i2c_adapter_poll(struct
 sifive_i2c_adapter *adap,
 ?#define sifive_i2c_adapter_poll_busy(adap)?????\
 ?sifive_i2c_adapter_poll(adap, SIFIVE_I2C_STATUS_BUSY)

?

lib/utils/i2c/fdt_i2c_sifive.c +0 -7
-static int sifive_i2c_adapter_start(struct sifive_i2c_adapter *adap,
-?????????????????????????????????? uint8_t addr, uint8_t bit)
-{
-???????uint8_t val = (addr << 1) | bit;
-
-???????sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, val);
-???????val = SIFIVE_I2C_CMD_STA | SIFIVE_I2C_CMD_WR |

SIFIVE_I2C_CMD_IACK; -???????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, val);

lib/utils/i2c/fdt_i2c_sifive.c +0 -5
-
-???????return sifive_i2c_adapter_poll_tip(adap);
-}
-
-static int sifive_i2c_adapter_write(struct i2c_adapter *ia, uint8_t

addr,
-?????????????????????????????????? uint8_t reg, uint8_t *buffer, int len)
+static int sifive_i2c_adapter_write_byte(struct i2c_adapter *ia, +???????????????????????????????uint8_t byte, bool send_start, bool send_stop)
?{
????????struct sifive_i2c_adapter *adap = ????????????????container_of(ia, struct sifive_i2c_adapter, adapter); -???????int rc = sifive_i2c_adapter_start(adap, addr, SIFIVE_I2C_WRITE_BIT);

lib/utils/i2c/fdt_i2c_sifive.c +7 -12
-
-???????if (rc)
-???????????????return rc;
-
-???????rc = sifive_i2c_adapter_rxack(adap);
-???????if (rc)
-???????????????return rc;
-
-???????/* set register address */
-???????sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, reg);
-???????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_WR |
-???????????????????????????????SIFIVE_I2C_CMD_IACK);
+???????int rc;
+???????uint8_t val;
+???????sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, byte);
+???????val = SIFIVE_I2C_CMD_WR | SIFIVE_I2C_CMD_IACK;
+???????if (send_start)
+???????????????val |= SIFIVE_I2C_CMD_STA;
+???????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, val);

????????rc = sifive_i2c_adapter_poll_tip(adap); ????????if (rc)
????????????????return rc;
-
????????rc = sifive_i2c_adapter_rxack(adap);
????????if (rc)
????????????????return rc;
?
-???????/* set value */
-???????while (len) { -???????????????sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, *buffer); -???????????????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_WR | -????????????????????????????????????????????????????? SIFIVE_I2C_CMD_IACK);

lib/utils/i2c/fdt_i2c_sifive.c +4 -6
-
-???????????????rc = sifive_i2c_adapter_poll_tip(adap);
-???????????????if (rc)
-???????????????????????return rc;
-
-???????????????rc = sifive_i2c_adapter_rxack(adap);
+???????if (send_stop) {
+???????????????sifive_i2c_setreg(adap, SIFIVE_I2C_CR,
+???????????????????????SIFIVE_I2C_CMD_STO | SIFIVE_I2C_CMD_IACK);
+???????????????rc = sifive_i2c_adapter_poll_busy(adap);

????????????????if (rc) ????????????????????????return rc;
-
-???????????????buffer++; -???????????????len--; +???????????????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_IACK);
????????}

lib/utils/i2c/fdt_i2c_sifive.c +0 -11
-
-???????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_STO |
-????????????????????????????????????????????? SIFIVE_I2C_CMD_IACK);
-
-???????/* poll BUSY instead of ACK*/
-???????rc = sifive_i2c_adapter_poll_busy(adap);
-???????if (rc)
-???????????????return rc;
-
-???????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_IACK);
-

????????return 0;
?}
?
-static int sifive_i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,
-????????????????????????????????? uint8_t reg, uint8_t *buffer, int len)
+static int sifive_i2c_adapter_read_byte(struct i2c_adapter *ia, +???????????????????????????????uint8_t *byte, bool nack, bool send_stop)
?{
????????struct sifive_i2c_adapter *adap = ????????????????container_of(ia, struct sifive_i2c_adapter, adapter); ????????int rc;
-
-???????rc = sifive_i2c_adapter_start(adap, addr, SIFIVE_I2C_WRITE_BIT);
-???????if (rc)
-???????????????return rc;

lib/utils/i2c/fdt_i2c_sifive.c +4 -9
-
-???????rc = sifive_i2c_adapter_rxack(adap);
-???????if (rc)
-???????????????return rc;
-
-???????sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, reg);
-???????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_WR |
-????????????????????????????????????????????? SIFIVE_I2C_CMD_IACK);
-
+???????uint8_t val = SIFIVE_I2C_CMD_RD | SIFIVE_I2C_CMD_IACK;
+???????if (nack)
+???????????????val |= SIFIVE_I2C_CMD_ACK;
+???????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, val);

????????rc = sifive_i2c_adapter_poll_tip(adap); ????????if (rc)
????????????????return rc;
+???????*byte = sifive_i2c_getreg(adap, SIFIVE_I2C_RXR); ?
-???????rc = sifive_i2c_adapter_rxack(adap); -???????if (rc)
-???????????????return rc;
-
-???????/* setting addr with high 0 bit */ -???????rc = sifive_i2c_adapter_start(adap, addr, SIFIVE_I2C_READ_BIT);
-???????if (rc)
-???????????????return rc;

lib/utils/i2c/fdt_i2c_sifive.c +4 -17
-
-???????rc = sifive_i2c_adapter_rxack(adap);
-???????if (rc)
-???????????????return rc;
-
-???????while (len) {
-???????????????if (len == 1)
-???????????????????????sifive_i2c_setreg(adap, SIFIVE_I2C_CR,
-???????????????????????????????????????? SIFIVE_I2C_CMD_ACK |
-???????????????????????????????????????? SIFIVE_I2C_CMD_RD |
-???????????????????????????????????????? SIFIVE_I2C_CMD_IACK);
-???????????????else
-???????????????????????sifive_i2c_setreg(adap, SIFIVE_I2C_CR,
-???????????????????????????????????????? SIFIVE_I2C_CMD_RD |
-???????????????????????????????????????? SIFIVE_I2C_CMD_IACK);
-
-???????????????rc = sifive_i2c_adapter_poll_tip(adap);
+???????if (send_stop) {
+???????????????sifive_i2c_setreg(adap, SIFIVE_I2C_CR,
+???????????????????????SIFIVE_I2C_CMD_STO | SIFIVE_I2C_CMD_IACK);
+???????????????rc = sifive_i2c_adapter_poll_busy(adap);

????????????????if (rc)
????????????????????????return rc;
-
-???????????????*buffer = sifive_i2c_getreg(adap, SIFIVE_I2C_RXR); -???????????????buffer++;
-???????????????len--; +???????????????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_IACK);
????????}

lib/utils/i2c/fdt_i2c_sifive.c +0 -9
-
-???????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_STO |
-????????????????????????????????????????????? SIFIVE_I2C_CMD_IACK);
-???????rc = sifive_i2c_adapter_poll_busy(adap);
-???????if (rc)
-???????????????return rc;
-
-???????sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_IACK);
-

????????return 0;
?}
?

lib/utils/i2c/fdt_i2c_sifive.c +2 -2
@@ -256,8 +176,8 @@ static int sifive_i2c_init(void *fdt, int nodeoff,
 ????????adapter->addr = addr;
 ????????adapter->adapter.driver = &fdt_i2c_adapter_sifive;
 ????????adapter->adapter.id = nodeoff;
-???????adapter->adapter.write = sifive_i2c_adapter_write;
-???????adapter->adapter.read = sifive_i2c_adapter_read;
+???????adapter->adapter.write_byte = sifive_i2c_adapter_write_byte;
+???????adapter->adapter.read_byte = sifive_i2c_adapter_read_byte;
 ????????rc = i2c_adapter_add(&adapter->adapter);
 ????????if (rc)
 ????????????????return rc;
lib/utils/i2c/i2c.c +0 -0
index 6be4e24..841756e 100644
@@ -12,6 +12,7 @@

? */
?
?#include <sbi/sbi_error.h> +#include <sbi/sbi_string.h> ?#include <sbi_utils/i2c/i2c.h>
?
?static SBI_LIST_HEAD(i2c_adapters_list);

lib/utils/i2c/i2c.c +0 -0
@@ -51,25 +52,56 @@ void i2c_adapter_remove(struct i2c_adapter *ia)
 ????????sbi_list_del(&(ia->node));
 ?}

?
-int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-??????????????????? uint8_t *buffer, int len)
+int i2c_adapter_trans(struct i2c_adapter *ia, struct i2c_msg *msg) ?{
-???????if (!ia)
-???????????????return SBI_EINVAL;
-???????if (!ia->write)
-???????????????return SBI_ENOSYS;

lib/utils/i2c/i2c.c +0 -6
-
-???????return ia->write(ia, addr, reg, buffer, len);
-}
-
-
-int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t

reg,
-??????????????????? uint8_t *buffer, int len)

lib/utils/i2c/i2c.c +9 -2
-{
-???????if (!ia)
+???????int ret;
+???????uint8_t rw = msg->flags & I2C_M_RD;
+???????uint8_t *buf = msg->buf;
+???????uint16_t len = msg->len;
+???????bool ten = msg->flags & I2C_M_TEN;
+???????bool send_start = !(msg->flags & I2C_M_NOSTART);
+???????bool send_stop = msg->flags & I2C_M_STOP;
+
+???????if(len == 0)

????????????????return SBI_EINVAL;
-???????if (!ia->read)
-???????????????return SBI_ENOSYS;
?
-???????return ia->read(ia, addr, reg, buffer, len);
-}
+???????if (ten) {
+???????????????uint8_t addr_h = ((msg->addr >> 8) & 3) | 0x7c; +???????????????uint8_t addr_l = msg->addr & 0xff; +???????????????if (send_start) { +???????????????????????ret = ia->write_byte(ia, (addr_h << 1) | 0, true, false);
+???????????????????????if (ret) +???????????????????????????????return ret; +???????????????????????ret = ia->write_byte(ia, addr_l, false, false);
+???????????????????????if (ret) +???????????????????????????????return ret; +???????????????????????if (rw) { +???????????????????????????????ret = ia->write_byte(ia, (addr_h << 1) | 1, true, false);
+???????????????????????????????if (ret) +???????????????????????????????????????return ret; +???????????????????????}
+???????????????} else {
+???????????????????????ret = ia->write_byte(ia, (addr_h << 1) | rw, false, false);
+???????????????????????if (ret) +???????????????????????????????return ret; +???????????????}
+???????} else {
+???????????????uint8_t addr = msg->addr & 0x7f; +???????????????ret = ia->write_byte(ia, (addr << 1) | rw, send_start, false);
+???????????????if (ret)
+???????????????????????return ret;
+???????}
+???????while (len > 0) {
+???????????????len--;
+???????????????bool S = (len == 0) && !send_stop; /* repeated START */
+???????????????bool P = (len == 0) &&? send_stop; /* STOP */ +???????????????if (rw)
+???????????????????????ret = ia->read_byte(ia, buf, S, P); +???????????????else
+???????????????????????ret = ia->write_byte(ia, *buf, S, P); +???????????????if(ret)
+???????????????????????return ret;
+???????????????buf++;
+???????}
+???????return 0;

lib/utils/i2c/i2c.c +1 -0
+}
\ No newline at end of file
platform/generic/sifive_fu740.c +0 -0
index 333b3fb..de82041 100644
@@ -34,6 +34,42 @@

?
?#define PMIC_CHIP_ID_DA9063????????????0x61
?
+static inline int da9063_read_reg(struct i2c_adapter *adap, uint8_t addr,
+???????uint8_t reg, uint8_t *val)

platform/generic/sifive_fu740.c +20 -0
+{
+???????struct i2c_msg msg1 = {
+???????????????.addr = addr,
+???????????????.flags = 0,
+???????????????.len = 1,
+???????????????.buf = &reg
+???????};
+???????struct i2c_msg msg2 = {
+???????????????.addr = addr,
+???????????????.flags = I2C_M_RD | I2C_M_NOSTART | I2C_M_STOP,
+???????????????.len = 1,
+???????????????.buf = val
+???????};
+???????int ret = i2c_adapter_trans(adap, &msg1);
+???????if (ret)
+???????????????return ret;
+???????return i2c_adapter_trans(adap, &msg2);
+}
+
+static inline int da9063_write_reg(struct i2c_adapter *adap, uint8_t

addr,
+???????uint8_t reg, uint8_t val)

platform/generic/sifive_fu740.c +13 -0
+{
+???????uint8_t buff[2];
+???????buff[0] = reg;
+???????buff[1] = val;
+???????struct i2c_msg msg = {
+???????????????.addr = addr,
+???????????????.flags = I2C_M_STOP,
+???????????????.len = sizeof(buff),
+???????????????.buf = buff
+???????};
+???????return i2c_adapter_trans(adap, &msg);
+}
+

?static struct {
????????struct i2c_adapter *adapter; ????????uint32_t reg;

platform/generic/sifive_fu740.c +1 -1
@@ -55,13 +91,13 @@ static int da9063_system_reset_check(u32 type, u32
 reason)
 ?static inline int da9063_sanity_check(struct i2c_adapter *adap,
 uint32_t reg)
 ?{
 ????????uint8_t val;
-???????int rc = i2c_adapter_reg_write(adap, reg, DA9063_REG_PAGE_CON,
 0x02);
+???????int rc = da9063_write_reg(adap, reg, DA9063_REG_PAGE_CON,
 0x02);

?
????????if (rc)
????????????????return rc;
?
????????/* check set page*/
-???????rc = i2c_adapter_reg_read(adap, reg, 0x0, &val); +???????rc = da9063_read_reg(adap, reg, 0x0, &val); ????????if (rc)
????????????????return rc;
?

platform/generic/sifive_fu740.c +0 -0
@@ -69,7 +105,7 @@ static inline int da9063_sanity_check(struct
 i2c_adapter *adap, uint32_t reg)
 ????????????????return SBI_ENODEV;

?
????????/* read and check device id */
-???????rc = i2c_adapter_reg_read(adap, reg, DA9063_REG_DEVICE_ID, &val);
+???????rc = da9063_read_reg(adap, reg, DA9063_REG_DEVICE_ID, &val); ????????if (rc)
????????????????return rc;
?

platform/generic/sifive_fu740.c +0 -0
@@ -81,32 +117,32 @@ static inline int da9063_sanity_check(struct
 i2c_adapter *adap, uint32_t reg)

?
?static inline int da9063_shutdown(struct i2c_adapter *adap, uint32_t reg)
?{
-???????int rc = i2c_adapter_reg_write(adap, da9063.reg, +???????int rc = da9063_write_reg(adap, da9063.reg, ????????????????????????????????????????DA9063_REG_PAGE_CON, 0x00); ?
????????if (rc)
????????????????return rc;
?
-???????return i2c_adapter_reg_write(adap, da9063.reg, +???????return da9063_write_reg(adap, da9063.reg, ???????????????????????????????????? DA9063_REG_CONTROL_F, ???????????????????????????????????? DA9063_CONTROL_F_SHUTDOWN); ?}
?
?static inline int da9063_reset(struct i2c_adapter *adap, uint32_t reg)
?{
-???????int rc = i2c_adapter_reg_write(adap, da9063.reg, +???????int rc = da9063_write_reg(adap, da9063.reg, ????????????????????????????????????????DA9063_REG_PAGE_CON, 0x00); ?
????????if (rc)
????????????????return rc;
?
-???????rc = i2c_adapter_reg_write(adap, da9063.reg, +???????rc = da9063_write_reg(adap, da9063.reg, ?????????????????????????????????? DA9063_REG_CONTROL_F, ?????????????????????????????????? DA9063_CONTROL_F_WAKEUP); ????????if (rc)
????????????????return rc;
?
-???????return i2c_adapter_reg_write(adap, da9063.reg, +???????return da9063_write_reg(adap, da9063.reg, ????????????????????????????????DA9063_REG_CONTROL_A, ????????????????????????????????DA9063_CONTROL_A_M_POWER1_EN | ????????????????????????????????DA9063_CONTROL_A_M_POWER_EN |

Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 1/1 lib: utils/i2c: update i2c interface

In reply to Xiang W

Hello Xiang!

Quoted 7 lines · On Thu, 30 Dec 2021 22:25:33 +0800 Xiang W <[email protected]> wrote:

1. Remove i2c register related operations
2. Simplify the low-level interface
3. Add 10bit address support
4. Add combination operation
5. Update fdt_i2c_sifive.c
6. Update sifive_fu740.c

Quoted 6 lines

---
Changes since v1:

* Simplify fdt_i2c_sifive.c
* Add struct i2c_msg, modify the interface

I have tried 5 times at least - all attempts fail with:
da9063_system_reset: chip is not da9063 PMIC

Me, David and Heinrich have tested reboot before v1.0 release, and it works for us, also i never encountered a reboot failure, after timing issue was fixed.

It's best to test this with a I2C analyser hooked up, unfortunate our office is currently closed for holidays.

What hardware are you using for testing with 10bit addresses and raw read/write ?

If you have some real use - could you please share ?

Also it seems you missed some comments from my previous email:
- doesn't we also need the hardware support for 10bit addr ?
- i think how to read/write is up to driver to decide and we shouldn't expose it to upper level wich is i2c.c

Quoted 12 lines
include/sbi_utils/i2c/i2c.h +6 -0
+struct i2c_msg {
+	uint16_t addr;
+	uint16_t flags;
+#define I2C_M_RD		0x0001
+#define I2C_M_TEN		0x0010
+#define I2C_M_RECV_LEN		0x0400 /* Fixup: unclear

meaning, currently unused */ +#define I2C_M_NO_RD_ACK 0x0800 /* Fixup: unclear meaning, currently unused */ +#define
I2C_M_IGNORE_NAK 0x1000 /* Fixup: unclear meaning, currently
unused */ +#define I2C_M_REV_DIR_ADDR 0x2000 /* Fixup: unclear
meaning, currently unused */ +#define I2C_M_NOSTART
0x4000 +#define I2C_M_STOP 0x8000

include/sbi_utils/i2c/i2c.h line 29

Quoted 3 lines
include/sbi_utils/i2c/i2c.h +3 -0
+	uint16_t len;
+	uint8_t *buf;
+};

May be we shouldn't include flags we don't use currently ?

include/sbi_utils/i2c/i2c.h struct i2c_adapter { · line 43

Quoted 4 lines
include/sbi_utils/i2c/i2c.h +4 -0
+	int (*write_byte)(struct i2c_adapter *ia, uint8_t byte,
+			bool send_start, bool send_stop);
+	int (*read_byte)(struct i2c_adapter *ia, uint8_t *byte,
+			bool nack, bool send_stop);

With i2c_msg we can wrap it with:
(struct i2c_adapter *ia, struct i2c_msg *msg)

Where msg->len = 1, msg->buf[0] = byte, msg->flags = ?I2C_M_STOP | ?I2C_M_NOSTART | ?I2C_M_RD;

And simply leave only trans/xfer function as a common interface for transmit.

i2c_adapter_trans:

Quoted 5 lines
lib/utils/i2c/i2c.c +3 -0
+	while (len > 0) {
+		len--;
+		bool S = (len == 0) && !send_stop; /* repeated START

*/

lib/utils/i2c/i2c.c +1 -0
+		bool P = (len == 0) &&  send_stop; /* STOP */

lib/utils/i2c/i2c.c void i2c_adapter_remove(struct i2c_adapter *ia) · line 107

Quoted 10 lines
lib/utils/i2c/i2c.c +10 -0
+		if (rw)
+			ret = ia->read_byte(ia, buf, S, P);
+		else
+			ret = ia->write_byte(ia, *buf, S, P);
+		if(ret)
+			return ret;
+		buf++;
+	}
+	return 0;
+}

I don't think we should expose len processing to upper level - i think it's up to driver to decide how to deal with actual sending.

author
#3 lore raw

PATCH v2 1/1 lib: utils/i2c: update i2c interface

? 2021-12-31???? 12:11 +0300?Nikita Shubin???

Quoted 16 lines

Hello Xiang!

Quoted 11 lines · On Thu, 30 Dec 2021 22:25:33 +0800 Xiang W <[email protected]> wrote:

1. Remove i2c register related operations
2. Simplify the low-level interface
3. Add 10bit address support
4. Add combination operation
5. Update fdt_i2c_sifive.c
6. Update sifive_fu740.c

---
Changes since v1:

Quoted 5 lines
Quoted 2 lines

* Simplify fdt_i2c_sifive.c
* Add struct i2c_msg, modify the interface

I have tried 5 times at least - all attempts fail with:
da9063_system_reset: chip is not da9063 PMIC

The following is my restart log.

[   41.368396] [1654]: Remounting '/' read-only in with options
'(null)'.
[   41.394126] EXT4-fs (nvme0n1p3): re-mounted. Opts: (null). Quota
mode: none.
[   41.416993] systemd-shutdown[1]: All filesystems unmounted.
[   41.421874] systemd-shutdown[1]: Deactivating swaps.
[   41.427011] systemd-shutdown[1]: All swaps deactivated.
[   41.432041] systemd-shutdown[1]: Detaching loop devices.
[   41.441608] systemd-shutdown[1]: All loop devices detached.
[   41.621746] reboot: Restarting system

U-Boot SPL 2021.07-dirty (Dec 31 2021 - 16:18:44 +0800) Trying to boot from MMC1

OpenSBI v1.0-1-g95bfa9f
Build time: 2022-01-02 01:04:19 +0800
Build compiler: gcc version 10.2.1 20210110 (Debian 10.2.1-6)
____ _____ ____ _____

  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|____/_____|
        | |
        |_|

Platform Name : SiFive HiFive Unmatched A00
Platform Features : medeleg
Platform HART Count : 5
Platform IPI Device : aclint-mswi
Platform Timer Device : aclint-mtimer @ 1000000Hz
Platform Console Device : sifive_uart
Platform HSM Device : ---
Platform Reboot Device : da9063-reset
Platform Shutdown Device : gpio-poweroff
Firmware Base : 0x80000000
Firmware Size : 284 KB
Runtime SBI Version : 0.3

Domain0 Name : root
Domain0 Boot HART : 3
Domain0 HARTs : 0*,1*,2*,3*,4*
Domain0 Region00 : 0x0000000002000000-0x000000000200ffff (I)
Domain0 Region01 : 0x0000000080000000-0x000000008007ffff ()
Domain0 Region02 : 0x0000000000000000-0xffffffffffffffff
(R,W,X)
Domain0 Next Address : 0x0000000080200000
Domain0 Next Arg1 : 0x0000000080295fc8
Domain0 Next Mode : S-mode
Domain0 SysReset : yes

Boot HART ID : 3
Boot HART Domain : root
Boot HART ISA : rv64imafdcsu
Boot HART Features : scounteren,mcounteren
Boot HART PMP Count : 16
Boot HART PMP Granularity : 4096
Boot HART PMP Address Bits: 36
Boot HART MHPM Count : 2
Boot HART MIDELEG : 0x0000000000000222
Boot HART MEDELEG : 0x000000000000b109

U-Boot 2022.01-rc4-00001-g19f15d870f (Jan 02 2022 - 01:04:29 +0800)

CPU:   rv64imafdc
Model: SiFive HiFive Unmatched A00
Quoted 10 lines

Me, David and Heinrich have tested reboot before v1.0 release, and it works for us, also i never encountered a reboot failure, after timing issue was fixed.

It's best to test this with a I2C analyser hooked up, unfortunate our office is currently closed for holidays.

What hardware are you using for testing with 10bit addresses and raw read/write ?

I don?t have a device with a 10bit address, I implemented it with reference to the protocol.

https://www.nxp.com/docs/en/user-guide/UM10204.pdf

Quoted 61 lines

If you have some real use - could you please share ?

Also it seems you missed some comments from my previous email:
- doesn't we also need the hardware support for 10bit addr ?
- i think how to read/write is up to driver to decide and we shouldn't ? expose it to upper level wich is i2c.c

Quoted 7 lines
(quoted diff) +6 -0
+struct i2c_msg {
+???????uint16_t addr;
+???????uint16_t flags;
+#define I2C_M_RD???????????????0x0001
+#define I2C_M_TEN??????????????0x0010
+#define I2C_M_RECV_LEN?????????0x0400 /* Fixup: unclear

meaning, currently unused */ +#define I2C_M_NO_RD_ACK

Quoted 8 lines

0x0800 /* Fixup: unclear meaning, currently unused */ +#define I2C_M_IGNORE_NAK????????0x1000 /* Fixup: unclear meaning, currently unused */ +#define I2C_M_REV_DIR_ADDR???0x2000 /* Fixup: unclear meaning, currently unused */ +#define I2C_M_NOSTART 0x4000 +#define I2C_M_STOP??????????????0x8000 +???????uint16_t len;
+???????uint8_t *buf;
+};

May be we shouldn't include flags we don't use currently ?

Quoted 4 lines

+???????int (*write_byte)(struct i2c_adapter *ia, uint8_t byte, +???????????????????????bool send_start, bool send_stop); +???????int (*read_byte)(struct i2c_adapter *ia, uint8_t *byte, +???????????????????????bool nack, bool send_stop);

With i2c_msg we can wrap it with:
(struct i2c_adapter *ia, struct i2c_msg *msg)

Where msg->len = 1, msg->buf[0] = byte, msg->flags = ?I2C_M_STOP | ?I2C_M_NOSTART | ?I2C_M_RD;

And simply leave only trans/xfer function as a common interface for transmit.

i2c_adapter_trans:

Quoted 7 lines

+???????while (len > 0) {
+???????????????len--;
+???????????????bool S = (len == 0) && !send_stop; /* repeated START */
+???????????????bool P = (len == 0) &&? send_stop; /* STOP */ +???????????????if (rw)
+???????????????????????ret = ia->read_byte(ia, buf, S, P);

Quoted 8 lines

+???????????????else
+???????????????????????ret = ia->write_byte(ia, *buf, S, P); +???????????????if(ret) +???????????????????????return ret; +???????????????buf++;
+???????}
+???????return 0;
+}

I don't think we should expose len processing to upper level - i think it's up to driver to decide how to deal with actual sending.

#4 lore raw

PATCH v2 1/1 lib: utils/i2c: update i2c interface

In reply to Xiang W
Quoted 25 lines · Xiang W

1. Remove i2c register related operations
2. Simplify the low-level interface
3. Add 10bit address support
4. Add combination operation
5. Update fdt_i2c_sifive.c
6. Update sifive_fu740.c

---
Changes since v1:

* Simplify fdt_i2c_sifive.c
* Add struct i2c_msg, modify the interface

  include/sbi_utils/i2c/i2c.h     |  65 ++++++---------
  lib/utils/i2c/fdt_i2c_sifive.c  | 136 +++++++-------------------------
  lib/utils/i2c/i2c.c             |  70 +++++++++++-----
  platform/generic/sifive_fu740.c |  52 ++++++++++--
  4 files changed, 148 insertions(+), 175 deletions(-)
include/sbi_utils/i2c/i2c.h +0 -0
index 5a70364..307fa55 100644

include/sbi_utils/i2c/i2c.h line 21

Quoted 9 lines
include/sbi_utils/i2c/i2c.h +6 -0
@@ -13,6 +13,21 @@
  #include <sbi/sbi_types.h>
  #include <sbi/sbi_list.h>
 
+struct i2c_msg {
+	uint16_t addr;
+	uint16_t flags;
+#define I2C_M_RD		0x0001
+#define I2C_M_TEN		0x0010
+#define I2C_M_RECV_LEN		0x0400 /* Fixup: unclear meaning, currently unused */

This flag is used for SMBUS emulation.

include/sbi_utils/i2c/i2c.h line 24

Quoted 3 lines
include/sbi_utils/i2c/i2c.h +3 -0
+#define I2C_M_NO_RD_ACK		0x0800 /* Fixup: unclear meaning, currently unused */
+#define I2C_M_IGNORE_NAK	0x1000 /* Fixup: unclear meaning, currently unused */
+#define I2C_M_REV_DIR_ADDR	0x2000 /* Fixup: unclear meaning, currently unused */

Please, have a look at
https://www.kernel.org/doc/Documentation/i2c/i2c-protocol

Best regards

Heinrich

Quoted 453 lines
include/sbi_utils/i2c/i2c.h +16 -40
+#define I2C_M_NOSTART		0x4000
+#define I2C_M_STOP		0x8000
+	uint16_t len;
+	uint8_t *buf;
+};
+
  /** Representation of a I2C adapter */
  struct i2c_adapter {
  	/** Pointer to I2C driver owning this I2C adapter */
@@ -21,21 +36,11 @@ struct i2c_adapter {
  	/** Unique ID of the I2C adapter assigned by the driver */
  	int id;
 
-	/**
-	 * Send buffer to given address, register
-	 *
-	 * @return 0 on success and negative error code on failure
-	 */
-	int (*write)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		    uint8_t *buffer, int len);
+	int (*write_byte)(struct i2c_adapter *ia, uint8_t byte,
+			bool send_start, bool send_stop);
 
-	/**
-	 * Read buffer from given address, register
-	 *
-	 * @return 0 on success and negative error code on failure
-	 */
-	int (*read)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		    uint8_t *buffer, int len);
+	int (*read_byte)(struct i2c_adapter *ia, uint8_t *byte,
+			bool nack, bool send_stop);
 
  	/** List */
  	struct sbi_dlist node;
@@ -55,31 +60,11 @@ int i2c_adapter_add(struct i2c_adapter *ia);
  /** Un-register I2C adapter */
  void i2c_adapter_remove(struct i2c_adapter *ia);
 
-/** Send to device on I2C adapter bus */
-int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		      uint8_t *buffer, int len);
-
-/** Read from device on I2C adapter bus */
-int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		     uint8_t *buffer, int len);
-
-static inline int i2c_adapter_reg_write(struct i2c_adapter *ia, uint8_t addr,
-				 uint8_t reg, uint8_t val)
-{
-	return i2c_adapter_write(ia, addr, reg, &val, 1);
-}
-
-static inline int i2c_adapter_reg_read(struct i2c_adapter *ia, uint8_t addr,
-				       uint8_t reg, uint8_t *val)
-{
-	uint8_t buf;
-	int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
-
-	if (ret)
-		return ret;
-
-	*val = buf;
-	return 0;
-}
+/**
+ * once  message transmission
+ * It can be a complete read or write
+ * can also be used in combination to form a combined transmission
+ */
+int i2c_adapter_trans(struct i2c_adapter *ia, struct i2c_msg *msg);
 
  #endif
lib/utils/i2c/fdt_i2c_sifive.c +28 -108
index 871241a..47d85f3 100644
@@ -101,139 +101,59 @@ static int sifive_i2c_adapter_poll(struct sifive_i2c_adapter *adap,
  #define sifive_i2c_adapter_poll_busy(adap)	\
  sifive_i2c_adapter_poll(adap, SIFIVE_I2C_STATUS_BUSY)
 
-static int sifive_i2c_adapter_start(struct sifive_i2c_adapter *adap,
-				    uint8_t addr, uint8_t bit)
-{
-	uint8_t val = (addr << 1) | bit;
-
-	sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, val);
-	val = SIFIVE_I2C_CMD_STA | SIFIVE_I2C_CMD_WR | SIFIVE_I2C_CMD_IACK;
-	sifive_i2c_setreg(adap, SIFIVE_I2C_CR, val);
-
-	return sifive_i2c_adapter_poll_tip(adap);
-}
-
-static int sifive_i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr,
-				    uint8_t reg, uint8_t *buffer, int len)
+static int sifive_i2c_adapter_write_byte(struct i2c_adapter *ia,
+				uint8_t byte, bool send_start, bool send_stop)
  {
  	struct sifive_i2c_adapter *adap =
  		container_of(ia, struct sifive_i2c_adapter, adapter);
-	int rc = sifive_i2c_adapter_start(adap, addr, SIFIVE_I2C_WRITE_BIT);
-
-	if (rc)
-		return rc;
-
-	rc = sifive_i2c_adapter_rxack(adap);
-	if (rc)
-		return rc;
-
-	/* set register address */
-	sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, reg);
-	sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_WR |
-				SIFIVE_I2C_CMD_IACK);
+	int rc;
+	uint8_t val;
+	sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, byte);
+	val = SIFIVE_I2C_CMD_WR | SIFIVE_I2C_CMD_IACK;
+	if (send_start)
+		val |= SIFIVE_I2C_CMD_STA;
+	sifive_i2c_setreg(adap, SIFIVE_I2C_CR, val);
  	rc = sifive_i2c_adapter_poll_tip(adap);
  	if (rc)
  		return rc;
-
  	rc = sifive_i2c_adapter_rxack(adap);
  	if (rc)
  		return rc;
 
-	/* set value */
-	while (len) {
-		sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, *buffer);
-		sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_WR |
-						       SIFIVE_I2C_CMD_IACK);
-
-		rc = sifive_i2c_adapter_poll_tip(adap);
-		if (rc)
-			return rc;
-
-		rc = sifive_i2c_adapter_rxack(adap);
+	if (send_stop) {
+		sifive_i2c_setreg(adap, SIFIVE_I2C_CR,
+			SIFIVE_I2C_CMD_STO | SIFIVE_I2C_CMD_IACK);
+		rc = sifive_i2c_adapter_poll_busy(adap);
  		if (rc)
  			return rc;
-
-		buffer++;
-		len--;
+		sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_IACK);
  	}
-
-	sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_STO |
-					       SIFIVE_I2C_CMD_IACK);
-
-	/* poll BUSY instead of ACK*/
-	rc = sifive_i2c_adapter_poll_busy(adap);
-	if (rc)
-		return rc;
-
-	sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_IACK);
-
  	return 0;
  }
 
-static int sifive_i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,
-				   uint8_t reg, uint8_t *buffer, int len)
+static int sifive_i2c_adapter_read_byte(struct i2c_adapter *ia,
+				uint8_t *byte, bool nack, bool send_stop)
  {
  	struct sifive_i2c_adapter *adap =
  		container_of(ia, struct sifive_i2c_adapter, adapter);
  	int rc;
-
-	rc = sifive_i2c_adapter_start(adap, addr, SIFIVE_I2C_WRITE_BIT);
-	if (rc)
-		return rc;
-
-	rc = sifive_i2c_adapter_rxack(adap);
-	if (rc)
-		return rc;
-
-	sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, reg);
-	sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_WR |
-					       SIFIVE_I2C_CMD_IACK);
-
+	uint8_t val = SIFIVE_I2C_CMD_RD | SIFIVE_I2C_CMD_IACK;
+	if (nack)
+		val |= SIFIVE_I2C_CMD_ACK;
+	sifive_i2c_setreg(adap, SIFIVE_I2C_CR, val);
  	rc = sifive_i2c_adapter_poll_tip(adap);
  	if (rc)
  		return rc;
+	*byte = sifive_i2c_getreg(adap, SIFIVE_I2C_RXR);
 
-	rc = sifive_i2c_adapter_rxack(adap);
-	if (rc)
-		return rc;
-
-	/* setting addr with high 0 bit */
-	rc = sifive_i2c_adapter_start(adap, addr, SIFIVE_I2C_READ_BIT);
-	if (rc)
-		return rc;
-
-	rc = sifive_i2c_adapter_rxack(adap);
-	if (rc)
-		return rc;
-
-	while (len) {
-		if (len == 1)
-			sifive_i2c_setreg(adap, SIFIVE_I2C_CR,
-					  SIFIVE_I2C_CMD_ACK |
-					  SIFIVE_I2C_CMD_RD |
-					  SIFIVE_I2C_CMD_IACK);
-		else
-			sifive_i2c_setreg(adap, SIFIVE_I2C_CR,
-					  SIFIVE_I2C_CMD_RD |
-					  SIFIVE_I2C_CMD_IACK);
-
-		rc = sifive_i2c_adapter_poll_tip(adap);
+	if (send_stop) {
+		sifive_i2c_setreg(adap, SIFIVE_I2C_CR,
+			SIFIVE_I2C_CMD_STO | SIFIVE_I2C_CMD_IACK);
+		rc = sifive_i2c_adapter_poll_busy(adap);
  		if (rc)
  			return rc;
-
-		*buffer = sifive_i2c_getreg(adap, SIFIVE_I2C_RXR);
-		buffer++;
-		len--;
+		sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_IACK);
  	}
-
-	sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_STO |
-					       SIFIVE_I2C_CMD_IACK);
-	rc = sifive_i2c_adapter_poll_busy(adap);
-	if (rc)
-		return rc;
-
-	sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_IACK);
-
  	return 0;
  }
 
@@ -256,8 +176,8 @@ static int sifive_i2c_init(void *fdt, int nodeoff,
  	adapter->addr = addr;
  	adapter->adapter.driver = &fdt_i2c_adapter_sifive;
  	adapter->adapter.id = nodeoff;
-	adapter->adapter.write = sifive_i2c_adapter_write;
-	adapter->adapter.read = sifive_i2c_adapter_read;
+	adapter->adapter.write_byte = sifive_i2c_adapter_write_byte;
+	adapter->adapter.read_byte = sifive_i2c_adapter_read_byte;
  	rc = i2c_adapter_add(&adapter->adapter);
  	if (rc)
  		return rc;
lib/utils/i2c/i2c.c +51 -19
index 6be4e24..841756e 100644
@@ -12,6 +12,7 @@
   */
 
  #include <sbi/sbi_error.h>
+#include <sbi/sbi_string.h>
  #include <sbi_utils/i2c/i2c.h>
 
  static SBI_LIST_HEAD(i2c_adapters_list);
@@ -51,25 +52,56 @@ void i2c_adapter_remove(struct i2c_adapter *ia)
  	sbi_list_del(&(ia->node));
  }
 
-int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		     uint8_t *buffer, int len)
+int i2c_adapter_trans(struct i2c_adapter *ia, struct i2c_msg *msg)
  {
-	if (!ia)
-		return SBI_EINVAL;
-	if (!ia->write)
-		return SBI_ENOSYS;
-
-	return ia->write(ia, addr, reg, buffer, len);
-}
-
-
-int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
-		     uint8_t *buffer, int len)
-{
-	if (!ia)
+	int ret;
+	uint8_t rw = msg->flags & I2C_M_RD;
+	uint8_t *buf = msg->buf;
+	uint16_t len = msg->len;
+	bool ten = msg->flags & I2C_M_TEN;
+	bool send_start = !(msg->flags & I2C_M_NOSTART);
+	bool send_stop = msg->flags & I2C_M_STOP;
+
+	if(len == 0)
  		return SBI_EINVAL;
-	if (!ia->read)
-		return SBI_ENOSYS;
 
-	return ia->read(ia, addr, reg, buffer, len);
-}
+	if (ten) {
+		uint8_t addr_h = ((msg->addr >> 8) & 3) | 0x7c;
+		uint8_t addr_l = msg->addr & 0xff;
+		if (send_start) {
+			ret = ia->write_byte(ia, (addr_h << 1) | 0, true, false);
+			if (ret)
+				return ret;
+			ret = ia->write_byte(ia, addr_l, false, false);
+			if (ret)
+				return ret;
+			if (rw) {
+				ret = ia->write_byte(ia, (addr_h << 1) | 1, true, false);
+				if (ret)
+					return ret;
+			}
+		} else {
+			ret = ia->write_byte(ia, (addr_h << 1) | rw, false, false);
+			if (ret)
+				return ret;
+		}
+	} else {
+		uint8_t addr = msg->addr & 0x7f;
+		ret = ia->write_byte(ia, (addr << 1) | rw, send_start, false);
+		if (ret)
+			return ret;
+	}
+	while (len > 0) {
+		len--;
+		bool S = (len == 0) && !send_stop; /* repeated START */
+		bool P = (len == 0) &&  send_stop; /* STOP */
+		if (rw)
+			ret = ia->read_byte(ia, buf, S, P);
+		else
+			ret = ia->write_byte(ia, *buf, S, P);
+		if(ret)
+			return ret;
+		buf++;
+	}
+	return 0;
+}
\ No newline at end of file
platform/generic/sifive_fu740.c +44 -8
index 333b3fb..de82041 100644
@@ -34,6 +34,42 @@
 
  #define PMIC_CHIP_ID_DA9063		0x61
 
+static inline int da9063_read_reg(struct i2c_adapter *adap, uint8_t addr,
+	uint8_t reg, uint8_t *val)
+{
+	struct i2c_msg msg1 = {
+		.addr = addr,
+		.flags = 0,
+		.len = 1,
+		.buf = &reg
+	};
+	struct i2c_msg msg2 = {
+		.addr = addr,
+		.flags = I2C_M_RD | I2C_M_NOSTART | I2C_M_STOP,
+		.len = 1,
+		.buf = val
+	};
+	int ret = i2c_adapter_trans(adap, &msg1);
+	if (ret)
+		return ret;
+	return i2c_adapter_trans(adap, &msg2);
+}
+
+static inline int da9063_write_reg(struct i2c_adapter *adap, uint8_t addr,
+	uint8_t reg, uint8_t val)
+{
+	uint8_t buff[2];
+	buff[0] = reg;
+	buff[1] = val;
+	struct i2c_msg msg = {
+		.addr = addr,
+		.flags = I2C_M_STOP,
+		.len = sizeof(buff),
+		.buf = buff
+	};
+	return i2c_adapter_trans(adap, &msg);
+}
+
  static struct {
  	struct i2c_adapter *adapter;
  	uint32_t reg;
@@ -55,13 +91,13 @@ static int da9063_system_reset_check(u32 type, u32 reason)
  static inline int da9063_sanity_check(struct i2c_adapter *adap, uint32_t reg)
  {
  	uint8_t val;
-	int rc = i2c_adapter_reg_write(adap, reg, DA9063_REG_PAGE_CON, 0x02);
+	int rc = da9063_write_reg(adap, reg, DA9063_REG_PAGE_CON, 0x02);
 
  	if (rc)
  		return rc;
 
  	/* check set page*/
-	rc = i2c_adapter_reg_read(adap, reg, 0x0, &val);
+	rc = da9063_read_reg(adap, reg, 0x0, &val);
  	if (rc)
  		return rc;
 
@@ -69,7 +105,7 @@ static inline int da9063_sanity_check(struct i2c_adapter *adap, uint32_t reg)
  		return SBI_ENODEV;
 
  	/* read and check device id */
-	rc = i2c_adapter_reg_read(adap, reg, DA9063_REG_DEVICE_ID, &val);
+	rc = da9063_read_reg(adap, reg, DA9063_REG_DEVICE_ID, &val);
  	if (rc)
  		return rc;
 
@@ -81,32 +117,32 @@ static inline int da9063_sanity_check(struct i2c_adapter *adap, uint32_t reg)
 
  static inline int da9063_shutdown(struct i2c_adapter *adap, uint32_t reg)
  {
-	int rc = i2c_adapter_reg_write(adap, da9063.reg,
+	int rc = da9063_write_reg(adap, da9063.reg,
  					DA9063_REG_PAGE_CON, 0x00);
 
  	if (rc)
  		return rc;
 
-	return i2c_adapter_reg_write(adap, da9063.reg,
+	return da9063_write_reg(adap, da9063.reg,
  				     DA9063_REG_CONTROL_F,
  				     DA9063_CONTROL_F_SHUTDOWN);
  }
 
  static inline int da9063_reset(struct i2c_adapter *adap, uint32_t reg)
  {
-	int rc = i2c_adapter_reg_write(adap, da9063.reg,
+	int rc = da9063_write_reg(adap, da9063.reg,
  					DA9063_REG_PAGE_CON, 0x00);
 
  	if (rc)
  		return rc;
 
-	rc = i2c_adapter_reg_write(adap, da9063.reg,
+	rc = da9063_write_reg(adap, da9063.reg,
  				   DA9063_REG_CONTROL_F,
  				   DA9063_CONTROL_F_WAKEUP);
  	if (rc)
  		return rc;
 
-	return i2c_adapter_reg_write(adap, da9063.reg,
+	return da9063_write_reg(adap, da9063.reg,
  				DA9063_REG_CONTROL_A,
  				DA9063_CONTROL_A_M_POWER1_EN |
  				DA9063_CONTROL_A_M_POWER_EN |