Skip to contentSkip to conversation

SMBus and tmp105 fixes

PATCH Applied Paolo Bonzini 3 participants 17 messages Mar 31, 2014 – May 7, 2014

lore
Share & tools
lore
#0 lore raw

cover SMBus and tmp105 fixes

This is a resend of the I2C patches I posted a while ago. Patches 1-3 are just a rebase.

Patch 4 is the same as before, patches 5-7 make the tmp105 testcase more complete in order to test that change.

Paolo Bonzini (7):
smbus: allow returning an error from reads
smbus: return -1 if nothing found at the given address
pm_smbus: correctly report unclaimed cycles
tmp105: read temperature in milli-celsius
tmp105-test: wrap simple building blocks for testing
tmp105-test: add a second sensor and test that one
tmp105-test: test QOM property and precision

 hw/i2c/pm_smbus.c      |  63 +++++++++++++++--------
 hw/i2c/smbus.c         |  68 +++++++++++++++++--------
 hw/misc/tmp105.c       |   8 +--
 include/hw/i2c/smbus.h |  18 +++----
 roms/SLOF              |   2 +-
 roms/openbios          |   2 +-
 roms/qemu-palcode      |   2 +-
 roms/seabios           |   2 +-
 tests/tmp105-test.c    | 136 +++++++++++++++++++++++++++++++++++++++++--------
 9 files changed, 220 insertions(+), 81 deletions(-)
Signature · 1 line
1.9.0
#1 lore raw

PATCH 1/7 smbus: allow returning an error from reads

---
 hw/i2c/smbus.c         | 6 +++---
 include/hw/i2c/smbus.h | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)
hw/i2c/smbus.c +3 -3
index 3febf3c..190f08e 100644
@@ -214,7 +214,7 @@ void smbus_quick_command(I2CBus *bus, uint8_t addr, int read)
     i2c_end_transfer(bus);
 }
 
-uint8_t smbus_receive_byte(I2CBus *bus, uint8_t addr)
+int smbus_receive_byte(I2CBus *bus, uint8_t addr)
 {
     uint8_t data;
 
@@ -232,7 +232,7 @@ void smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
     i2c_end_transfer(bus);
 }
 
-uint8_t smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
+int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
 {
     uint8_t data;
     i2c_start_transfer(bus, addr, 0);
@@ -252,7 +252,7 @@ void smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data)
     i2c_end_transfer(bus);
 }
 
-uint16_t smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
+int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
 {
     uint16_t data;
     i2c_start_transfer(bus, addr, 0);
include/hw/i2c/smbus.h +3 -3
index 63f0cc4..285d3b5 100644
@@ -67,11 +67,11 @@ struct SMBusDevice {
 
 /* Master device commands.  */
 void smbus_quick_command(I2CBus *bus, uint8_t addr, int read);
-uint8_t smbus_receive_byte(I2CBus *bus, uint8_t addr);
+int smbus_receive_byte(I2CBus *bus, uint8_t addr);
 void smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data);
-uint8_t smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command);
+int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command);
 void smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data);
-uint16_t smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command);
+int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command);
 void smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data);
 int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data);
 void smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data,
Signature · 1 line
1.9.0
Reviewed-by on v1 1/7
#2 lore raw

Re: [Qemu-devel] [PATCH for-2.0 1/7] smbus: allow returning an error from reads

Am 31.03.2014 18:26, schrieb Paolo Bonzini:

Quoted 5 lines
---
 hw/i2c/smbus.c         | 6 +++---
 include/hw/i2c/smbus.h | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#3 lore raw

PATCH 2/7 smbus: return -1 if nothing found at the given address

In reply to Paolo Bonzini
---
 hw/i2c/smbus.c         | 62 +++++++++++++++++++++++++++++++++++---------------
 include/hw/i2c/smbus.h | 12 +++++-----
 2 files changed, 50 insertions(+), 24 deletions(-)
hw/i2c/smbus.c +44 -18
index 190f08e..6e27ae8 100644
@@ -208,34 +208,44 @@ static int smbus_device_init(I2CSlave *i2c)
 }
 
 /* Master device commands.  */
