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