2 * QTest testcase for the M48T59 and M48T08 real-time clocks
4 * Based on MC146818 RTC test:
5 * Copyright IBM, Corp. 2012
8 * Anthony Liguori <aliguori@us.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
19 #define RTC_SECONDS 0x9
20 #define RTC_MINUTES 0xa
23 #define RTC_DAY_OF_WEEK 0xc
24 #define RTC_DAY_OF_MONTH 0xd
29 static uint16_t reg_base = 0x1ff0; /* 0x7f0 for m48t02 */
31 static const char *base_machine;
34 static uint8_t cmos_read_mmio(QTestState *s, uint8_t reg)
36 return qtest_readb(s, base + (uint32_t)reg_base + (uint32_t)reg);
39 static void cmos_write_mmio(QTestState *s, uint8_t reg, uint8_t val)
43 qtest_writeb(s, base + (uint32_t)reg_base + (uint32_t)reg, data);
46 static uint8_t cmos_read_ioio(QTestState *s, uint8_t reg)
48 qtest_outw(s, base + 0, reg_base + (uint16_t)reg);
49 return qtest_inb(s, base + 3);
52 static void cmos_write_ioio(QTestState *s, uint8_t reg, uint8_t val)
54 qtest_outw(s, base + 0, reg_base + (uint16_t)reg);
55 qtest_outb(s, base + 3, val);
58 static uint8_t cmos_read(QTestState *s, uint8_t reg)
61 return cmos_read_mmio(s, reg);
63 return cmos_read_ioio(s, reg);
67 static void cmos_write(QTestState *s, uint8_t reg, uint8_t val)
70 cmos_write_mmio(s, reg, val);
72 cmos_write_ioio(s, reg, val);
76 static int bcd2dec(int value)
78 return (((value >> 4) & 0x0F) * 10) + (value & 0x0F);
81 static int tm_cmp(struct tm *lhs, struct tm *rhs)
86 memcpy(&d1, lhs, sizeof(d1));
87 memcpy(&d2, rhs, sizeof(d2));
102 static void print_tm(struct tm *tm)
104 printf("%04d-%02d-%02d %02d:%02d:%02d %+02ld\n",
105 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
106 tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
110 static void cmos_get_date_time(QTestState *s, struct tm *date)
112 int sec, min, hour, mday, mon, year;
116 sec = cmos_read(s, RTC_SECONDS);
117 min = cmos_read(s, RTC_MINUTES);
118 hour = cmos_read(s, RTC_HOURS);
119 mday = cmos_read(s, RTC_DAY_OF_MONTH);
120 mon = cmos_read(s, RTC_MONTH);
121 year = cmos_read(s, RTC_YEAR);
125 hour = bcd2dec(hour);
126 mday = bcd2dec(mday);
128 year = bcd2dec(year);
131 localtime_r(&ts, &dummy);
133 date->tm_isdst = dummy.tm_isdst;
136 date->tm_hour = hour;
137 date->tm_mday = mday;
138 date->tm_mon = mon - 1;
139 date->tm_year = base_year + year - 1900;
140 #if !defined(__sun__) && !defined(_WIN32)
147 static QTestState *m48t59_qtest_start(void)
149 return qtest_initf("-M %s -rtc clock=vm", base_machine);
152 static void bcd_check_time(void)
154 struct tm start, date[4], end;
157 const int wiggle = 2;
158 QTestState *qts = m48t59_qtest_start();
161 * This check assumes a few things. First, we cannot guarantee that we get
162 * a consistent reading from the wall clock because we may hit an edge of
163 * the clock while reading. To work around this, we read four clock readings
164 * such that at least two of them should match. We need to assume that one
165 * reading is corrupt so we need four readings to ensure that we have at
166 * least two consecutive identical readings
168 * It's also possible that we'll cross an edge reading the host clock so
169 * simply check to make sure that the clock reading is within the period of
170 * when we expect it to be.
174 gmtime_r(&ts, &start);
176 cmos_get_date_time(qts, &date[0]);
177 cmos_get_date_time(qts, &date[1]);
178 cmos_get_date_time(qts, &date[2]);
179 cmos_get_date_time(qts, &date[3]);
184 if (tm_cmp(&date[0], &date[1]) == 0) {
186 } else if (tm_cmp(&date[1], &date[2]) == 0) {
188 } else if (tm_cmp(&date[2], &date[3]) == 0) {
191 g_assert_not_reached();
194 if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) {
195 long date_s, start_s;
198 start.tm_isdst = datep->tm_isdst;
200 date_s = (long)mktime(datep);
201 start_s = (long)mktime(&start);
202 if (date_s < start_s) {
203 diff = start_s - date_s;
204 g_test_message("RTC is %ld second(s) behind wall-clock", diff);
206 diff = date_s - start_s;
207 g_test_message("RTC is %ld second(s) ahead of wall-clock", diff);
210 g_assert_cmpint(diff, <=, wiggle);
216 /* success if no crash or abort */
217 static void fuzz_registers(void)
220 QTestState *s = m48t59_qtest_start();
222 for (i = 0; i < 1000; i++) {
225 reg = (uint8_t)g_test_rand_int_range(0, 16);
226 val = (uint8_t)g_test_rand_int_range(0, 256);
229 /* watchdog setup register, may trigger system reset, skip */
233 cmos_write(s, reg, val);
240 static void base_setup(void)
242 const char *arch = qtest_get_arch();
244 if (g_str_equal(arch, "sparc")) {
245 /* Note: For sparc64, we'd need to map-in the PCI bridge memory first */
248 base_machine = "SS-5";
250 } else if (g_str_equal(arch, "ppc") || g_str_equal(arch, "ppc64")) {
253 base_machine = "ref405ep";
256 g_assert_not_reached();
260 int main(int argc, char **argv)
264 g_test_init(&argc, &argv, NULL);
267 /* Do not run this in timing-sensitive environments */
268 qtest_add_func("/rtc/bcd-check-time", bcd_check_time);
270 qtest_add_func("/rtc/fuzz-registers", fuzz_registers);