-void smbus_quick_command(I2CBus *bus, uint8_t addr, int read)
+int smbus_quick_command(I2CBus *bus, uint8_t addr, int read)
 {
-    i2c_start_transfer(bus, addr, read);
+    if (i2c_start_transfer(bus, addr, read)) {
+        return -1;
+    }
     i2c_end_transfer(bus);
+    return 0;
 }
 
 int smbus_receive_byte(I2CBus *bus, uint8_t addr)
 {
     uint8_t data;
 
-    i2c_start_transfer(bus, addr, 1);
+    if (i2c_start_transfer(bus, addr, 1)) {
+        return -1;
+    }
     data = i2c_recv(bus);
     i2c_nack(bus);
     i2c_end_transfer(bus);
     return data;
 }
 
-void smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
+int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
 {
-    i2c_start_transfer(bus, addr, 0);
+    if (i2c_start_transfer(bus, addr, 0)) {
+        return -1;
+    }
     i2c_send(bus, data);
     i2c_end_transfer(bus);
+    return 0;
 }
 
 int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
 {
     uint8_t data;
-    i2c_start_transfer(bus, addr, 0);
+    if (i2c_start_transfer(bus, addr, 0)) {
+        return -1;
+    }
     i2c_send(bus, command);
     i2c_start_transfer(bus, addr, 1);
     data = i2c_recv(bus);
@@ -244,18 +254,23 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
     return data;
 }
 
-void smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data)
+int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data)
 {
-    i2c_start_transfer(bus, addr, 0);
+    if (i2c_start_transfer(bus, addr, 0)) {
+        return -1;
+    }
     i2c_send(bus, command);
     i2c_send(bus, data);
     i2c_end_transfer(bus);
+    return 0;
 }
 
 int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
 {
     uint16_t data;
-    i2c_start_transfer(bus, addr, 0);
+    if (i2c_start_transfer(bus, addr, 0)) {
+        return -1;
+    }
     i2c_send(bus, command);
     i2c_start_transfer(bus, addr, 1);
     data = i2c_recv(bus);
@@ -265,13 +280,16 @@ int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
     return data;
 }
 
-void smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data)
+int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data)
 {
-    i2c_start_transfer(bus, addr, 0);
+    if (i2c_start_transfer(bus, addr, 0)) {
+        return -1;
+    }
     i2c_send(bus, command);
     i2c_send(bus, data & 0xff);
     i2c_send(bus, data >> 8);
     i2c_end_transfer(bus);
+    return 0;
 }
 
 int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data)
@@ -279,33 +297,41 @@ int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data)
     int len;
     int i;
 
-    i2c_start_transfer(bus, addr, 0);
+    if (i2c_start_transfer(bus, addr, 0)) {
+        return -1;
+    }
     i2c_send(bus, command);
     i2c_start_transfer(bus, addr, 1);
     len = i2c_recv(bus);
-    if (len > 32)
+    if (len > 32) {
         len = 0;
-    for (i = 0; i < len; i++)
+    }
+    for (i = 0; i < len; i++) {
         data[i] = i2c_recv(bus);
+    }
     i2c_nack(bus);
     i2c_end_transfer(bus);
     return len;
 }
 
-void smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data,
-                       int len)
+int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data,
+                      int len)
 {
     int i;
 
     if (len > 32)
         len = 32;
 
-    i2c_start_transfer(bus, addr, 0);
+    if (i2c_start_transfer(bus, addr, 0)) {
+        return -1;
+    }
     i2c_send(bus, command);
     i2c_send(bus, len);
-    for (i = 0; i < len; i++)
+    for (i = 0; i < len; i++) {
         i2c_send(bus, data[i]);
+    }
     i2c_end_transfer(bus);
+    return 0;
 }
 
 static void smbus_device_class_init(ObjectClass *klass, void *data)
include/hw/i2c/smbus.h +6 -6
index 285d3b5..544bbc1 100644
@@ -66,16 +66,16 @@ struct SMBusDevice {
 };
 
 /* Master device commands.  */
