Skip to contentSkip to conversation

Generalize Dallas/Maxim I2C RTC devices

PATCH In review Michael Davidsaver 3 participants 11 messages Feb 19, 2018 – Apr 5, 2018

lore
Share & tools
lore
#0 lore raw

cover Generalize Dallas/Maxim I2C RTC devices

These changes previously appeared as part of a series "Add MVME3100 PPC SBC v2" back in November. David Gibson, who looked that that series, suggested getting this reviewed separately. There doesn't appear to be a listed maintainer for this code, so I'm addressing this to the 3 people who have made more than cosmetic changes to it. The most recent of these was in 2012.

This series replaces the ds1338 RTC with a model covering a number of these similar chips: ds1307, ds1337, ds1338, ds1339, ds1340, ds1375, ds1388, and ds3231.

The limits of the new model are the same as the old. Only the time of day registers, and NVRAM are modeled. The alarm and control registers are not.

I've added a more thorough test of the time of day function, covering reading and setting in both 12 and 24 hour mode. In the process I found two minor issues with the ds1338 model. These are described in the commit message for #3. So this series first adds those tests which pass with both old and new model. Then later adds some additional tests which only pass with the new model.

Michael Davidsaver (5):
timer: ds1338 add magic reset for test code
tests: more thorough test of ds1338
timer: generalize Dallas/Maxim RTC i2c devices
tests: ds-rtc-i2c-test test 12 hour mode and DoW
tests: drop ds1338-test

 default-configs/arm-softmmu.mak |   2 +-
 hw/timer/Makefile.objs          |   2 +-
 hw/timer/ds-rtc-i2c.c           | 466 ++++++++++++++++++++++++++++++++++++++++
 hw/timer/ds1338.c               | 239 ---------------------
 tests/Makefile.include          |   4 +-
 tests/ds-rtc-i2c-test.c         | 245 +++++++++++++++++++++
 tests/ds1338-test.c             |  75 -------
 7 files changed, 715 insertions(+), 318 deletions(-)
 create mode 100644 hw/timer/ds-rtc-i2c.c
 delete mode 100644 hw/timer/ds1338.c
 create mode 100644 tests/ds-rtc-i2c-test.c
 delete mode 100644 tests/ds1338-test.c
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/5 timer: ds1338 add magic reset for test code

When running w/ QTest, allow the tester to reliably zero time offsets. Allows tests to read the current time, and set time, independent of test order.

---
 hw/timer/ds1338.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/timer/ds1338.c +9 -0
index 3849b74a68..41c2d7dac6 100644
@@ -14,6 +14,7 @@
 #include "qemu-common.h"
 #include "hw/i2c/i2c.h"
 #include "qemu/bcd.h"
+#include "sysemu/qtest.h"
 
 /* Size of NVRAM including both the user-accessible area and the
  * secondary register area.
@@ -132,6 +133,14 @@ static int ds1338_send(I2CSlave *i2c, uint8_t data)
     DS1338State *s = DS1338(i2c);
 
     if (s->addr_byte) {
+        if (data == 0xff && qtest_enabled()) {
+            /* magic, out of bounds, address to allow test code
+             * to reset offset
+             */
+            s->offset = 0;
+            s->wday_offset = 0;
+            return 0;
+        }
         s->ptr = data & (NVRAM_SIZE - 1);
         s->addr_byte = false;
         return 0;
Signature · 1 line
2.11.0
#2 lore raw

PATCH 2/5 tests: more thorough test of ds1338

In reply to Michael Davidsaver

Test current time and set+get round trip.

The set+get test is repeated 4 times. These cases are spread across a single day in an attempt to trigger some potential issues regardless of the timezone of the machine running the tests.

---
 tests/Makefile.include  |   2 +
 tests/ds-rtc-i2c-test.c | 193 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 195 insertions(+)
 create mode 100644 tests/ds-rtc-i2c-test.c
tests/Makefile.include +2 -0
index a1bcbffe12..f5dcd274e0 100644
@@ -360,6 +360,7 @@ check-qtest-sparc64-y += tests/boot-serial-test$(EXESUF)
 
 check-qtest-arm-y = tests/tmp105-test$(EXESUF)
 check-qtest-arm-y += tests/ds1338-test$(EXESUF)
+check-qtest-arm-y += tests/ds-rtc-i2c-test$(EXESUF)
 check-qtest-arm-y += tests/m25p80-test$(EXESUF)
 gcov-files-arm-y += hw/misc/tmp105.c
 check-qtest-arm-y += tests/virtio-blk-test$(EXESUF)
@@ -764,6 +765,7 @@ tests/bios-tables-test$(EXESUF): tests/bios-tables-test.o \
 tests/pxe-test$(EXESUF): tests/pxe-test.o tests/boot-sector.o $(libqos-obj-y)
 tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y)
 tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y)
