From nobody Sun Feb 8 23:32:39 2026 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1511733913665630.4934752877933; Sun, 26 Nov 2017 14:05:13 -0800 (PST) Received: from localhost ([::1]:58160 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eJ532-0002x4-Ua for importer@patchew.org; Sun, 26 Nov 2017 17:05:12 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56884) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eJ4xs-00075N-RL for qemu-devel@nongnu.org; Sun, 26 Nov 2017 16:59:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eJ4xo-0002tt-Cv for qemu-devel@nongnu.org; Sun, 26 Nov 2017 16:59:52 -0500 Received: from mail-io0-x243.google.com ([2607:f8b0:4001:c06::243]:32959) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eJ4xo-0002tg-8A; Sun, 26 Nov 2017 16:59:48 -0500 Received: by mail-io0-x243.google.com with SMTP id i184so26886058ioa.0; Sun, 26 Nov 2017 13:59:48 -0800 (PST) Received: from localhost.localdomain (173-29-146-33.client.mchsi.com. [173.29.146.33]) by smtp.gmail.com with ESMTPSA id n184sm6517218itg.9.2017.11.26.13.59.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 26 Nov 2017 13:59:46 -0800 (PST) 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 :in-reply-to:references; bh=En+JWL/qkAVFTvSzskEOJVA+r+axFS5Jwyg0ggs3F0M=; b=H4Qj+qfQsbILdHpbcAq6egQBrAsiNdoInxB9IwJ0YFFN8q30L5BEoE1VnaIV0cvvYv TPs4d1SU5mpNMWy7BqIqYtKNooud45HYxI+7v0YZhusQSTlSy4YbBdBlCcRBT2nd0wOf 0eIg/hd5Tbo86HxGskNzuRwvvUqdVy+f1s8ifn8wsR/AvOsrshAZ+T32/8m5oIfs0zzm HJxtmkNkxTnAFKPW0zW9vyDmxizT0eToNJ+6dETByfhEczFNjCDGijPeGDLt2g4mBbC9 pMt+xJcgjYj/HUGxdkxaKIIAEbhI13/blJkRooGnaBnneNfVDgpTl1M/m0duZKeqJj3S xlGg== 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:in-reply-to:references; bh=En+JWL/qkAVFTvSzskEOJVA+r+axFS5Jwyg0ggs3F0M=; b=j/6UXdfg7dKhglvGH0ICbmY9WtHCEA5jqJSygdpUq3iZ1ixIg61L73sC8T367aqCxT 3YyoFw8tzwS03XQ+P7USjDo6gm6Mc1Sp3YISELmkf3fHr71vxs5cAOVDiyIbEYog87HW TiN6mjE1MG2WjU1gI9XJAYa6VroL7vZknrJxrKy4MeBSfn6PTA5No6AASUVBXtV9X/vF WnKIVg9g+Xnvul1CXFw3K15waiIVp9FUuWhkj94pKNfJUnx9Lrq3YfyTNV2sgVIbgAFL lQ1MEZYJENAPx/doJK0YLgqfIsoemYsI3Ah4NzKRhxz9op+P9Ny9QTI90byd1ggS/kK/ gpSQ== X-Gm-Message-State: AJaThX50GTQghyKLVO0QGSeaaO0eMaq/6RT3y//tvSqxfg4Q4LxqKE+o CcKBWlypc7ykHRfVLgXyILTkPg== X-Google-Smtp-Source: AGs4zMaKF/291IxdeGxBjNlU5o0/AgkB4ieJO7oeLG0fn7uMa3LSqGoy3w9B1KOeLC8GgXImNq6Afw== X-Received: by 10.107.70.6 with SMTP id t6mr39537406ioa.72.1511733587415; Sun, 26 Nov 2017 13:59:47 -0800 (PST) From: Michael Davidsaver To: Alexander Graf , David Gibson Date: Sun, 26 Nov 2017 15:59:04 -0600 Message-Id: <027efd940a2268e69d9d6d0363858688b22baf11.1511731946.git.mdavidsaver@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4001:c06::243 Subject: [Qemu-devel] [PATCH 06/17] tests: rewrite testing for DS RTC devices 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: Michael Davidsaver , qemu-ppc@nongnu.org, qemu-devel@nongnu.org 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" Replace existing ds1338-test with more thorough test of time read and set. Signed-off-by: Michael Davidsaver --- tests/Makefile.include | 4 +- tests/ds-rtc-i2c-test.c | 162 ++++++++++++++++++++++++++++++++++++++++++++= ++++ tests/ds1338-test.c | 77 ----------------------- 3 files changed, 164 insertions(+), 79 deletions(-) create mode 100644 tests/ds-rtc-i2c-test.c delete mode 100644 tests/ds1338-test.c diff --git a/tests/Makefile.include b/tests/Makefile.include index ad1c219423..56045cdf09 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -348,7 +348,7 @@ check-qtest-sparc64-y =3D tests/endianness-test$(EXESUF) check-qtest-sparc64-y +=3D tests/prom-env-test$(EXESUF) =20 check-qtest-arm-y =3D tests/tmp105-test$(EXESUF) -check-qtest-arm-y +=3D tests/ds1338-test$(EXESUF) +check-qtest-arm-y +=3D tests/ds-rtc-i2c-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) @@ -745,7 +745,7 @@ 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/q35-test$(EXESUF): tests/q35-test.o $(libqos-pc-obj-y) diff --git a/tests/ds-rtc-i2c-test.c b/tests/ds-rtc-i2c-test.c new file mode 100644 index 0000000000..0586dbd467 --- /dev/null +++ b/tests/ds-rtc-i2c-test.c @@ -0,0 +1,162 @@ +/* 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 + +#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] =3D 0; + i2c_send(i2c, addr, buf, 1); + i2c_recv(i2c, addr, buf, 7); + + parts.tm_sec =3D from_bcd(buf[0]); + parts.tm_min =3D from_bcd(buf[1]); + if (buf[2] & 0x40) { + /* 12 hour */ + parts.tm_hour =3D from_bcd(buf[2] & 0x1f) % 12u; + 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); +} + +/* read back and compare with current system time */ +static +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 =3D 0xff; + i2c_send(i2c, addr, &buf, 1); + + actual =3D time(NULL); + /* new second may start here */ + expected =3D rtc_gettime(); + g_assert_cmpuint(expected, <=3D, actual + 1); + g_assert_cmpuint(expected, >=3D, actual); +} + + +static uint8_t test_time_24[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, +}; + +static uint8_t test_time_12[8] =3D { + 0, /* address */ + /* Wed, 22 Nov 2017 18:30:53 +0000 */ + 0x53, + 0x30, + 0x67, /* 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) +{ + const uint8_t *testtime =3D raw; + uint8_t buf[7]; + unsigned retry =3D 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] =3D=3D buf[0]) { + break; + } + /* we raced start of second, retry */ + }; + + g_assert_cmpuint(testtime[1], =3D=3D, buf[0]); + g_assert_cmpuint(testtime[2], =3D=3D, buf[1]); + g_assert_cmpuint(testtime[3], =3D=3D, buf[2]); + g_assert_cmpuint(testtime[4], =3D=3D, buf[3]); + g_assert_cmpuint(testtime[5], =3D=3D, buf[4]); + if (use_century) { + g_assert_cmpuint(testtime[6], =3D=3D, buf[5]); + } else { + g_assert_cmpuint(testtime[6] & 0x7f, =3D=3D, buf[5]); + } + g_assert_cmpuint(testtime[7], =3D=3D, buf[6]); + + g_assert_cmpuint(retry, >, 0); +} + +int main(int argc, char *argv[]) +{ + int ret; + const char *arch =3D qtest_get_arch(); + + g_test_init(&argc, &argv, NULL); + + if (strcmp(arch, "arm") =3D=3D 0) { + qtest_start("-display none -machine imx25-pdk"); + i2c =3D imx_i2c_create(IMX25_I2C_0_BASE); + addr =3D DS1338_ADDR; + use_century =3D false; + + } + + qtest_add_data_func("/ds-rtc-i2c/set24", test_time_24, test_rtc_set); + qtest_add_data_func("/ds-rtc-i2c/set12", test_time_12, test_rtc_set); + qtest_add_func("/ds-rtc-i2c/current", test_rtc_current); + + ret =3D g_test_run(); + + qtest_end(); + + return ret; +} diff --git a/tests/ds1338-test.c b/tests/ds1338-test.c deleted file mode 100644 index 26968bc82a..0000000000 --- a/tests/ds1338-test.c +++ /dev/null @@ -1,77 +0,0 @@ -/* - * QTest testcase for the DS1338 RTC - * - * Copyright (c) 2013 Jean-Christophe Dubois - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WI= THOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, see . - */ - -#include "qemu/osdep.h" -#include "libqtest.h" -#include "libqos/i2c.h" - -#define IMX25_I2C_0_BASE 0x43F80000 - -#define DS1338_ADDR 0x68 - -static I2CAdapter *i2c; -static uint8_t addr; - -static inline uint8_t bcd2bin(uint8_t x) -{ - return ((x) & 0x0f) + ((x) >> 4) * 10; -} - -static void send_and_receive(void) -{ - uint8_t cmd[1]; - uint8_t resp[7]; - time_t now =3D time(NULL); - struct tm *tm_ptr =3D gmtime(&now); - - /* reset the index in the RTC memory */ - cmd[0] =3D 0; - i2c_send(i2c, addr, cmd, 1); - - /* retrieve the date */ - i2c_recv(i2c, addr, resp, 7); - - /* check retrieved time againt local time */ - g_assert_cmpuint(bcd2bin(resp[4]), =3D=3D , tm_ptr->tm_mday); - g_assert_cmpuint(bcd2bin(resp[5]), =3D=3D , 1 + tm_ptr->tm_mon); - g_assert_cmpuint(2000 + bcd2bin(resp[6]), =3D=3D , 1900 + tm_ptr->tm_y= ear); -} - -int main(int argc, char **argv) -{ - QTestState *s =3D NULL; - int ret; - - g_test_init(&argc, &argv, NULL); - - s =3D qtest_start("-display none -machine imx25-pdk"); - i2c =3D imx_i2c_create(IMX25_I2C_0_BASE); - addr =3D DS1338_ADDR; - - qtest_add_func("/ds1338/tx-rx", send_and_receive); - - ret =3D g_test_run(); - - if (s) { - qtest_quit(s); - } - g_free(i2c); - - return ret; -} --=20 2.11.0