-void smbus_quick_command(I2CBus *bus, uint8_t addr, int read);
+int smbus_quick_command(I2CBus *bus, uint8_t addr, int read);
 int smbus_receive_byte(I2CBus *bus, uint8_t addr);
-void smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data);
+int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data);
 int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command);
-void smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data);
+int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data);
 int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command);
-void smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data);
+int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data);
 int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data);
-void smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data,
-                       int len);
+int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data,
+                      int len);
 
 void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom,
                        const uint8_t *eeprom_spd, int size);
Signature · 1 line
1.9.0
Reviewed-by on v1 2/7
#4 lore raw

Re: [Qemu-devel] [PATCH for-2.0 2/7] smbus: return -1 if nothing found at the given address

Am 31.03.2014 18:26, schrieb Paolo Bonzini:

Quoted 11 lines
---
 hw/i2c/smbus.c         | 62 +++++++++++++++++++++++++++++++++++---------------
 include/hw/i2c/smbus.h | 12 +++++-----
 2 files changed, 50 insertions(+), 24 deletions(-)
hw/i2c/smbus.c +0 -0
index 190f08e..6e27ae8 100644

hw/i2c/smbus.c static int smbus_device_init(I2CSlave *i2c) · line 213

Quoted 8 lines
hw/i2c/smbus.c +2 -2
@@ -208,34 +208,44 @@ static int smbus_device_init(I2CSlave *i2c)
 }
 
 /* Master device commands.  */
-void smbus_quick_command(I2CBus *bus, uint8_t addr, int read)
+int smbus_quick_command(I2CBus *bus, uint8_t addr, int read)
 {
-    i2c_start_transfer(bus, addr, read);
+    if (i2c_start_transfer(bus, addr, read)) {

For anyone else who wondered about this condition: i2c_start_transfer() returns 1 if slave device is not found and 0 on success, so no normal return codes interfering here.

Quoted 5 lines
hw/i2c/smbus.c +3 -0
+        return -1;
+    }
     i2c_end_transfer(bus);
+    return 0;
 }

[snip]

Might have mentioned also doing some adjacent Coding Style cleanups.

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#5 lore raw

PATCH 3/7 pm_smbus: correctly report unclaimed cycles

In reply to Paolo Bonzini

Without this patch, i2cdetect will report all addresses as present. With it, only 0x50..0x57 are present.

Before:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
    10: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
    20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
    30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
    40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
    50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
    60: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
    70: 70 71 72 73 74 75 76 77

After:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: 50 51 52 53 54 55 56 57 -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
---
 hw/i2c/pm_smbus.c | 63 ++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 41 insertions(+), 22 deletions(-)
hw/i2c/pm_smbus.c +41 -22
index 9f50067..7730100 100644
@@ -60,59 +60,78 @@ static void smb_transaction(PMSMBus *s)
     uint8_t cmd = s->smb_cmd;
     uint8_t addr = s->smb_addr >> 1;
     I2CBus *bus = s->smbus;
+    int ret;
 
     SMBUS_DPRINTF("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
     /* Transaction isn't exec if STS_DEV_ERR bit set */
     if ((s->smb_stat & STS_DEV_ERR) != 0)  {
-            goto error;
-        }
+        goto error;
+    }
     switch(prot) {
     case 0x0:
-        smbus_quick_command(bus, addr, read);
-        s->smb_stat |= STS_BYTE_DONE | STS_INTR;
-        break;
+        ret = smbus_quick_command(bus, addr, read);
+        goto done;
     case 0x1:
         if (read) {
-            s->smb_data0 = smbus_receive_byte(bus, addr);
+            ret = smbus_receive_byte(bus, addr);
+            goto data8;
         } else {
-            smbus_send_byte(bus, addr, cmd);
+            ret = smbus_send_byte(bus, addr, cmd);
+            goto done;
         }
-        s->smb_stat |= STS_BYTE_DONE | STS_INTR;
-        break;
     case 0x2:
         if (read) {
-            s->smb_data0 = smbus_read_byte(bus, addr, cmd);
+            ret = smbus_read_byte(bus, addr, cmd);
+            goto data8;
         } else {
-            smbus_write_byte(bus, addr, cmd, s->smb_data0);
+            ret = smbus_write_byte(bus, addr, cmd, s->smb_data0);
+            goto done;
         }
-        s->smb_stat |= STS_BYTE_DONE | STS_INTR;
         break;
     case 0x3:
         if (read) {
-            uint16_t val;
-            val = smbus_read_word(bus, addr, cmd);
-            s->smb_data0 = val;
-            s->smb_data1 = val >> 8;
+            ret = smbus_read_word(bus, addr, cmd);
+            goto data16;
         } else {
-            smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
+            ret = smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
+            goto done;
         }
-        s->smb_stat |= STS_BYTE_DONE | STS_INTR;
         break;
     case 0x5:
         if (read) {
-            s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data);
+            ret = smbus_read_block(bus, addr, cmd, s->smb_data);
+            goto data8;
         } else {
-            smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
+            ret = smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
+            goto done;
         }
