xref: /openbmc/qemu/tests/qtest/m48t59-test.c (revision 168d4674)
11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth  * QTest testcase for the M48T59 and M48T08 real-time clocks
31e8a1faeSThomas Huth  *
41e8a1faeSThomas Huth  * Based on MC146818 RTC test:
51e8a1faeSThomas Huth  * Copyright IBM, Corp. 2012
61e8a1faeSThomas Huth  *
71e8a1faeSThomas Huth  * Authors:
81e8a1faeSThomas Huth  *  Anthony Liguori   <aliguori@us.ibm.com>
91e8a1faeSThomas Huth  *
101e8a1faeSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
111e8a1faeSThomas Huth  * See the COPYING file in the top-level directory.
121e8a1faeSThomas Huth  *
131e8a1faeSThomas Huth  */
141e8a1faeSThomas Huth 
151e8a1faeSThomas Huth #include "qemu/osdep.h"
161e8a1faeSThomas Huth 
17907b5105SMarc-André Lureau #include "libqtest.h"
181e8a1faeSThomas Huth 
191e8a1faeSThomas Huth #define RTC_SECONDS             0x9
201e8a1faeSThomas Huth #define RTC_MINUTES             0xa
211e8a1faeSThomas Huth #define RTC_HOURS               0xb
221e8a1faeSThomas Huth 
231e8a1faeSThomas Huth #define RTC_DAY_OF_WEEK         0xc
241e8a1faeSThomas Huth #define RTC_DAY_OF_MONTH        0xd
251e8a1faeSThomas Huth #define RTC_MONTH               0xe
261e8a1faeSThomas Huth #define RTC_YEAR                0xf
271e8a1faeSThomas Huth 
281e8a1faeSThomas Huth static uint32_t base;
291e8a1faeSThomas Huth static uint16_t reg_base = 0x1ff0; /* 0x7f0 for m48t02 */
301e8a1faeSThomas Huth static int base_year;
311e8a1faeSThomas Huth static const char *base_machine;
321e8a1faeSThomas Huth static bool use_mmio;
331e8a1faeSThomas Huth 
cmos_read_mmio(QTestState * s,uint8_t reg)341e8a1faeSThomas Huth static uint8_t cmos_read_mmio(QTestState *s, uint8_t reg)
351e8a1faeSThomas Huth {
361e8a1faeSThomas Huth     return qtest_readb(s, base + (uint32_t)reg_base + (uint32_t)reg);
371e8a1faeSThomas Huth }
381e8a1faeSThomas Huth 
cmos_write_mmio(QTestState * s,uint8_t reg,uint8_t val)391e8a1faeSThomas Huth static void cmos_write_mmio(QTestState *s, uint8_t reg, uint8_t val)
401e8a1faeSThomas Huth {
411e8a1faeSThomas Huth     uint8_t data = val;
421e8a1faeSThomas Huth 
431e8a1faeSThomas Huth     qtest_writeb(s, base + (uint32_t)reg_base + (uint32_t)reg, data);
441e8a1faeSThomas Huth }
451e8a1faeSThomas Huth 
cmos_read_ioio(QTestState * s,uint8_t reg)461e8a1faeSThomas Huth static uint8_t cmos_read_ioio(QTestState *s, uint8_t reg)
471e8a1faeSThomas Huth {
481e8a1faeSThomas Huth     qtest_outw(s, base + 0, reg_base + (uint16_t)reg);
491e8a1faeSThomas Huth     return qtest_inb(s, base + 3);
501e8a1faeSThomas Huth }
511e8a1faeSThomas Huth 
cmos_write_ioio(QTestState * s,uint8_t reg,uint8_t val)521e8a1faeSThomas Huth static void cmos_write_ioio(QTestState *s, uint8_t reg, uint8_t val)
531e8a1faeSThomas Huth {
541e8a1faeSThomas Huth     qtest_outw(s, base + 0, reg_base + (uint16_t)reg);
551e8a1faeSThomas Huth     qtest_outb(s, base + 3, val);
561e8a1faeSThomas Huth }
571e8a1faeSThomas Huth 
cmos_read(QTestState * s,uint8_t reg)581e8a1faeSThomas Huth static uint8_t cmos_read(QTestState *s, uint8_t reg)
591e8a1faeSThomas Huth {
601e8a1faeSThomas Huth     if (use_mmio) {
611e8a1faeSThomas Huth         return cmos_read_mmio(s, reg);
621e8a1faeSThomas Huth     } else {
631e8a1faeSThomas Huth         return cmos_read_ioio(s, reg);
641e8a1faeSThomas Huth     }
651e8a1faeSThomas Huth }
661e8a1faeSThomas Huth 
cmos_write(QTestState * s,uint8_t reg,uint8_t val)671e8a1faeSThomas Huth static void cmos_write(QTestState *s, uint8_t reg, uint8_t val)
681e8a1faeSThomas Huth {
691e8a1faeSThomas Huth     if (use_mmio) {
701e8a1faeSThomas Huth         cmos_write_mmio(s, reg, val);
711e8a1faeSThomas Huth     } else {
721e8a1faeSThomas Huth         cmos_write_ioio(s, reg, val);
731e8a1faeSThomas Huth     }
741e8a1faeSThomas Huth }
751e8a1faeSThomas Huth 
bcd2dec(int value)761e8a1faeSThomas Huth static int bcd2dec(int value)
771e8a1faeSThomas Huth {
781e8a1faeSThomas Huth     return (((value >> 4) & 0x0F) * 10) + (value & 0x0F);
791e8a1faeSThomas Huth }
801e8a1faeSThomas Huth 
tm_cmp(struct tm * lhs,struct tm * rhs)811e8a1faeSThomas Huth static int tm_cmp(struct tm *lhs, struct tm *rhs)
821e8a1faeSThomas Huth {
831e8a1faeSThomas Huth     time_t a, b;
841e8a1faeSThomas Huth     struct tm d1, d2;
851e8a1faeSThomas Huth 
861e8a1faeSThomas Huth     memcpy(&d1, lhs, sizeof(d1));
871e8a1faeSThomas Huth     memcpy(&d2, rhs, sizeof(d2));
881e8a1faeSThomas Huth 
891e8a1faeSThomas Huth     a = mktime(&d1);
901e8a1faeSThomas Huth     b = mktime(&d2);
911e8a1faeSThomas Huth 
921e8a1faeSThomas Huth     if (a < b) {
931e8a1faeSThomas Huth         return -1;
941e8a1faeSThomas Huth     } else if (a > b) {
951e8a1faeSThomas Huth         return 1;
961e8a1faeSThomas Huth     }
971e8a1faeSThomas Huth 
981e8a1faeSThomas Huth     return 0;
991e8a1faeSThomas Huth }
1001e8a1faeSThomas Huth 
1011e8a1faeSThomas Huth #if 0
1021e8a1faeSThomas Huth static void print_tm(struct tm *tm)
1031e8a1faeSThomas Huth {
1041e8a1faeSThomas Huth     printf("%04d-%02d-%02d %02d:%02d:%02d %+02ld\n",
1051e8a1faeSThomas Huth            tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1061e8a1faeSThomas Huth            tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
1071e8a1faeSThomas Huth }
1081e8a1faeSThomas Huth #endif
1091e8a1faeSThomas Huth 
cmos_get_date_time(QTestState * s,struct tm * date)1101e8a1faeSThomas Huth static void cmos_get_date_time(QTestState *s, struct tm *date)
1111e8a1faeSThomas Huth {
1121e8a1faeSThomas Huth     int sec, min, hour, mday, mon, year;
1131e8a1faeSThomas Huth     time_t ts;
1141e8a1faeSThomas Huth     struct tm dummy;
1151e8a1faeSThomas Huth 
1161e8a1faeSThomas Huth     sec = cmos_read(s, RTC_SECONDS);
1171e8a1faeSThomas Huth     min = cmos_read(s, RTC_MINUTES);
1181e8a1faeSThomas Huth     hour = cmos_read(s, RTC_HOURS);
1191e8a1faeSThomas Huth     mday = cmos_read(s, RTC_DAY_OF_MONTH);
1201e8a1faeSThomas Huth     mon = cmos_read(s, RTC_MONTH);
1211e8a1faeSThomas Huth     year = cmos_read(s, RTC_YEAR);
1221e8a1faeSThomas Huth 
1231e8a1faeSThomas Huth     sec = bcd2dec(sec);
1241e8a1faeSThomas Huth     min = bcd2dec(min);
1251e8a1faeSThomas Huth     hour = bcd2dec(hour);
1261e8a1faeSThomas Huth     mday = bcd2dec(mday);
1271e8a1faeSThomas Huth     mon = bcd2dec(mon);
1281e8a1faeSThomas Huth     year = bcd2dec(year);
1291e8a1faeSThomas Huth 
1301e8a1faeSThomas Huth     ts = time(NULL);
1311e8a1faeSThomas Huth     localtime_r(&ts, &dummy);
1321e8a1faeSThomas Huth 
1331e8a1faeSThomas Huth     date->tm_isdst = dummy.tm_isdst;
1341e8a1faeSThomas Huth     date->tm_sec = sec;
1351e8a1faeSThomas Huth     date->tm_min = min;
1361e8a1faeSThomas Huth     date->tm_hour = hour;
1371e8a1faeSThomas Huth     date->tm_mday = mday;
1381e8a1faeSThomas Huth     date->tm_mon = mon - 1;
1391e8a1faeSThomas Huth     date->tm_year = base_year + year - 1900;
140f10225d7SBin Meng #if !defined(__sun__) && !defined(_WIN32)
1411e8a1faeSThomas Huth     date->tm_gmtoff = 0;
1421e8a1faeSThomas Huth #endif
1431e8a1faeSThomas Huth 
1441e8a1faeSThomas Huth     ts = mktime(date);
1451e8a1faeSThomas Huth }
1461e8a1faeSThomas Huth 
m48t59_qtest_start(void)1471e8a1faeSThomas Huth static QTestState *m48t59_qtest_start(void)
1481e8a1faeSThomas Huth {
1491e8a1faeSThomas Huth     return qtest_initf("-M %s -rtc clock=vm", base_machine);
1501e8a1faeSThomas Huth }
1511e8a1faeSThomas Huth 
bcd_check_time(void)1521e8a1faeSThomas Huth static void bcd_check_time(void)
1531e8a1faeSThomas Huth {
1541e8a1faeSThomas Huth     struct tm start, date[4], end;
1551e8a1faeSThomas Huth     struct tm *datep;
1561e8a1faeSThomas Huth     time_t ts;
1571e8a1faeSThomas Huth     const int wiggle = 2;
158926bef1dSThomas Huth     QTestState *qts = m48t59_qtest_start();
1591e8a1faeSThomas Huth 
1601e8a1faeSThomas Huth     /*
1611e8a1faeSThomas Huth      * This check assumes a few things.  First, we cannot guarantee that we get
1621e8a1faeSThomas Huth      * a consistent reading from the wall clock because we may hit an edge of
1631e8a1faeSThomas Huth      * the clock while reading.  To work around this, we read four clock readings
1641e8a1faeSThomas Huth      * such that at least two of them should match.  We need to assume that one
1651e8a1faeSThomas Huth      * reading is corrupt so we need four readings to ensure that we have at
1661e8a1faeSThomas Huth      * least two consecutive identical readings
1671e8a1faeSThomas Huth      *
1681e8a1faeSThomas Huth      * It's also possible that we'll cross an edge reading the host clock so
1691e8a1faeSThomas Huth      * simply check to make sure that the clock reading is within the period of
1701e8a1faeSThomas Huth      * when we expect it to be.
1711e8a1faeSThomas Huth      */
1721e8a1faeSThomas Huth 
1731e8a1faeSThomas Huth     ts = time(NULL);
1741e8a1faeSThomas Huth     gmtime_r(&ts, &start);
1751e8a1faeSThomas Huth 
176926bef1dSThomas Huth     cmos_get_date_time(qts, &date[0]);
177926bef1dSThomas Huth     cmos_get_date_time(qts, &date[1]);
178926bef1dSThomas Huth     cmos_get_date_time(qts, &date[2]);
179926bef1dSThomas Huth     cmos_get_date_time(qts, &date[3]);
1801e8a1faeSThomas Huth 
1811e8a1faeSThomas Huth     ts = time(NULL);
1821e8a1faeSThomas Huth     gmtime_r(&ts, &end);
1831e8a1faeSThomas Huth 
1841e8a1faeSThomas Huth     if (tm_cmp(&date[0], &date[1]) == 0) {
1851e8a1faeSThomas Huth         datep = &date[0];
1861e8a1faeSThomas Huth     } else if (tm_cmp(&date[1], &date[2]) == 0) {
1871e8a1faeSThomas Huth         datep = &date[1];
1881e8a1faeSThomas Huth     } else if (tm_cmp(&date[2], &date[3]) == 0) {
1891e8a1faeSThomas Huth         datep = &date[2];
1901e8a1faeSThomas Huth     } else {
1911e8a1faeSThomas Huth         g_assert_not_reached();
1921e8a1faeSThomas Huth     }
1931e8a1faeSThomas Huth 
1941e8a1faeSThomas Huth     if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) {
195*168d4674SPaolo Bonzini         long date_s, start_s;
196*168d4674SPaolo Bonzini         unsigned long diff;
1971e8a1faeSThomas Huth 
1981e8a1faeSThomas Huth         start.tm_isdst = datep->tm_isdst;
1991e8a1faeSThomas Huth 
200*168d4674SPaolo Bonzini         date_s = (long)mktime(datep);
201*168d4674SPaolo Bonzini         start_s = (long)mktime(&start);
202*168d4674SPaolo Bonzini         if (date_s < start_s) {
203*168d4674SPaolo Bonzini             diff = start_s - date_s;
204*168d4674SPaolo Bonzini             g_test_message("RTC is %ld second(s) behind wall-clock", diff);
2051e8a1faeSThomas Huth         } else {
206*168d4674SPaolo Bonzini             diff = date_s - start_s;
207*168d4674SPaolo Bonzini             g_test_message("RTC is %ld second(s) ahead of wall-clock", diff);
2081e8a1faeSThomas Huth         }
2091e8a1faeSThomas Huth 
210*168d4674SPaolo Bonzini         g_assert_cmpint(diff, <=, wiggle);
2111e8a1faeSThomas Huth     }
2121e8a1faeSThomas Huth 
213926bef1dSThomas Huth     qtest_quit(qts);
2141e8a1faeSThomas Huth }
2151e8a1faeSThomas Huth 
2161e8a1faeSThomas Huth /* success if no crash or abort */
fuzz_registers(void)2171e8a1faeSThomas Huth static void fuzz_registers(void)
2181e8a1faeSThomas Huth {
2191e8a1faeSThomas Huth     unsigned int i;
2201e8a1faeSThomas Huth     QTestState *s = m48t59_qtest_start();
2211e8a1faeSThomas Huth 
2221e8a1faeSThomas Huth     for (i = 0; i < 1000; i++) {
2231e8a1faeSThomas Huth         uint8_t reg, val;
2241e8a1faeSThomas Huth 
2251e8a1faeSThomas Huth         reg = (uint8_t)g_test_rand_int_range(0, 16);
2261e8a1faeSThomas Huth         val = (uint8_t)g_test_rand_int_range(0, 256);
2271e8a1faeSThomas Huth 
2281e8a1faeSThomas Huth         if (reg == 7) {
2291e8a1faeSThomas Huth             /* watchdog setup register, may trigger system reset, skip */
2301e8a1faeSThomas Huth             continue;
2311e8a1faeSThomas Huth         }
2321e8a1faeSThomas Huth 
2331e8a1faeSThomas Huth         cmos_write(s, reg, val);
2341e8a1faeSThomas Huth         cmos_read(s, reg);
2351e8a1faeSThomas Huth     }
2361e8a1faeSThomas Huth 
2371e8a1faeSThomas Huth     qtest_quit(s);
2381e8a1faeSThomas Huth }
2391e8a1faeSThomas Huth 
base_setup(void)2401e8a1faeSThomas Huth static void base_setup(void)
2411e8a1faeSThomas Huth {
2421e8a1faeSThomas Huth     const char *arch = qtest_get_arch();
2431e8a1faeSThomas Huth 
2441e8a1faeSThomas Huth     if (g_str_equal(arch, "sparc")) {
2451e8a1faeSThomas Huth         /* Note: For sparc64, we'd need to map-in the PCI bridge memory first */
2461e8a1faeSThomas Huth         base = 0x71200000;
2471e8a1faeSThomas Huth         base_year = 1968;
2481e8a1faeSThomas Huth         base_machine = "SS-5";
2491e8a1faeSThomas Huth         use_mmio = true;
2501e8a1faeSThomas Huth     } else if (g_str_equal(arch, "ppc") || g_str_equal(arch, "ppc64")) {
2511e8a1faeSThomas Huth         base = 0xF0000000;
2521e8a1faeSThomas Huth         base_year = 1968;
2531e8a1faeSThomas Huth         base_machine = "ref405ep";
2541e8a1faeSThomas Huth         use_mmio = true;
2551e8a1faeSThomas Huth     } else {
2561e8a1faeSThomas Huth         g_assert_not_reached();
2571e8a1faeSThomas Huth     }
2581e8a1faeSThomas Huth }
2591e8a1faeSThomas Huth 
main(int argc,char ** argv)2601e8a1faeSThomas Huth int main(int argc, char **argv)
2611e8a1faeSThomas Huth {
2621e8a1faeSThomas Huth     base_setup();
2631e8a1faeSThomas Huth 
2641e8a1faeSThomas Huth     g_test_init(&argc, &argv, NULL);
2651e8a1faeSThomas Huth 
2661e8a1faeSThomas Huth     if (g_test_slow()) {
2671e8a1faeSThomas Huth         /* Do not run this in timing-sensitive environments */
2681e8a1faeSThomas Huth         qtest_add_func("/rtc/bcd-check-time", bcd_check_time);
2691e8a1faeSThomas Huth     }
2701e8a1faeSThomas Huth     qtest_add_func("/rtc/fuzz-registers", fuzz_registers);
2711e8a1faeSThomas Huth     return g_test_run();
2721e8a1faeSThomas Huth }
273