+tests/ds-rtc-i2c-test$(EXESUF): tests/ds-rtc-i2c-test.o $(libqos-imx-obj-y)
 tests/m25p80-test$(EXESUF): tests/m25p80-test.o
 tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y)
 tests/q35-test$(EXESUF): tests/q35-test.o $(libqos-pc-obj-y)
tests/ds-rtc-i2c-test.c new file +193 -0

193 lines not on this page · raw message

Signature · 1 line
2.11.0
#9 lore raw

PATCH 4/5 tests: ds-rtc-i2c-test test 12 hour mode and DoW

In reply to Michael Davidsaver

Test time set+get in 12 hour mode.
Also test handling of day of week
offset.

---
 tests/ds-rtc-i2c-test.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)
tests/ds-rtc-i2c-test.c +53 -1
index 464eb08558..226ac1399e 100644
@@ -92,6 +92,18 @@ static uint8_t test_time_24_12am[8] = {
     0x17,
 };
 
+static uint8_t test_time_12_12am[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 00:30:53 +0000 */
+    0x53,
+    0x30,
+    0x52, /* 12 AM in 12 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
 static uint8_t test_time_24_6am[8] = {
     0, /* address */
     /* Wed, 22 Nov 2017 06:30:53 +0000 */
@@ -104,6 +116,18 @@ static uint8_t test_time_24_6am[8] = {
     0x17,
 };
 
+static uint8_t test_time_12_6am[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 06:30:53 +0000 */
+    0x53,
+    0x30,
+    0x46, /* 6 AM in 12 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
 static uint8_t test_time_24_12pm[8] = {
     0, /* address */
     /* Wed, 22 Nov 2017 12:30:53 +0000 */
@@ -116,6 +140,18 @@ static uint8_t test_time_24_12pm[8] = {
     0x17,
 };
 
+static uint8_t test_time_12_12pm[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 12:30:53 +0000 */
+    0x53,
+    0x30,
+    0x72, /* 12 PM in 24 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
 static uint8_t test_time_24_6pm[8] = {
     0, /* address */
     /* Wed, 22 Nov 2017 18:30:53 +0000 */
@@ -128,6 +164,18 @@ static uint8_t test_time_24_6pm[8] = {
     0x17,
 };
 
+static uint8_t test_time_12_6pm[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 18:30:53 +0000 */
+    0x53,
+    0x30,
+    0x66, /* 6 PM in 12 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
 /* write in and read back known time */
 static
 void test_rtc_set(const void *raw)
@@ -151,7 +199,7 @@ void test_rtc_set(const void *raw)
     g_assert_cmpuint(testtime[1], ==, buf[0]); /* SEC */
     g_assert_cmpuint(testtime[2], ==, buf[1]); /* MIN */
     g_assert_cmpuint(testtime[3], ==, buf[2]); /* HOUR */
-    /* skip comparing Day of Week.  Not handled correctly */
+    g_assert_cmpuint(testtime[4], ==, buf[3]); /* DoW */
     g_assert_cmpuint(testtime[5], ==, buf[4]); /* DoM */
     if (use_century) {
         g_assert_cmpuint(testtime[6], ==, buf[5]); /* MON+century */
@@ -183,6 +231,10 @@ int main(int argc, char *argv[])
     qtest_add_data_func("/ds-rtc-i2c/set24_6am", test_time_24_6am, test_rtc_set);
     qtest_add_data_func("/ds-rtc-i2c/set24_12pm", test_time_24_12pm, test_rtc_set);
     qtest_add_data_func("/ds-rtc-i2c/set24_6pm", test_time_24_6pm, test_rtc_set);
+    qtest_add_data_func("/ds-rtc-i2c/set12_12am", test_time_12_12am, test_rtc_set);
+    qtest_add_data_func("/ds-rtc-i2c/set12_6am", test_time_12_6am, test_rtc_set);
+    qtest_add_data_func("/ds-rtc-i2c/set12_12pm", test_time_12_12pm, test_rtc_set);
+    qtest_add_data_func("/ds-rtc-i2c/set12_6pm", test_time_12_6pm, test_rtc_set);
     qtest_add_func("/ds-rtc-i2c/current", test_rtc_current);
 
     ret = g_test_run();
Signature · 1 line
2.11.0
#7 lore raw

PATCH 3/5 timer: generalize Dallas/Maxim RTC i2c devices

In reply to Michael Davidsaver

Support for: ds1307, ds1337, ds1338, ds1339, ds1340, ds1375, ds1388, and ds3231.

Tested with ds1338 and ds1375.

The existing ds1338 model has two bugs with almost no practical impact.

1. Trying to set time in 12-hour mode works, but the 12 hour mode bit isn't stored.
So time always reads in 24 hour mode.

2. wday_offset is always stored for the local time zone. When the RTC is set
and rtc_utc=1 and the local timezone
has a different day than UTC, then wday_offset will be off by one.

---
 default-configs/arm-softmmu.mak |   2 +-
 hw/timer/Makefile.objs          |   2 +-
 hw/timer/ds-rtc-i2c.c           | 466 ++++++++++++++++++++++++++++++++++++++++
 hw/timer/ds1338.c               | 248 ---------------------
 4 files changed, 468 insertions(+), 250 deletions(-)
 create mode 100644 hw/timer/ds-rtc-i2c.c
 delete mode 100644 hw/timer/ds1338.c
default-configs/arm-softmmu.mak +1 -1
index ca34cf4462..510a92c9a8 100644
@@ -31,7 +31,7 @@ CONFIG_SMC91C111=y
 CONFIG_ALLWINNER_EMAC=y
 CONFIG_IMX_FEC=y
 CONFIG_FTGMAC100=y
-CONFIG_DS1338=y
+CONFIG_DSRTCI2C=y
 CONFIG_PFLASH_CFI01=y
 CONFIG_PFLASH_CFI02=y
 CONFIG_MICRODRIVE=y
hw/timer/Makefile.objs +1 -1
index 8c19eac3b6..290015ebec 100644
@@ -3,7 +3,7 @@ common-obj-$(CONFIG_ARM_MPTIMER) += arm_mptimer.o
 common-obj-$(CONFIG_ARM_V7M) += armv7m_systick.o
 common-obj-$(CONFIG_A9_GTIMER) += a9gtimer.o
 common-obj-$(CONFIG_CADENCE) += cadence_ttc.o
-common-obj-$(CONFIG_DS1338) += ds1338.o
+common-obj-$(CONFIG_DSRTCI2C) += ds-rtc-i2c.o
 common-obj-$(CONFIG_HPET) += hpet.o
 common-obj-$(CONFIG_I8254) += i8254_common.o i8254.o
 common-obj-$(CONFIG_M48T59) += m48t59.o
hw/timer/ds-rtc-i2c.c new file +466 -0

466 lines not on this page · raw message

hw/timer/ds1338.c deleted +0 -248

248 lines not on this page · raw message

Signature · 1 line
2.11.0
#10 lore raw

PATCH 5/5 tests: drop ds1338-test

In reply to Michael Davidsaver

Now redundant to ds-rtc-i2c-test.

---
 tests/Makefile.include |  2 --
 tests/ds1338-test.c    | 75 --------------------------------------------------
 2 files changed, 77 deletions(-)
 delete mode 100644 tests/ds1338-test.c
tests/Makefile.include +0 -2
index f5dcd274e0..8b1e486e32 100644
@@ -359,7 +359,6 @@ check-qtest-sparc64-y += tests/prom-env-test$(EXESUF)
 check-qtest-sparc64-y += tests/boot-serial-test$(EXESUF)
 
 check-qtest-arm-y = tests/tmp105-test$(EXESUF)
-check-qtest-arm-y += tests/ds1338-test$(EXESUF)
 check-qtest-arm-y += tests/ds-rtc-i2c-test$(EXESUF)
 check-qtest-arm-y += tests/m25p80-test$(EXESUF)
 gcov-files-arm-y += hw/misc/tmp105.c
@@ -764,7 +763,6 @@ tests/bios-tables-test$(EXESUF): tests/bios-tables-test.o \
 	tests/boot-sector.o tests/acpi-utils.o $(libqos-obj-y)
 tests/pxe-test$(EXESUF): tests/pxe-test.o tests/boot-sector.o $(libqos-obj-y)
 tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y)
-tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y)
 tests/ds-rtc-i2c-test$(EXESUF): tests/ds-rtc-i2c-test.o $(libqos-imx-obj-y)
 tests/m25p80-test$(EXESUF): tests/m25p80-test.o
 tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y)
tests/ds1338-test.c deleted +0 -75

75 lines not on this page · raw message

Signature · 1 line
2.11.0
#3 lore raw

Re: [Qemu-devel] [PATCH 2/5] tests: more thorough test of ds1338

In reply to Michael Davidsaver
Quoted 12 lines · Michael Davidsaver

Test current time and set+get round trip.

The set+get test is repeated 4 times. These cases are spread across a single day in an attempt to trigger some potential issues regardless of the timezone of the machine running the tests.

---
 tests/Makefile.include  |   2 +
 tests/ds-rtc-i2c-test.c | 193 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 195 insertions(+)
 create mode 100644 tests/ds-rtc-i2c-test.c

[...]

Quoted 70 lines

tests/q35-test$(EXESUF): tests/q35-test.o $(libqos-pc-obj-y)

tests/ds-rtc-i2c-test.c new file +63 -0
new file mode 100644
index 0000000000..464eb08558
@@ -0,0 +1,193 @@
+/* Testing of Dallas/Maxim I2C bus RTC devices
+ *
+ * Copyright (c) 2017 Michael Davidsaver
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the LICENSE file in the top-level directory.
+ */
+#include <stdio.h>
+
+#include "qemu/osdep.h"
+#include "qemu/bcd.h"
+#include "qemu/cutils.h"
+#include "qemu/timer.h"
+#include "libqtest.h"
+#include "libqos/libqos.h"
+#include "libqos/i2c.h"
+
+#define IMX25_I2C_0_BASE 0x43F80000
+#define DS1338_ADDR 0x68
+
+static I2CAdapter *i2c;
+static uint8_t addr;
+static bool use_century;
+
+static
+time_t rtc_gettime(void)
+{
+    struct tm parts;
+    uint8_t buf[7];
+
+    buf[0] = 0;
+    i2c_send(i2c, addr, buf, 1);
+    i2c_recv(i2c, addr, buf, 7);
+
+    parts.tm_sec = from_bcd(buf[0]);
+    parts.tm_min = from_bcd(buf[1]);
+    if (buf[2] & 0x40) {
+        /* 12 hour */
+        /* HOUR register is 1-12. */
+        parts.tm_hour = from_bcd(buf[2] & 0x1f);
+        g_assert_cmpuint(parts.tm_hour, >=, 1);
+        g_assert_cmpuint(parts.tm_hour, <=, 12);
+        parts.tm_hour %= 12u; /* wrap 12 -> 0 */
+        if (buf[2] & 0x20) {
+            parts.tm_hour += 12u;
+        }
+    } else {
+        /* 24 hour */
+        parts.tm_hour = from_bcd(buf[2] & 0x3f);
+    }
+    parts.tm_wday = from_bcd(buf[3]);
+    parts.tm_mday = from_bcd(buf[4]);
+    parts.tm_mon =  from_bcd((buf[5] & 0x1f) - 1u);
+    parts.tm_year = from_bcd(buf[6]);
+    if (!use_century || (buf[5] & 0x80)) {
+        parts.tm_year += 100u;
+    }
+
+    return mktimegm(&parts);
+}
+
+/* read back and compare with current system time */
+static

tests/ds-rtc-i2c-test.c line 73

Quoted 10 lines
tests/ds-rtc-i2c-test.c new file +10 -0
+void test_rtc_current(void)
+{
+    uint8_t buf;
+    time_t expected, actual;
+
+    /* magic address to zero RTC time offset
+     * as tests may be run in any order
+     */
+    buf = 0xff;
+    i2c_send(i2c, addr, &buf, 1);

That magic (together with patch 1/5) is IMHO a little bit ugly. I've hit the same problem with the m48t59 test recently, and I solved it by moving the qtest_start() and qtest_end() calls from the main() function into the single tests instead, so that each test starts with a clean state:

https://git.qemu.org/?p=qemu.git;a=commitdiff;h=9c29830c90d82f27f

Could you maybe try whether that approach works for your test cases here, too? Then you could do this without the "0xff" hack here...

Quoted 62 lines
tests/ds-rtc-i2c-test.c new file +62 -0
+
+    actual = time(NULL);
+    /* new second may start here */
+    expected = rtc_gettime();
+    g_assert_cmpuint(expected, <=, actual + 1);
+    g_assert_cmpuint(expected, >=, actual);
+}
+
+
+static uint8_t test_time_24_12am[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 00:30:53 +0000 */
+    0x53,
+    0x30,
+    0x00, /* 12 AM in 24 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
+static uint8_t test_time_24_6am[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 06:30:53 +0000 */
+    0x53,
+    0x30,
+    0x06, /* 6 AM in 24 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
+static uint8_t test_time_24_12pm[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 12:30:53 +0000 */
+    0x53,
+    0x30,
+    0x12, /* 12 PM in 24 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
+static uint8_t test_time_24_6pm[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 18:30:53 +0000 */
+    0x53,
+    0x30,
+    0x18, /* 6 PM in 24 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
+/* write in and read back known time */
+static
+void test_rtc_set(const void *raw)
+{
+    const uint8_t *testtime = raw;

tests/ds-rtc-i2c-test.c line 145

Quoted 10 lines
tests/ds-rtc-i2c-test.c new file +10 -0
+    uint8_t buf[7];
+    unsigned retry = 2;
+
+    for (; retry; retry--) {
+        i2c_send(i2c, addr, testtime, 8);
+        /* new second may start here */
+        i2c_send(i2c, addr, testtime, 1);
+        i2c_recv(i2c, addr, buf, 7);
+
+        if (testtime[1] == buf[0]) {

Please also check the minutes here (reason: see below).

tests/ds-rtc-i2c-test.c line 152

Quoted 7 lines
tests/ds-rtc-i2c-test.c new file +7 -0
+            break;
+        }
+        /* we raced start of second, retry */
+    };
+
+    g_assert_cmpuint(testtime[1], ==, buf[0]); /* SEC */
+    g_assert_cmpuint(testtime[2], ==, buf[1]); /* MIN */

Could you please wrap the SEC and MIN lines in a "if (!g_test_slow()) { ... }" statement? The problem is: The "make check" tests are run as CI on a system that is sometimes *very* overloaded. It might happen that the test is sometimes interrupted for dozens of seconds, so it might fail to properly read the time on a granularity of seconds. With !g_test_slow() you can make sure that the check is not done on such overloaded CI systems.

Quoted 13 lines
tests/ds-rtc-i2c-test.c new file +13 -0
+    g_assert_cmpuint(testtime[3], ==, buf[2]); /* HOUR */
+    /* skip comparing Day of Week.  Not handled correctly */
+    g_assert_cmpuint(testtime[5], ==, buf[4]); /* DoM */
+    if (use_century) {
+        g_assert_cmpuint(testtime[6], ==, buf[5]); /* MON+century */
+    } else {
+        g_assert_cmpuint(testtime[6] & 0x7f, ==, buf[5]); /* MON */
+    }
+    g_assert_cmpuint(testtime[7], ==, buf[6]); /* YEAR */
+
+    g_assert_cmpuint(retry, >, 0);
+}
+

tests/ds-rtc-i2c-test.c line 175

Quoted 10 lines
tests/ds-rtc-i2c-test.c new file +10 -0
+int main(int argc, char *argv[])
+{
+    int ret;
+    const char *arch = qtest_get_arch();
+    QTestState *s = NULL;
+
+    g_test_init(&argc, &argv, NULL);
+
+    if (strcmp(arch, "arm") == 0) {
+        s = qtest_start("-display none -machine imx25-pdk");

Do you really need the "-display none" parameter here? ... I thought that was the default for qtests anyway?

Quoted 9 lines
tests/ds-rtc-i2c-test.c new file +8 -0
+        i2c = imx_i2c_create(s, IMX25_I2C_0_BASE);
+        addr = DS1338_ADDR;
+        use_century = false;
+
+    }
+
+    qtest_add_data_func("/ds-rtc-i2c/set24_12am", test_time_24_12am, test_rtc_set);
+    qtest_add_data_func("/ds-rtc-i2c/set24_6am", test_time_24_6am, test_rtc_set);

tests/ds-rtc-i2c-test.c line 193

Quoted 10 lines
tests/ds-rtc-i2c-test.c new file +10 -0
+    qtest_add_data_func("/ds-rtc-i2c/set24_12pm", test_time_24_12pm, test_rtc_set);
+    qtest_add_data_func("/ds-rtc-i2c/set24_6pm", test_time_24_6pm, test_rtc_set);
+    qtest_add_func("/ds-rtc-i2c/current", test_rtc_current);
+
+    ret = g_test_run();
+
+    qtest_end();
+
+    return ret;
+}

Thomas

#4 lore raw

Re: [Qemu-devel] [PATCH 2/5] tests: more thorough test of ds1338

Quoted 100 lines · Thomas Huth
Quoted 12 lines · Michael Davidsaver

Test current time and set+get round trip.

The set+get test is repeated 4 times. These cases are spread across a single day in an attempt to trigger some potential issues regardless of the timezone of the machine running the tests.

---
 tests/Makefile.include  |   2 +
 tests/ds-rtc-i2c-test.c | 193 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 195 insertions(+)
 create mode 100644 tests/ds-rtc-i2c-test.c

[...]

Quoted 70 lines

tests/q35-test$(EXESUF): tests/q35-test.o $(libqos-pc-obj-y)

tests/ds-rtc-i2c-test.c new file +63 -0
new file mode 100644
index 0000000000..464eb08558
@@ -0,0 +1,193 @@
+/* Testing of Dallas/Maxim I2C bus RTC devices
+ *
+ * Copyright (c) 2017 Michael Davidsaver
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the LICENSE file in the top-level directory.
+ */
+#include <stdio.h>
+
+#include "qemu/osdep.h"
+#include "qemu/bcd.h"
+#include "qemu/cutils.h"
+#include "qemu/timer.h"
+#include "libqtest.h"
+#include "libqos/libqos.h"
+#include "libqos/i2c.h"
+
+#define IMX25_I2C_0_BASE 0x43F80000
+#define DS1338_ADDR 0x68
+
+static I2CAdapter *i2c;
+static uint8_t addr;
+static bool use_century;
+
+static
+time_t rtc_gettime(void)
+{
+    struct tm parts;
+    uint8_t buf[7];
+
+    buf[0] = 0;
+    i2c_send(i2c, addr, buf, 1);
+    i2c_recv(i2c, addr, buf, 7);
+
+    parts.tm_sec = from_bcd(buf[0]);
+    parts.tm_min = from_bcd(buf[1]);
+    if (buf[2] & 0x40) {
+        /* 12 hour */
+        /* HOUR register is 1-12. */
+        parts.tm_hour = from_bcd(buf[2] & 0x1f);
+        g_assert_cmpuint(parts.tm_hour, >=, 1);
+        g_assert_cmpuint(parts.tm_hour, <=, 12);
+        parts.tm_hour %= 12u; /* wrap 12 -> 0 */
+        if (buf[2] & 0x20) {
+            parts.tm_hour += 12u;
+        }
+    } else {
+        /* 24 hour */
+        parts.tm_hour = from_bcd(buf[2] & 0x3f);
+    }
+    parts.tm_wday = from_bcd(buf[3]);
+    parts.tm_mday = from_bcd(buf[4]);
+    parts.tm_mon =  from_bcd((buf[5] & 0x1f) - 1u);
+    parts.tm_year = from_bcd(buf[6]);
+    if (!use_century || (buf[5] & 0x80)) {
+        parts.tm_year += 100u;
+    }
+
+    return mktimegm(&parts);
+}
+
+/* read back and compare with current system time */
+static

tests/ds-rtc-i2c-test.c line 73

Quoted 10 lines
tests/ds-rtc-i2c-test.c new file +10 -0
+void test_rtc_current(void)
+{
+    uint8_t buf;
+    time_t expected, actual;
+
+    /* magic address to zero RTC time offset
+     * as tests may be run in any order
+     */
+    buf = 0xff;
+    i2c_send(i2c, addr, &buf, 1);

That magic (together with patch 1/5) is IMHO a little bit ugly. I've hit the same problem with the m48t59 test recently, and I solved it by moving the qtest_start() and qtest_end() calls from the main() function into the single tests instead, so that each test starts with a clean state:

Quoted 4 lines

https://git.qemu.org/?p=qemu.git;a=commitdiff;h=9c29830c90d82f27f

Could you maybe try whether that approach works for your test cases here, too? Then you could do this without the "0xff" hack here...

Your right, this looks clearer. I'll try this approach.

Quoted 83 lines
Quoted 62 lines
tests/ds-rtc-i2c-test.c new file +62 -0
+
+    actual = time(NULL);
+    /* new second may start here */
+    expected = rtc_gettime();
+    g_assert_cmpuint(expected, <=, actual + 1);
+    g_assert_cmpuint(expected, >=, actual);
+}
+
+
+static uint8_t test_time_24_12am[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 00:30:53 +0000 */
+    0x53,
+    0x30,
+    0x00, /* 12 AM in 24 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
+static uint8_t test_time_24_6am[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 06:30:53 +0000 */
+    0x53,
+    0x30,
+    0x06, /* 6 AM in 24 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
+static uint8_t test_time_24_12pm[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 12:30:53 +0000 */
+    0x53,
+    0x30,
+    0x12, /* 12 PM in 24 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
+static uint8_t test_time_24_6pm[8] = {
+    0, /* address */
+    /* Wed, 22 Nov 2017 18:30:53 +0000 */
+    0x53,
+    0x30,
+    0x18, /* 6 PM in 24 hour mode */
+    0x03, /* monday is our day 1 */
+    0x22,
+    0x11 | 0x80,
+    0x17,
+};
+
+/* write in and read back known time */
+static
+void test_rtc_set(const void *raw)
+{
+    const uint8_t *testtime = raw;

tests/ds-rtc-i2c-test.c line 145

Quoted 10 lines
tests/ds-rtc-i2c-test.c new file +10 -0
+    uint8_t buf[7];
+    unsigned retry = 2;
+
+    for (; retry; retry--) {
+        i2c_send(i2c, addr, testtime, 8);
+        /* new second may start here */
+        i2c_send(i2c, addr, testtime, 1);
+        i2c_recv(i2c, addr, buf, 7);
+
+        if (testtime[1] == buf[0]) {

Please also check the minutes here (reason: see below).

tests/ds-rtc-i2c-test.c line 152

Quoted 7 lines
tests/ds-rtc-i2c-test.c new file +7 -0
+            break;
+        }
+        /* we raced start of second, retry */
+    };
+
+    g_assert_cmpuint(testtime[1], ==, buf[0]); /* SEC */
+    g_assert_cmpuint(testtime[2], ==, buf[1]); /* MIN */
Quoted 7 lines

Could you please wrap the SEC and MIN lines in a "if (!g_test_slow()) { ... }" statement? The problem is: The "make check" tests are run as CI on a system that is sometimes *very* overloaded. It might happen that the test is sometimes interrupted for dozens of seconds, so it might fail to properly read the time on a granularity of seconds. With !g_test_slow() you can make sure that the check is not done on such overloaded CI systems.

Ok I guess. I certainly don't want to add more false positive test failures.

Quoted 18 lines
Quoted 18 lines
tests/ds-rtc-i2c-test.c new file +18 -0
+    g_assert_cmpuint(testtime[3], ==, buf[2]); /* HOUR */
+    /* skip comparing Day of Week.  Not handled correctly */
+    g_assert_cmpuint(testtime[5], ==, buf[4]); /* DoM */
+    if (use_century) {
+        g_assert_cmpuint(testtime[6], ==, buf[5]); /* MON+century */
+    } else {
+        g_assert_cmpuint(testtime[6] & 0x7f, ==, buf[5]); /* MON */
+    }
+    g_assert_cmpuint(testtime[7], ==, buf[6]); /* YEAR */
+
+    g_assert_cmpuint(retry, >, 0);
+}
+
+int main(int argc, char *argv[])
+{
+    int ret;
+    const char *arch = qtest_get_arch();
+    QTestState *s = NULL;
Quoted 8 lines

tests/ds-rtc-i2c-test.c line 175

Quoted 5 lines
tests/ds-rtc-i2c-test.c new file +5 -0
+
+    g_test_init(&argc, &argv, NULL);
+
+    if (strcmp(arch, "arm") == 0) {
+        s = qtest_start("-display none -machine imx25-pdk");

Do you really need the "-display none" parameter here? ... I thought that was the default for qtests anyway?

This is a straight copy+paste from the ds1338-test I'm replacing. I'll remove it.

Quoted 22 lines
Quoted 9 lines
tests/ds-rtc-i2c-test.c new file +8 -0
+        i2c = imx_i2c_create(s, IMX25_I2C_0_BASE);
+        addr = DS1338_ADDR;
+        use_century = false;
+
+    }
+
+    qtest_add_data_func("/ds-rtc-i2c/set24_12am", test_time_24_12am, test_rtc_set);
+    qtest_add_data_func("/ds-rtc-i2c/set24_6am", test_time_24_6am, test_rtc_set);

tests/ds-rtc-i2c-test.c line 193

Quoted 10 lines
tests/ds-rtc-i2c-test.c new file +10 -0
+    qtest_add_data_func("/ds-rtc-i2c/set24_12pm", test_time_24_12pm, test_rtc_set);
+    qtest_add_data_func("/ds-rtc-i2c/set24_6pm", test_time_24_6pm, test_rtc_set);
+    qtest_add_func("/ds-rtc-i2c/current", test_rtc_current);
+
+    ret = g_test_run();
+
+    qtest_end();
+
+    return ret;
+}

Thomas

#8 lore raw

Re: [Qemu-devel] [PATCH 3/5] timer: generalize Dallas/Maxim RTC i2c devices

In reply to Michael Davidsaver
Quoted 18 lines · Michael Davidsaver

Support for: ds1307, ds1337, ds1338, ds1339, ds1340, ds1375, ds1388, and ds3231.

Tested with ds1338 and ds1375.

The existing ds1338 model has two bugs with almost no practical impact.

1. Trying to set time in 12-hour mode works, but the 12 hour mode bit isn't stored. So time always reads in 24 hour mode.

2. wday_offset is always stored for the local time zone. When the RTC is set and rtc_utc=1 and the local timezone has a different day than UTC, then wday_offset will be off by one.

Quoted 6 lines
---
 default-configs/arm-softmmu.mak |   2 +-
 hw/timer/Makefile.objs          |   2 +-
 hw/timer/ds-rtc-i2c.c           | 466 ++++++++++++++++++++++++++++++++++++++++
 hw/timer/ds1338.c               | 248 ---------------------

This patch is really hard for me to read because it's basically "throw away the old file and implement a new one". Can you split it up a bit please? I'd recommend starting with "just a plain file rename" and then piecemeal changes of behaviour. Then it's easy to see what has been changed.

hw/timer/ds-rtc-i2c.c line 10

Quoted 13 lines
hw/timer/ds-rtc-i2c.c new file +10 -0
@@ -0,0 +1,466 @@
+/* Emulation of various Dallas/Maxim RTCs accessed via I2C bus
+ *
+ * Copyright (c) 2017 Michael Davidsaver
+ * Copyright (c) 2009 CodeSourcery
+ *
+ * Authors: Michael Davidsaver
+ *          Paul Brook
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the LICENSE file in the top-level directory.

This has lost the "contributions after 2012 are GPL-2-or-later" part of the licensing information.

hw/timer/ds-rtc-i2c.c line 41

Quoted 5 lines
hw/timer/ds-rtc-i2c.c new file +5 -0
+#ifdef DEBUG_DSRTC
+#define DPRINTK(FMT, ...) info_report(TYPE_DSRTC " : " FMT, ## __VA_ARGS__)
+#else
+#define DPRINTK(FMT, ...) do {} while (0)
+#endif

If we're overhauling the device it might be nice to convert it to tracepoints, but I don't insist on that.

Quoted 11 lines
hw/timer/ds-rtc-i2c.c new file +11 -0
+#define TYPE_DSRTC "ds-rtc-i2c"
+#define DSRTC(obj) OBJECT_CHECK(DSRTCState, (obj), TYPE_DSRTC)
+#define DSRTC_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(DSRTCClass, obj, TYPE_DSRTC)
+#define DSRTC_CLASS(klass) \
+    OBJECT_CLASS_CHECK(DSRTCClass, klass, TYPE_DSRTC)
+
+static const VMStateDescription vmstate_dsrtc = {
+    .name = TYPE_DSRTC,
+    .version_id = 1,
+    .minimum_version_id = 1,

hw/timer/ds-rtc-i2c.c line 129

Quoted 10 lines
hw/timer/ds-rtc-i2c.c new file +10 -0
+    .fields = (VMStateField[]) {
+        VMSTATE_I2C_SLAVE(parent_obj, DSRTCState),
+        VMSTATE_INT64(time_offset, DSRTCState),
+        VMSTATE_INT8_V(wday_offset, DSRTCState, 2),
+        VMSTATE_UINT8_ARRAY(regs, DSRTCState, DSRTC_REGSIZE),
+        VMSTATE_UINT8(addr, DSRTCState),
+        VMSTATE_BOOL(addrd, DSRTCState),
+        VMSTATE_END_OF_LIST()
+    }
+};

Changing the VMState name and fields will break cross-version migration from old QEMU versions of all the boards which use a DS1338. I'd strongly prefer to avoid that. There are ways to do that, like subsections and pre/post-load functions, but a big part of it is not changing fields if you don't really need to.

hw/timer/ds-rtc-i2c.c line 134

Quoted 5 lines
hw/timer/ds-rtc-i2c.c new file +5 -0
+
+static void dsrtc_reset(DeviceState *device);
+
+/* update current time registers */
+static

Can you not put newlines after the 'static' keyword, please?

hw/timer/ds-rtc-i2c.c line 43

Quoted 2 lines
hw/timer/ds-rtc-i2c.c new file +2 -0
+void dsrtc_latch(DSRTCState *ds)
+{

thanks
-- PMM

#5 lore raw

Re: [Qemu-devel] [PATCH 2/5] tests: more thorough test of ds1338

In reply to Michael Davidsaver
Quoted 2 lines · Michael Davidsaver
Quoted 1 line · Thomas Huth

...

Quoted 11 lines
Quoted 9 lines

That magic (together with patch 1/5) is IMHO a little bit ugly. I've hit the same problem with the m48t59 test recently, and I solved it by moving the qtest_start() and qtest_end() calls from the main() function into the single tests instead, so that each test starts with a clean state:

https://git.qemu.org/?p=qemu.git;a=commitdiff;h=9c29830c90d82f27f

Could you maybe try whether that approach works for your test cases here, too? Then you could do this without the "0xff" hack here...

Your right, this looks clearer. I'll try this approach.

I ultimately decided not to go with this approach as test failures didn't call qtest_quit(), and the process under test is left running after gtester exits.

Instead I split the current time test off into a second executable. This avoids all of the magic.

FYI. I also looked at using g_test_add(), keeping the QTestState* in a Fixture struct, and using setup and teardown functions to call qtest_start()/qtest_quit(). This works, but seemed to me like too much effort in this case.

#6 lore raw

Re: [Qemu-devel] [PATCH 2/5] tests: more thorough test of ds1338

Quoted 13 lines · Michael Davidsaver
Quoted 2 lines · Michael Davidsaver
Quoted 1 line · Thomas Huth

...

Quoted 10 lines
Quoted 9 lines

That magic (together with patch 1/5) is IMHO a little bit ugly. I've hit the same problem with the m48t59 test recently, and I solved it by moving the qtest_start() and qtest_end() calls from the main() function into the single tests instead, so that each test starts with a clean state:

https://git.qemu.org/?p=qemu.git;a=commitdiff;h=9c29830c90d82f27f

Could you maybe try whether that approach works for your test cases here, too? Then you could do this without the "0xff" hack here...

Quoted 8 lines
Quoted 1 line

Your right, this looks clearer. I'll try this approach.

I ultimately decided not to go with this approach as test failures didn't call qtest_quit(), and the process under test is left running after gtester exits.

Instead I split the current time test off into a second executable. This avoids all of the magic.

Honestly, I don't like the idea that we put tests into different files just because of this. We're not doing this for any other tests so far, so just using this pattern for the ds1338 / ds1375 tests sounds wrong.

If there is an issue with test processes not being killed correctly, I think we should rather fix that issue instead, maybe by adding a special function with atexit() or something similar ... I haven't looked closely into that yet.

Just my 0.02 €.

Thomas