-        s->smb_stat |= STS_BYTE_DONE | STS_INTR;
         break;
     default:
         goto error;
     }
+    abort();
+
+data16:
+    if (ret < 0) {
+        goto error;
+    }
+    s->smb_data1 = ret >> 8;
+data8:
+    if (ret < 0) {
+        goto error;
+    }
+    s->smb_data0 = ret;
+done:
+    if (ret < 0) {
+        goto error;
+    }
+    s->smb_stat |= STS_BYTE_DONE | STS_INTR;
     return;
 
-  error:
+error:
     s->smb_stat |= STS_DEV_ERR;
+    return;
+
 }
 
 static void smb_ioport_writeb(void *opaque, hwaddr addr, uint64_t val,
Signature · 1 line
1.9.0
Reviewed-by on v1 3/7
#6 lore raw

Re: [Qemu-devel] [PATCH for-2.0 3/7] pm_smbus: correctly report unclaimed cycles

Am 31.03.2014 18:26, schrieb Paolo Bonzini:

Quoted 27 lines

Without this patch, i2cdetect will report all addresses as present. With it, only 0x50..0x57 are present.

Before:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
    10: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
    20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
    30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
    40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
    50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
    60: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
    70: 70 71 72 73 74 75 76 77

After:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: 50 51 52 53 54 55 56 57 -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
Quoted 4 lines
---
 hw/i2c/pm_smbus.c | 63 ++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 41 insertions(+), 22 deletions(-)

The pure goto refactoring looks okay to me; don't have a datasheet to check the changing error smb_stat behavior against though, so FWIW

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#7 lore raw

PATCH 4/7 tmp105: read temperature in milli-celsius

In reply to Paolo Bonzini

Right now, the temperature property must be written in milli-celsius, but it reads back the value in 8.8 fixed point. Fix this by letting the property read back the original value (possibly rounded). Also simplify the code that does the conversion.

Before:

(QEMU) qom-set path=/machine/peripheral/sensor property=temperature value=20000 {u'return': {}}
(QEMU) qom-get path=sensor property=temperature
{u'return': 5120}

After:

(QEMU) qom-set path=/machine/peripheral/sensor property=temperature value=20000 {u'return': {}}
(QEMU) qom-get path=sensor property=temperature
{u'return': 20000}

---
 hw/misc/tmp105.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
hw/misc/tmp105.c +5 -3
index caec4a5..244c1ef 100644
@@ -56,12 +56,14 @@ static void tmp105_get_temperature(Object *obj, Visitor *v, void *opaque,
                                    const char *name, Error **errp)
 {
     TMP105State *s = TMP105(obj);
-    int64_t value = s->temperature;
+    int64_t value = s->temperature * 1000 / 256;
 
     visit_type_int(v, &value, name, errp);
 }
 
-/* Units are 0.001 centigrades relative to 0 C.  */
+/* Units are 0.001 centigrades relative to 0 C.  s->temperature is 8.8
+ * fixed point, so units are 1/256 centigrades.  A simple ratio will do.
+ */
 static void tmp105_set_temperature(Object *obj, Visitor *v, void *opaque,
                                    const char *name, Error **errp)
 {
@@ -80,7 +82,7 @@ static void tmp105_set_temperature(Object *obj, Visitor *v, void *opaque,
         return;
     }
 
-    s->temperature = ((int16_t) (temp * 0x800 / 128000)) << 4;
+    s->temperature = (int16_t) (temp * 256 / 1000);
 
     tmp105_alarm_update(s);
 }
Signature · 1 line
1.9.0
#8 lore raw

PATCH 5/7 tmp105-test: wrap simple building blocks for testing

In reply to Paolo Bonzini

The next patches will add more reads and writes. Add a simple testing API for this.

---
 tests/tmp105-test.c | 54 +++++++++++++++++++++++++++++++++++------------------
 1 file changed, 36 insertions(+), 18 deletions(-)
tests/tmp105-test.c +36 -18
index 0834219..20a1894 100644
@@ -20,39 +20,57 @@
 static I2CAdapter *i2c;
 static uint8_t addr;
 
-static void send_and_receive(void)
+static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
 {
-    uint8_t cmd[3];
     uint8_t resp[2];
-
-    cmd[0] = TMP105_REG_TEMPERATURE;
-    i2c_send(i2c, addr, cmd, 1);
+    i2c_send(i2c, addr, &reg, 1);
     i2c_recv(i2c, addr, resp, 2);
-    g_assert_cmpuint(((uint16_t)resp[0] << 8) | resp[1], ==, 0);
+    return (resp[0] << 8) | resp[1];
+}
+
+static void tmp105_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
+                        uint8_t value)
+{
+    uint8_t cmd[2];
+    uint8_t resp[1];
 
-    cmd[0] = TMP105_REG_CONFIG;
-    cmd[1] = 0x0; /* matches the reset value */
+    cmd[0] = reg;
+    cmd[1] = value;
     i2c_send(i2c, addr, cmd, 2);
     i2c_recv(i2c, addr, resp, 1);
     g_assert_cmphex(resp[0], ==, cmd[1]);
+}
 
