Skip to contentSkip to conversation

i2c: pcf2127: fix bug that read wrong time

PATCH New Chuanhua Han 3 participants 12 messages May 22, 2019 – May 23, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: pcf2127: fix bug that read wrong time

Because i2c driver does not generate a stop bit when reading registers of pcf2127

---
 drivers/i2c/i2c-uclass.c |  2 ++
 drivers/i2c/mxc_i2c.c    | 70 +++++++++++++++++++++++++++++++++++-----
 drivers/rtc/pcf2127.c    | 18 ++++++-----
 include/i2c.h            |  2 ++
 4 files changed, 76 insertions(+), 16 deletions(-)
drivers/i2c/i2c-uclass.c +2 -0
index e47abf1833..18f7364d72 100644
@@ -141,6 +141,8 @@ int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
 	if (len) {
 		ptr->addr = chip->chip_addr;
 		ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
+		ptr->flags |= chip->flags & DM_I2C_CHIP_RD_NEED_STOP_BIT ?
+			      I2C_M_RD_NEED_STOP_BIT : 0;
 		ptr->flags |= I2C_M_RD;
 		ptr->len = len;
 		ptr->buf = buffer;
drivers/i2c/mxc_i2c.c +62 -8

103 lines not on this page · raw message

drivers/rtc/pcf2127.c +10 -8
index dcf0340b4d..010c45ecbf 100644
@@ -24,12 +24,9 @@
 
 static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm)
 {
-	uchar buf[8];
+	uchar buf[7] = {0};
 	int i = 0, ret;
 
-	/* start register address */
-	buf[i++] = PCF2127_REG_SC;
-
 	/* hours, minutes and seconds */
 	buf[i++] = bin2bcd(tm->tm_sec);
 	buf[i++] = bin2bcd(tm->tm_min);
@@ -44,7 +41,7 @@ static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm)
 	buf[i++] = bin2bcd(tm->tm_year % 100);
 
 	/* write register's data */
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
+	ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
 
 	return ret;
 }
@@ -54,9 +51,6 @@ static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm)
 	int ret = 0;
 	uchar buf[10] = { PCF2127_REG_CTRL1 };
 
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
-	if (ret < 0)
-		return ret;
 	ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
 	if (ret < 0)
 		return ret;
@@ -90,6 +84,13 @@ static int pcf2127_rtc_reset(struct udevice *dev)
 	return 0;
 }
 
