Skip to contentSkip to conversation

hw/nvram: at24 return 0xff if 1 byte address

PATCH Superseded Patrick Venture 2 participants 4 messages Dec 20, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/nvram: at24 return 0xff if 1 byte address

The at24 eeproms are 2 byte devices that return 0xff when they are read from with a partial (1-byte) address written. This distinction was found comparing model behavior to real hardware testing.

Tested: i2ctransfer -f -y 45 w1@85 0 r1 returns 0xff instead of next byte

---
 hw/nvram/eeprom_at24c.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/nvram/eeprom_at24c.c +7 -1
index a9e3702b00..184fac9702 100644
@@ -62,7 +62,9 @@ int at24c_eeprom_event(I2CSlave *s, enum i2c_event event)
     case I2C_START_SEND:
     case I2C_START_RECV:
     case I2C_FINISH:
-        ee->haveaddr = 0;
+        if (event != I2C_START_RECV) {
+            ee->haveaddr = 0;
+        }
         DPRINTK("clear\n");
         if (ee->blk && ee->changed) {
             int len = blk_pwrite(ee->blk, 0, ee->mem, ee->rsize, 0);
@@ -86,6 +88,10 @@ uint8_t at24c_eeprom_recv(I2CSlave *s)
     EEPROMState *ee = AT24C_EE(s);
     uint8_t ret;
 
+    if (ee->haveaddr == 1) {
+        return 0xff;
+    }
+
     ret = ee->mem[ee->cur];
 
     ee->cur = (ee->cur + 1u) % ee->rsize;
Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
#1 lore raw

Hi Patrick,

Quoted 17 lines · Patrick Venture

The at24 eeproms are 2 byte devices that return 0xff when they are read from with a partial (1-byte) address written. This distinction was found comparing model behavior to real hardware testing.

Tested: i2ctransfer -f -y 45 w1@85 0 r1 returns 0xff instead of next byte

---
 hw/nvram/eeprom_at24c.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/nvram/eeprom_at24c.c +0 -0
index a9e3702b00..184fac9702 100644

hw/nvram/eeprom_at24c.c int at24c_eeprom_event(I2CSlave *s, enum i2c_event event) · line 67

Quoted 7 lines
hw/nvram/eeprom_at24c.c +3 -1
@@ -62,7 +62,9 @@ int at24c_eeprom_event(I2CSlave *s, enum i2c_event event)
     case I2C_START_SEND:
     case I2C_START_RECV:
     case I2C_FINISH:
-        ee->haveaddr = 0;
+        if (event != I2C_START_RECV) {
+            ee->haveaddr = 0;
+        }

Alternatively (matter of taste, I find it easier to read):

       case I2C_START_SEND:
       case I2C_FINISH:
           ee->haveaddr = 0;
           /* fallthrough */
       case I2C_START_RECV:

hw/nvram/eeprom_at24c.c uint8_t at24c_eeprom_recv(I2CSlave *s) · line 92

Quoted 9 lines
hw/nvram/eeprom_at24c.c +2 -0
         DPRINTK("clear\n");
         if (ee->blk && ee->changed) {
             int len = blk_pwrite(ee->blk, 0, ee->mem, ee->rsize, 0);
@@ -86,6 +88,10 @@ uint8_t at24c_eeprom_recv(I2CSlave *s)
     EEPROMState *ee = AT24C_EE(s);
     uint8_t ret;
 
+    if (ee->haveaddr == 1) {
+        return 0xff;

Don't we need to increase ee->haveaddr?

hw/nvram/eeprom_at24c.c uint8_t at24c_eeprom_recv(I2CSlave *s) · line 97

Quoted 5 lines
hw/nvram/eeprom_at24c.c +2 -0
+    }
+
     ret = ee->mem[ee->cur];
 
     ee->cur = (ee->cur + 1u) % ee->rsize;

Here for parity with send, what about:

    if (ee->haveaddr < 2) {
        ret = 0xff;
        ee->haveaddr++;
    } else {
        ret = ee->mem[ee->cur];
        ee->cur = (ee->cur + 1u) % ee->rsize;
    }

?

#2 lore raw
Quoted 29 lines · Philippe Mathieu-Daudé

Hi Patrick,

hw/nvram/eeprom_at24c.c int at24c_eeprom_event(I2CSlave *s, enum i2c_event

Quoted 17 lines · Patrick Venture

The at24 eeproms are 2 byte devices that return 0xff when they are read from with a partial (1-byte) address written. This distinction was found comparing model behavior to real hardware testing.

Tested: i2ctransfer -f -y 45 w1@85 0 r1 returns 0xff instead of next byte

---
 hw/nvram/eeprom_at24c.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/nvram/eeprom_at24c.c +0 -0
index a9e3702b00..184fac9702 100644
@@ -62,7 +62,9 @@ int at24c_eeprom_event(I2CSlave *s, enum i2c_event

event)

hw/nvram/eeprom_at24c.c int at24c_eeprom_event(I2CSlave *s, enum i2c_event event) · line 67

Quoted 7 lines
hw/nvram/eeprom_at24c.c +3 -1
     case I2C_START_SEND:
     case I2C_START_RECV:
     case I2C_FINISH:
-        ee->haveaddr = 0;
+        if (event != I2C_START_RECV) {
+            ee->haveaddr = 0;
+        }
Quoted 8 lines

Alternatively (matter of taste, I find it easier to read):

       case I2C_START_SEND:
       case I2C_FINISH:
           ee->haveaddr = 0;
           /* fallthrough */
       case I2C_START_RECV:

That may be easier to read :) I'm not sure, but I'm willing to bend and change my patch to behave this way. Sometimes the fallthrough things leads to compiler annoyances in my experience. We might need __attribute__(fallthrough) or the like to convince the system that's what we really want.

Quoted 8 lines
Quoted 8 lines
hw/nvram/eeprom_at24c.c +0 -0
         DPRINTK("clear\n");
         if (ee->blk && ee->changed) {
             int len = blk_pwrite(ee->blk, 0, ee->mem, ee->rsize, 0);
@@ -86,6 +88,10 @@ uint8_t at24c_eeprom_recv(I2CSlave *s)
     EEPROMState *ee = AT24C_EE(s);
     uint8_t ret;
 
Quoted 5 lines

hw/nvram/eeprom_at24c.c uint8_t at24c_eeprom_recv(I2CSlave *s) · line 92

Quoted 2 lines
hw/nvram/eeprom_at24c.c +2 -0
+    if (ee->haveaddr == 1) {
+        return 0xff;

Don't we need to increase ee->haveaddr?

We don't because the call to recv doesn't set any addr bytes. This patch is primarily a behavioral fix to handle the device being treated as 8-bit addressable. This is typically tested by writing a 1 byte address and then trying to read. The chip itself will not have enough address bytes and reject this read by returning 0xff. The haveaddr variable is strictly updated when they've written another byte to the address, or they've changed states in such a way that should clear any previously written address. You can read from an eeprom by just reading or by setting an address and then reading.

Quoted 20 lines

hw/nvram/eeprom_at24c.c uint8_t at24c_eeprom_recv(I2CSlave *s) · line 97

Quoted 6 lines
hw/nvram/eeprom_at24c.c +2 -0
+    }
+
     ret = ee->mem[ee->cur];
 
     ee->cur = (ee->cur + 1u) % ee->rsize;

Here for parity with send, what about:

    if (ee->haveaddr < 2) {
        ret = 0xff;
        ee->haveaddr++;
    } else {
        ret = ee->mem[ee->cur];
        ee->cur = (ee->cur + 1u) % ee->rsize;
    }

?

#3 lore raw
Quoted 43 lines · Patrick Venture

On Mon, Dec 20, 2021 at 1:12 AM Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:

    Hi Patrick,

    On 12/20/21 01:32, Patrick Venture wrote:
    > The at24 eeproms are 2 byte devices that return 0xff when they are
    read
    > from with a partial (1-byte) address written.  This distinction was
    > found comparing model behavior to real hardware testing.
    >
    > Tested: `i2ctransfer -f -y 45 w1@85 0 r1` returns 0xff instead of next
    > byte
    >
    > Signed-off-by: Patrick Venture <[email protected]
    <mailto:[email protected]>>
    > ---
    >  hw/nvram/eeprom_at24c.c | 8 +++++++-
    >  1 file changed, 7 insertions(+), 1 deletion(-)
    >
    > diff --git a/hw/nvram/eeprom_at24c.c b/hw/nvram/eeprom_at24c.c
    > index a9e3702b00..184fac9702 100644
    > --- a/hw/nvram/eeprom_at24c.c
    > +++ b/hw/nvram/eeprom_at24c.c
    > @@ -62,7 +62,9 @@ int at24c_eeprom_event(I2CSlave *s, enum
    i2c_event event)
    >      case I2C_START_SEND:
    >      case I2C_START_RECV:
    >      case I2C_FINISH:
    > -        ee->haveaddr = 0;
    > +        if (event != I2C_START_RECV) {
    > +            ee->haveaddr = 0;
    > +        }

    Alternatively (matter of taste, I find it easier to read):

           case I2C_START_SEND:
           case I2C_FINISH:
               ee->haveaddr = 0;
               /* fallthrough */
           case I2C_START_RECV:
Quoted 5 lines

That may be easier to read :) I'm not sure, but I'm willing to bend and change my patch to behave this way.  Sometimes the fallthrough things leads to compiler annoyances in my experience.  We might  need __attribute__(fallthrough) or the like to convince the system that's what we really want.

OK then.

Quoted 15 lines
Quoted 11 lines

          DPRINTK("clear\n");
          if (ee->blk && ee->changed) {
              int len = blk_pwrite(ee->blk, 0, ee->mem, ee->rsize, 0);

hw/nvram/eeprom_at24c.c +0 -0
@@ -86,6 +88,10 @@ uint8_t at24c_eeprom_recv(I2CSlave *s)
       EEPROMState *ee = AT24C_EE(s);
       uint8_t ret;


+    if (ee->haveaddr == 1) {
+        return 0xff;

    Don't we need to increase ee->haveaddr?
Quoted 9 lines

We don't because the call to recv doesn't set any addr bytes.  This patch is primarily a behavioral fix to handle the device being treated as 8-bit addressable.  This is typically tested by writing a 1 byte address and then trying to read.  The chip itself will not have enough address bytes and reject this read by returning 0xff.  The haveaddr variable is strictly updated when they've written another byte to the address, or they've changed states in such a way that should clear any previously written address.  You can read from an eeprom by just reading or by setting an address and then reading.

Yes. And your approach is simple enough.

Thanks,

Phil.