-    cmd[0] = TMP105_REG_T_LOW;
-    cmd[1] = 0x12;
-    cmd[2] = 0x34;
-    i2c_send(i2c, addr, cmd, 3);
-    i2c_recv(i2c, addr, resp, 2);
-    g_assert_cmphex(resp[0], ==, cmd[1]);
-    g_assert_cmphex(resp[1], ==, cmd[2]);
+static void tmp105_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
+                         uint16_t value)
+{
+    uint8_t cmd[3];
+    uint8_t resp[2];
 
-    cmd[0] = TMP105_REG_T_HIGH;
-    cmd[1] = 0x42;
-    cmd[2] = 0x31;
+    cmd[0] = reg;
+    cmd[1] = value >> 8;
+    cmd[2] = value & 255;
     i2c_send(i2c, addr, cmd, 3);
     i2c_recv(i2c, addr, resp, 2);
     g_assert_cmphex(resp[0], ==, cmd[1]);
     g_assert_cmphex(resp[1], ==, cmd[2]);
 }
 
+
+static void send_and_receive(void)
+{
+    uint16_t value;
+
+    value = tmp105_get16(i2c, addr, TMP105_REG_TEMPERATURE);
+    g_assert_cmpuint(value, ==, 0);
+
+    /* reset */
+    tmp105_set8(i2c, addr, TMP105_REG_CONFIG, 0);
+
+    tmp105_set16(i2c, addr, TMP105_REG_T_LOW, 0x1234);
+    tmp105_set16(i2c, addr, TMP105_REG_T_HIGH, 0x4231);
+}
+
 int main(int argc, char **argv)
 {
     QTestState *s = NULL;
Signature · 1 line
1.9.0
#9 lore raw

PATCH 6/7 tmp105-test: add a second sensor and test that one

In reply to Paolo Bonzini

This will make it easier to reach the device under test via QOM.

---
 tests/tmp105-test.c | 17 +++++++++--------
 5 files changed, 13 insertions(+), 12 deletions(-)
tests/tmp105-test.c +9 -8
index 20a1894..4e640b4 100644
@@ -15,10 +15,10 @@
 
 #define OMAP2_I2C_1_BASE 0x48070000
 
-#define N8X0_ADDR 0x48
+#define TMP105_TEST_ID   "tmp105-test"
+#define TMP105_TEST_ADDR 0x49
 
 static I2CAdapter *i2c;
-static uint8_t addr;
 
 static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
 {
@@ -61,14 +61,14 @@ static void send_and_receive(void)
 {
     uint16_t value;
 
-    value = tmp105_get16(i2c, addr, TMP105_REG_TEMPERATURE);
+    value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
     g_assert_cmpuint(value, ==, 0);
 
     /* reset */
-    tmp105_set8(i2c, addr, TMP105_REG_CONFIG, 0);
+    tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0);
 
-    tmp105_set16(i2c, addr, TMP105_REG_T_LOW, 0x1234);
-    tmp105_set16(i2c, addr, TMP105_REG_T_HIGH, 0x4231);
+    tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234);
+    tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231);
 }
 
 int main(int argc, char **argv)
