2 * QTest testcase for the M48T59 and M48T08 real-time clocks
4 * Based on MC146818 RTC test:
5 * Copyright IBM, Corp. 2012
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 */
33 static uint8_t cmos_read_mmio(uint8_t reg)
35 return readb(base + (uint32_t)reg_base + (uint32_t)reg);
38 static void cmos_write_mmio(uint8_t reg, uint8_t val)
42 writeb(base + (uint32_t)reg_base + (uint32_t)reg, data);
45 static uint8_t cmos_read_ioio(uint8_t reg)
47 outw(base + 0, reg_base + (uint16_t)reg);
51 static void cmos_write_ioio(uint8_t reg, uint8_t val)
53 outw(base + 0, reg_base + (uint16_t)reg);
57 static uint8_t cmos_read(uint8_t reg)
60 return cmos_read_mmio(reg);
62 return cmos_read_ioio(reg);
66 static void cmos_write(uint8_t reg, uint8_t val)
69 cmos_write_mmio(reg, val);
71 cmos_write_ioio(reg, val);
75 static int bcd2dec(int value)
77 return (((value >> 4) & 0x0F) * 10) + (value & 0x0F);
80 static int tm_cmp(struct tm *lhs, struct tm *rhs)
85 memcpy(&d1, lhs, sizeof(d1));
86 memcpy(&d2, rhs, sizeof(d2));
101 static void print_tm(struct tm *tm)
103 printf("%04d-%02d-%02d %02d:%02d:%02d %+02ld\n",
104 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
105 tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
109 static void cmos_get_date_time(struct tm *date)
111 int sec, min, hour, mday, mon, year;
115 sec = cmos_read(RTC_SECONDS);
116 min = cmos_read(RTC_MINUTES);
117 hour = cmos_read(RTC_HOURS);
118 mday = cmos_read(RTC_DAY_OF_MONTH);
119 mon = cmos_read(RTC_MONTH);
120 year = cmos_read(RTC_YEAR);
124 hour = bcd2dec(hour);
125 mday = bcd2dec(mday);
127 year = bcd2dec(year);
130 localtime_r(&ts, &dummy);
132 date->tm_isdst = dummy.tm_isdst;
135 date->tm_hour = hour;
136 date->tm_mday = mday;
137 date->tm_mon = mon - 1;
138 date->tm_year = base_year + year - 1900;
146 static void check_time(int wiggle)
148 struct tm start, date[4], end;
153 * This check assumes a few things. First, we cannot guarantee that we get
154 * a consistent reading from the wall clock because we may hit an edge of
155 * the clock while reading. To work around this, we read four clock readings
156 * such that at least two of them should match. We need to assume that one
157 * reading is corrupt so we need four readings to ensure that we have at
158 * least two consecutive identical readings
160 * It's also possible that we'll cross an edge reading the host clock so
161 * simply check to make sure that the clock reading is within the period of
162 * when we expect it to be.
166 gmtime_r(&ts, &start);
168 cmos_get_date_time(&date[0]);
169 cmos_get_date_time(&date[1]);
170 cmos_get_date_time(&date[2]);
171 cmos_get_date_time(&date[3]);
176 if (tm_cmp(&date[0], &date[1]) == 0) {
178 } else if (tm_cmp(&date[1], &date[2]) == 0) {
180 } else if (tm_cmp(&date[2], &date[3]) == 0) {
183 g_assert_not_reached();
186 if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) {
189 start.tm_isdst = datep->tm_isdst;
191 t = (long)mktime(datep);
192 s = (long)mktime(&start);
194 g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t));
196 g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
199 g_assert_cmpint(ABS(t - s), <=, wiggle);
203 static int wiggle = 2;
205 static void bcd_check_time(void)
207 if (strcmp(qtest_get_arch(), "sparc64") == 0) {
211 } else if (strcmp(qtest_get_arch(), "sparc") == 0) {
215 } else { /* PPC: need to map macio in PCI */
216 g_assert_not_reached();
221 /* success if no crash or abort */
222 static void fuzz_registers(void)
226 for (i = 0; i < 1000; i++) {
229 reg = (uint8_t)g_test_rand_int_range(0, 16);
230 val = (uint8_t)g_test_rand_int_range(0, 256);
233 /* watchdog setup register, may trigger system reset, skip */
237 cmos_write(reg, val);
242 int main(int argc, char **argv)
244 QTestState *s = NULL;
247 g_test_init(&argc, &argv, NULL);
249 s = qtest_start("-rtc clock=vm");
251 qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
252 qtest_add_func("/rtc/fuzz-registers", fuzz_registers);