From nobody Tue Nov 4 21:57:06 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1530815286117821.9794236891488; Thu, 5 Jul 2018 11:28:06 -0700 (PDT) Received: from localhost ([::1]:54311 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fb8z7-0001rW-91 for importer@patchew.org; Thu, 05 Jul 2018 14:28:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57934) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fb8rc-0004Jv-Si for qemu-devel@nongnu.org; Thu, 05 Jul 2018 14:20:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fb8rZ-00068y-FR for qemu-devel@nongnu.org; Thu, 05 Jul 2018 14:20:20 -0400 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:45231) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fb8rZ-00068W-6L for qemu-devel@nongnu.org; Thu, 05 Jul 2018 14:20:17 -0400 Received: by mail-pf0-x244.google.com with SMTP id y24-v6so6116264pfe.12 for ; Thu, 05 Jul 2018 11:20:17 -0700 (PDT) Received: from localhost.localdomain (c-73-158-53-43.hsd1.ca.comcast.net. [73.158.53.43]) by smtp.gmail.com with ESMTPSA id g75-v6sm14170847pfg.145.2018.07.05.11.20.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 05 Jul 2018 11:20:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=EVPkASpdRMTfisHb7M9T5q00AvrdrRgGL282yc5R9E0=; b=nyohCgKuxWMnh1d30WhKsIPNsfzTH+qMRBzfYEKyFMSxBulhp4XQyJc3dbadJ+yH1W qTkw2G8w5m7BLZhbyOxC1nK8483Gs8iAxSnpGkOdvwgGcQmf5xEtmkjLlJ5LqGYy/Fvr mKODZKEdralu/lfAOA0Yrjxs9svOW1dHJHWYk19vAjSIjgICPbQ7b4PKZRHySbIdP0gK 9OFpymWNkAR9se4uEqaRvgjBTxR5je2kALJtLYwiFwsEgYMNLm0hGxpsytKk0baxscWA GJAcWmLVE9KGwwNCdKDIUK54t50VEZNYMekARc9oMBpT2gGTKYzCS7drd37f7dRlvFby ZIdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=EVPkASpdRMTfisHb7M9T5q00AvrdrRgGL282yc5R9E0=; b=GJvJ11K7dkIcGL9jWTkpfpArIRQenKndq6riyLrGwjL2Zf66txUf74U1936IudlSU5 /1i5tV5BDQ5phZJTWpyzbrVY64o7geRlPxhSGfbdyg/Cas578B7OtojUBMRuthNc+lTs qZACs1Xnmub+Dq9pywP1cdco5692Y3ZzufPaY0A7Gf4YhmpE8a5qQYNTCmrSxD2FJPzj CTTOVq/pgtyoiRbECLA9V6pAymOD6XeytaY11fzaE0f4hyK+pK2f4aQAlYOi/uZwrD3E 9+Ng6thEV0IM6dZPglfPAIlazLBX/Fdt9RWprd1/ZxurJl2omrRCqIRtWjKAT2D8SYLP Vuhw== X-Gm-Message-State: APt69E2R8P1CruWbrJWVPocOnV0M/K3c/yK14aORJLzyZ5yeEJQImvGR hzPxvY5i6BQ7oJzI/FICM7Q= X-Google-Smtp-Source: AAOMgpdCxO/NxNkpo2iNsMxl/3efG5hejIzAaiLr2xr8Ehn6ImiWjpjjZH9osopjCYTnsivtOsNx0w== X-Received: by 2002:a63:5c7:: with SMTP id 190-v6mr6449606pgf.385.1530814816130; Thu, 05 Jul 2018 11:20:16 -0700 (PDT) From: Michael Davidsaver To: Peter Maydell , Paolo Bonzini Date: Thu, 5 Jul 2018 11:19:48 -0700 Message-Id: <20180705182001.16537-2-mdavidsaver@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180705182001.16537-1-mdavidsaver@gmail.com> References: <20180705182001.16537-1-mdavidsaver@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::244 Subject: [Qemu-devel] [PATCH 01/14] tests: more thorough tests of ds1338 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Antoine Mathys , Thomas Huth , Michael Davidsaver , qemu-devel@nongnu.org, David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Test current time and set+get round trip. Separate current time test from set/get tests to avoid test needing to impose order, or to have a magic handshaketo reset RTC to current time. Signed-off-by: Michael Davidsaver --- tests/Makefile.include | 4 ++ tests/ds-rtc-common.h | 67 +++++++++++++++++++++++++ tests/ds-rtc-current-test.c | 59 ++++++++++++++++++++++ tests/ds-rtc-set-test.c | 117 ++++++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 247 insertions(+) create mode 100644 tests/ds-rtc-common.h create mode 100644 tests/ds-rtc-current-test.c create mode 100644 tests/ds-rtc-set-test.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 8859e88ffb..6ce5a9ff4d 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -378,6 +378,8 @@ check-qtest-sparc64-y +=3D tests/boot-serial-test$(EXES= UF) check-qtest-arm-y =3D tests/tmp105-test$(EXESUF) check-qtest-arm-y +=3D tests/pca9552-test$(EXESUF) check-qtest-arm-y +=3D tests/ds1338-test$(EXESUF) +check-qtest-arm-y +=3D tests/ds-rtc-current-test$(EXESUF) +check-qtest-arm-y +=3D tests/ds-rtc-set-test$(EXESUF) check-qtest-arm-y +=3D tests/m25p80-test$(EXESUF) gcov-files-arm-y +=3D hw/misc/tmp105.c check-qtest-arm-y +=3D tests/virtio-blk-test$(EXESUF) @@ -787,6 +789,8 @@ tests/pxe-test$(EXESUF): tests/pxe-test.o tests/boot-se= ctor.o $(libqos-obj-y) tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y) tests/pca9552-test$(EXESUF): tests/pca9552-test.o $(libqos-omap-obj-y) tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y) +tests/ds-rtc-current-test$(EXESUF): tests/ds-rtc-current-test.o $(libqos-i= mx-obj-y) +tests/ds-rtc-set-test$(EXESUF): tests/ds-rtc-set-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) diff --git a/tests/ds-rtc-common.h b/tests/ds-rtc-common.h new file mode 100644 index 0000000000..c8e6c2bc5b --- /dev/null +++ b/tests/ds-rtc-common.h @@ -0,0 +1,67 @@ +/* Common code for testing of Dallas/Maxim I2C bus RTC devices + * + * Copyright (c) 2018 Michael Davidsaver + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the LICENSE file in the top-level directory. + */ +#ifndef DSRTCCOMMON_H +#define DSRTCCOMMON_H + +#include "qemu/osdep.h" +#include "qemu/cutils.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; + +/* input buffer must have at least 7 elements */ +static inline time_t rtc_parse(const uint8_t *buf) +{ + struct tm parts; + + parts.tm_sec =3D from_bcd(buf[0]); + parts.tm_min =3D from_bcd(buf[1]); + if (buf[2] & 0x40) { + /* 12 hour */ + /* HOUR register is 1-12. */ + parts.tm_hour =3D from_bcd(buf[2] & 0x1f); + g_assert_cmpuint(parts.tm_hour, >=3D, 1); + g_assert_cmpuint(parts.tm_hour, <=3D, 12); + parts.tm_hour %=3D 12u; /* wrap 12 -> 0 */ + if (buf[2] & 0x20) { + parts.tm_hour +=3D 12u; + } + } else { + /* 24 hour */ + parts.tm_hour =3D from_bcd(buf[2] & 0x3f); + } + parts.tm_wday =3D from_bcd(buf[3]); + parts.tm_mday =3D from_bcd(buf[4]); + parts.tm_mon =3D from_bcd((buf[5] & 0x1f) - 1u); + parts.tm_year =3D from_bcd(buf[6]); + if (!use_century || (buf[5] & 0x80)) { + parts.tm_year +=3D 100u; + } + + return mktimegm(&parts); +} + +static time_t rtc_gettime(void) +{ + uint8_t buf[7]; + + /* zero address pointer */ + buf[0] =3D 0; + i2c_send(i2c, addr, buf, 1); + /* read back current time registers */ + i2c_recv(i2c, addr, buf, 7); + + return rtc_parse(buf); +} + +#endif /* DSRTCCOMMON_H */ diff --git a/tests/ds-rtc-current-test.c b/tests/ds-rtc-current-test.c new file mode 100644 index 0000000000..6acbbed9a6 --- /dev/null +++ b/tests/ds-rtc-current-test.c @@ -0,0 +1,59 @@ +/* Testing of Dallas/Maxim I2C bus RTC devices + * + * Copyright (c) 2018 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 + +#include "qemu/osdep.h" +#include "qemu/bcd.h" +#include "qemu/cutils.h" +#include "libqtest.h" +#include "libqos/libqos.h" +#include "libqos/i2c.h" + +#include "ds-rtc-common.h" + +/* read back and compare with current system time */ +static +void test_rtc_current(void) +{ + time_t expected, actual; + /* relax test to limit false positives when host may be overloaded. + * Allow larger delta when running "-m quick" + */ + time_t max_delta =3D g_test_slow() ? 1 : 30; + + actual =3D time(NULL); + /* new second may start here */ + expected =3D rtc_gettime(); + g_assert_cmpuint(expected, <=3D, actual + max_delta); + g_assert_cmpuint(expected, >=3D, actual); +} + +int main(int argc, char *argv[]) +{ + int ret; + const char *arch =3D qtest_get_arch(); + QTestState *s =3D NULL; + + g_test_init(&argc, &argv, NULL); + + if (strcmp(arch, "arm") =3D=3D 0) { + s =3D qtest_start("-machine imx25-pdk"); + i2c =3D imx_i2c_create(s, IMX25_I2C_0_BASE); + addr =3D DS1338_ADDR; + use_century =3D false; + + } + + qtest_add_func("/ds-rtc-i2c/current", test_rtc_current); + + ret =3D g_test_run(); + + qtest_end(); + + return ret; +} diff --git a/tests/ds-rtc-set-test.c b/tests/ds-rtc-set-test.c new file mode 100644 index 0000000000..35e1a36281 --- /dev/null +++ b/tests/ds-rtc-set-test.c @@ -0,0 +1,117 @@ +/* Testing of Dallas/Maxim I2C bus RTC devices + * + * Copyright (c) 2018 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 + +#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" + +#include "ds-rtc-common.h" + +static uint8_t test_time_24_12am[8] =3D { + 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] =3D { + 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] =3D { + 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] =3D { + 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) +{ + /* relax test to limit false positives when host may be overloaded. + * Allow larger delta when running "-m quick" + */ + time_t max_delta =3D g_test_slow() ? 1 : 30; + + const uint8_t *testtime =3D raw; + time_t expected, actual; + + /* skip address pointer and parse remainder */ + expected =3D rtc_parse(&testtime[1]); + + i2c_send(i2c, addr, testtime, 8); + /* host may start new second here */ + actual =3D rtc_gettime(); + + g_assert_cmpuint(expected, <=3D, actual); + g_assert_cmpuint(expected + max_delta, >=3D, actual); +} + +int main(int argc, char *argv[]) +{ + int ret; + const char *arch =3D qtest_get_arch(); + QTestState *s =3D NULL; + + g_test_init(&argc, &argv, NULL); + + if (strcmp(arch, "arm") =3D=3D 0) { + s =3D qtest_start("-display none -machine imx25-pdk"); + i2c =3D imx_i2c_create(s, IMX25_I2C_0_BASE); + addr =3D DS1338_ADDR; + use_century =3D 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_rt= c_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_rt= c_set); + + ret =3D g_test_run(); + + qtest_end(); + + return ret; +} --=20 2.11.0