@@ -78,9 +78,10 @@ int main(int argc, char **argv)
 
     g_test_init(&argc, &argv, NULL);
 
-    s = qtest_start("-machine n800");
+    s = qtest_start("-machine n800 "
+                    "-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
+                    ",address=0x49");
     i2c = omap_i2c_create(OMAP2_I2C_1_BASE);
-    addr = N8X0_ADDR;
 
     qtest_add_func("/tmp105/tx-rx", send_and_receive);
 
Signature · 1 line
1.9.0
#10 lore raw

Re: [Qemu-devel] [PATCH for-2.0 6/7] tmp105-test: add a second sensor and test that one

Am 31.03.2014 18:26, schrieb Paolo Bonzini:

Quoted 44 lines

This will make it easier to reach the device under test via QOM.

---
 tests/tmp105-test.c | 17 +++++++++--------
 5 files changed, 13 insertions(+), 12 deletions(-)
tests/tmp105-test.c +6 -6
index 20a1894..4e640b4 100644
@@ -15,10 +15,10 @@
 
 #define OMAP2_I2C_1_BASE 0x48070000
 
-#define N8X0_ADDR 0x48
+#define TMP105_TEST_ID   "tmp105-test"
+#define TMP105_TEST_ADDR 0x49
 
 static I2CAdapter *i2c;
-static uint8_t addr;
 
 static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
 {
@@ -61,14 +61,14 @@ static void send_and_receive(void)
 {
     uint16_t value;
 
-    value = tmp105_get16(i2c, addr, TMP105_REG_TEMPERATURE);
+    value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
     g_assert_cmpuint(value, ==, 0);
 
     /* reset */
-    tmp105_set8(i2c, addr, TMP105_REG_CONFIG, 0);
+    tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0);
 
-    tmp105_set16(i2c, addr, TMP105_REG_T_LOW, 0x1234);
-    tmp105_set16(i2c, addr, TMP105_REG_T_HIGH, 0x4231);
+    tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234);
+    tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231);
 }
 
 int main(int argc, char **argv)

tests/tmp105-test.c int main(int argc, char **argv) · line 83

Quoted 7 lines
tests/tmp105-test.c +3 -1
@@ -78,9 +78,10 @@ int main(int argc, char **argv)
 
     g_test_init(&argc, &argv, NULL);
 
-    s = qtest_start("-machine n800");
+    s = qtest_start("-machine n800 "
+                    "-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
+                    ",address=0x49");

Probably we could do some clever stringify() tricks here to reuse your TMP105_TEST_ADDR constant, but I'll leave it as is for now.

Andreas

Quoted 6 lines
tests/tmp105-test.c +0 -1
     i2c = omap_i2c_create(OMAP2_I2C_1_BASE);
-    addr = N8X0_ADDR;
 
     qtest_add_func("/tmp105/tx-rx", send_and_receive);
 
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#11 lore raw

PATCH 7/7 tmp105-test: test QOM property and precision

In reply to Paolo Bonzini

This lets us add a regression test for the first commit in this series.

---
 tests/tmp105-test.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 3 deletions(-)
tests/tmp105-test.c +76 -3
index 4e640b4..15ddaf3 100644
@@ -20,6 +20,14 @@
 
 static I2CAdapter *i2c;
 
