Skip to contentSkip to conversation

i2c: mvtwsi: Swab the register address if its size is > 1

PATCH In review Stefan Roese 3 participants 5 messages Nov 18, 2021 – Dec 20, 2021 I2C

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: mvtwsi: Swab the register address if its size is > 1

Testing on Armada XP with an EEPROM using register address with size of 2 has shown, that the register address bytes are sent to the I2C EEPROM in the incorrect order. This patch swabs the address bytes so that the correct address is transferred to the I2C device.

BTW: This worked without any issues before migrating Armada XP to DM I2C.

Cc Heiko Schocher <[email protected]>
Cc Samuel Holland <[email protected]>
Cc Baruch Siach <[email protected]>
Cc Pali Rohár <[email protected]>
Cc Marek Behún <[email protected]>

---
It would be good if other users of this I2C driver could test this change with e.g. I2C EEPROM devices using 2 bytes (or more) for addressing.

 drivers/i2c/mvtwsi.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/i2c/mvtwsi.c +10 -2
index 236bfb8d8e7f..ff21e3c52b58 100644
@@ -860,6 +860,9 @@ static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
 {
 	struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
 	struct i2c_msg *dmsg, *omsg, dummy;
+	u8 *addr_buf_ptr;
+	u8 addr_buf[4];
+	int i;
 
 	memset(&dummy, 0, sizeof(struct i2c_msg));
 
@@ -873,12 +876,17 @@ static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
 	omsg = nmsgs == 1 ? &dummy : msg;
 	dmsg = nmsgs == 1 ? msg : msg + 1;
 
+	/* We need to swap the register address if its size is > 1 */
+	addr_buf_ptr = &addr_buf[0];
+	for (i = omsg->len; i > 0; i--)
+		*addr_buf_ptr++ = omsg->buf[i - 1];
+
 	if (dmsg->flags & I2C_M_RD)
-		return __twsi_i2c_read(dev->base, dmsg->addr, omsg->buf,
+		return __twsi_i2c_read(dev->base, dmsg->addr, addr_buf,
 				       omsg->len, dmsg->buf, dmsg->len,
 				       dev->tick);
 	else
-		return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf,
+		return __twsi_i2c_write(dev->base, dmsg->addr, addr_buf,
 					omsg->len, dmsg->buf, dmsg->len,
 					dev->tick);
 }
Signature · 1 line
2.34.0
#1 lore raw

Hi Heiko,

Quoted 14 lines · Stefan Roese

Testing on Armada XP with an EEPROM using register address with size of 2 has shown, that the register address bytes are sent to the I2C EEPROM in the incorrect order. This patch swabs the address bytes so that the correct address is transferred to the I2C device.

BTW: This worked without any issues before migrating Armada XP to DM I2C.

Cc Heiko Schocher <[email protected]>
Cc Samuel Holland <[email protected]>
Cc Baruch Siach <[email protected]>
Cc Pali Rohár <[email protected]>
Cc Marek Behún <[email protected]>
Quoted 3 lines

---
It would be good if other users of this I2C driver could test this change with e.g. I2C EEPROM devices using 2 bytes (or more) for addressing.

Could you and other please take a look at this? Would be great, if this could be pulled in the next merge window.

Thanks,
Stefan

Quoted 28 lines
  drivers/i2c/mvtwsi.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)
drivers/i2c/mvtwsi.c +8 -0
index 236bfb8d8e7f..ff21e3c52b58 100644
@@ -860,6 +860,9 @@ static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  {
  	struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
  	struct i2c_msg *dmsg, *omsg, dummy;
+	u8 *addr_buf_ptr;
+	u8 addr_buf[4];
+	int i;
 
  	memset(&dummy, 0, sizeof(struct i2c_msg));
 
@@ -873,12 +876,17 @@ static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  	omsg = nmsgs == 1 ? &dummy : msg;
  	dmsg = nmsgs == 1 ? msg : msg + 1;
 
+	/* We need to swap the register address if its size is > 1 */
+	addr_buf_ptr = &addr_buf[0];
+	for (i = omsg->len; i > 0; i--)
+		*addr_buf_ptr++ = omsg->buf[i - 1];
+
  	if (dmsg->flags & I2C_M_RD)

drivers/i2c/mvtwsi.c static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) · line 892

