]>
Commit | Line | Data |
---|---|---|
80cabfad FB |
1 | /* |
2 | * QEMU MC146818 RTC emulation | |
5fafdf24 | 3 | * |
80cabfad | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
5fafdf24 | 5 | * |
80cabfad FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
83c9f4ca | 24 | #include "hw/hw.h" |
1de7afc9 | 25 | #include "qemu/timer.h" |
9c17d615 | 26 | #include "sysemu/sysemu.h" |
0d09e41a | 27 | #include "hw/timer/mc146818rtc.h" |
7b1b5d19 | 28 | #include "qapi/visitor.h" |
80cabfad | 29 | |
d362e757 | 30 | #ifdef TARGET_I386 |
0d09e41a | 31 | #include "hw/i386/apic.h" |
d362e757 JK |
32 | #endif |
33 | ||
80cabfad | 34 | //#define DEBUG_CMOS |
aa6f63ff | 35 | //#define DEBUG_COALESCED |
80cabfad | 36 | |
ec51e364 IY |
37 | #ifdef DEBUG_CMOS |
38 | # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) | |
39 | #else | |
40 | # define CMOS_DPRINTF(format, ...) do { } while (0) | |
41 | #endif | |
42 | ||
aa6f63ff BS |
43 | #ifdef DEBUG_COALESCED |
44 | # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__) | |
45 | #else | |
46 | # define DPRINTF_C(format, ...) do { } while (0) | |
47 | #endif | |
48 | ||
56038ef6 | 49 | #define NSEC_PER_SEC 1000000000LL |
00cf5774 PB |
50 | #define SEC_PER_MIN 60 |
51 | #define MIN_PER_HOUR 60 | |
52 | #define SEC_PER_HOUR 3600 | |
53 | #define HOUR_PER_DAY 24 | |
54 | #define SEC_PER_DAY 86400 | |
56038ef6 | 55 | |
dd17765b | 56 | #define RTC_REINJECT_ON_ACK_COUNT 20 |
e46deaba | 57 | #define RTC_CLOCK_RATE 32768 |
56038ef6 | 58 | #define UIP_HOLD_LENGTH (8 * NSEC_PER_SEC / 32768) |
ba32edab | 59 | |
0e41271e AF |
60 | #define MC146818_RTC(obj) OBJECT_CHECK(RTCState, (obj), TYPE_MC146818_RTC) |
61 | ||
1d914fa0 | 62 | typedef struct RTCState { |
0e41271e AF |
63 | ISADevice parent_obj; |
64 | ||
b2c5009b | 65 | MemoryRegion io; |
dff38e7b FB |
66 | uint8_t cmos_data[128]; |
67 | uint8_t cmos_index; | |
32e0c826 | 68 | int32_t base_year; |
56038ef6 YZ |
69 | uint64_t base_rtc; |
70 | uint64_t last_update; | |
71 | int64_t offset; | |
d537cf6c | 72 | qemu_irq irq; |
100d9891 | 73 | qemu_irq sqw_irq; |
18c6e2ff | 74 | int it_shift; |
dff38e7b FB |
75 | /* periodic timer */ |
76 | QEMUTimer *periodic_timer; | |
77 | int64_t next_periodic_time; | |
56038ef6 YZ |
78 | /* update-ended timer */ |
79 | QEMUTimer *update_timer; | |
00cf5774 | 80 | uint64_t next_alarm_time; |
ba32edab | 81 | uint16_t irq_reinject_on_ack_count; |
73822ec8 AL |
82 | uint32_t irq_coalesced; |
83 | uint32_t period; | |
93b66569 | 84 | QEMUTimer *coalesced_timer; |
17604dac | 85 | Notifier clock_reset_notifier; |
433acf0d | 86 | LostTickPolicy lost_tick_policy; |
da98c8eb | 87 | Notifier suspend_notifier; |
1d914fa0 | 88 | } RTCState; |
dff38e7b FB |
89 | |
90 | static void rtc_set_time(RTCState *s); | |
56038ef6 | 91 | static void rtc_update_time(RTCState *s); |
e2826cf4 | 92 | static void rtc_set_cmos(RTCState *s, const struct tm *tm); |
56038ef6 | 93 | static inline int rtc_from_bcd(RTCState *s, int a); |
00cf5774 | 94 | static uint64_t get_next_alarm(RTCState *s); |
56038ef6 | 95 | |
41a9b8b2 YZ |
96 | static inline bool rtc_running(RTCState *s) |
97 | { | |
98 | return (!(s->cmos_data[RTC_REG_B] & REG_B_SET) && | |
99 | (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20); | |
100 | } | |
101 | ||
56038ef6 YZ |
102 | static uint64_t get_guest_rtc_ns(RTCState *s) |
103 | { | |
104 | uint64_t guest_rtc; | |
884f17c2 | 105 | uint64_t guest_clock = qemu_clock_get_ns(rtc_clock); |
56038ef6 YZ |
106 | |
107 | guest_rtc = s->base_rtc * NSEC_PER_SEC | |
108 | + guest_clock - s->last_update + s->offset; | |
109 | return guest_rtc; | |
110 | } | |
dff38e7b | 111 | |
93b66569 AL |
112 | #ifdef TARGET_I386 |
113 | static void rtc_coalesced_timer_update(RTCState *s) | |
114 | { | |
115 | if (s->irq_coalesced == 0) { | |
bc72ad67 | 116 | timer_del(s->coalesced_timer); |
93b66569 AL |
117 | } else { |
118 | /* divide each RTC interval to 2 - 8 smaller intervals */ | |
119 | int c = MIN(s->irq_coalesced, 7) + 1; | |
884f17c2 | 120 | int64_t next_clock = qemu_clock_get_ns(rtc_clock) + |
e46deaba | 121 | muldiv64(s->period / c, get_ticks_per_sec(), RTC_CLOCK_RATE); |
bc72ad67 | 122 | timer_mod(s->coalesced_timer, next_clock); |
93b66569 AL |
123 | } |
124 | } | |
125 | ||
126 | static void rtc_coalesced_timer(void *opaque) | |
127 | { | |
128 | RTCState *s = opaque; | |
129 | ||
130 | if (s->irq_coalesced != 0) { | |
131 | apic_reset_irq_delivered(); | |
132 | s->cmos_data[RTC_REG_C] |= 0xc0; | |
aa6f63ff | 133 | DPRINTF_C("cmos: injecting from timer\n"); |
7d932dfd | 134 | qemu_irq_raise(s->irq); |
93b66569 AL |
135 | if (apic_get_irq_delivered()) { |
136 | s->irq_coalesced--; | |
aa6f63ff BS |
137 | DPRINTF_C("cmos: coalesced irqs decreased to %d\n", |
138 | s->irq_coalesced); | |
93b66569 AL |
139 | } |
140 | } | |
141 | ||
142 | rtc_coalesced_timer_update(s); | |
143 | } | |
144 | #endif | |
145 | ||
56038ef6 | 146 | /* handle periodic timer */ |
c4c18e24 | 147 | static void periodic_timer_update(RTCState *s, int64_t current_time) |
dff38e7b FB |
148 | { |
149 | int period_code, period; | |
150 | int64_t cur_clock, next_irq_clock; | |
151 | ||
152 | period_code = s->cmos_data[RTC_REG_A] & 0x0f; | |
100d9891 | 153 | if (period_code != 0 |
7d932dfd | 154 | && ((s->cmos_data[RTC_REG_B] & REG_B_PIE) |
100d9891 | 155 | || ((s->cmos_data[RTC_REG_B] & REG_B_SQWE) && s->sqw_irq))) { |
dff38e7b FB |
156 | if (period_code <= 2) |
157 | period_code += 7; | |
158 | /* period in 32 Khz cycles */ | |
159 | period = 1 << (period_code - 1); | |
73822ec8 | 160 | #ifdef TARGET_I386 |
aa6f63ff | 161 | if (period != s->period) { |
73822ec8 | 162 | s->irq_coalesced = (s->irq_coalesced * s->period) / period; |
aa6f63ff BS |
163 | DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s->irq_coalesced); |
164 | } | |
73822ec8 AL |
165 | s->period = period; |
166 | #endif | |
dff38e7b | 167 | /* compute 32 khz clock */ |
e46deaba | 168 | cur_clock = muldiv64(current_time, RTC_CLOCK_RATE, get_ticks_per_sec()); |
dff38e7b | 169 | next_irq_clock = (cur_clock & ~(period - 1)) + period; |
6875204c | 170 | s->next_periodic_time = |
e46deaba | 171 | muldiv64(next_irq_clock, get_ticks_per_sec(), RTC_CLOCK_RATE) + 1; |
bc72ad67 | 172 | timer_mod(s->periodic_timer, s->next_periodic_time); |
dff38e7b | 173 | } else { |
73822ec8 AL |
174 | #ifdef TARGET_I386 |
175 | s->irq_coalesced = 0; | |
176 | #endif | |
bc72ad67 | 177 | timer_del(s->periodic_timer); |
dff38e7b FB |
178 | } |
179 | } | |
180 | ||
181 | static void rtc_periodic_timer(void *opaque) | |
182 | { | |
183 | RTCState *s = opaque; | |
184 | ||
c4c18e24 | 185 | periodic_timer_update(s, s->next_periodic_time); |
663447d4 | 186 | s->cmos_data[RTC_REG_C] |= REG_C_PF; |
100d9891 | 187 | if (s->cmos_data[RTC_REG_B] & REG_B_PIE) { |
663447d4 | 188 | s->cmos_data[RTC_REG_C] |= REG_C_IRQF; |
93b66569 | 189 | #ifdef TARGET_I386 |
433acf0d | 190 | if (s->lost_tick_policy == LOST_TICK_SLEW) { |
ba32edab GN |
191 | if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT) |
192 | s->irq_reinject_on_ack_count = 0; | |
93b66569 | 193 | apic_reset_irq_delivered(); |
7d932dfd | 194 | qemu_irq_raise(s->irq); |
93b66569 AL |
195 | if (!apic_get_irq_delivered()) { |
196 | s->irq_coalesced++; | |
197 | rtc_coalesced_timer_update(s); | |
aa6f63ff BS |
198 | DPRINTF_C("cmos: coalesced irqs increased to %d\n", |
199 | s->irq_coalesced); | |
93b66569 AL |
200 | } |
201 | } else | |
202 | #endif | |
7d932dfd | 203 | qemu_irq_raise(s->irq); |
100d9891 AJ |
204 | } |
205 | if (s->cmos_data[RTC_REG_B] & REG_B_SQWE) { | |
206 | /* Not square wave at all but we don't want 2048Hz interrupts! | |
207 | Must be seen as a pulse. */ | |
208 | qemu_irq_raise(s->sqw_irq); | |
209 | } | |
dff38e7b | 210 | } |
80cabfad | 211 | |
56038ef6 YZ |
212 | /* handle update-ended timer */ |
213 | static void check_update_timer(RTCState *s) | |
214 | { | |
215 | uint64_t next_update_time; | |
216 | uint64_t guest_nsec; | |
00cf5774 | 217 | int next_alarm_sec; |
56038ef6 | 218 | |
41a9b8b2 YZ |
219 | /* From the data sheet: "Holding the dividers in reset prevents |
220 | * interrupts from operating, while setting the SET bit allows" | |
221 | * them to occur. However, it will prevent an alarm interrupt | |
222 | * from occurring, because the time of day is not updated. | |
56038ef6 | 223 | */ |
41a9b8b2 | 224 | if ((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) { |
bc72ad67 | 225 | timer_del(s->update_timer); |
41a9b8b2 YZ |
226 | return; |
227 | } | |
56038ef6 YZ |
228 | if ((s->cmos_data[RTC_REG_C] & REG_C_UF) && |
229 | (s->cmos_data[RTC_REG_B] & REG_B_SET)) { | |
bc72ad67 | 230 | timer_del(s->update_timer); |
56038ef6 YZ |
231 | return; |
232 | } | |
233 | if ((s->cmos_data[RTC_REG_C] & REG_C_UF) && | |
234 | (s->cmos_data[RTC_REG_C] & REG_C_AF)) { | |
bc72ad67 | 235 | timer_del(s->update_timer); |
56038ef6 YZ |
236 | return; |
237 | } | |
238 | ||
239 | guest_nsec = get_guest_rtc_ns(s) % NSEC_PER_SEC; | |
00cf5774 | 240 | /* if UF is clear, reprogram to next second */ |
884f17c2 | 241 | next_update_time = qemu_clock_get_ns(rtc_clock) |
56038ef6 | 242 | + NSEC_PER_SEC - guest_nsec; |
00cf5774 PB |
243 | |
244 | /* Compute time of next alarm. One second is already accounted | |
245 | * for in next_update_time. | |
246 | */ | |
247 | next_alarm_sec = get_next_alarm(s); | |
248 | s->next_alarm_time = next_update_time + (next_alarm_sec - 1) * NSEC_PER_SEC; | |
249 | ||
250 | if (s->cmos_data[RTC_REG_C] & REG_C_UF) { | |
251 | /* UF is set, but AF is clear. Program the timer to target | |
252 | * the alarm time. */ | |
253 | next_update_time = s->next_alarm_time; | |
254 | } | |
e93379b0 | 255 | if (next_update_time != timer_expire_time_ns(s->update_timer)) { |
bc72ad67 | 256 | timer_mod(s->update_timer, next_update_time); |
56038ef6 YZ |
257 | } |
258 | } | |
259 | ||
260 | static inline uint8_t convert_hour(RTCState *s, uint8_t hour) | |
261 | { | |
262 | if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) { | |
263 | hour %= 12; | |
264 | if (s->cmos_data[RTC_HOURS] & 0x80) { | |
265 | hour += 12; | |
266 | } | |
267 | } | |
268 | return hour; | |
269 | } | |
270 | ||
00cf5774 | 271 | static uint64_t get_next_alarm(RTCState *s) |
56038ef6 | 272 | { |
00cf5774 PB |
273 | int32_t alarm_sec, alarm_min, alarm_hour, cur_hour, cur_min, cur_sec; |
274 | int32_t hour, min, sec; | |
275 | ||
276 | rtc_update_time(s); | |
56038ef6 YZ |
277 | |
278 | alarm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]); | |
279 | alarm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]); | |
280 | alarm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]); | |
00cf5774 | 281 | alarm_hour = alarm_hour == -1 ? -1 : convert_hour(s, alarm_hour); |
56038ef6 YZ |
282 | |
283 | cur_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]); | |
284 | cur_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]); | |
285 | cur_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS]); | |
286 | cur_hour = convert_hour(s, cur_hour); | |
287 | ||
00cf5774 PB |
288 | if (alarm_hour == -1) { |
289 | alarm_hour = cur_hour; | |
290 | if (alarm_min == -1) { | |
291 | alarm_min = cur_min; | |
292 | if (alarm_sec == -1) { | |
293 | alarm_sec = cur_sec + 1; | |
294 | } else if (cur_sec > alarm_sec) { | |
295 | alarm_min++; | |
296 | } | |
297 | } else if (cur_min == alarm_min) { | |
298 | if (alarm_sec == -1) { | |
299 | alarm_sec = cur_sec + 1; | |
300 | } else { | |
301 | if (cur_sec > alarm_sec) { | |
302 | alarm_hour++; | |
303 | } | |
304 | } | |
305 | if (alarm_sec == SEC_PER_MIN) { | |
306 | /* wrap to next hour, minutes is not in don't care mode */ | |
307 | alarm_sec = 0; | |
308 | alarm_hour++; | |
309 | } | |
310 | } else if (cur_min > alarm_min) { | |
311 | alarm_hour++; | |
312 | } | |
313 | } else if (cur_hour == alarm_hour) { | |
314 | if (alarm_min == -1) { | |
315 | alarm_min = cur_min; | |
316 | if (alarm_sec == -1) { | |
317 | alarm_sec = cur_sec + 1; | |
318 | } else if (cur_sec > alarm_sec) { | |
319 | alarm_min++; | |
320 | } | |
321 | ||
322 | if (alarm_sec == SEC_PER_MIN) { | |
323 | alarm_sec = 0; | |
324 | alarm_min++; | |
325 | } | |
326 | /* wrap to next day, hour is not in don't care mode */ | |
327 | alarm_min %= MIN_PER_HOUR; | |
328 | } else if (cur_min == alarm_min) { | |
329 | if (alarm_sec == -1) { | |
330 | alarm_sec = cur_sec + 1; | |
331 | } | |
332 | /* wrap to next day, hours+minutes not in don't care mode */ | |
333 | alarm_sec %= SEC_PER_MIN; | |
334 | } | |
56038ef6 | 335 | } |
56038ef6 | 336 | |
00cf5774 PB |
337 | /* values that are still don't care fire at the next min/sec */ |
338 | if (alarm_min == -1) { | |
339 | alarm_min = 0; | |
340 | } | |
341 | if (alarm_sec == -1) { | |
342 | alarm_sec = 0; | |
343 | } | |
344 | ||
345 | /* keep values in range */ | |
346 | if (alarm_sec == SEC_PER_MIN) { | |
347 | alarm_sec = 0; | |
348 | alarm_min++; | |
349 | } | |
350 | if (alarm_min == MIN_PER_HOUR) { | |
351 | alarm_min = 0; | |
352 | alarm_hour++; | |
353 | } | |
354 | alarm_hour %= HOUR_PER_DAY; | |
355 | ||
356 | hour = alarm_hour - cur_hour; | |
357 | min = hour * MIN_PER_HOUR + alarm_min - cur_min; | |
358 | sec = min * SEC_PER_MIN + alarm_sec - cur_sec; | |
359 | return sec <= 0 ? sec + SEC_PER_DAY : sec; | |
56038ef6 YZ |
360 | } |
361 | ||
362 | static void rtc_update_timer(void *opaque) | |
363 | { | |
364 | RTCState *s = opaque; | |
365 | int32_t irqs = REG_C_UF; | |
366 | int32_t new_irqs; | |
367 | ||
41a9b8b2 YZ |
368 | assert((s->cmos_data[RTC_REG_A] & 0x60) != 0x60); |
369 | ||
56038ef6 YZ |
370 | /* UIP might have been latched, update time and clear it. */ |
371 | rtc_update_time(s); | |
372 | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; | |
373 | ||
884f17c2 | 374 | if (qemu_clock_get_ns(rtc_clock) >= s->next_alarm_time) { |
56038ef6 YZ |
375 | irqs |= REG_C_AF; |
376 | if (s->cmos_data[RTC_REG_B] & REG_B_AIE) { | |
377 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC); | |
378 | } | |
379 | } | |
00cf5774 | 380 | |
56038ef6 YZ |
381 | new_irqs = irqs & ~s->cmos_data[RTC_REG_C]; |
382 | s->cmos_data[RTC_REG_C] |= irqs; | |
383 | if ((new_irqs & s->cmos_data[RTC_REG_B]) != 0) { | |
384 | s->cmos_data[RTC_REG_C] |= REG_C_IRQF; | |
385 | qemu_irq_raise(s->irq); | |
386 | } | |
387 | check_update_timer(s); | |
388 | } | |
389 | ||
0da8c842 AG |
390 | static void cmos_ioport_write(void *opaque, hwaddr addr, |
391 | uint64_t data, unsigned size) | |
80cabfad | 392 | { |
b41a2cd1 | 393 | RTCState *s = opaque; |
80cabfad FB |
394 | |
395 | if ((addr & 1) == 0) { | |
396 | s->cmos_index = data & 0x7f; | |
397 | } else { | |
ec51e364 IY |
398 | CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n", |
399 | s->cmos_index, data); | |
dff38e7b | 400 | switch(s->cmos_index) { |
80cabfad FB |
401 | case RTC_SECONDS_ALARM: |
402 | case RTC_MINUTES_ALARM: | |
403 | case RTC_HOURS_ALARM: | |
80cabfad | 404 | s->cmos_data[s->cmos_index] = data; |
56038ef6 | 405 | check_update_timer(s); |
80cabfad | 406 | break; |
e67edb94 PB |
407 | case RTC_IBM_PS2_CENTURY_BYTE: |
408 | s->cmos_index = RTC_CENTURY; | |
409 | /* fall through */ | |
410 | case RTC_CENTURY: | |
80cabfad FB |
411 | case RTC_SECONDS: |
412 | case RTC_MINUTES: | |
413 | case RTC_HOURS: | |
414 | case RTC_DAY_OF_WEEK: | |
415 | case RTC_DAY_OF_MONTH: | |
416 | case RTC_MONTH: | |
417 | case RTC_YEAR: | |
418 | s->cmos_data[s->cmos_index] = data; | |
dff38e7b | 419 | /* if in set mode, do not update the time */ |
41a9b8b2 | 420 | if (rtc_running(s)) { |
dff38e7b | 421 | rtc_set_time(s); |
56038ef6 | 422 | check_update_timer(s); |
dff38e7b | 423 | } |
80cabfad FB |
424 | break; |
425 | case RTC_REG_A: | |
41a9b8b2 YZ |
426 | if ((data & 0x60) == 0x60) { |
427 | if (rtc_running(s)) { | |
428 | rtc_update_time(s); | |
429 | } | |
430 | /* What happens to UIP when divider reset is enabled is | |
431 | * unclear from the datasheet. Shouldn't matter much | |
432 | * though. | |
433 | */ | |
434 | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; | |
435 | } else if (((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) && | |
436 | (data & 0x70) <= 0x20) { | |
437 | /* when the divider reset is removed, the first update cycle | |
438 | * begins one-half second later*/ | |
439 | if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) { | |
440 | s->offset = 500000000; | |
441 | rtc_set_time(s); | |
442 | } | |
443 | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; | |
444 | } | |
dff38e7b FB |
445 | /* UIP bit is read only */ |
446 | s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) | | |
447 | (s->cmos_data[RTC_REG_A] & REG_A_UIP); | |
884f17c2 | 448 | periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); |
56038ef6 | 449 | check_update_timer(s); |
dff38e7b | 450 | break; |
80cabfad | 451 | case RTC_REG_B: |
dff38e7b | 452 | if (data & REG_B_SET) { |
56038ef6 | 453 | /* update cmos to when the rtc was stopping */ |
41a9b8b2 | 454 | if (rtc_running(s)) { |
56038ef6 YZ |
455 | rtc_update_time(s); |
456 | } | |
dff38e7b FB |
457 | /* set mode: reset UIP mode */ |
458 | s->cmos_data[RTC_REG_A] &= ~REG_A_UIP; | |
459 | data &= ~REG_B_UIE; | |
460 | } else { | |
461 | /* if disabling set mode, update the time */ | |
41a9b8b2 YZ |
462 | if ((s->cmos_data[RTC_REG_B] & REG_B_SET) && |
463 | (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20) { | |
56038ef6 | 464 | s->offset = get_guest_rtc_ns(s) % NSEC_PER_SEC; |
dff38e7b FB |
465 | rtc_set_time(s); |
466 | } | |
467 | } | |
9324cc50 YZ |
468 | /* if an interrupt flag is already set when the interrupt |
469 | * becomes enabled, raise an interrupt immediately. */ | |
470 | if (data & s->cmos_data[RTC_REG_C] & REG_C_MASK) { | |
471 | s->cmos_data[RTC_REG_C] |= REG_C_IRQF; | |
472 | qemu_irq_raise(s->irq); | |
473 | } else { | |
474 | s->cmos_data[RTC_REG_C] &= ~REG_C_IRQF; | |
475 | qemu_irq_lower(s->irq); | |
476 | } | |
bedc572e | 477 | s->cmos_data[RTC_REG_B] = data; |
884f17c2 | 478 | periodic_timer_update(s, qemu_clock_get_ns(rtc_clock)); |
56038ef6 | 479 | check_update_timer(s); |
80cabfad FB |
480 | break; |
481 | case RTC_REG_C: | |
482 | case RTC_REG_D: | |
483 | /* cannot write to them */ | |
484 | break; | |
485 | default: | |
486 | s->cmos_data[s->cmos_index] = data; | |
487 | break; | |
488 | } | |
489 | } | |
490 | } | |
491 | ||
abd0c6bd | 492 | static inline int rtc_to_bcd(RTCState *s, int a) |
80cabfad | 493 | { |
6f1bf24d | 494 | if (s->cmos_data[RTC_REG_B] & REG_B_DM) { |
dff38e7b FB |
495 | return a; |
496 | } else { | |
497 | return ((a / 10) << 4) | (a % 10); | |
498 | } | |
80cabfad FB |
499 | } |
500 | ||
abd0c6bd | 501 | static inline int rtc_from_bcd(RTCState *s, int a) |
80cabfad | 502 | { |
00cf5774 PB |
503 | if ((a & 0xc0) == 0xc0) { |
504 | return -1; | |
505 | } | |
6f1bf24d | 506 | if (s->cmos_data[RTC_REG_B] & REG_B_DM) { |
dff38e7b FB |
507 | return a; |
508 | } else { | |
509 | return ((a >> 4) * 10) + (a & 0x0f); | |
510 | } | |
511 | } | |
512 | ||
e2826cf4 | 513 | static void rtc_get_time(RTCState *s, struct tm *tm) |
dff38e7b | 514 | { |
abd0c6bd PB |
515 | tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]); |
516 | tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]); | |
517 | tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f); | |
3b89eb43 PB |
518 | if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) { |
519 | tm->tm_hour %= 12; | |
520 | if (s->cmos_data[RTC_HOURS] & 0x80) { | |
521 | tm->tm_hour += 12; | |
522 | } | |
43f493af | 523 | } |
abd0c6bd PB |
524 | tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1; |
525 | tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]); | |
526 | tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1; | |
b8994faf PB |
527 | tm->tm_year = |
528 | rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year + | |
529 | rtc_from_bcd(s, s->cmos_data[RTC_CENTURY]) * 100 - 1900; | |
e2826cf4 PB |
530 | } |
531 | ||
532 | static void rtc_set_time(RTCState *s) | |
533 | { | |
534 | struct tm tm; | |
80cd3478 | 535 | |
e2826cf4 | 536 | rtc_get_time(s, &tm); |
e2826cf4 | 537 | s->base_rtc = mktimegm(&tm); |
884f17c2 | 538 | s->last_update = qemu_clock_get_ns(rtc_clock); |
56038ef6 | 539 | |
e2826cf4 | 540 | rtc_change_mon_event(&tm); |
43f493af FB |
541 | } |
542 | ||
e2826cf4 | 543 | static void rtc_set_cmos(RTCState *s, const struct tm *tm) |
43f493af | 544 | { |
42fc73a1 | 545 | int year; |
dff38e7b | 546 | |
abd0c6bd PB |
547 | s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec); |
548 | s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min); | |
c29cd656 | 549 | if (s->cmos_data[RTC_REG_B] & REG_B_24H) { |
43f493af | 550 | /* 24 hour format */ |
abd0c6bd | 551 | s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour); |
43f493af FB |
552 | } else { |
553 | /* 12 hour format */ | |
3b89eb43 PB |
554 | int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12; |
555 | s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h); | |
43f493af FB |
556 | if (tm->tm_hour >= 12) |
557 | s->cmos_data[RTC_HOURS] |= 0x80; | |
558 | } | |
abd0c6bd PB |
559 | s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1); |
560 | s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday); | |
561 | s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1); | |
b8994faf PB |
562 | year = tm->tm_year + 1900 - s->base_year; |
563 | s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year % 100); | |
564 | s->cmos_data[RTC_CENTURY] = rtc_to_bcd(s, year / 100); | |
43f493af FB |
565 | } |
566 | ||
56038ef6 | 567 | static void rtc_update_time(RTCState *s) |
43f493af | 568 | { |
56038ef6 YZ |
569 | struct tm ret; |
570 | time_t guest_sec; | |
571 | int64_t guest_nsec; | |
572 | ||
573 | guest_nsec = get_guest_rtc_ns(s); | |
574 | guest_sec = guest_nsec / NSEC_PER_SEC; | |
575 | gmtime_r(&guest_sec, &ret); | |
02c6ccc6 AH |
576 | |
577 | /* Is SET flag of Register B disabled? */ | |
578 | if ((s->cmos_data[RTC_REG_B] & REG_B_SET) == 0) { | |
579 | rtc_set_cmos(s, &ret); | |
580 | } | |
43f493af FB |
581 | } |
582 | ||
56038ef6 | 583 | static int update_in_progress(RTCState *s) |
43f493af | 584 | { |
56038ef6 | 585 | int64_t guest_nsec; |
3b46e624 | 586 | |
41a9b8b2 | 587 | if (!rtc_running(s)) { |
56038ef6 | 588 | return 0; |
dff38e7b | 589 | } |
e93379b0 AB |
590 | if (timer_pending(s->update_timer)) { |
591 | int64_t next_update_time = timer_expire_time_ns(s->update_timer); | |
56038ef6 | 592 | /* Latch UIP until the timer expires. */ |
884f17c2 AB |
593 | if (qemu_clock_get_ns(rtc_clock) >= |
594 | (next_update_time - UIP_HOLD_LENGTH)) { | |
56038ef6 YZ |
595 | s->cmos_data[RTC_REG_A] |= REG_A_UIP; |
596 | return 1; | |
dff38e7b FB |
597 | } |
598 | } | |
599 | ||
56038ef6 YZ |
600 | guest_nsec = get_guest_rtc_ns(s); |
601 | /* UIP bit will be set at last 244us of every second. */ | |
602 | if ((guest_nsec % NSEC_PER_SEC) >= (NSEC_PER_SEC - UIP_HOLD_LENGTH)) { | |
603 | return 1; | |
dff38e7b | 604 | } |
56038ef6 | 605 | return 0; |
80cabfad FB |
606 | } |
607 | ||
0da8c842 AG |
608 | static uint64_t cmos_ioport_read(void *opaque, hwaddr addr, |
609 | unsigned size) | |
80cabfad | 610 | { |
b41a2cd1 | 611 | RTCState *s = opaque; |
80cabfad FB |
612 | int ret; |
613 | if ((addr & 1) == 0) { | |
614 | return 0xff; | |
615 | } else { | |
616 | switch(s->cmos_index) { | |
e67edb94 PB |
617 | case RTC_IBM_PS2_CENTURY_BYTE: |
618 | s->cmos_index = RTC_CENTURY; | |
619 | /* fall through */ | |
620 | case RTC_CENTURY: | |
80cabfad FB |
621 | case RTC_SECONDS: |
622 | case RTC_MINUTES: | |
623 | case RTC_HOURS: | |
624 | case RTC_DAY_OF_WEEK: | |
625 | case RTC_DAY_OF_MONTH: | |
626 | case RTC_MONTH: | |
627 | case RTC_YEAR: | |
56038ef6 YZ |
628 | /* if not in set mode, calibrate cmos before |
629 | * reading*/ | |
41a9b8b2 | 630 | if (rtc_running(s)) { |
56038ef6 YZ |
631 | rtc_update_time(s); |
632 | } | |
80cabfad FB |
633 | ret = s->cmos_data[s->cmos_index]; |
634 | break; | |
635 | case RTC_REG_A: | |
56038ef6 YZ |
636 | if (update_in_progress(s)) { |
637 | s->cmos_data[s->cmos_index] |= REG_A_UIP; | |
638 | } else { | |
639 | s->cmos_data[s->cmos_index] &= ~REG_A_UIP; | |
640 | } | |
80cabfad | 641 | ret = s->cmos_data[s->cmos_index]; |
80cabfad FB |
642 | break; |
643 | case RTC_REG_C: | |
644 | ret = s->cmos_data[s->cmos_index]; | |
d537cf6c | 645 | qemu_irq_lower(s->irq); |
fbc15e27 | 646 | s->cmos_data[RTC_REG_C] = 0x00; |
56038ef6 YZ |
647 | if (ret & (REG_C_UF | REG_C_AF)) { |
648 | check_update_timer(s); | |
649 | } | |
ba32edab GN |
650 | #ifdef TARGET_I386 |
651 | if(s->irq_coalesced && | |
fbc15e27 | 652 | (s->cmos_data[RTC_REG_B] & REG_B_PIE) && |
ba32edab GN |
653 | s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) { |
654 | s->irq_reinject_on_ack_count++; | |
fbc15e27 | 655 | s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF; |
ba32edab | 656 | apic_reset_irq_delivered(); |
aa6f63ff | 657 | DPRINTF_C("cmos: injecting on ack\n"); |
ba32edab | 658 | qemu_irq_raise(s->irq); |
aa6f63ff | 659 | if (apic_get_irq_delivered()) { |
ba32edab | 660 | s->irq_coalesced--; |
aa6f63ff BS |
661 | DPRINTF_C("cmos: coalesced irqs decreased to %d\n", |
662 | s->irq_coalesced); | |
663 | } | |
ba32edab GN |
664 | } |
665 | #endif | |
80cabfad FB |
666 | break; |
667 | default: | |
668 | ret = s->cmos_data[s->cmos_index]; | |
669 | break; | |
670 | } | |
ec51e364 IY |
671 | CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n", |
672 | s->cmos_index, ret); | |
80cabfad FB |
673 | return ret; |
674 | } | |
675 | } | |
676 | ||
1d914fa0 | 677 | void rtc_set_memory(ISADevice *dev, int addr, int val) |
dff38e7b | 678 | { |
0e41271e | 679 | RTCState *s = MC146818_RTC(dev); |
dff38e7b FB |
680 | if (addr >= 0 && addr <= 127) |
681 | s->cmos_data[addr] = val; | |
682 | } | |
683 | ||
b8b7456d IM |
684 | int rtc_get_memory(ISADevice *dev, int addr) |
685 | { | |
686 | RTCState *s = MC146818_RTC(dev); | |
687 | assert(addr >= 0 && addr <= 127); | |
688 | return s->cmos_data[addr]; | |
689 | } | |
690 | ||
1d914fa0 | 691 | static void rtc_set_date_from_host(ISADevice *dev) |
ea55ffb3 | 692 | { |
0e41271e | 693 | RTCState *s = MC146818_RTC(dev); |
f6503059 | 694 | struct tm tm; |
ea55ffb3 | 695 | |
f6503059 | 696 | qemu_get_timedate(&tm, 0); |
56038ef6 YZ |
697 | |
698 | s->base_rtc = mktimegm(&tm); | |
884f17c2 | 699 | s->last_update = qemu_clock_get_ns(rtc_clock); |
56038ef6 YZ |
700 | s->offset = 0; |
701 | ||
702 | /* set the CMOS date */ | |
e2826cf4 | 703 | rtc_set_cmos(s, &tm); |
ea55ffb3 TS |
704 | } |
705 | ||
6b075b8a | 706 | static int rtc_post_load(void *opaque, int version_id) |
80cabfad | 707 | { |
dff38e7b FB |
708 | RTCState *s = opaque; |
709 | ||
56038ef6 YZ |
710 | if (version_id <= 2) { |
711 | rtc_set_time(s); | |
712 | s->offset = 0; | |
713 | check_update_timer(s); | |
714 | } | |
715 | ||
716 | #ifdef TARGET_I386 | |
048c74c4 | 717 | if (version_id >= 2) { |
433acf0d | 718 | if (s->lost_tick_policy == LOST_TICK_SLEW) { |
048c74c4 JQ |
719 | rtc_coalesced_timer_update(s); |
720 | } | |
048c74c4 | 721 | } |
6b075b8a | 722 | #endif |
73822ec8 AL |
723 | return 0; |
724 | } | |
73822ec8 | 725 | |
6b075b8a JQ |
726 | static const VMStateDescription vmstate_rtc = { |
727 | .name = "mc146818rtc", | |
56038ef6 | 728 | .version_id = 3, |
6b075b8a JQ |
729 | .minimum_version_id = 1, |
730 | .minimum_version_id_old = 1, | |
731 | .post_load = rtc_post_load, | |
732 | .fields = (VMStateField []) { | |
733 | VMSTATE_BUFFER(cmos_data, RTCState), | |
734 | VMSTATE_UINT8(cmos_index, RTCState), | |
89166459 | 735 | VMSTATE_UNUSED(7*4), |
6b075b8a JQ |
736 | VMSTATE_TIMER(periodic_timer, RTCState), |
737 | VMSTATE_INT64(next_periodic_time, RTCState), | |
56038ef6 | 738 | VMSTATE_UNUSED(3*8), |
6b075b8a JQ |
739 | VMSTATE_UINT32_V(irq_coalesced, RTCState, 2), |
740 | VMSTATE_UINT32_V(period, RTCState, 2), | |
56038ef6 YZ |
741 | VMSTATE_UINT64_V(base_rtc, RTCState, 3), |
742 | VMSTATE_UINT64_V(last_update, RTCState, 3), | |
743 | VMSTATE_INT64_V(offset, RTCState, 3), | |
744 | VMSTATE_TIMER_V(update_timer, RTCState, 3), | |
00cf5774 | 745 | VMSTATE_UINT64_V(next_alarm_time, RTCState, 3), |
6b075b8a JQ |
746 | VMSTATE_END_OF_LIST() |
747 | } | |
748 | }; | |
749 | ||
17604dac JK |
750 | static void rtc_notify_clock_reset(Notifier *notifier, void *data) |
751 | { | |
752 | RTCState *s = container_of(notifier, RTCState, clock_reset_notifier); | |
753 | int64_t now = *(int64_t *)data; | |
754 | ||
0e41271e | 755 | rtc_set_date_from_host(ISA_DEVICE(s)); |
c4c18e24 | 756 | periodic_timer_update(s, now); |
56038ef6 | 757 | check_update_timer(s); |
17604dac | 758 | #ifdef TARGET_I386 |
433acf0d | 759 | if (s->lost_tick_policy == LOST_TICK_SLEW) { |
17604dac JK |
760 | rtc_coalesced_timer_update(s); |
761 | } | |
762 | #endif | |
763 | } | |
764 | ||
da98c8eb GH |
765 | /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE) |
766 | BIOS will read it and start S3 resume at POST Entry */ | |
767 | static void rtc_notify_suspend(Notifier *notifier, void *data) | |
768 | { | |
769 | RTCState *s = container_of(notifier, RTCState, suspend_notifier); | |
0e41271e | 770 | rtc_set_memory(ISA_DEVICE(s), 0xF, 0xFE); |
da98c8eb GH |
771 | } |
772 | ||
eeb7c03c GN |
773 | static void rtc_reset(void *opaque) |
774 | { | |
775 | RTCState *s = opaque; | |
776 | ||
72716184 AL |
777 | s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE); |
778 | s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF); | |
56038ef6 | 779 | check_update_timer(s); |
eeb7c03c | 780 | |
72716184 | 781 | qemu_irq_lower(s->irq); |
eeb7c03c GN |
782 | |
783 | #ifdef TARGET_I386 | |
433acf0d JK |
784 | if (s->lost_tick_policy == LOST_TICK_SLEW) { |
785 | s->irq_coalesced = 0; | |
786 | } | |
eeb7c03c GN |
787 | #endif |
788 | } | |
789 | ||
b2c5009b | 790 | static const MemoryRegionOps cmos_ops = { |
0da8c842 AG |
791 | .read = cmos_ioport_read, |
792 | .write = cmos_ioport_write, | |
793 | .impl = { | |
794 | .min_access_size = 1, | |
795 | .max_access_size = 1, | |
796 | }, | |
797 | .endianness = DEVICE_LITTLE_ENDIAN, | |
b2c5009b RH |
798 | }; |
799 | ||
57c9fafe | 800 | static void rtc_get_date(Object *obj, Visitor *v, void *opaque, |
18297050 AL |
801 | const char *name, Error **errp) |
802 | { | |
0e41271e | 803 | RTCState *s = MC146818_RTC(obj); |
e2826cf4 | 804 | struct tm current_tm; |
18297050 | 805 | |
56038ef6 | 806 | rtc_update_time(s); |
e2826cf4 | 807 | rtc_get_time(s, ¤t_tm); |
18297050 | 808 | visit_start_struct(v, NULL, "struct tm", name, 0, errp); |
e2826cf4 PB |
809 | visit_type_int32(v, ¤t_tm.tm_year, "tm_year", errp); |
810 | visit_type_int32(v, ¤t_tm.tm_mon, "tm_mon", errp); | |
811 | visit_type_int32(v, ¤t_tm.tm_mday, "tm_mday", errp); | |
812 | visit_type_int32(v, ¤t_tm.tm_hour, "tm_hour", errp); | |
813 | visit_type_int32(v, ¤t_tm.tm_min, "tm_min", errp); | |
814 | visit_type_int32(v, ¤t_tm.tm_sec, "tm_sec", errp); | |
18297050 AL |
815 | visit_end_struct(v, errp); |
816 | } | |
817 | ||
db895a1e | 818 | static void rtc_realizefn(DeviceState *dev, Error **errp) |
dff38e7b | 819 | { |
db895a1e | 820 | ISADevice *isadev = ISA_DEVICE(dev); |
0e41271e | 821 | RTCState *s = MC146818_RTC(dev); |
32e0c826 | 822 | int base = 0x70; |
80cabfad | 823 | |
80cabfad FB |
824 | s->cmos_data[RTC_REG_A] = 0x26; |
825 | s->cmos_data[RTC_REG_B] = 0x02; | |
826 | s->cmos_data[RTC_REG_C] = 0x00; | |
827 | s->cmos_data[RTC_REG_D] = 0x80; | |
828 | ||
b8994faf PB |
829 | /* This is for historical reasons. The default base year qdev property |
830 | * was set to 2000 for most machine types before the century byte was | |
831 | * implemented. | |
832 | * | |
833 | * This if statement means that the century byte will be always 0 | |
834 | * (at least until 2079...) for base_year = 1980, but will be set | |
835 | * correctly for base_year = 2000. | |
836 | */ | |
837 | if (s->base_year == 2000) { | |
838 | s->base_year = 0; | |
839 | } | |
840 | ||
db895a1e | 841 | rtc_set_date_from_host(isadev); |
ea55ffb3 | 842 | |
93b66569 | 843 | #ifdef TARGET_I386 |
433acf0d JK |
844 | switch (s->lost_tick_policy) { |
845 | case LOST_TICK_SLEW: | |
6875204c | 846 | s->coalesced_timer = |
884f17c2 | 847 | timer_new_ns(rtc_clock, rtc_coalesced_timer, s); |
433acf0d JK |
848 | break; |
849 | case LOST_TICK_DISCARD: | |
850 | break; | |
851 | default: | |
db895a1e AF |
852 | error_setg(errp, "Invalid lost tick policy."); |
853 | return; | |
433acf0d | 854 | } |
93b66569 | 855 | #endif |
433acf0d | 856 | |
884f17c2 AB |
857 | s->periodic_timer = timer_new_ns(rtc_clock, rtc_periodic_timer, s); |
858 | s->update_timer = timer_new_ns(rtc_clock, rtc_update_timer, s); | |
56038ef6 | 859 | check_update_timer(s); |
dff38e7b | 860 | |
17604dac | 861 | s->clock_reset_notifier.notify = rtc_notify_clock_reset; |
884f17c2 AB |
862 | qemu_clock_register_reset_notifier(QEMU_CLOCK_REALTIME, |
863 | &s->clock_reset_notifier); | |
17604dac | 864 | |
da98c8eb GH |
865 | s->suspend_notifier.notify = rtc_notify_suspend; |
866 | qemu_register_suspend_notifier(&s->suspend_notifier); | |
867 | ||
853dca12 | 868 | memory_region_init_io(&s->io, OBJECT(s), &cmos_ops, s, "rtc", 2); |
db895a1e | 869 | isa_register_ioport(isadev, &s->io, base); |
dff38e7b | 870 | |
db895a1e | 871 | qdev_set_legacy_instance_id(dev, base, 3); |
a08d4367 | 872 | qemu_register_reset(rtc_reset, s); |
18297050 | 873 | |
57c9fafe AL |
874 | object_property_add(OBJECT(s), "date", "struct tm", |
875 | rtc_get_date, NULL, NULL, s, NULL); | |
32e0c826 GH |
876 | } |
877 | ||
48a18b3c | 878 | ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq) |
32e0c826 | 879 | { |
0e41271e AF |
880 | DeviceState *dev; |
881 | ISADevice *isadev; | |
7d932dfd | 882 | RTCState *s; |
eeb7c03c | 883 | |
0e41271e AF |
884 | isadev = isa_create(bus, TYPE_MC146818_RTC); |
885 | dev = DEVICE(isadev); | |
886 | s = MC146818_RTC(isadev); | |
887 | qdev_prop_set_int32(dev, "base_year", base_year); | |
888 | qdev_init_nofail(dev); | |
7d932dfd JK |
889 | if (intercept_irq) { |
890 | s->irq = intercept_irq; | |
891 | } else { | |
0e41271e | 892 | isa_init_irq(isadev, &s->irq, RTC_ISA_IRQ); |
7d932dfd | 893 | } |
0e41271e | 894 | return isadev; |
80cabfad FB |
895 | } |
896 | ||
39bffca2 AL |
897 | static Property mc146818rtc_properties[] = { |
898 | DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980), | |
899 | DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState, | |
900 | lost_tick_policy, LOST_TICK_DISCARD), | |
901 | DEFINE_PROP_END_OF_LIST(), | |
902 | }; | |
903 | ||
8f04ee08 AL |
904 | static void rtc_class_initfn(ObjectClass *klass, void *data) |
905 | { | |
39bffca2 | 906 | DeviceClass *dc = DEVICE_CLASS(klass); |
db895a1e AF |
907 | |
908 | dc->realize = rtc_realizefn; | |
39bffca2 AL |
909 | dc->no_user = 1; |
910 | dc->vmsd = &vmstate_rtc; | |
911 | dc->props = mc146818rtc_properties; | |
8f04ee08 AL |
912 | } |
913 | ||
8c43a6f0 | 914 | static const TypeInfo mc146818rtc_info = { |
0e41271e | 915 | .name = TYPE_MC146818_RTC, |
39bffca2 AL |
916 | .parent = TYPE_ISA_DEVICE, |
917 | .instance_size = sizeof(RTCState), | |
918 | .class_init = rtc_class_initfn, | |
32e0c826 GH |
919 | }; |
920 | ||
83f7d43a | 921 | static void mc146818rtc_register_types(void) |
100d9891 | 922 | { |
39bffca2 | 923 | type_register_static(&mc146818rtc_info); |
100d9891 | 924 | } |
83f7d43a AF |
925 | |
926 | type_init(mc146818rtc_register_types) |