+static uint16_t tmp105_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
+{
+    uint8_t resp[1];
+    i2c_send(i2c, addr, &reg, 1);
+    i2c_recv(i2c, addr, resp, 1);
+    return resp[0];
+}
+
 static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
 {
     uint8_t resp[2];
@@ -56,16 +64,81 @@ static void tmp105_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
     g_assert_cmphex(resp[1], ==, cmd[2]);
 }
 
+static int qmp_tmp105_get_temperature(const char *id)
+{
+    QDict *response;
+    int ret;
 
+    response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': '%s', "
+                   "'property': 'temperature' } }", id);
+    g_assert(qdict_haskey(response, "return"));
+    ret = qdict_get_int(response, "return");
+    QDECREF(response);
+    return ret;
+}
+
+static void qmp_tmp105_set_temperature(const char *id, int value)
+{
+    QDict *response;
+
+    response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': '%s', "
+                   "'property': 'temperature', 'value': %d } }", id, value);
+    g_assert(qdict_haskey(response, "return"));
+    QDECREF(response);
+}
+
+#define TMP105_PRECISION (1000/16)
 static void send_and_receive(void)
 {
     uint16_t value;
 
-    value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
+    value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
     g_assert_cmpuint(value, ==, 0);
 
-    /* reset */
-    tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0);
+    value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
+    g_assert_cmphex(value, ==, 0);
+
+    qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
+    value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
+    g_assert_cmpuint(value, ==, 20000);
+
+    value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
+    g_assert_cmphex(value, ==, 0x1400);
+
+    qmp_tmp105_set_temperature(TMP105_TEST_ID, 20938); /* 20 + 15/16 */
+    value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
+    g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
+    g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
+
+    /* Set config */
+    tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60);
+    value = tmp105_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG);
+    g_assert_cmphex(value, ==, 0x60);
+
+    value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
+    g_assert_cmphex(value, ==, 0x14f0);
+
+    /* Set precision to 9, 10, 11 bits.  */
+    tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x00);
+    value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
+    g_assert_cmphex(value, ==, 0x1480);
+
+    tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x20);
+    value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
+    g_assert_cmphex(value, ==, 0x14c0);
+
+    tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x40);
+    value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
+    g_assert_cmphex(value, ==, 0x14e0);
+
+    /* stored precision remains the same */
+    value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
+    g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
+    g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
+
+    tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60);
+    value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
+    g_assert_cmphex(value, ==, 0x14f0);
 
     tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234);
     tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231);
Signature · 1 line
1.9.0
#12 lore raw
In reply to Paolo Bonzini

Hi,

Am 31.03.2014 18:26, schrieb Paolo Bonzini:

Quoted 10 lines

This is a resend of the I2C patches I posted a while ago. Patches 1-3 are just a rebase.

Patch 4 is the same as before, patches 5-7 make the tmp105 testcase more complete in order to test that change.

Paolo Bonzini (7):
smbus: allow returning an error from reads
smbus: return -1 if nothing found at the given address
pm_smbus: correctly report unclaimed cycles

I've reviewed these and they look sane and safe for 2.0. mst, could you have a second look as PC maintainer and take them?

Quoted 4 lines

tmp105: read temperature in milli-celsius
tmp105-test: wrap simple building blocks for testing
tmp105-test: add a second sensor and test that one
tmp105-test: test QOM property and precision

Thanks, this is more than I would've done myself or asked for. I've re-broken the text paragraphs of 4/7 to fit into 76 chars and filled in the commit info in 7/7:
https://github.com/afaerber/qemu-cpu/commits/qom-next

And for the record, this demonstrates how pretty much any I2C device (such as ds1338) can be tested for lack of other libqos drivers: Add it via -device to n800 or n810 machine using a free address (such as 0x49 here) and give the device an ID for accessing it via /machine/peripheral/<id> (just shorthand <id> here, I let that slip through).