Quoted 10 lines
drivers/i2c/mvtwsi.c +2 -2
-		return __twsi_i2c_read(dev->base, dmsg->addr, omsg->buf,
+		return __twsi_i2c_read(dev->base, dmsg->addr, addr_buf,
  				       omsg->len, dmsg->buf, dmsg->len,
  				       dev->tick);
  	else
-		return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf,
+		return __twsi_i2c_write(dev->base, dmsg->addr, addr_buf,
  					omsg->len, dmsg->buf, dmsg->len,
  					dev->tick);
  }

Viele Grüße,
Stefan Roese

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-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Tested-by
#2 lore raw
Quoted 21 lines · On Sat, 18 Dec 2021 14:42:51 +0100 Stefan Roese <[email protected]> wrote:

Hi Heiko,

Quoted 14 lines · Stefan Roese

Testing on Armada XP with an EEPROM using register address with size of 2 has shown, that the register address bytes are sent to the I2C EEPROM in the incorrect order. This patch swabs the address bytes so that the correct address is transferred to the I2C device.

BTW: This worked without any issues before migrating Armada XP to DM I2C.

Cc Heiko Schocher <[email protected]>
Cc Samuel Holland <[email protected]>
Cc Baruch Siach <[email protected]>
Cc Pali Rohár <[email protected]>
Cc Marek Behún <[email protected]>
Quoted 3 lines

---
It would be good if other users of this I2C driver could test this change with e.g. I2C EEPROM devices using 2 bytes (or more) for addressing.

Quoted 2 lines

Could you and other please take a look at this? Would be great, if this could be pulled in the next merge window.

#3 lore raw

Hello Marek,

Quoted 24 lines · Marek Behún
Quoted 21 lines · On Sat, 18 Dec 2021 14:42:51 +0100 Stefan Roese <[email protected]> wrote:

Hi Heiko,

Quoted 17 lines · Stefan Roese

Testing on Armada XP with an EEPROM using register address with size of 2 has shown, that the register address bytes are sent to the I2C EEPROM in the incorrect order. This patch swabs the address bytes so that the correct address is transferred to the I2C device.

BTW: This worked without any issues before migrating Armada XP to DM I2C.

Cc Heiko Schocher <[email protected]>
Cc Samuel Holland <[email protected]>
Cc Baruch Siach <[email protected]>
Cc Pali Rohár <[email protected]>
Cc Marek Behún <[email protected]>

---
It would be good if other users of this I2C driver could test this change with e.g. I2C EEPROM devices using 2 bytes (or more) for addressing.

Quoted 5 lines
Quoted 2 lines

Could you and other please take a look at this? Would be great, if this could be pulled in the next merge window.

Thanks for testing. Just triggered azure build now, if all works fine, I send a pull request to Tom.

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]
#4 lore raw
In reply to Stefan Roese

Hello Stefan,

Quoted 21 lines · Stefan Roese

Hi Heiko,

Quoted 14 lines · Stefan Roese

Testing on Armada XP with an EEPROM using register address with size of 2 has shown, that the register address bytes are sent to the I2C EEPROM in the incorrect order. This patch swabs the address bytes so that the correct address is transferred to the I2C device.

BTW: This worked without any issues before migrating Armada XP to DM I2C.

Cc Heiko Schocher <[email protected]>
Cc Samuel Holland <[email protected]>
Cc Baruch Siach <[email protected]>
Cc Pali Rohár <[email protected]>
Cc Marek Behún <[email protected]>
Quoted 3 lines

---
It would be good if other users of this I2C driver could test this change with e.g. I2C EEPROM devices using 2 bytes (or more) for addressing.

Quoted 2 lines

Could you and other please take a look at this? Would be great, if this could be pulled in the next merge window.

Applied to u-boot-i2c master

Thanks!

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]