+static int pcf2127_probe(struct udevice *dev)
+{
+	i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_NEED_STOP_BIT);
+
+	return 0;
+}
+
 static const struct rtc_ops pcf2127_rtc_ops = {
 	.get = pcf2127_rtc_get,
 	.set = pcf2127_rtc_set,
@@ -104,6 +105,7 @@ static const struct udevice_id pcf2127_rtc_ids[] = {
 U_BOOT_DRIVER(rtc_pcf2127) = {
 	.name	= "rtc-pcf2127",
 	.id	= UCLASS_RTC,
+	.probe	= pcf2127_probe,
 	.of_match = pcf2127_rtc_ids,
 	.ops	= &pcf2127_rtc_ops,
 };
include/i2c.h +2 -0
index a5c760c711..beaa028349 100644
@@ -28,6 +28,7 @@ enum dm_i2c_chip_flags {
 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit addressing */
 	DM_I2C_CHIP_RD_ADDRESS	= 1 << 1, /* Send address for each read byte */
 	DM_I2C_CHIP_WR_ADDRESS	= 1 << 2, /* Send address for each write byte */
+	DM_I2C_CHIP_RD_NEED_STOP_BIT    = 1 << 3, /* Need generate stop bit */
 };
 
 struct udevice;
@@ -87,6 +88,7 @@ enum dm_i2c_msg_flags {
 	I2C_M_IGNORE_NAK	= 0x1000, /* continue after NAK */
 	I2C_M_NO_RD_ACK		= 0x0800, /* skip the Ack bit on reads */
 	I2C_M_RECV_LEN		= 0x0400, /* length is first received byte */
+	I2C_M_RD_NEED_STOP_BIT  = 0x0002, /* need generate stop bit */
 };
 
 /**
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 2 lines

Because i2c driver does not generate a stop bit when reading registers of pcf2127

The change (in the common i2c code) is rather large when considering the description above.

Could you write a more detailed patch description? Is this only the problem with i2c in DM?

Is the same code (as introduced in this commit) available in Linux kernel?
How the error is reproduced? What are the symptoms of it?

In which way the pcf2127 needs special treatment (non I2C compatible)? How does it differ from other RTC devices?

Quoted 233 lines
---
 drivers/i2c/i2c-uclass.c |  2 ++
 drivers/i2c/mxc_i2c.c    | 70
+++++++++++++++++++++++++++++++++++----- drivers/rtc/pcf2127.c    |
18 ++++++----- include/i2c.h            |  2 ++
 4 files changed, 76 insertions(+), 16 deletions(-)
drivers/i2c/i2c-uclass.c +1 -0
index e47abf1833..18f7364d72 100644
@@ -141,6 +141,8 @@ int dm_i2c_read(struct udevice *dev, uint offset,
 uint8_t *buffer, int len) if (len) {
 		ptr->addr = chip->chip_addr;
 		ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ?
 I2C_M_TEN : 0;
+		ptr->flags |= chip->flags &

DM_I2C_CHIP_RD_NEED_STOP_BIT ?

drivers/i2c/i2c-uclass.c +1 -0
+			      I2C_M_RD_NEED_STOP_BIT : 0;
 		ptr->flags |= I2C_M_RD;
 		ptr->len = len;
 		ptr->buf = buffer;
drivers/i2c/mxc_i2c.c +0 -0
index 73b9807598..241367bac8 100644
@@ -962,6 +962,8 @@ static int mxc_i2c_xfer(struct udevice *bus,

struct i2c_msg *msg, int nmsgs) int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;

drivers/i2c/mxc_i2c.c +1 -0
 	int read_mode;
+	bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true :

false;

drivers/i2c/mxc_i2c.c +1 -0
+	unsigned int temp;
 
 	/* Here address len is set to -1 to not send any address at

first.
* Otherwise i2c_init_transfer will send the chip address
with write @@ -976,6 +978,7 @@ static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) read_mode = -1; /* So it's always different on the first message */ for (; nmsgs > 0; nmsgs--, msg++) { const int msg_is_read = !!(msg->flags & I2C_M_RD);

drivers/i2c/mxc_i2c.c +1 -0
+		bool next_is_read = nmsgs > 1 && (msg[1].flags &

I2C_M_RD);
debug("i2c_xfer: chip=0x%x, len=0x%x, dir=%c\n",
msg->addr, msg->len, msg_is_read ? 'R' : 'W');

drivers/i2c/mxc_i2c.c +6 -4
@@ -983,13 +986,16 @@ static int mxc_i2c_xfer(struct udevice *bus,
 struct i2c_msg *msg, int nmsgs) if (msg_is_read != read_mode) {
 			/* Send repeated start if not 1st message */
 			if (read_mode != -1) {
-				debug("i2c_xfer: [RSTART]\n");
-				ret = readb(base + (I2CR <<
 reg_shift));
-				ret |= I2CR_RSTA;
-				writeb(ret, base + (I2CR <<
 reg_shift));
+				if (!(msg[1].flags &
 I2C_M_RD_NEED_STOP_BIT)) {
+					debug("i2c_xfer:
 [RSTART]\n");
+					ret = readb(base + (I2CR <<
 reg_shift));
+					ret |= I2CR_RSTA;
+					writeb(ret, base + (I2CR <<
 reg_shift));
+				}
 			}
 			debug("i2c_xfer: [ADDR %02x | %c]\n",

msg->addr, msg_is_read ? 'R' : 'W');

drivers/i2c/mxc_i2c.c +1 -0
+
 			ret = tx_byte(i2c_bus, (msg->addr << 1) |

msg_is_read); if (ret < 0) {

drivers/i2c/mxc_i2c.c +1 -1
 				debug("i2c_xfer: [STOP]\n");
@@ -999,16 +1005,64 @@ static int mxc_i2c_xfer(struct udevice *bus,
 struct i2c_msg *msg, int nmsgs) read_mode = msg_is_read;
 		}
 
-		if (msg->flags & I2C_M_RD)
+		if (msg->flags & I2C_M_RD) {
 			ret = i2c_read_data(i2c_bus, msg->addr,

msg->buf, msg->len, nmsgs == 1 ||
(msg->flags &
I2C_M_STOP));

drivers/i2c/mxc_i2c.c +0 -2
-		else
-			ret = i2c_write_data(i2c_bus, msg->addr,

msg->buf,

drivers/i2c/mxc_i2c.c +21 -1
-					     msg->len);
+			if (ret < 0)
+				break;
+			continue;
+		}
 
+		 /* Write message */
+		ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
+				     msg->len);
 		if (ret < 0)
 			break;
+
+		if (!next_is_read)
+			continue;
+
+		/* Read message following write message */
+		if (msg[1].flags & I2C_M_RD_NEED_STOP_BIT) {
+			/* Generate a stop bit */
+			i2c_imx_stop(i2c_bus);
+			/* Reset i2c slave */
+			i2c_force_reset_slave();
+
+			/* Enable I2C controller */
+			if (quirk)
+				ret = readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+					& I2CR_IDIS;
+			else
+				ret = !(readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+						& I2CR_IEN);
+			if (ret) {
+				writeb(I2CR_IEN, base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+				/* Wait for controller to be stable

*/

drivers/i2c/mxc_i2c.c +5 -0
+				udelay(50);
+			}
+
+			/* Clear interrupt bit */
+			writeb(I2SR_IIF_CLEAR, base + (I2SR <<

reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_IDLE);

drivers/i2c/mxc_i2c.c +9 -0
+			if (ret < 0)
+				return ret;
+
+			/* Start I2C transaction */
+			temp = readb(base + (I2CR << reg_shift));
+			temp |= I2CR_MSTA;
+			writeb(temp, base + (I2CR << reg_shift));
+
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_BUSY);

drivers/i2c/mxc_i2c.c +8 -0
+			if (ret < 0)
+				return ret;
+
+			/* Enter transfer mode */
+			temp |= I2CR_MTX | I2CR_TX_NO_AK;
+			writeb(temp, base + (I2CR << reg_shift));
+			udelay(50);
+		}
 	}
 
 	if (ret)
drivers/rtc/pcf2127.c +10 -8
index dcf0340b4d..010c45ecbf 100644
@@ -24,12 +24,9 @@
 
 static int pcf2127_rtc_set(struct udevice *dev, const struct
 rtc_time *tm) {
-	uchar buf[8];
+	uchar buf[7] = {0};
 	int i = 0, ret;
 
-	/* start register address */
-	buf[i++] = PCF2127_REG_SC;
-
 	/* hours, minutes and seconds */
 	buf[i++] = bin2bcd(tm->tm_sec);
 	buf[i++] = bin2bcd(tm->tm_min);
@@ -44,7 +41,7 @@ static int pcf2127_rtc_set(struct udevice *dev,
 const struct rtc_time *tm) buf[i++] = bin2bcd(tm->tm_year % 100);
 
 	/* write register's data */
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
+	ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
 
 	return ret;
 }
@@ -54,9 +51,6 @@ static int pcf2127_rtc_get(struct udevice *dev,
 struct rtc_time *tm) int ret = 0;
 	uchar buf[10] = { PCF2127_REG_CTRL1 };
 
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
-	if (ret < 0)
-		return ret;
 	ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
 	if (ret < 0)
 		return ret;
@@ -90,6 +84,13 @@ static int pcf2127_rtc_reset(struct udevice *dev)
 	return 0;
 }
 
+static int pcf2127_probe(struct udevice *dev)
+{
+	i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_NEED_STOP_BIT);
+
+	return 0;
+}
+
 static const struct rtc_ops pcf2127_rtc_ops = {
 	.get = pcf2127_rtc_get,
 	.set = pcf2127_rtc_set,
@@ -104,6 +105,7 @@ static const struct udevice_id pcf2127_rtc_ids[]
 = { U_BOOT_DRIVER(rtc_pcf2127) = {
 	.name	= "rtc-pcf2127",
 	.id	= UCLASS_RTC,
+	.probe	= pcf2127_probe,
 	.of_match = pcf2127_rtc_ids,
 	.ops	= &pcf2127_rtc_ops,
 };
include/i2c.h +0 -0
index a5c760c711..beaa028349 100644
@@ -28,6 +28,7 @@ enum dm_i2c_chip_flags {
 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit addressing
 */ DM_I2C_CHIP_RD_ADDRESS	= 1 << 1, /* Send address for each

read byte */ DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address
for each write byte */

include/i2c.h +1 -0
+	DM_I2C_CHIP_RD_NEED_STOP_BIT    = 1 << 3, /* Need generate

stop bit */ };

struct udevice;

include/i2c.h +0 -0
@@ -87,6 +88,7 @@ enum dm_i2c_msg_flags {
 	I2C_M_IGNORE_NAK	= 0x1000, /* continue after NAK */
 	I2C_M_NO_RD_ACK		= 0x0800, /* skip the Ack bit

on reads */ I2C_M_RECV_LEN = 0x0400, /* length is
first received byte */

include/i2c.h +1 -0
+	I2C_M_RD_NEED_STOP_BIT  = 0x0002, /* need generate stop bit

*/ };

Quoted 1 line

/**

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de -------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190522/4f80dec4/attachment.sig>

#2 lore raw
Quoted 12 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 15:16
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org
Subject: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 8 lines
Quoted 2 lines

Because i2c driver does not generate a stop bit when reading registers of pcf2127

The change (in the common i2c code) is rather large when considering the description above.

Could you write a more detailed patch description? Is this only the problem with i2c in DM?

OK, This is a problem with the i2c transport framework. After writing the register address that you want to read, the stop signal is missing before reading the register data.

Quoted 2 lines

Is the same code (as introduced in this commit) available in Linux kernel?

The kernel does not have such a problem

Quoted 1 line

How the error is reproduced? What are the symptoms of it?

You can use the i2c command to read the register data of pcf2127. You will find that you want to read the address 0 of the register, but the data of the 1 address is read, and the data read later will be offset.

Quoted 3 lines

In which way the pcf2127 needs special treatment (non I2C compatible)? How does it differ from other RTC devices?

According to the pcf2127 chip manual, when you need to read the internal register data, you need to write the register address first and then need stop bit(it is necessary).

Quoted 247 lines
Quoted 232 lines
---
 drivers/i2c/i2c-uclass.c |  2 ++
 drivers/i2c/mxc_i2c.c    | 70
+++++++++++++++++++++++++++++++++++----- drivers/rtc/pcf2127.c    |
18 ++++++----- include/i2c.h            |  2 ++
 4 files changed, 76 insertions(+), 16 deletions(-)
drivers/i2c/i2c-uclass.c index renamed from drivers/i2c/i2c-uclass.c +0 -0

e47abf1833..18f7364d72 100644

drivers/i2c/i2c-uclass.c +1 -0
@@ -141,6 +141,8 @@ int dm_i2c_read(struct udevice *dev, uint offset,
 uint8_t *buffer, int len) if (len) {
 		ptr->addr = chip->chip_addr;
 		ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ?
 I2C_M_TEN : 0;
+		ptr->flags |= chip->flags &

DM_I2C_CHIP_RD_NEED_STOP_BIT ?

drivers/i2c/i2c-uclass.c +1 -0
+			      I2C_M_RD_NEED_STOP_BIT : 0;
 		ptr->flags |= I2C_M_RD;
 		ptr->len = len;
 		ptr->buf = buffer;
drivers/i2c/mxc_i2c.c index renamed from drivers/i2c/mxc_i2c.c +0 -0

73b9807598..241367bac8 100644

drivers/i2c/mxc_i2c.c +0 -0
@@ -962,6 +962,8 @@ static int mxc_i2c_xfer(struct udevice *bus,

struct i2c_msg *msg, int nmsgs) int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;

drivers/i2c/mxc_i2c.c +1 -0
 	int read_mode;
+	bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true :

false;

drivers/i2c/mxc_i2c.c +1 -0
+	unsigned int temp;
 
 	/* Here address len is set to -1 to not send any address at first.
 	 * Otherwise i2c_init_transfer will send the chip address with write
@@ -976,6 +978,7 @@ static int mxc_i2c_xfer(struct udevice *bus,

struct i2c_msg *msg, int nmsgs) read_mode = -1; /* So it's always different on the first message */ for (; nmsgs > 0; nmsgs--, msg++) { const int msg_is_read = !!(msg->flags & I2C_M_RD);

drivers/i2c/mxc_i2c.c +1 -0
+		bool next_is_read = nmsgs > 1 && (msg[1].flags &

I2C_M_RD);
debug("i2c_xfer: chip=0x%x, len=0x%x, dir=%c\n",
msg->addr, msg->len, msg_is_read ? 'R' : 'W');

drivers/i2c/mxc_i2c.c +6 -4
@@ -983,13 +986,16 @@ static int mxc_i2c_xfer(struct udevice *bus,
 struct i2c_msg *msg, int nmsgs) if (msg_is_read != read_mode) {
 			/* Send repeated start if not 1st message */
 			if (read_mode != -1) {
-				debug("i2c_xfer: [RSTART]\n");
-				ret = readb(base + (I2CR <<
 reg_shift));
-				ret |= I2CR_RSTA;
-				writeb(ret, base + (I2CR <<
 reg_shift));
+				if (!(msg[1].flags &
 I2C_M_RD_NEED_STOP_BIT)) {
+					debug("i2c_xfer:
 [RSTART]\n");
+					ret = readb(base + (I2CR <<
 reg_shift));
+					ret |= I2CR_RSTA;
+					writeb(ret, base + (I2CR <<
 reg_shift));
+				}
 			}
 			debug("i2c_xfer: [ADDR %02x | %c]\n",

msg->addr, msg_is_read ? 'R' : 'W');

drivers/i2c/mxc_i2c.c +1 -0
+
 			ret = tx_byte(i2c_bus, (msg->addr << 1) | msg_is_read); if (ret <

0) {

drivers/i2c/mxc_i2c.c +1 -1
 				debug("i2c_xfer: [STOP]\n");
@@ -999,16 +1005,64 @@ static int mxc_i2c_xfer(struct udevice *bus,
 struct i2c_msg *msg, int nmsgs) read_mode = msg_is_read;
 		}
 
-		if (msg->flags & I2C_M_RD)
+		if (msg->flags & I2C_M_RD) {
 			ret = i2c_read_data(i2c_bus, msg->addr,

msg->buf, msg->len, nmsgs == 1 ||
(msg->flags &
I2C_M_STOP));

drivers/i2c/mxc_i2c.c +0 -2
-		else
-			ret = i2c_write_data(i2c_bus, msg->addr,

msg->buf,

drivers/i2c/mxc_i2c.c +21 -1
-					     msg->len);
+			if (ret < 0)
+				break;
+			continue;
+		}
 
+		 /* Write message */
+		ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
+				     msg->len);
 		if (ret < 0)
 			break;
+
+		if (!next_is_read)
+			continue;
+
+		/* Read message following write message */
+		if (msg[1].flags & I2C_M_RD_NEED_STOP_BIT) {
+			/* Generate a stop bit */
+			i2c_imx_stop(i2c_bus);
+			/* Reset i2c slave */
+			i2c_force_reset_slave();
+
+			/* Enable I2C controller */
+			if (quirk)
+				ret = readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+					& I2CR_IDIS;
+			else
+				ret = !(readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+						& I2CR_IEN);
+			if (ret) {
+				writeb(I2CR_IEN, base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+				/* Wait for controller to be stable

*/

drivers/i2c/mxc_i2c.c +5 -0
+				udelay(50);
+			}
+
+			/* Clear interrupt bit */
+			writeb(I2SR_IIF_CLEAR, base + (I2SR <<

reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_IDLE);

drivers/i2c/mxc_i2c.c +9 -0
+			if (ret < 0)
+				return ret;
+
+			/* Start I2C transaction */
+			temp = readb(base + (I2CR << reg_shift));
+			temp |= I2CR_MSTA;
+			writeb(temp, base + (I2CR << reg_shift));
+
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_BUSY);

drivers/i2c/mxc_i2c.c +8 -0
+			if (ret < 0)
+				return ret;
+
+			/* Enter transfer mode */
+			temp |= I2CR_MTX | I2CR_TX_NO_AK;
+			writeb(temp, base + (I2CR << reg_shift));
+			udelay(50);
+		}
 	}
 
 	if (ret)
drivers/rtc/pcf2127.c index renamed from drivers/rtc/pcf2127.c +0 -0

dcf0340b4d..010c45ecbf 100644

drivers/rtc/pcf2127.c +9 -8
@@ -24,12 +24,9 @@
 
 static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time
 *tm) {
-	uchar buf[8];
+	uchar buf[7] = {0};
 	int i = 0, ret;
 
-	/* start register address */
-	buf[i++] = PCF2127_REG_SC;
-
 	/* hours, minutes and seconds */
 	buf[i++] = bin2bcd(tm->tm_sec);
 	buf[i++] = bin2bcd(tm->tm_min);
@@ -44,7 +41,7 @@ static int pcf2127_rtc_set(struct udevice *dev,
 const struct rtc_time *tm) buf[i++] = bin2bcd(tm->tm_year % 100);
 
 	/* write register's data */
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
+	ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
 
 	return ret;
 }
@@ -54,9 +51,6 @@ static int pcf2127_rtc_get(struct udevice *dev,
 struct rtc_time *tm) int ret = 0;
 	uchar buf[10] = { PCF2127_REG_CTRL1 };
 
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
-	if (ret < 0)
-		return ret;
 	ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
 	if (ret < 0)
 		return ret;
@@ -90,6 +84,13 @@ static int pcf2127_rtc_reset(struct udevice *dev)
 	return 0;
 }
 
+static int pcf2127_probe(struct udevice *dev) {
+	i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_NEED_STOP_BIT);
+
+	return 0;
+}
+
 static const struct rtc_ops pcf2127_rtc_ops = {
 	.get = pcf2127_rtc_get,
 	.set = pcf2127_rtc_set,
@@ -104,6 +105,7 @@ static const struct udevice_id pcf2127_rtc_ids[] =
 { U_BOOT_DRIVER(rtc_pcf2127) = {
 	.name	= "rtc-pcf2127",
 	.id	= UCLASS_RTC,
+	.probe	= pcf2127_probe,
 	.of_match = pcf2127_rtc_ids,
 	.ops	= &pcf2127_rtc_ops,
 };
include/i2c.h index renamed from include/i2c.h +0 -0

a5c760c711..beaa028349 100644

include/i2c.h +0 -0
@@ -28,6 +28,7 @@ enum dm_i2c_chip_flags {
 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit addressing
 */ DM_I2C_CHIP_RD_ADDRESS	= 1 << 1, /* Send address for each

read byte */ DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address
for each write byte */

include/i2c.h +1 -0
+	DM_I2C_CHIP_RD_NEED_STOP_BIT    = 1 << 3, /* Need generate

stop bit */ };

struct udevice;

include/i2c.h +0 -0
@@ -87,6 +88,7 @@ enum dm_i2c_msg_flags {
 	I2C_M_IGNORE_NAK	= 0x1000, /* continue after NAK */
 	I2C_M_NO_RD_ACK		= 0x0800, /* skip the Ack bit

on reads */ I2C_M_RECV_LEN = 0x0400, /* length is
first received byte */

include/i2c.h +1 -0
+	I2C_M_RD_NEED_STOP_BIT  = 0x0002, /* need generate stop bit

*/ };

Quoted 1 line

/**

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email:
lukma at denx.de

#3 lore raw

Hi Chuanhua,

Quoted 19 lines
Quoted 11 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 15:16
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org
Subject: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 8 lines
Quoted 2 lines

Because i2c driver does not generate a stop bit when reading registers of pcf2127

The change (in the common i2c code) is rather large when considering the description above.

Could you write a more detailed patch description? Is this only the problem with i2c in DM?

Quoted 7 lines

OK, This is a problem with the i2c transport framework. After writing the register address that you want to read, the stop signal is missing before reading the register data.

Quoted 3 lines

Is the same code (as introduced in this commit) available in Linux kernel?

The kernel does not have such a problem

Do you know maybe why such issue is not present on Linux kernel?

Quoted 2 lines
Quoted 1 line

How the error is reproduced? What are the symptoms of it?

You can use the i2c command to read the register data of pcf2127.

So the problem is with using "i2c ..." commands from U-Boot prompt?

Quoted 3 lines

You will find that you want to read the address 0 of the register, but the data of the 1 address is read, and the data read later will be offset.

As fair as I can tell the pcf2127 has its own DM aware driver at driver/rtc/pcf2127.c.

Is this driver broken so you need to modify the generic i.MX I2C code? Have you tried to test this driver on your setup?

Quoted 257 lines
Quoted 3 lines

In which way the pcf2127 needs special treatment (non I2C compatible)? How does it differ from other RTC devices?

According to the pcf2127 chip manual, when you need to read the internal register data, you need to write the register address first and then need stop bit(it is necessary).

Quoted 251 lines
Quoted 236 lines
---
 drivers/i2c/i2c-uclass.c |  2 ++
 drivers/i2c/mxc_i2c.c    | 70
+++++++++++++++++++++++++++++++++++----- drivers/rtc/pcf2127.c
| 18 ++++++----- include/i2c.h            |  2 ++
 4 files changed, 76 insertions(+), 16 deletions(-)
drivers/i2c/i2c-uclass.c +1 -0
index e47abf1833..18f7364d72 100644
@@ -141,6 +141,8 @@ int dm_i2c_read(struct udevice *dev, uint
 offset, uint8_t *buffer, int len) if (len) {
 		ptr->addr = chip->chip_addr;
 		ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ?
 I2C_M_TEN : 0;
+		ptr->flags |= chip->flags &

DM_I2C_CHIP_RD_NEED_STOP_BIT ?

drivers/i2c/i2c-uclass.c +1 -0
+			      I2C_M_RD_NEED_STOP_BIT : 0;
 		ptr->flags |= I2C_M_RD;
 		ptr->len = len;
 		ptr->buf = buffer;
drivers/i2c/mxc_i2c.c index renamed from drivers/i2c/mxc_i2c.c +0 -0

73b9807598..241367bac8 100644

drivers/i2c/mxc_i2c.c +0 -0
@@ -962,6 +962,8 @@ static int mxc_i2c_xfer(struct udevice *bus,

struct i2c_msg *msg, int nmsgs) int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ? VF610_I2C_REGSHIFT :
IMX_I2C_REGSHIFT; int read_mode;

drivers/i2c/mxc_i2c.c +1 -0
+	bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ?

true : false;

drivers/i2c/mxc_i2c.c +1 -0
+	unsigned int temp;
 
 	/* Here address len is set to -1 to not send any address

at first.
* Otherwise i2c_init_transfer will send the chip
address with write @@ -976,6 +978,7 @@ static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) read_mode = -1; /* So it's always different on the first message */ for (; nmsgs > 0; nmsgs--, msg++) { const int msg_is_read = !!(msg->flags & I2C_M_RD);

drivers/i2c/mxc_i2c.c +1 -0
+		bool next_is_read = nmsgs > 1 && (msg[1].flags &

I2C_M_RD);
debug("i2c_xfer: chip=0x%x, len=0x%x, dir=%c\n",
msg->addr, msg->len, msg_is_read ? 'R' : 'W');

drivers/i2c/mxc_i2c.c +6 -4
@@ -983,13 +986,16 @@ static int mxc_i2c_xfer(struct udevice *bus,
 struct i2c_msg *msg, int nmsgs) if (msg_is_read != read_mode) {
 			/* Send repeated start if not 1st
 message */ if (read_mode != -1) {
-				debug("i2c_xfer: [RSTART]\n");
-				ret = readb(base + (I2CR <<
 reg_shift));
-				ret |= I2CR_RSTA;
-				writeb(ret, base + (I2CR <<
 reg_shift));
+				if (!(msg[1].flags &
 I2C_M_RD_NEED_STOP_BIT)) {
+					debug("i2c_xfer:
 [RSTART]\n");
+					ret = readb(base + (I2CR
 << reg_shift));
+					ret |= I2CR_RSTA;
+					writeb(ret, base + (I2CR
 << reg_shift));
+				}
 			}
 			debug("i2c_xfer: [ADDR %02x | %c]\n",

msg->addr, msg_is_read ? 'R' : 'W');

drivers/i2c/mxc_i2c.c +1 -0
+
 			ret = tx_byte(i2c_bus, (msg->addr << 1)

| msg_is_read); if (ret < 0) {

drivers/i2c/mxc_i2c.c +1 -1
 				debug("i2c_xfer: [STOP]\n");
@@ -999,16 +1005,64 @@ static int mxc_i2c_xfer(struct udevice
 *bus, struct i2c_msg *msg, int nmsgs) read_mode = msg_is_read;
 		}
 
-		if (msg->flags & I2C_M_RD)
+		if (msg->flags & I2C_M_RD) {
 			ret = i2c_read_data(i2c_bus, msg->addr,

msg->buf, msg->len, nmsgs == 1 ||
(msg->flags
& I2C_M_STOP));

drivers/i2c/mxc_i2c.c +0 -2
-		else
-			ret = i2c_write_data(i2c_bus, msg->addr,

msg->buf,

drivers/i2c/mxc_i2c.c +6 -1
-					     msg->len);
+			if (ret < 0)
+				break;
+			continue;
+		}
 
+		 /* Write message */
+		ret = i2c_write_data(i2c_bus, msg->addr,

msg->buf,

drivers/i2c/mxc_i2c.c +15 -0
+				     msg->len);
 		if (ret < 0)
 			break;
+
+		if (!next_is_read)
+			continue;
+
+		/* Read message following write message */
+		if (msg[1].flags & I2C_M_RD_NEED_STOP_BIT) {
+			/* Generate a stop bit */
+			i2c_imx_stop(i2c_bus);
+			/* Reset i2c slave */
+			i2c_force_reset_slave();
+
+			/* Enable I2C controller */
+			if (quirk)
+				ret = readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+					& I2CR_IDIS;
+			else
+				ret = !(readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+						& I2CR_IEN);
+			if (ret) {
+				writeb(I2CR_IEN, base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+				/* Wait for controller to be

stable */

drivers/i2c/mxc_i2c.c +5 -0
+				udelay(50);
+			}
+
+			/* Clear interrupt bit */
+			writeb(I2SR_IIF_CLEAR, base + (I2SR <<

reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_IDLE);

drivers/i2c/mxc_i2c.c +9 -0
+			if (ret < 0)
+				return ret;
+
+			/* Start I2C transaction */
+			temp = readb(base + (I2CR << reg_shift));
+			temp |= I2CR_MSTA;
+			writeb(temp, base + (I2CR << reg_shift));
+
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_BUSY);

drivers/i2c/mxc_i2c.c +8 -0
+			if (ret < 0)
+				return ret;
+
+			/* Enter transfer mode */
+			temp |= I2CR_MTX | I2CR_TX_NO_AK;
+			writeb(temp, base + (I2CR << reg_shift));
+			udelay(50);
+		}
 	}
 
 	if (ret)
drivers/rtc/pcf2127.c index renamed from drivers/rtc/pcf2127.c +0 -0

dcf0340b4d..010c45ecbf 100644

drivers/rtc/pcf2127.c +2 -5
@@ -24,12 +24,9 @@
 
 static int pcf2127_rtc_set(struct udevice *dev, const struct
 rtc_time *tm) {
-	uchar buf[8];
+	uchar buf[7] = {0};
 	int i = 0, ret;
 
-	/* start register address */
-	buf[i++] = PCF2127_REG_SC;
-
 	/* hours, minutes and seconds */
 	buf[i++] = bin2bcd(tm->tm_sec);
 	buf[i++] = bin2bcd(tm->tm_min);
@@ -44,7 +41,7 @@ static int pcf2127_rtc_set(struct udevice *dev,
 const struct rtc_time *tm) buf[i++] = bin2bcd(tm->tm_year % 100);
 
 	/* write register's data */
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf,
 sizeof(buf));
+	ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
 
 	return ret;

}

drivers/rtc/pcf2127.c +7 -3
@@ -54,9 +51,6 @@ static int pcf2127_rtc_get(struct udevice *dev,
 struct rtc_time *tm) int ret = 0;
 	uchar buf[10] = { PCF2127_REG_CTRL1 };
 
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
-	if (ret < 0)
-		return ret;
 	ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf,
 sizeof(buf)); if (ret < 0)
 		return ret;
@@ -90,6 +84,13 @@ static int pcf2127_rtc_reset(struct udevice
 *dev) return 0;
 }
 
+static int pcf2127_probe(struct udevice *dev) {
+	i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_NEED_STOP_BIT);
+
+	return 0;
+}
+
 static const struct rtc_ops pcf2127_rtc_ops = {
 	.get = pcf2127_rtc_get,
 	.set = pcf2127_rtc_set,
@@ -104,6 +105,7 @@ static const struct udevice_id
 pcf2127_rtc_ids[] = { U_BOOT_DRIVER(rtc_pcf2127) = {
 	.name	= "rtc-pcf2127",
 	.id	= UCLASS_RTC,
+	.probe	= pcf2127_probe,
 	.of_match = pcf2127_rtc_ids,
 	.ops	= &pcf2127_rtc_ops,
 };
include/i2c.h index renamed from include/i2c.h +0 -0

a5c760c711..beaa028349 100644

include/i2c.h +0 -0
@@ -28,6 +28,7 @@ enum dm_i2c_chip_flags {
 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit

addressing */ DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send
address for each read byte */ DM_I2C_CHIP_WR_ADDRESS = 1
<< 2, /* Send address for each write byte */

include/i2c.h +1 -0
+	DM_I2C_CHIP_RD_NEED_STOP_BIT    = 1 << 3, /* Need

generate stop bit */ };

struct udevice;

include/i2c.h +0 -0
@@ -87,6 +88,7 @@ enum dm_i2c_msg_flags {
 	I2C_M_IGNORE_NAK	= 0x1000, /* continue after NAK
 */ I2C_M_NO_RD_ACK		= 0x0800, /* skip the Ack bit

on reads */ I2C_M_RECV_LEN = 0x0400, /* length is
first received byte */

include/i2c.h +1 -0
+	I2C_M_RD_NEED_STOP_BIT  = 0x0002, /* need generate stop

bit */ };

Quoted 1 line

/**

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang
Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email:
lukma at denx.de

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de -------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190522/9c0c877a/attachment.sig>

#4 lore raw
Quoted 35 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 16:41
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 23 lines
Quoted 11 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 15:16
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org
Subject: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 8 lines
Quoted 2 lines

Because i2c driver does not generate a stop bit when reading registers of pcf2127

The change (in the common i2c code) is rather large when considering the description above.

Could you write a more detailed patch description? Is this only the problem with i2c in DM?

OK, This is a problem with the i2c transport framework. After writing the register address that you want to read, the stop signal is missing before reading the register data.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Is the same code (as introduced in this commit) available in Linux kernel?

The kernel does not have such a problem

Do you know maybe why such issue is not present on Linux kernel?

The kernel code is encapsulated when reading the pcf2127 register, which separates the read and write, thus generating a stop signal before reading the register.
Eg: Here is the wrapper made by the kernel code:
static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
void *val, size_t val_size)

{
        struct device *dev = context;
        struct i2c_client *client = to_i2c_client(dev);
        int ret;

        if (WARN_ON(reg_size != 1)) 
                return -EINVAL;

        ret = i2c_master_send(client, reg, 1); 
        if (ret != 1)
                return ret < 0 ? ret : -EIO;

        ret = i2c_master_recv(client, val, val_size);
        if (ret != val_size)
                return ret < 0 ? ret : -EIO;

        return 0;
}
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

How the error is reproduced? What are the symptoms of it?

You can use the i2c command to read the register data of pcf2127.

So the problem is with using "i2c ..." commands from U-Boot prompt?

Yes,but adding debugging to the rtc driver is also the same problem

Quoted 10 lines
Quoted 4 lines

You will find that you want to read the address 0 of the register, but the data of the 1 address is read, and the data read later will be offset.

As fair as I can tell the pcf2127 has its own DM aware driver at driver/rtc/pcf2127.c.

Is this driver broken so you need to modify the generic i.MX I2C code? Have you tried to test this driver on your setup?

Pcf2127 driver also has problems, has been modified, need i2c general code to support

Quoted 272 lines
Quoted 258 lines
Quoted 4 lines

In which way the pcf2127 needs special treatment (non I2C compatible)? How does it differ from other RTC devices?

According to the pcf2127 chip manual, when you need to read the internal register data, you need to write the register address first and then need stop bit(it is necessary).

Quoted 251 lines
Quoted 236 lines
---
 drivers/i2c/i2c-uclass.c |  2 ++
 drivers/i2c/mxc_i2c.c    | 70
+++++++++++++++++++++++++++++++++++----- drivers/rtc/pcf2127.c
| 18 ++++++----- include/i2c.h            |  2 ++
 4 files changed, 76 insertions(+), 16 deletions(-)
drivers/i2c/i2c-uclass.c +1 -0
index e47abf1833..18f7364d72 100644
@@ -141,6 +141,8 @@ int dm_i2c_read(struct udevice *dev, uint
 offset, uint8_t *buffer, int len) if (len) {
 		ptr->addr = chip->chip_addr;
 		ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ?
 I2C_M_TEN : 0;
+		ptr->flags |= chip->flags &

DM_I2C_CHIP_RD_NEED_STOP_BIT ?

drivers/i2c/i2c-uclass.c +1 -0
+			      I2C_M_RD_NEED_STOP_BIT : 0;
 		ptr->flags |= I2C_M_RD;
 		ptr->len = len;
 		ptr->buf = buffer;
drivers/i2c/mxc_i2c.c index renamed from drivers/i2c/mxc_i2c.c +0 -0

73b9807598..241367bac8 100644

drivers/i2c/mxc_i2c.c +0 -0
@@ -962,6 +962,8 @@ static int mxc_i2c_xfer(struct udevice *bus,

struct i2c_msg *msg, int nmsgs) int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ? VF610_I2C_REGSHIFT :
IMX_I2C_REGSHIFT; int read_mode;

drivers/i2c/mxc_i2c.c +1 -0
+	bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ?

true : false;

drivers/i2c/mxc_i2c.c +1 -0
+	unsigned int temp;
 
 	/* Here address len is set to -1 to not send any address

at first.
* Otherwise i2c_init_transfer will send the chip
address with write @@ -976,6 +978,7 @@ static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) read_mode = -1; /* So it's always different on the first message */ for (; nmsgs > 0; nmsgs--, msg++) { const int msg_is_read = !!(msg->flags & I2C_M_RD);

drivers/i2c/mxc_i2c.c +1 -0
+		bool next_is_read = nmsgs > 1 && (msg[1].flags &

I2C_M_RD);
debug("i2c_xfer: chip=0x%x, len=0x%x, dir=%c\n",
msg->addr, msg->len, msg_is_read ? 'R' : 'W');

drivers/i2c/mxc_i2c.c +6 -4
@@ -983,13 +986,16 @@ static int mxc_i2c_xfer(struct udevice *bus,
 struct i2c_msg *msg, int nmsgs) if (msg_is_read != read_mode) {
 			/* Send repeated start if not 1st
 message */ if (read_mode != -1) {
-				debug("i2c_xfer: [RSTART]\n");
-				ret = readb(base + (I2CR <<
 reg_shift));
-				ret |= I2CR_RSTA;
-				writeb(ret, base + (I2CR <<
 reg_shift));
+				if (!(msg[1].flags &
 I2C_M_RD_NEED_STOP_BIT)) {
+					debug("i2c_xfer:
 [RSTART]\n");
+					ret = readb(base + (I2CR
 << reg_shift));
+					ret |= I2CR_RSTA;
+					writeb(ret, base + (I2CR
 << reg_shift));
+				}
 			}
 			debug("i2c_xfer: [ADDR %02x | %c]\n",

msg->addr, msg_is_read ? 'R' : 'W');

drivers/i2c/mxc_i2c.c +1 -0
+
 			ret = tx_byte(i2c_bus, (msg->addr << 1)

| msg_is_read); if (ret < 0) {

drivers/i2c/mxc_i2c.c +1 -1
 				debug("i2c_xfer: [STOP]\n");
@@ -999,16 +1005,64 @@ static int mxc_i2c_xfer(struct udevice
 *bus, struct i2c_msg *msg, int nmsgs) read_mode = msg_is_read;
 		}
 
-		if (msg->flags & I2C_M_RD)
+		if (msg->flags & I2C_M_RD) {
 			ret = i2c_read_data(i2c_bus, msg->addr,

msg->buf, msg->len, nmsgs == 1 ||
(msg->flags
& I2C_M_STOP));

drivers/i2c/mxc_i2c.c +0 -2
-		else
-			ret = i2c_write_data(i2c_bus, msg->addr,

msg->buf,

drivers/i2c/mxc_i2c.c +6 -1
-					     msg->len);
+			if (ret < 0)
+				break;
+			continue;
+		}
 
+		 /* Write message */
+		ret = i2c_write_data(i2c_bus, msg->addr,

msg->buf,

drivers/i2c/mxc_i2c.c +15 -0
+				     msg->len);
 		if (ret < 0)
 			break;
+
+		if (!next_is_read)
+			continue;
+
+		/* Read message following write message */
+		if (msg[1].flags & I2C_M_RD_NEED_STOP_BIT) {
+			/* Generate a stop bit */
+			i2c_imx_stop(i2c_bus);
+			/* Reset i2c slave */
+			i2c_force_reset_slave();
+
+			/* Enable I2C controller */
+			if (quirk)
+				ret = readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+					& I2CR_IDIS;
+			else
+				ret = !(readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+						& I2CR_IEN);
+			if (ret) {
+				writeb(I2CR_IEN, base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+				/* Wait for controller to be

stable */

drivers/i2c/mxc_i2c.c +5 -0
+				udelay(50);
+			}
+
+			/* Clear interrupt bit */
+			writeb(I2SR_IIF_CLEAR, base + (I2SR <<

reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_IDLE);

drivers/i2c/mxc_i2c.c +9 -0
+			if (ret < 0)
+				return ret;
+
+			/* Start I2C transaction */
+			temp = readb(base + (I2CR << reg_shift));
+			temp |= I2CR_MSTA;
+			writeb(temp, base + (I2CR << reg_shift));
+
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_BUSY);

drivers/i2c/mxc_i2c.c +8 -0
+			if (ret < 0)
+				return ret;
+
+			/* Enter transfer mode */
+			temp |= I2CR_MTX | I2CR_TX_NO_AK;
+			writeb(temp, base + (I2CR << reg_shift));
+			udelay(50);
+		}
 	}
 
 	if (ret)
drivers/rtc/pcf2127.c index renamed from drivers/rtc/pcf2127.c +0 -0

dcf0340b4d..010c45ecbf 100644

drivers/rtc/pcf2127.c +2 -5
@@ -24,12 +24,9 @@
 
 static int pcf2127_rtc_set(struct udevice *dev, const struct
 rtc_time *tm) {
-	uchar buf[8];
+	uchar buf[7] = {0};
 	int i = 0, ret;
 
-	/* start register address */
-	buf[i++] = PCF2127_REG_SC;
-
 	/* hours, minutes and seconds */
 	buf[i++] = bin2bcd(tm->tm_sec);
 	buf[i++] = bin2bcd(tm->tm_min);
@@ -44,7 +41,7 @@ static int pcf2127_rtc_set(struct udevice *dev,
 const struct rtc_time *tm) buf[i++] = bin2bcd(tm->tm_year % 100);
 
 	/* write register's data */
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf,
 sizeof(buf));
+	ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
 
 	return ret;

}

drivers/rtc/pcf2127.c +7 -3
@@ -54,9 +51,6 @@ static int pcf2127_rtc_get(struct udevice *dev,
 struct rtc_time *tm) int ret = 0;
 	uchar buf[10] = { PCF2127_REG_CTRL1 };
 
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
-	if (ret < 0)
-		return ret;
 	ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf,
 sizeof(buf)); if (ret < 0)
 		return ret;
@@ -90,6 +84,13 @@ static int pcf2127_rtc_reset(struct udevice
 *dev) return 0;
 }
 
+static int pcf2127_probe(struct udevice *dev) {
+	i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_NEED_STOP_BIT);
+
+	return 0;
+}
+
 static const struct rtc_ops pcf2127_rtc_ops = {
 	.get = pcf2127_rtc_get,
 	.set = pcf2127_rtc_set,
@@ -104,6 +105,7 @@ static const struct udevice_id
 pcf2127_rtc_ids[] = { U_BOOT_DRIVER(rtc_pcf2127) = {
 	.name	= "rtc-pcf2127",
 	.id	= UCLASS_RTC,
+	.probe	= pcf2127_probe,
 	.of_match = pcf2127_rtc_ids,
 	.ops	= &pcf2127_rtc_ops,
 };
include/i2c.h index renamed from include/i2c.h +0 -0

a5c760c711..beaa028349 100644

include/i2c.h +0 -0
@@ -28,6 +28,7 @@ enum dm_i2c_chip_flags {
 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit

addressing */ DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send
address for each read byte */ DM_I2C_CHIP_WR_ADDRESS = 1
<< 2, /* Send address for each write byte */

include/i2c.h +1 -0
+	DM_I2C_CHIP_RD_NEED_STOP_BIT    = 1 << 3, /* Need

generate stop bit */ };

struct udevice;

include/i2c.h +0 -0
@@ -87,6 +88,7 @@ enum dm_i2c_msg_flags {
 	I2C_M_IGNORE_NAK	= 0x1000, /* continue after NAK
 */ I2C_M_NO_RD_ACK		= 0x0800, /* skip the Ack bit

on reads */ I2C_M_RECV_LEN = 0x0400, /* length is
first received byte */

include/i2c.h +1 -0
+	I2C_M_RD_NEED_STOP_BIT  = 0x0002, /* need generate stop

bit */ };

Quoted 1 line

/**

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang
Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email:
lukma at denx.de

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email:
lukma at denx.de

#5 lore raw
Quoted 54 lines · On Wed, 22 May 2019 09:31:35 +0000 Chuanhua Han <[email protected]> wrote:
Quoted 34 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 16:41
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 23 lines
Quoted 11 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 15:16
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org
Subject: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 8 lines
Quoted 2 lines

Because i2c driver does not generate a stop bit when reading registers of pcf2127

The change (in the common i2c code) is rather large when considering the description above.

Could you write a more detailed patch description? Is this only the problem with i2c in DM?

OK, This is a problem with the i2c transport framework. After writing the register address that you want to read, the stop signal is missing before reading the register data.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Is the same code (as introduced in this commit) available in Linux kernel?

The kernel does not have such a problem

Do you know maybe why such issue is not present on Linux kernel?

The kernel code is encapsulated when reading the pcf2127 register, which separates the read and write, thus generating a stop signal before reading the register. Eg: Here is the wrapper made by the kernel code: static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size, void *val, size_t val_size)

{
        struct device *dev = context;
        struct i2c_client *client = to_i2c_client(dev);
        int ret;

        if (WARN_ON(reg_size != 1)) 
                return -EINVAL;

        ret = i2c_master_send(client, reg, 1); 
        if (ret != 1)
Quoted 8 lines
                return ret < 0 ? ret : -EIO;

        ret = i2c_master_recv(client, val, val_size);
        if (ret != val_size)
                return ret < 0 ? ret : -EIO;

        return 0;
}

That was my point - the same shall be done in the pcf2127 driver. This is a slow device, so we can afford for this.

Quoted 13 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

How the error is reproduced? What are the symptoms of it?

You can use the i2c command to read the register data of pcf2127.

So the problem is with using "i2c ..." commands from U-Boot prompt?

Yes,but adding debugging to the rtc driver is also the same problem

Quoted 5 lines
Quoted 4 lines

You will find that you want to read the address 0 of the register, but the data of the 1 address is read, and the data read later will be offset.

Quoted 7 lines
Quoted 5 lines

As fair as I can tell the pcf2127 has its own DM aware driver at driver/rtc/pcf2127.c.

Is this driver broken so you need to modify the generic i.MX I2C code? Have you tried to test this driver on your setup?

Pcf2127 driver also has problems, has been modified, need i2c general code to support

Just one remark the mxc_i2c.c is IMX specific I2C code. Not the generic one.

Moreover, it looks like the same approach (first send, then read) is performed in the pcf2127 driver at pcf2127_rtc_get() function.

I think that the driver shall be first thoroughly checked, then fixes shall be added to it.

Quoted 268 lines
Quoted 268 lines
Quoted 262 lines
Quoted 4 lines

In which way the pcf2127 needs special treatment (non I2C compatible)? How does it differ from other RTC devices?

According to the pcf2127 chip manual, when you need to read the internal register data, you need to write the register address first and then need stop bit(it is necessary).

Quoted 255 lines
Quoted 240 lines
---
 drivers/i2c/i2c-uclass.c |  2 ++
 drivers/i2c/mxc_i2c.c    | 70
+++++++++++++++++++++++++++++++++++----- drivers/rtc/pcf2127.c
| 18 ++++++----- include/i2c.h            |  2 ++
 4 files changed, 76 insertions(+), 16 deletions(-)
(quoted diff) +0 -0

b/drivers/i2c/i2c-uclass.c index e47abf1833..18f7364d72 100644

drivers/i2c/i2c-uclass.c +1 -0
@@ -141,6 +141,8 @@ int dm_i2c_read(struct udevice *dev, uint
 offset, uint8_t *buffer, int len) if (len) {
 		ptr->addr = chip->chip_addr;
 		ptr->flags = chip->flags &
 DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
+		ptr->flags |= chip->flags &

DM_I2C_CHIP_RD_NEED_STOP_BIT ?

drivers/i2c/i2c-uclass.c +1 -0
+			      I2C_M_RD_NEED_STOP_BIT : 0;
 		ptr->flags |= I2C_M_RD;
 		ptr->len = len;
 		ptr->buf = buffer;
drivers/i2c/mxc_i2c.c +0 -0
index 73b9807598..241367bac8 100644
@@ -962,6 +962,8 @@ static int mxc_i2c_xfer(struct udevice
 *bus, struct i2c_msg *msg, int nmsgs) int reg_shift =

i2c_bus->driver_data & I2C_QUIRK_FLAG ? VF610_I2C_REGSHIFT :
IMX_I2C_REGSHIFT; int read_mode;

drivers/i2c/mxc_i2c.c +1 -0
+	bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ?

true : false;

drivers/i2c/mxc_i2c.c +1 -0
+	unsigned int temp;
 
 	/* Here address len is set to -1 to not send any

address at first.
* Otherwise i2c_init_transfer will send the chip
address with write @@ -976,6 +978,7 @@ static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) read_mode = -1; /* So it's always different on the first message */ for (; nmsgs > 0; nmsgs--, msg++) { const int msg_is_read = !!(msg->flags & I2C_M_RD);

drivers/i2c/mxc_i2c.c +1 -0
+		bool next_is_read = nmsgs > 1 &&

(msg[1].flags & I2C_M_RD);
debug("i2c_xfer: chip=0x%x, len=0x%x,
dir=%c\n", msg->addr, msg->len, msg_is_read ? 'R' : 'W');

drivers/i2c/mxc_i2c.c +0 -0
@@ -983,13 +986,16 @@ static int mxc_i2c_xfer(struct udevice
 *bus, struct i2c_msg *msg, int nmsgs) if (msg_is_read !=

read_mode) { /* Send repeated start if not 1st message */ if (read_mode != -1) {

drivers/i2c/mxc_i2c.c +0 -1
-				debug("i2c_xfer:

[RSTART]\n");

drivers/i2c/mxc_i2c.c +0 -1
-				ret = readb(base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +0 -2
-				ret |= I2CR_RSTA;
-				writeb(ret, base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+				if (!(msg[1].flags &

I2C_M_RD_NEED_STOP_BIT)) {

drivers/i2c/mxc_i2c.c +1 -0
+					debug("i2c_xfer:

[RSTART]\n");

drivers/i2c/mxc_i2c.c +1 -0
+					ret = readb(base +

(I2CR << reg_shift));

drivers/i2c/mxc_i2c.c +2 -0
+					ret |= I2CR_RSTA;
+					writeb(ret, base +

(I2CR << reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+				}
 			}
 			debug("i2c_xfer: [ADDR %02x | %c]\n",

msg->addr, msg_is_read ? 'R' : 'W');

drivers/i2c/mxc_i2c.c +1 -0
+
 			ret = tx_byte(i2c_bus, (msg->addr <<

1) | msg_is_read); if (ret < 0) {

drivers/i2c/mxc_i2c.c +1 -1
 				debug("i2c_xfer: [STOP]\n");
@@ -999,16 +1005,64 @@ static int mxc_i2c_xfer(struct udevice
 *bus, struct i2c_msg *msg, int nmsgs) read_mode = msg_is_read;
 		}
 
-		if (msg->flags & I2C_M_RD)
+		if (msg->flags & I2C_M_RD) {
 			ret = i2c_read_data(i2c_bus,

msg->addr, msg->buf, msg->len, nmsgs == 1 ||
(msg->flags
& I2C_M_STOP));

drivers/i2c/mxc_i2c.c +0 -2
-		else
-			ret = i2c_write_data(i2c_bus,

msg->addr, msg->buf,

drivers/i2c/mxc_i2c.c +6 -1
-					     msg->len);
+			if (ret < 0)
+				break;
+			continue;
+		}
 
+		 /* Write message */
+		ret = i2c_write_data(i2c_bus, msg->addr,

msg->buf,

drivers/i2c/mxc_i2c.c +15 -0
+				     msg->len);
 		if (ret < 0)
 			break;
+
+		if (!next_is_read)
+			continue;
+
+		/* Read message following write message */
+		if (msg[1].flags & I2C_M_RD_NEED_STOP_BIT) {
+			/* Generate a stop bit */
+			i2c_imx_stop(i2c_bus);
+			/* Reset i2c slave */
+			i2c_force_reset_slave();
+
+			/* Enable I2C controller */
+			if (quirk)
+				ret = readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+					& I2CR_IDIS;
+			else
+				ret = !(readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+						& I2CR_IEN);
+			if (ret) {
+				writeb(I2CR_IEN, base +

(I2CR << reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+				/* Wait for controller to be

stable */

drivers/i2c/mxc_i2c.c +5 -0
+				udelay(50);
+			}
+
+			/* Clear interrupt bit */
+			writeb(I2SR_IIF_CLEAR, base + (I2SR

<< reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_IDLE);

drivers/i2c/mxc_i2c.c +5 -0
+			if (ret < 0)
+				return ret;
+
+			/* Start I2C transaction */
+			temp = readb(base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +2 -0
+			temp |= I2CR_MSTA;
+			writeb(temp, base + (I2CR <<

reg_shift)); +

drivers/i2c/mxc_i2c.c +1 -0
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_BUSY);

drivers/i2c/mxc_i2c.c +6 -0
+			if (ret < 0)
+				return ret;
+
+			/* Enter transfer mode */
+			temp |= I2CR_MTX | I2CR_TX_NO_AK;
+			writeb(temp, base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +2 -0
+			udelay(50);
+		}
 	}
 
 	if (ret)
drivers/rtc/pcf2127.c +2 -5
index dcf0340b4d..010c45ecbf 100644
@@ -24,12 +24,9 @@
 
 static int pcf2127_rtc_set(struct udevice *dev, const struct
 rtc_time *tm) {
-	uchar buf[8];
+	uchar buf[7] = {0};
 	int i = 0, ret;
 
-	/* start register address */
-	buf[i++] = PCF2127_REG_SC;
-
 	/* hours, minutes and seconds */
 	buf[i++] = bin2bcd(tm->tm_sec);
 	buf[i++] = bin2bcd(tm->tm_min);
@@ -44,7 +41,7 @@ static int pcf2127_rtc_set(struct udevice
 *dev, const struct rtc_time *tm) buf[i++] =
 bin2bcd(tm->tm_year % 100);
 
 	/* write register's data */
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf,
 sizeof(buf));
+	ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
 
drivers/rtc/pcf2127.c +2 -3
 	return ret;
 }
@@ -54,9 +51,6 @@ static int pcf2127_rtc_get(struct udevice
 *dev, struct rtc_time *tm) int ret = 0;
 	uchar buf[10] = { PCF2127_REG_CTRL1 };
 
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
-	if (ret < 0)
-		return ret;
 	ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf,
 sizeof(buf)); if (ret < 0)
 		return ret;
@@ -90,6 +84,13 @@ static int pcf2127_rtc_reset(struct udevice
 *dev) return 0;
 }
 
+static int pcf2127_probe(struct udevice *dev) {
+	i2c_set_chip_flags(dev,

DM_I2C_CHIP_RD_NEED_STOP_BIT); +

drivers/rtc/pcf2127.c +4 -0
+	return 0;
+}
+
 static const struct rtc_ops pcf2127_rtc_ops = {
 	.get = pcf2127_rtc_get,
 	.set = pcf2127_rtc_set,
@@ -104,6 +105,7 @@ static const struct udevice_id
 pcf2127_rtc_ids[] = { U_BOOT_DRIVER(rtc_pcf2127) = {
 	.name	= "rtc-pcf2127",
 	.id	= UCLASS_RTC,
+	.probe	= pcf2127_probe,
 	.of_match = pcf2127_rtc_ids,
 	.ops	= &pcf2127_rtc_ops,
 };
include/i2c.h index renamed from include/i2c.h +0 -0

a5c760c711..beaa028349 100644

include/i2c.h +0 -0
@@ -28,6 +28,7 @@ enum dm_i2c_chip_flags {
 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit

addressing */ DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send
address for each read byte */ DM_I2C_CHIP_WR_ADDRESS =
1 << 2, /* Send address for each write byte */

include/i2c.h +1 -0
+	DM_I2C_CHIP_RD_NEED_STOP_BIT    = 1 << 3, /* Need

generate stop bit */ };

struct udevice;

include/i2c.h +0 -0
@@ -87,6 +88,7 @@ enum dm_i2c_msg_flags {
 	I2C_M_IGNORE_NAK	= 0x1000, /* continue after

NAK */ I2C_M_NO_RD_ACK = 0x0800, /* skip the
Ack bit on reads */ I2C_M_RECV_LEN =
0x0400, /* length is first received byte */

include/i2c.h +1 -0
+	I2C_M_RD_NEED_STOP_BIT  = 0x0002, /* need generate

stop bit */ };

Quoted 1 line

/**

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang
Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-59 Fax:
(+49)-8142-66989-80 Email: lukma at denx.de

Best regards,

Quoted 8 lines
Quoted 8 lines

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang
Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email:
lukma at denx.de

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de -------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190522/4c4da1ef/attachment.sig>

#6 lore raw
Quoted 72 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 19:32
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Quoted 59 lines · On Wed, 22 May 2019 09:31:35 +0000 Chuanhua Han <[email protected]> wrote:
Quoted 34 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 16:41
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 23 lines
Quoted 11 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 15:16
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org
Subject: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 8 lines
Quoted 2 lines

Because i2c driver does not generate a stop bit when reading registers of pcf2127

The change (in the common i2c code) is rather large when considering the description above.

Could you write a more detailed patch description? Is this only the problem with i2c in DM?

OK, This is a problem with the i2c transport framework. After writing the register address that you want to read, the stop signal is missing before reading the register data.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Is the same code (as introduced in this commit) available in Linux kernel?

The kernel does not have such a problem

Do you know maybe why such issue is not present on Linux kernel?

The kernel code is encapsulated when reading the pcf2127 register, which separates the read and write, thus generating a stop signal before reading the register. Eg: Here is the wrapper made by the kernel code: static int pcf2127_i2c_read(void *context, const void

*reg, size_t reg_size, void *val, size_t val_size) {
        struct device *dev = context;
        struct i2c_client *client = to_i2c_client(dev);
        int ret;

        if (WARN_ON(reg_size != 1))
                return -EINVAL;

        ret = i2c_master_send(client, reg, 1);
        if (ret != 1)
                return ret < 0 ? ret : -EIO;

        ret = i2c_master_recv(client, val, val_size);
        if (ret != val_size)
                return ret < 0 ? ret : -EIO;
Quoted 5 lines
Quoted 2 lines
        return 0;
}

That was my point - the same shall be done in the pcf2127 driver. This is a slow device, so we can afford for this.

There is no similar api in the uboot code to split the read data into the address of the send register and receive the data. Uboot encapsulates the transmit register address and the read data, so no stop signal is generated.

Quoted 15 lines
Quoted 15 lines
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

How the error is reproduced? What are the symptoms of it?

You can use the i2c command to read the register data of pcf2127.

So the problem is with using "i2c ..." commands from U-Boot prompt?

Yes,but adding debugging to the rtc driver is also the same problem

Quoted 8 lines
Quoted 4 lines

You will find that you want to read the address 0 of the register, but the data of the 1 address is read, and the data read later will be offset.

As fair as I can tell the pcf2127 has its own DM aware driver at driver/rtc/pcf2127.c.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is this driver broken so you need to modify the generic i.MX I2C code? Have you tried to test this driver on your setup?

Pcf2127 driver also has problems, has been modified, need i2c general code to support

Just one remark the mxc_i2c.c is IMX specific I2C code. Not the generic one.

ok

Quoted 299 lines

Moreover, it looks like the same approach (first send, then read) is performed in the pcf2127 driver at pcf2127_rtc_get() function.

I think that the driver shall be first thoroughly checked, then fixes shall be added to it.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

In which way the pcf2127 needs special treatment (non I2C compatible)? How does it differ from other RTC devices?

According to the pcf2127 chip manual, when you need to read the internal register data, you need to write the register address first and then need stop bit(it is necessary).

Quoted 2 lines
Quoted 2 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
---
 drivers/i2c/i2c-uclass.c |  2 ++
 drivers/i2c/mxc_i2c.c    | 70

+++++++++++++++++++++++++++++++++++-----

drivers/rtc/pcf2127.c

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines

| 18 ++++++----- include/i2c.h | 2 ++

 4 files changed, 76 insertions(+), 16 deletions(-)
(quoted diff) +0 -0

b/drivers/i2c/i2c-uclass.c index e47abf1833..18f7364d72 100644

drivers/i2c/i2c-uclass.c +1 -0
@@ -141,6 +141,8 @@ int dm_i2c_read(struct udevice *dev, uint
 offset, uint8_t *buffer, int len) if (len) {
 		ptr->addr = chip->chip_addr;
 		ptr->flags = chip->flags &
 DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
+		ptr->flags |= chip->flags &

DM_I2C_CHIP_RD_NEED_STOP_BIT ?

drivers/i2c/i2c-uclass.c +1 -0
+			      I2C_M_RD_NEED_STOP_BIT : 0;
 		ptr->flags |= I2C_M_RD;
 		ptr->len = len;
 		ptr->buf = buffer;
drivers/i2c/mxc_i2c.c +0 -0
index 73b9807598..241367bac8 100644
@@ -962,6 +962,8 @@ static int mxc_i2c_xfer(struct udevice
 *bus, struct i2c_msg *msg, int nmsgs) int reg_shift =

i2c_bus->driver_data & I2C_QUIRK_FLAG ? VF610_I2C_REGSHIFT :
IMX_I2C_REGSHIFT; int read_mode;

drivers/i2c/mxc_i2c.c +1 -0
+	bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ?

true : false;

drivers/i2c/mxc_i2c.c +1 -0
+	unsigned int temp;
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

/* Here address len is set to -1 to not send any address at
first.
* Otherwise i2c_init_transfer will send the chip address
with write @@ -976,6 +978,7 @@ static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int
nmsgs) read_mode = -1; /* So it's always different on the first message */ for (; nmsgs > 0; nmsgs--, msg++) { const int msg_is_read = !!(msg->flags & I2C_M_RD);

drivers/i2c/mxc_i2c.c +1 -0
+		bool next_is_read = nmsgs > 1 &&

(msg[1].flags & I2C_M_RD);
debug("i2c_xfer: chip=0x%x, len=0x%x, dir=%c\n",

msg->addr,

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

msg->len, msg_is_read ? 'R' : 'W'); @@ -983,13 +986,16 @@ static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) if (msg_is_read !=
read_mode) { /* Send repeated start if not 1st message */ if (read_mode != -1) {

drivers/i2c/mxc_i2c.c +0 -1
-				debug("i2c_xfer:

[RSTART]\n");

drivers/i2c/mxc_i2c.c +0 -1
-				ret = readb(base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +0 -2
-				ret |= I2CR_RSTA;
-				writeb(ret, base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+				if (!(msg[1].flags &

I2C_M_RD_NEED_STOP_BIT)) {

drivers/i2c/mxc_i2c.c +1 -0
+					debug("i2c_xfer:

[RSTART]\n");

drivers/i2c/mxc_i2c.c +1 -0
+					ret = readb(base +

(I2CR << reg_shift));

drivers/i2c/mxc_i2c.c +2 -0
+					ret |= I2CR_RSTA;
+					writeb(ret, base +

(I2CR << reg_shift));

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/i2c/mxc_i2c.c +1 -0
+				}
 			}
 			debug("i2c_xfer: [ADDR %02x | %c]\n",

msg->addr, msg_is_read ? 'R' : 'W');

drivers/i2c/mxc_i2c.c +1 -0
+
 			ret = tx_byte(i2c_bus, (msg->addr <<

1) | msg_is_read); if (ret < 0) {
debug("i2c_xfer: [STOP]\n"); @@ -999,16

+1005,64 @@

Quoted 182 lines
Quoted 182 lines
Quoted 168 lines
Quoted 168 lines
Quoted 161 lines

static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) read_mode = msg_is_read;

drivers/i2c/mxc_i2c.c +1 -1
 		}
 
-		if (msg->flags & I2C_M_RD)
+		if (msg->flags & I2C_M_RD) {
 			ret = i2c_read_data(i2c_bus,

msg->addr, msg->buf, msg->len, nmsgs == 1 ||
(msg->flags
& I2C_M_STOP));

drivers/i2c/mxc_i2c.c +0 -2
-		else
-			ret = i2c_write_data(i2c_bus,

msg->addr, msg->buf,

drivers/i2c/mxc_i2c.c +6 -1
-					     msg->len);
+			if (ret < 0)
+				break;
+			continue;
+		}
 
+		 /* Write message */
+		ret = i2c_write_data(i2c_bus, msg->addr,

msg->buf,

drivers/i2c/mxc_i2c.c +15 -0
+				     msg->len);
 		if (ret < 0)
 			break;
+
+		if (!next_is_read)
+			continue;
+
+		/* Read message following write message */
+		if (msg[1].flags & I2C_M_RD_NEED_STOP_BIT) {
+			/* Generate a stop bit */
+			i2c_imx_stop(i2c_bus);
+			/* Reset i2c slave */
+			i2c_force_reset_slave();
+
+			/* Enable I2C controller */
+			if (quirk)
+				ret = readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+					& I2CR_IDIS;
+			else
+				ret = !(readb(base + (I2CR <<

reg_shift))

drivers/i2c/mxc_i2c.c +3 -0
+						& I2CR_IEN);
+			if (ret) {
+				writeb(I2CR_IEN, base +

(I2CR << reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+				/* Wait for controller to be

stable */

drivers/i2c/mxc_i2c.c +5 -0
+				udelay(50);
+			}
+
+			/* Clear interrupt bit */
+			writeb(I2SR_IIF_CLEAR, base + (I2SR

<< reg_shift));

drivers/i2c/mxc_i2c.c +1 -0
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_IDLE);

drivers/i2c/mxc_i2c.c +5 -0
+			if (ret < 0)
+				return ret;
+
+			/* Start I2C transaction */
+			temp = readb(base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +2 -0
+			temp |= I2CR_MSTA;
+			writeb(temp, base + (I2CR <<

reg_shift)); +

drivers/i2c/mxc_i2c.c +1 -0
+			ret = wait_for_sr_state(i2c_bus,

ST_BUS_BUSY);

drivers/i2c/mxc_i2c.c +6 -0
+			if (ret < 0)
+				return ret;
+
+			/* Enter transfer mode */
+			temp |= I2CR_MTX | I2CR_TX_NO_AK;
+			writeb(temp, base + (I2CR <<

reg_shift));

drivers/i2c/mxc_i2c.c +2 -0
+			udelay(50);
+		}
 	}
 
 	if (ret)
drivers/rtc/pcf2127.c +1 -4
index dcf0340b4d..010c45ecbf 100644
@@ -24,12 +24,9 @@
 
 static int pcf2127_rtc_set(struct udevice *dev, const struct
 rtc_time *tm) {
-	uchar buf[8];
+	uchar buf[7] = {0};
 	int i = 0, ret;
 
-	/* start register address */
-	buf[i++] = PCF2127_REG_SC;
-
 	/* hours, minutes and seconds */
 	buf[i++] = bin2bcd(tm->tm_sec);
 	buf[i++] = bin2bcd(tm->tm_min); @@ -44,7 +41,7 @@ static int

pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm) buf[i++] = bin2bcd(tm->tm_year % 100);

drivers/rtc/pcf2127.c +0 -1
 	/* write register's data */
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf,

sizeof(buf));

drivers/rtc/pcf2127.c +3 -3
+	ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
 
 	return ret;
 }
@@ -54,9 +51,6 @@ static int pcf2127_rtc_get(struct udevice
 *dev, struct rtc_time *tm) int ret = 0;
 	uchar buf[10] = { PCF2127_REG_CTRL1 };
 
-	ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
-	if (ret < 0)
-		return ret;
 	ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
 if (ret < 0)
 		return ret;
@@ -90,6 +84,13 @@ static int pcf2127_rtc_reset(struct udevice
 *dev) return 0;
 }
 
+static int pcf2127_probe(struct udevice *dev) {
+	i2c_set_chip_flags(dev,

DM_I2C_CHIP_RD_NEED_STOP_BIT); +

drivers/rtc/pcf2127.c +4 -0
+	return 0;
+}
+
 static const struct rtc_ops pcf2127_rtc_ops = {
 	.get = pcf2127_rtc_get,
 	.set = pcf2127_rtc_set,
@@ -104,6 +105,7 @@ static const struct udevice_id
 pcf2127_rtc_ids[] = { U_BOOT_DRIVER(rtc_pcf2127) = {
 	.name	= "rtc-pcf2127",
 	.id	= UCLASS_RTC,
+	.probe	= pcf2127_probe,
 	.of_match = pcf2127_rtc_ids,
 	.ops	= &pcf2127_rtc_ops,
 };
include/i2c.h index renamed from include/i2c.h +0 -0

a5c760c711..beaa028349 100644

include/i2c.h +0 -0
@@ -28,6 +28,7 @@ enum dm_i2c_chip_flags {
 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit

addressing */ DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send
address for each read byte */ DM_I2C_CHIP_WR_ADDRESS =
1 << 2, /* Send address for each write byte */

include/i2c.h +1 -0
+	DM_I2C_CHIP_RD_NEED_STOP_BIT    = 1 << 3, /* Need

generate stop bit */ };

struct udevice;

include/i2c.h +0 -0
@@ -87,6 +88,7 @@ enum dm_i2c_msg_flags {
 	I2C_M_IGNORE_NAK	= 0x1000, /* continue after

NAK */ I2C_M_NO_RD_ACK = 0x0800, /* skip the
Ack bit on reads */ I2C_M_RECV_LEN =
0x0400, /* length is first received byte */

include/i2c.h +1 -0
+	I2C_M_RD_NEED_STOP_BIT  = 0x0002, /* need generate

stop bit */ };

Quoted 1 line

/**

Best regards,

Quoted 8 lines
Quoted 8 lines

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang
Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-59 Fax:
(+49)-8142-66989-80 Email: lukma at denx.de

Best regards,

Quoted 8 lines
Quoted 8 lines

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang
Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email:
lukma at denx.de

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email:
lukma at denx.de

#7 lore raw

Hello Chuanhua Han,

Am 22.05.2019 um 14:45 schrieb Chuanhua Han:

Quoted 72 lines
Quoted 72 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 19:32
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Quoted 59 lines · On Wed, 22 May 2019 09:31:35 +0000 Chuanhua Han <[email protected]> wrote:
Quoted 34 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 16:41
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 23 lines
Quoted 11 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 15:16
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org
Subject: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 8 lines
Quoted 2 lines

Because i2c driver does not generate a stop bit when reading registers of pcf2127

The change (in the common i2c code) is rather large when considering the description above.

Could you write a more detailed patch description? Is this only the problem with i2c in DM?

OK, This is a problem with the i2c transport framework. After writing the register address that you want to read, the stop signal is missing before reading the register data.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Is the same code (as introduced in this commit) available in Linux kernel?

The kernel does not have such a problem

Do you know maybe why such issue is not present on Linux kernel?

The kernel code is encapsulated when reading the pcf2127 register, which separates the read and write, thus generating a stop signal before reading the register. Eg: Here is the wrapper made by the kernel code: static int pcf2127_i2c_read(void *context, const void

*reg, size_t reg_size, void *val, size_t val_size) {
         struct device *dev = context;
         struct i2c_client *client = to_i2c_client(dev);
         int ret;

         if (WARN_ON(reg_size != 1))
                 return -EINVAL;

         ret = i2c_master_send(client, reg, 1);
         if (ret != 1)
                 return ret < 0 ? ret : -EIO;

         ret = i2c_master_recv(client, val, val_size);
         if (ret != val_size)
                 return ret < 0 ? ret : -EIO;
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
         return 0;
}

That was my point - the same shall be done in the pcf2127 driver. This is a slow device, so we can afford for this.

There is no similar api in the uboot code to split the read data into the address of the send register and receive the data. Uboot encapsulates the transmit register address and the read data, so no stop signal is generated.

If this API is missing, please introduce it. But not in one big patch instead split it into:

- introduce I2C_M_RD_NEED_STOP_BIT

- support flag I2C_M_RD_NEED_STOP_BIT in driver drivers/i2c/mxc_i2c.c

or may we need a more common approach to this?

- pcf2127 driver changes

Also, as Lukasz mentioned, provide commit messages with more information, what you do and why.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

How the error is reproduced? What are the symptoms of it?

You can use the i2c command to read the register data of pcf2127.

So the problem is with using "i2c ..." commands from U-Boot prompt?

Yes,but adding debugging to the rtc driver is also the same problem

Quoted 8 lines
Quoted 4 lines

You will find that you want to read the address 0 of the register, but the data of the 1 address is read, and the data read later will be offset.

As fair as I can tell the pcf2127 has its own DM aware driver at driver/rtc/pcf2127.c.

Quoted 13 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is this driver broken so you need to modify the generic i.MX I2C code? Have you tried to test this driver on your setup?

Pcf2127 driver also has problems, has been modified, need i2c general code to support

Just one remark the mxc_i2c.c is IMX specific I2C code. Not the generic one.

ok

Quoted 6 lines

Moreover, it looks like the same approach (first send, then read) is performed in the pcf2127 driver at pcf2127_rtc_get() function.

I think that the driver shall be first thoroughly checked, then fixes shall be added to it.

I have no such device, so hard to say ... and as Lukasz alread mentioned the driver seems to make such an approach:

  52 static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm)
  53 {
  54         int ret = 0;
  55         uchar buf[10] = { PCF2127_REG_CTRL1 };
  56
  57         ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
  58         if (ret < 0)
  59                 return ret;
  60         ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
  61         if (ret < 0)
  62                 return ret;

It seems there are currently no real users of this driver:

pollux:u-boot hs [master] $ grep -lr RTC_PCF2127 . ./drivers/rtc/Kconfig ./drivers/rtc/Makefile
pollux:u-boot hs [master] $

I added Meng Yi to cc, as he is the author of this driver. May he can say here more... at last I hope, the driver worked for him.

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
#8 lore raw

Hi Heiko,

Quoted 92 lines

Hello Chuanhua Han,

Am 22.05.2019 um 14:45 schrieb Chuanhua Han:

Quoted 77 lines
Quoted 77 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 19:32
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Quoted 62 lines · On Wed, 22 May 2019 09:31:35 +0000 Chuanhua Han <[email protected]> wrote:
Quoted 34 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 16:41
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]> Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127:
fix bug that read wrong time

Hi Chuanhua,

Quoted 23 lines
Quoted 11 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 15:16
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org
Subject: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 8 lines
Quoted 2 lines

Because i2c driver does not generate a stop bit when reading registers of pcf2127

The change (in the common i2c code) is rather large when considering the description above.

Could you write a more detailed patch description? Is this only the problem with i2c in DM?

OK, This is a problem with the i2c transport framework. After writing the register address that you want to read, the stop signal is missing before reading the register data.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

Is the same code (as introduced in this commit) available in Linux kernel?

The kernel does not have such a problem

Do you know maybe why such issue is not present on Linux kernel?

The kernel code is encapsulated when reading the pcf2127 register, which separates the read and write, thus generating a stop signal before reading the register. Eg: Here is the wrapper made by the kernel code: static int pcf2127_i2c_read(void *context, const void

*reg, size_t reg_size, void *val, size_t val_size) {
         struct device *dev = context;
         struct i2c_client *client = to_i2c_client(dev);
         int ret;

         if (WARN_ON(reg_size != 1))
                 return -EINVAL;

         ret = i2c_master_send(client, reg, 1);
         if (ret != 1)
                 return ret < 0 ? ret : -EIO;

         ret = i2c_master_recv(client, val, val_size);
         if (ret != val_size)
                 return ret < 0 ? ret : -EIO;

         return 0;
}  
Quoted 6 lines
Quoted 2 lines

That was my point - the same shall be done in the pcf2127 driver. This is a slow device, so we can afford for this.

There is no similar api in the uboot code to split the read data into the address of the send register and receive the data. Uboot encapsulates the transmit register address and the read data, so no stop signal is generated.

If this API is missing, please introduce it. But not in one big patch instead split it into:

- introduce I2C_M_RD_NEED_STOP_BIT

Quoted 8 lines

- support flag I2C_M_RD_NEED_STOP_BIT in driver drivers/i2c/mxc_i2c.c

or may we need a more common approach to this?

- pcf2127 driver changes

Also, as Lukasz mentioned, provide commit messages with more information, what you do and why.

Verbose commit messages help with understanding the _real_ problem being solved by the patch. Also are helpful in the future as a documentation and reference.

Quoted 37 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

How the error is reproduced? What are the symptoms of it?

You can use the i2c command to read the register data of pcf2127.

So the problem is with using "i2c ..." commands from U-Boot prompt?

Yes,but adding debugging to the rtc driver is also the same problem

Quoted 8 lines
Quoted 4 lines

You will find that you want to read the address 0 of the register, but the data of the 1 address is read, and the data read later will be offset.

As fair as I can tell the pcf2127 has its own DM aware driver at driver/rtc/pcf2127.c.

Quoted 14 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Is this driver broken so you need to modify the generic i.MX I2C code? Have you tried to test this driver on your setup?

Pcf2127 driver also has problems, has been modified, need i2c general code to support

Just one remark the mxc_i2c.c is IMX specific I2C code. Not the generic one.

ok

Quoted 6 lines

Moreover, it looks like the same approach (first send, then read) is performed in the pcf2127 driver at pcf2127_rtc_get() function.

I think that the driver shall be first thoroughly checked, then fixes shall be added to it.

I have no such device, so hard to say ... and as Lukasz alread mentioned the driver seems to make such an approach:

Quoted 12 lines

52 static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time
*tm) 53 {
54 int ret = 0;
55 uchar buf[10] = { PCF2127_REG_CTRL1 };
56
57 ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
58 if (ret < 0)
59 return ret;
60 ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf,
sizeof(buf)); 61 if (ret < 0)
62 return ret;

I would prefer to fix the issue in the driver itself. Only when it is not possible we shall introduce extra flags and modify the common I2C code.

Quoted 8 lines

It seems there are currently no real users of this driver:

pollux:u-boot hs [master] $ grep -lr RTC_PCF2127 . ./drivers/rtc/Kconfig ./drivers/rtc/Makefile
pollux:u-boot hs [master] $

Quoted 5 lines

I added Meng Yi to cc, as he is the author of this driver. May he can say here more... at last I hope, the driver worked for him.

bye,
Heiko

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de -------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190523/91a1ce26/attachment.sig>

#9 lore raw

Hello Lukasz,

Am 23.05.2019 um 07:54 schrieb Lukasz Majewski:

Quoted 98 lines

Hi Heiko,

Quoted 96 lines

Hello Chuanhua Han,

Am 22.05.2019 um 14:45 schrieb Chuanhua Han:

Quoted 77 lines
Quoted 77 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 19:32
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Quoted 62 lines · On Wed, 22 May 2019 09:31:35 +0000 Chuanhua Han <[email protected]> wrote:
Quoted 34 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 16:41
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]> Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127:
fix bug that read wrong time

Hi Chuanhua,

Quoted 23 lines
Quoted 11 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 15:16
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org
Subject: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 8 lines
Quoted 2 lines

Because i2c driver does not generate a stop bit when reading registers of pcf2127

The change (in the common i2c code) is rather large when considering the description above.

Could you write a more detailed patch description? Is this only the problem with i2c in DM?

OK, This is a problem with the i2c transport framework. After writing the register address that you want to read, the stop signal is missing before reading the register data.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

Is the same code (as introduced in this commit) available in Linux kernel?

The kernel does not have such a problem

Do you know maybe why such issue is not present on Linux kernel?

The kernel code is encapsulated when reading the pcf2127 register, which separates the read and write, thus generating a stop signal before reading the register. Eg: Here is the wrapper made by the kernel code: static int pcf2127_i2c_read(void *context, const void

*reg, size_t reg_size, void *val, size_t val_size) {
          struct device *dev = context;
          struct i2c_client *client = to_i2c_client(dev);
          int ret;

          if (WARN_ON(reg_size != 1))
                  return -EINVAL;

          ret = i2c_master_send(client, reg, 1);
          if (ret != 1)
                  return ret < 0 ? ret : -EIO;

          ret = i2c_master_recv(client, val, val_size);
          if (ret != val_size)
                  return ret < 0 ? ret : -EIO;

          return 0;
}
Quoted 6 lines
Quoted 2 lines

That was my point - the same shall be done in the pcf2127 driver. This is a slow device, so we can afford for this.

There is no similar api in the uboot code to split the read data into the address of the send register and receive the data. Uboot encapsulates the transmit register address and the read data, so no stop signal is generated.

If this API is missing, please introduce it. But not in one big patch instead split it into:

- introduce I2C_M_RD_NEED_STOP_BIT

- support flag I2C_M_RD_NEED_STOP_BIT in driver drivers/i2c/mxc_i2c.c

or may we need a more common approach to this?

Quoted 8 lines
Quoted 4 lines

- pcf2127 driver changes

Also, as Lukasz mentioned, provide commit messages with more information, what you do and why.

Verbose commit messages help with understanding the _real_ problem being solved by the patch. Also are helpful in the future as a documentation and reference.

Exactly.

Quoted 47 lines
Quoted 43 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

How the error is reproduced? What are the symptoms of it?

You can use the i2c command to read the register data of pcf2127.

So the problem is with using "i2c ..." commands from U-Boot prompt?

Yes,but adding debugging to the rtc driver is also the same problem

Quoted 8 lines
Quoted 4 lines

You will find that you want to read the address 0 of the register, but the data of the 1 address is read, and the data read later will be offset.

As fair as I can tell the pcf2127 has its own DM aware driver at driver/rtc/pcf2127.c.

Quoted 14 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Is this driver broken so you need to modify the generic i.MX I2C code? Have you tried to test this driver on your setup?

Pcf2127 driver also has problems, has been modified, need i2c general code to support

Just one remark the mxc_i2c.c is IMX specific I2C code. Not the generic one.

ok

Quoted 6 lines

Moreover, it looks like the same approach (first send, then read) is performed in the pcf2127 driver at pcf2127_rtc_get() function.

I think that the driver shall be first thoroughly checked, then fixes shall be added to it.

I have no such device, so hard to say ... and as Lukasz alread mentioned the driver seems to make such an approach:

52 static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time

*tm) 53 {
   54         int ret = 0;
   55         uchar buf[10] = { PCF2127_REG_CTRL1 };
   56
   57         ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
   58         if (ret < 0)
   59                 return ret;
   60         ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf,
Quoted 3 lines

sizeof(buf)); 61 if (ret < 0)
62 return ret;

Quoted 3 lines

I would prefer to fix the issue in the driver itself. Only when it is not possible we shall introduce extra flags and modify the common I2C code.

If possible, yes! I wonder: if this driver is in mainline, it must at least worked for Meng Yi (as driver author) ... unfortunately the email address is not valid ... so removed him from cc list.

bye,
Heiko

Quoted 28 lines
Quoted 9 lines

It seems there are currently no real users of this driver:

pollux:u-boot hs [master] $ grep -lr RTC_PCF2127 . ./drivers/rtc/Kconfig ./drivers/rtc/Makefile
pollux:u-boot hs [master] $

Quoted 5 lines

I added Meng Yi to cc, as he is the author of this driver. May he can say here more... at last I hope, the driver worked for him.

bye,
Heiko

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: hs at denx.de
#10 lore raw
In reply to Lukasz Majewski
Quoted 108 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月23日 13:54
To: Heiko Schocher <[email protected]>
Cc: Chuanhua Han <[email protected]>; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>; Meng Yi <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Heiko,

Quoted 95 lines

Hello Chuanhua Han,

Am 22.05.2019 um 14:45 schrieb Chuanhua Han:

Quoted 76 lines
Quoted 76 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 19:32
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Quoted 61 lines · On Wed, 22 May 2019 09:31:35 +0000 Chuanhua Han <[email protected]> wrote:
Quoted 34 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 16:41
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]> Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127:
fix bug that read wrong time

Hi Chuanhua,

Quoted 23 lines
Quoted 11 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 15:16
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org
Subject: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 8 lines
Quoted 2 lines

Because i2c driver does not generate a stop bit when reading registers of pcf2127

The change (in the common i2c code) is rather large when considering the description above.

Could you write a more detailed patch description? Is this only the problem with i2c in DM?

OK, This is a problem with the i2c transport framework. After writing the register address that you want to read, the stop signal is missing before reading the register data.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Is the same code (as introduced in this commit) available in Linux kernel?

The kernel does not have such a problem

Do you know maybe why such issue is not present on Linux kernel?

The kernel code is encapsulated when reading the pcf2127 register, which separates the read and write, thus generating a stop signal before reading the register. Eg: Here is the wrapper made by the kernel code: static int pcf2127_i2c_read(void *context, const void

*reg, size_t reg_size, void *val, size_t val_size) {
         struct device *dev = context;
         struct i2c_client *client = to_i2c_client(dev);
         int ret;

         if (WARN_ON(reg_size != 1))
                 return -EINVAL;

         ret = i2c_master_send(client, reg, 1);
         if (ret != 1)
                 return ret < 0 ? ret : -EIO;

         ret = i2c_master_recv(client, val, val_size);
         if (ret != val_size)
                 return ret < 0 ? ret : -EIO;

         return 0;
}
Quoted 6 lines
Quoted 2 lines

That was my point - the same shall be done in the pcf2127 driver. This is a slow device, so we can afford for this.

There is no similar api in the uboot code to split the read data into the address of the send register and receive the data. Uboot encapsulates the transmit register address and the read data, so no stop signal is generated.

If this API is missing, please introduce it. But not in one big patch instead split it into:

- introduce I2C_M_RD_NEED_STOP_BIT

- support flag I2C_M_RD_NEED_STOP_BIT in driver drivers/i2c/mxc_i2c.c

or may we need a more common approach to this?

Quoted 8 lines
Quoted 4 lines

- pcf2127 driver changes

Also, as Lukasz mentioned, provide commit messages with more information, what you do and why.

Verbose commit messages help with understanding the _real_ problem being solved by the patch. Also are helpful in the future as a documentation and reference.

I'll sort out the details in the next patch

Quoted 51 lines
Quoted 38 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

How the error is reproduced? What are the symptoms of it?

You can use the i2c command to read the register data of pcf2127.

So the problem is with using "i2c ..." commands from U-Boot prompt?

Yes,but adding debugging to the rtc driver is also the same problem

Quoted 8 lines
Quoted 4 lines

You will find that you want to read the address 0 of the register, but the data of the 1 address is read, and the data read later will be offset.

As fair as I can tell the pcf2127 has its own DM aware driver at driver/rtc/pcf2127.c.

Quoted 14 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Is this driver broken so you need to modify the generic i.MX I2C code? Have you tried to test this driver on your setup?

Pcf2127 driver also has problems, has been modified, need i2c general code to support

Just one remark the mxc_i2c.c is IMX specific I2C code. Not the generic one.

ok

Quoted 6 lines

Moreover, it looks like the same approach (first send, then read) is performed in the pcf2127 driver at pcf2127_rtc_get() function.

I think that the driver shall be first thoroughly checked, then fixes shall be added to it.

I have no such device, so hard to say ... and as Lukasz alread mentioned the driver seems to make such an approach:

Quoted 12 lines

52 static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time
*tm) 53 {
54 int ret = 0;
55 uchar buf[10] = { PCF2127_REG_CTRL1 };
56
57 ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
58 if (ret < 0)
59 return ret;
60 ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf,
sizeof(buf)); 61 if (ret < 0)
62 return ret;

Quoted 2 lines

I would prefer to fix the issue in the driver itself. Only when it is not possible we shall introduce extra flags and modify the common I2C code.

Because the pcf2127 chip is special, it must have a stop signal after writing the register to perform the following operations of reading the register.

/***********************
*Generate the first part timings of reading from registers.
* write bit STOP
* | |
* ----------------------------------------------------------------------------------------------------------------
* | S | 1 0 1 0 0 0 1 0 | A | 00h-1Dh | A | P |
* ----------------------------------------------------------------------------------------------------------------
* | slave address = 0x51 | | | register address | |
* | |
* ACK from slave ACK from slave
**/
ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 0); if (ret < 0)
return ret;

/***********************
*Generate the second part timings of reading from registers.
* read bit STOP
* | |
* ----------------------------------------------------------------------------------------------------------------------------------------------------------
* | S | 1 0 1 0 0 0 1 1 | A | data byte(0-n) | A | last data byte | NA | P |
* ------------------------------------------------------------------------------------------------------------------------------------------------------------
* |slave address = 0x51 | | | data from slave | | |
* | | |
* ACK from slave ACK from master No ACK
*/
ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf)); if (ret < 0)
return ret;>

Quoted 27 lines
Quoted 8 lines

It seems there are currently no real users of this driver:

pollux:u-boot hs [master] $ grep -lr RTC_PCF2127 . ./drivers/rtc/Kconfig ./drivers/rtc/Makefile
pollux:u-boot hs [master] $

Quoted 5 lines

I added Meng Yi to cc, as he is the author of this driver. May he can say here more... at last I hope, the driver worked for him.

bye,
Heiko

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email:
lukma at denx.de

#11 lore raw
In reply to Heiko Schocher
Quoted 104 lines

-----Original Message-----
From: Heiko Schocher <[email protected]>
Sent: 2019年5月23日 11:29
To: Chuanhua Han <[email protected]>
Cc: Lukasz Majewski <[email protected]>; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>; Meng Yi <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Caution: EXT Email

Hello Chuanhua Han,

Am 22.05.2019 um 14:45 schrieb Chuanhua Han:

Quoted 73 lines
Quoted 73 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 19:32
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Quoted 59 lines · On Wed, 22 May 2019 09:31:35 +0000 Chuanhua Han <[email protected]> wrote:
Quoted 34 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 16:41
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org; Stefano Babic <[email protected]>
Subject: Re: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 23 lines
Quoted 11 lines

-----Original Message-----
From: Lukasz Majewski <[email protected]>
Sent: 2019年5月22日 15:16
To: Chuanhua Han <[email protected]>
Cc: hs at denx.de; u-boot at lists.denx.de; Biwen Li <[email protected]>; sjg at chromium.org
Subject: [EXT] Re: [PATCH] i2c: pcf2127: fix bug that read wrong time

Hi Chuanhua,

Quoted 8 lines
Quoted 2 lines

Because i2c driver does not generate a stop bit when reading registers of pcf2127

The change (in the common i2c code) is rather large when considering the description above.

Could you write a more detailed patch description? Is this only the problem with i2c in DM?

OK, This is a problem with the i2c transport framework. After writing the register address that you want to read, the stop signal is missing before reading the register data.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Is the same code (as introduced in this commit) available in Linux kernel?

The kernel does not have such a problem

Do you know maybe why such issue is not present on Linux kernel?

The kernel code is encapsulated when reading the pcf2127 register, which separates the read and write, thus generating a stop signal before reading the register. Eg: Here is the wrapper made by the kernel code: static int pcf2127_i2c_read(void *context, const void

*reg, size_t reg_size, void *val, size_t val_size) {
         struct device *dev = context;
         struct i2c_client *client = to_i2c_client(dev);
         int ret;

         if (WARN_ON(reg_size != 1))
                 return -EINVAL;

         ret = i2c_master_send(client, reg, 1);
         if (ret != 1)
                 return ret < 0 ? ret : -EIO;

         ret = i2c_master_recv(client, val, val_size);
         if (ret != val_size)
                 return ret < 0 ? ret : -EIO;
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
         return 0;
}

That was my point - the same shall be done in the pcf2127 driver. This is a slow device, so we can afford for this.

There is no similar api in the uboot code to split the read data into the

address of the send register and receive the data.

Quoted 1 line

Uboot encapsulates the transmit register address and the read data, so no

stop signal is generated.

If this API is missing, please introduce it. But not in one big patch instead split it into:

- introduce I2C_M_RD_NEED_STOP_BIT

Quoted 8 lines

- support flag I2C_M_RD_NEED_STOP_BIT in driver drivers/i2c/mxc_i2c.c

or may we need a more common approach to this?

- pcf2127 driver changes

Also, as Lukasz mentioned, provide commit messages with more information, what you do and why.

Ok, Next I will split the patch and provide a more detailed description messages.

Quoted 33 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

How the error is reproduced? What are the symptoms of it?

You can use the i2c command to read the register data of pcf2127.

So the problem is with using "i2c ..." commands from U-Boot prompt?

Yes,but adding debugging to the rtc driver is also the same problem

Quoted 8 lines
Quoted 4 lines

You will find that you want to read the address 0 of the register, but the data of the 1 address is read, and the data read later will be offset.

As fair as I can tell the pcf2127 has its own DM aware driver at driver/rtc/pcf2127.c.

Quoted 13 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Is this driver broken so you need to modify the generic i.MX I2C code? Have you tried to test this driver on your setup?

Pcf2127 driver also has problems, has been modified, need i2c general code to support

Just one remark the mxc_i2c.c is IMX specific I2C code. Not the generic one.

ok

Quoted 6 lines

Moreover, it looks like the same approach (first send, then read) is performed in the pcf2127 driver at pcf2127_rtc_get() function.

I think that the driver shall be first thoroughly checked, then fixes shall be added to it.

I have no such device, so hard to say ... and as Lukasz alread mentioned the driver seems to make such an approach:

Quoted 13 lines
  52 static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm)
  53 {
  54         int ret = 0;
  55         uchar buf[10] = { PCF2127_REG_CTRL1 };
  56
  57         ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
  58         if (ret < 0)
  59                 return ret;
  60         ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
  61         if (ret < 0)
  62                 return ret;

Although this can be done logically, the dm_i2c_read function is designed to put the address of the write register and the read data in a call to the ops->xfer(bus, msg, msg_count) function, so that we can't get us. Expected effect (there is no stop signal after the address of the write register but the restart signal)

Quoted 9 lines

It seems there are currently no real users of this driver:

pollux:u-boot hs [master] $ grep -lr RTC_PCF2127 . ./drivers/rtc/Kconfig ./drivers/rtc/Makefile
pollux:u-boot hs [master] $

I added Meng Yi to cc, as he is the author of this driver. May he can say here more... at last I hope, the driver worked for him.

True, currently only the NXP layerscape platform board users the pcf2127 chip, but Meng Yi has now left the company.
Now I will fix the patch and send the next version.

Quoted 7 lines

bye,
Heiko
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52 Fax: +49-8142-66989-80 Email: hs at denx.de