Quoted 10 lines
 hw/i2c/pm_smbus.c      |  63 +++++++++++++++--------
 hw/i2c/smbus.c         |  68 +++++++++++++++++--------
 hw/misc/tmp105.c       |   8 +--
 include/hw/i2c/smbus.h |  18 +++----
 roms/SLOF              |   2 +-
 roms/openbios          |   2 +-
 roms/qemu-palcode      |   2 +-
 roms/seabios           |   2 +-
 tests/tmp105-test.c    | 136 +++++++++++++++++++++++++++++++++++++++++--------
 9 files changed, 220 insertions(+), 81 deletions(-)

This diffstat luckily differs from the patches. Please double-check your setup. :)

Regards,
Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#13 lore raw
Quoted 9 lines · Andreas Färber

Hi,

Am 31.03.2014 18:26, schrieb Paolo Bonzini:

Quoted 6 lines

This is a resend of the I2C patches I posted a while ago. Patches 1-3 are just a rebase.

Patch 4 is the same as before, patches 5-7 make the tmp105 testcase more complete in order to test that change.

Quoted 7 lines
Quoted 4 lines

Paolo Bonzini (7):
smbus: allow returning an error from reads
smbus: return -1 if nothing found at the given address
pm_smbus: correctly report unclaimed cycles

I've reviewed these and they look sane and safe for 2.0. mst, could you have a second look as PC maintainer and take them?

I'd rather delay to 2.1.
It's not a regression is it?

Quoted 37 lines
Quoted 4 lines

tmp105: read temperature in milli-celsius
tmp105-test: wrap simple building blocks for testing
tmp105-test: add a second sensor and test that one
tmp105-test: test QOM property and precision

Thanks, this is more than I would've done myself or asked for. I've re-broken the text paragraphs of 4/7 to fit into 76 chars and filled in the commit info in 7/7:
https://github.com/afaerber/qemu-cpu/commits/qom-next

And for the record, this demonstrates how pretty much any I2C device (such as ds1338) can be tested for lack of other libqos drivers: Add it via -device to n800 or n810 machine using a free address (such as 0x49 here) and give the device an ID for accessing it via /machine/peripheral/<id> (just shorthand <id> here, I let that slip through).

Quoted 10 lines
 hw/i2c/pm_smbus.c      |  63 +++++++++++++++--------
 hw/i2c/smbus.c         |  68 +++++++++++++++++--------
 hw/misc/tmp105.c       |   8 +--
 include/hw/i2c/smbus.h |  18 +++----
 roms/SLOF              |   2 +-
 roms/openbios          |   2 +-
 roms/qemu-palcode      |   2 +-
 roms/seabios           |   2 +-
 tests/tmp105-test.c    | 136 +++++++++++++++++++++++++++++++++++++++++--------
 9 files changed, 220 insertions(+), 81 deletions(-)

This diffstat luckily differs from the patches. Please double-check your setup. :)

Regards,
Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#14 lore raw

Il 02/04/2014 17:55, Michael S. Tsirkin ha scritto:

Quoted 5 lines
Quoted 2 lines

I've reviewed these and they look sane and safe for 2.0. mst, could you have a second look as PC maintainer and take them?

I'd rather delay to 2.1.
It's not a regression is it?

No, I'm fine with 2.1 since I won't be around to fix mess.

Paolo

#15 lore raw
In reply to Michael S. Tsirkin

Il 02/04/2014 17:55, Michael S. Tsirkin ha scritto:

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

smbus: allow returning an error from reads
smbus: return -1 if nothing found at the given address
pm_smbus: correctly report unclaimed cycles

I've reviewed these and they look sane and safe for 2.0. mst, could you have a second look as PC maintainer and take them?

I'd rather delay to 2.1.
It's not a regression is it?

Ping?

Paolo

#16 lore raw
Quoted 7 lines · Paolo Bonzini

Il 02/04/2014 17:55, Michael S. Tsirkin ha scritto:

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

smbus: allow returning an error from reads
smbus: return -1 if nothing found at the given address
pm_smbus: correctly report unclaimed cycles

I've reviewed these and they look sane and safe for 2.0. mst, could you have a second look as PC maintainer and take them?

Quoted 7 lines
Quoted 3 lines

I'd rather delay to 2.1.
It's not a regression is it?

Ping?

Paolo

Applied, thanks!