From patchwork Mon Feb 19 04:03:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Davidsaver X-Patchwork-Id: 874959 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="XnWP+rY0"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zl9BH5Xsjz9s0W for ; Mon, 19 Feb 2018 15:04:55 +1100 (AEDT) Received: from localhost ([::1]:47438 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1enchB-0007b9-Qz for incoming@patchwork.ozlabs.org; Sun, 18 Feb 2018 23:04:53 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49501) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1encgV-0007Z3-1q for qemu-devel@nongnu.org; Sun, 18 Feb 2018 23:04:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1encgT-0000TK-Mw for qemu-devel@nongnu.org; Sun, 18 Feb 2018 23:04:11 -0500 Received: from mail-it0-x241.google.com ([2607:f8b0:4001:c0b::241]:52331) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1encgT-0000TB-HN for qemu-devel@nongnu.org; Sun, 18 Feb 2018 23:04:09 -0500 Received: by mail-it0-x241.google.com with SMTP id o13so7693085ito.2 for ; Sun, 18 Feb 2018 20:04:09 -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; bh=RzmBa3Q9ZKmQ9XMkdNGHR6/Qm3Np+kkYXXqShEMXTFc=; b=XnWP+rY0cCgrc+QfdX+dR7nQijND7mSayUZUej+YNXhwS+KyDzybrMyg/gB1XeAUVL S2sCRRU8KUJFgPp/PXUCALB5FNFVHL41PXhwVfYI+X/0HVf5D6rYB+iSVLMp7HaXYSVZ LU8xuIizNwPr/AmLMbE45n3incf2RB9sS1rmcLBre/hQmDGLRZWEhD9OdAfsywAy6QpW 4jkupP1av75oq8OHzP6BNEUM+T2EbopkdGMMXJ62mOv9vmaCUhO6a1j43cj2Xg+Tdcg+ wUfyse1V7HYSOmi9HplMnCWCeKOQaKs6fM5q2uKdvY5973gkQhVZDKWayNFhUyGvoXTT XLwg== 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=RzmBa3Q9ZKmQ9XMkdNGHR6/Qm3Np+kkYXXqShEMXTFc=; b=FB6Td5AdG8afZljuG5Md4OzHB88UETY81V72bEmjXd2pBY9898TEjjeI4TqjlMTfHx Uq3ipanIrDVtT7arac1SWZkyyoMMgywE8wDfHk3O/EYQ9/vOjVnLUFeoLDlNNkUPKT+2 TtVja0EgqidvGxQv0ImPi0JIJR0yKbUA1tm6jJQjwb15qnFBqqE0NNNl5KfGiwok3On0 WP8EQ/dWyEDfkU6eiarXNwI+v6uZFVc+h3/wAPsaw1YSuGkkdkLORglpfi6D4Mh3KUbO JOKuE0X8MUnq9HTE+dXdLsztNGq68SIr2s4QtheNihLqNSdx1i0cozAtt7TrSLObGFVW 1Bsw== X-Gm-Message-State: APf1xPAG/JQWzGUs7zMXOvvQ5B3u9kYWzBSHMHoMR1et4zPWUHHGgNiS kIRsqKTmMUSoBUx6zl3Jbf4= X-Google-Smtp-Source: AH8x225z6nSjkjqwImeDDtCOaU5u9WELbea+zAR6cLFSkvWzzA5BCZOku3myurVYSDINgbqp9xEF/g== X-Received: by 10.36.67.145 with SMTP id s139mr758342itb.5.1519013048749; Sun, 18 Feb 2018 20:04:08 -0800 (PST) Received: from localhost.localdomain ([184.250.204.84]) by smtp.gmail.com with ESMTPSA id a13sm19158075itj.13.2018.02.18.20.04.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 18 Feb 2018 20:04:07 -0800 (PST) From: Michael Davidsaver To: Paul Brook , Peter Maydell , Antoine Mathys Date: Sun, 18 Feb 2018 20:03:39 -0800 Message-Id: <20180219040342.12686-3-mdavidsaver@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180219040342.12686-1-mdavidsaver@gmail.com> References: <20180219040342.12686-1-mdavidsaver@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4001:c0b::241 Subject: [Qemu-devel] [PATCH 2/5] tests: more thorough test 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: Michael Davidsaver , qemu-devel@nongnu.org, David Gibson Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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. Signed-off-by: Michael Davidsaver --- 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 diff --git a/tests/Makefile.include b/tests/Makefile.include index a1bcbffe12..f5dcd274e0 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -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) diff --git a/tests/ds-rtc-i2c-test.c b/tests/ds-rtc-i2c-test.c new file mode 100644 index 0000000000..464eb08558 --- /dev/null +++ b/tests/ds-rtc-i2c-test.c @@ -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 + +#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 +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); + + 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; + 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]) { + break; + } + /* we raced start of second, retry */ + }; + + 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[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; + + g_test_init(&argc, &argv, NULL); + + if (strcmp(arch, "arm") == 0) { + s = qtest_start("-display none -machine imx25-pdk"); + 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); + 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; +}