]>
Commit | Line | Data |
---|---|---|
f91837a7 BS |
1 | /* |
2 | * QTest testcase for the M48T59 and M48T08 real-time clocks | |
3 | * | |
4 | * Based on MC146818 RTC test: | |
5 | * Copyright IBM, Corp. 2012 | |
6 | * | |
7 | * Authors: | |
8 | * Anthony Liguori <aliguori@us.ibm.com> | |
9 | * | |
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. | |
12 | * | |
13 | */ | |
f91837a7 | 14 | |
681c28a3 | 15 | #include "qemu/osdep.h" |
f91837a7 | 16 | |
91f32b0c SH |
17 | #include "libqtest.h" |
18 | ||
f91837a7 BS |
19 | #define RTC_SECONDS 0x9 |
20 | #define RTC_MINUTES 0xa | |
21 | #define RTC_HOURS 0xb | |
22 | ||
23 | #define RTC_DAY_OF_WEEK 0xc | |
24 | #define RTC_DAY_OF_MONTH 0xd | |
25 | #define RTC_MONTH 0xe | |
26 | #define RTC_YEAR 0xf | |
27 | ||
28 | static uint32_t base; | |
29 | static uint16_t reg_base = 0x1ff0; /* 0x7f0 for m48t02 */ | |
30 | static int base_year; | |
31 | static bool use_mmio; | |
32 | ||
33 | static uint8_t cmos_read_mmio(uint8_t reg) | |
34 | { | |
872536bf | 35 | return readb(base + (uint32_t)reg_base + (uint32_t)reg); |
f91837a7 BS |
36 | } |
37 | ||
38 | static void cmos_write_mmio(uint8_t reg, uint8_t val) | |
39 | { | |
40 | uint8_t data = val; | |
41 | ||
872536bf | 42 | writeb(base + (uint32_t)reg_base + (uint32_t)reg, data); |
f91837a7 BS |
43 | } |
44 | ||
45 | static uint8_t cmos_read_ioio(uint8_t reg) | |
46 | { | |
47 | outw(base + 0, reg_base + (uint16_t)reg); | |
48 | return inb(base + 3); | |
49 | } | |
50 | ||
51 | static void cmos_write_ioio(uint8_t reg, uint8_t val) | |
52 | { | |
53 | outw(base + 0, reg_base + (uint16_t)reg); | |
54 | outb(base + 3, val); | |
55 | } | |
56 | ||
57 | static uint8_t cmos_read(uint8_t reg) | |
58 | { | |
59 | if (use_mmio) { | |
60 | return cmos_read_mmio(reg); | |
61 | } else { | |
62 | return cmos_read_ioio(reg); | |
63 | } | |
64 | } | |
65 | ||
66 | static void cmos_write(uint8_t reg, uint8_t val) | |
67 | { | |
68 | if (use_mmio) { | |
69 | cmos_write_mmio(reg, val); | |
70 | } else { | |
71 | cmos_write_ioio(reg, val); | |
72 | } | |
73 | } | |
74 | ||
75 | static int bcd2dec(int value) | |
76 | { | |
77 | return (((value >> 4) & 0x0F) * 10) + (value & 0x0F); | |
78 | } | |
79 | ||
80 | static int tm_cmp(struct tm *lhs, struct tm *rhs) | |
81 | { | |
82 | time_t a, b; | |
83 | struct tm d1, d2; | |
84 | ||
85 | memcpy(&d1, lhs, sizeof(d1)); | |
86 | memcpy(&d2, rhs, sizeof(d2)); | |
87 | ||
88 | a = mktime(&d1); | |
89 | b = mktime(&d2); | |
90 | ||
91 | if (a < b) { | |
92 | return -1; | |
93 | } else if (a > b) { | |
94 | return 1; | |
95 | } | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
100 | #if 0 | |
101 | static void print_tm(struct tm *tm) | |
102 | { | |
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); | |
106 | } | |
107 | #endif | |
108 | ||
109 | static void cmos_get_date_time(struct tm *date) | |
110 | { | |
111 | int sec, min, hour, mday, mon, year; | |
112 | time_t ts; | |
113 | struct tm dummy; | |
114 | ||
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); | |
121 | ||
122 | sec = bcd2dec(sec); | |
123 | min = bcd2dec(min); | |
124 | hour = bcd2dec(hour); | |
125 | mday = bcd2dec(mday); | |
126 | mon = bcd2dec(mon); | |
127 | year = bcd2dec(year); | |
128 | ||
129 | ts = time(NULL); | |
130 | localtime_r(&ts, &dummy); | |
131 | ||
132 | date->tm_isdst = dummy.tm_isdst; | |
133 | date->tm_sec = sec; | |
134 | date->tm_min = min; | |
135 | date->tm_hour = hour; | |
136 | date->tm_mday = mday; | |
137 | date->tm_mon = mon - 1; | |
138 | date->tm_year = base_year + year - 1900; | |
a05ddd92 | 139 | #ifndef __sun__ |
f91837a7 | 140 | date->tm_gmtoff = 0; |
a05ddd92 | 141 | #endif |
f91837a7 BS |
142 | |
143 | ts = mktime(date); | |
144 | } | |
145 | ||
9c29830c TH |
146 | static QTestState *m48t59_qtest_start(void) |
147 | { | |
148 | return qtest_start("-rtc clock=vm"); | |
149 | } | |
150 | ||
151 | static void bcd_check_time(void) | |
f91837a7 BS |
152 | { |
153 | struct tm start, date[4], end; | |
154 | struct tm *datep; | |
155 | time_t ts; | |
9c29830c TH |
156 | const int wiggle = 2; |
157 | QTestState *s = m48t59_qtest_start(); | |
f91837a7 BS |
158 | |
159 | /* | |
160 | * This check assumes a few things. First, we cannot guarantee that we get | |
161 | * a consistent reading from the wall clock because we may hit an edge of | |
162 | * the clock while reading. To work around this, we read four clock readings | |
163 | * such that at least two of them should match. We need to assume that one | |
164 | * reading is corrupt so we need four readings to ensure that we have at | |
165 | * least two consecutive identical readings | |
166 | * | |
167 | * It's also possible that we'll cross an edge reading the host clock so | |
168 | * simply check to make sure that the clock reading is within the period of | |
169 | * when we expect it to be. | |
170 | */ | |
171 | ||
172 | ts = time(NULL); | |
173 | gmtime_r(&ts, &start); | |
174 | ||
175 | cmos_get_date_time(&date[0]); | |
176 | cmos_get_date_time(&date[1]); | |
177 | cmos_get_date_time(&date[2]); | |
178 | cmos_get_date_time(&date[3]); | |
179 | ||
180 | ts = time(NULL); | |
181 | gmtime_r(&ts, &end); | |
182 | ||
183 | if (tm_cmp(&date[0], &date[1]) == 0) { | |
184 | datep = &date[0]; | |
185 | } else if (tm_cmp(&date[1], &date[2]) == 0) { | |
186 | datep = &date[1]; | |
187 | } else if (tm_cmp(&date[2], &date[3]) == 0) { | |
188 | datep = &date[2]; | |
189 | } else { | |
190 | g_assert_not_reached(); | |
191 | } | |
192 | ||
193 | if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) { | |
194 | long t, s; | |
195 | ||
196 | start.tm_isdst = datep->tm_isdst; | |
197 | ||
198 | t = (long)mktime(datep); | |
199 | s = (long)mktime(&start); | |
200 | if (t < s) { | |
201 | g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t)); | |
202 | } else { | |
203 | g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s)); | |
204 | } | |
205 | ||
206 | g_assert_cmpint(ABS(t - s), <=, wiggle); | |
207 | } | |
f91837a7 | 208 | |
9c29830c | 209 | qtest_quit(s); |
f91837a7 BS |
210 | } |
211 | ||
212 | /* success if no crash or abort */ | |
213 | static void fuzz_registers(void) | |
214 | { | |
215 | unsigned int i; | |
9c29830c | 216 | QTestState *s = m48t59_qtest_start(); |
f91837a7 BS |
217 | |
218 | for (i = 0; i < 1000; i++) { | |
219 | uint8_t reg, val; | |
220 | ||
221 | reg = (uint8_t)g_test_rand_int_range(0, 16); | |
222 | val = (uint8_t)g_test_rand_int_range(0, 256); | |
223 | ||
067f0691 GH |
224 | if (reg == 7) { |
225 | /* watchdog setup register, may trigger system reset, skip */ | |
226 | continue; | |
227 | } | |
228 | ||
f91837a7 BS |
229 | cmos_write(reg, val); |
230 | cmos_read(reg); | |
231 | } | |
9c29830c TH |
232 | |
233 | qtest_quit(s); | |
234 | } | |
235 | ||
236 | static void base_setup(void) | |
237 | { | |
238 | const char *arch = qtest_get_arch(); | |
239 | ||
240 | if (g_str_equal(arch, "sparc")) { | |
241 | /* Note: For sparc64, we'd need to map-in the PCI bridge memory first */ | |
242 | base = 0x71200000; | |
243 | base_year = 1968; | |
244 | use_mmio = true; | |
245 | } else { | |
246 | g_assert_not_reached(); | |
247 | } | |
f91837a7 BS |
248 | } |
249 | ||
250 | int main(int argc, char **argv) | |
251 | { | |
f91837a7 BS |
252 | int ret; |
253 | ||
9c29830c | 254 | base_setup(); |
f91837a7 | 255 | |
9c29830c | 256 | g_test_init(&argc, &argv, NULL); |
f91837a7 | 257 | |
9c29830c TH |
258 | if (g_test_slow()) { |
259 | /* Do not run this in timing-sensitive environments */ | |
260 | qtest_add_func("/rtc/bcd-check-time", bcd_check_time); | |
261 | } | |
f91837a7 BS |
262 | qtest_add_func("/rtc/fuzz-registers", fuzz_registers); |
263 | ret = g_test_run(); | |
264 | ||
f91837a7 BS |
265 | return ret; |
266 | } |