2 * QEMU MC146818 RTC emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/timer.h"
26 #include "sysemu/sysemu.h"
27 #include "hw/timer/mc146818rtc.h"
28 #include "qapi/visitor.h"
29 #include "qapi-event.h"
32 #include "hw/i386/apic.h"
36 //#define DEBUG_COALESCED
39 # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
41 # define CMOS_DPRINTF(format, ...) do { } while (0)
44 #ifdef DEBUG_COALESCED
45 # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
47 # define DPRINTF_C(format, ...) do { } while (0)
50 #define NSEC_PER_SEC 1000000000LL
51 #define SEC_PER_MIN 60
52 #define MIN_PER_HOUR 60
53 #define SEC_PER_HOUR 3600
54 #define HOUR_PER_DAY 24
55 #define SEC_PER_DAY 86400
57 #define RTC_REINJECT_ON_ACK_COUNT 20
58 #define RTC_CLOCK_RATE 32768
59 #define UIP_HOLD_LENGTH (8 * NSEC_PER_SEC / 32768)
61 #define MC146818_RTC(obj) OBJECT_CHECK(RTCState, (obj), TYPE_MC146818_RTC)
63 typedef struct RTCState {
67 uint8_t cmos_data[128];
76 QEMUTimer *periodic_timer;
77 int64_t next_periodic_time;
78 /* update-ended timer */
79 QEMUTimer *update_timer;
80 uint64_t next_alarm_time;
81 uint16_t irq_reinject_on_ack_count;
82 uint32_t irq_coalesced;
84 QEMUTimer *coalesced_timer;
85 Notifier clock_reset_notifier;
86 LostTickPolicy lost_tick_policy;
87 Notifier suspend_notifier;
90 static void rtc_set_time(RTCState *s);
91 static void rtc_update_time(RTCState *s);
92 static void rtc_set_cmos(RTCState *s, const struct tm *tm);
93 static inline int rtc_from_bcd(RTCState *s, int a);
94 static uint64_t get_next_alarm(RTCState *s);
96 static inline bool rtc_running(RTCState *s)
98 return (!(s->cmos_data[RTC_REG_B] & REG_B_SET) &&
99 (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20);
102 static uint64_t get_guest_rtc_ns(RTCState *s)
105 uint64_t guest_clock = qemu_clock_get_ns(rtc_clock);
107 guest_rtc = s->base_rtc * NSEC_PER_SEC
108 + guest_clock - s->last_update + s->offset;
113 static void rtc_coalesced_timer_update(RTCState *s)
115 if (s->irq_coalesced == 0) {
116 timer_del(s->coalesced_timer);
118 /* divide each RTC interval to 2 - 8 smaller intervals */
119 int c = MIN(s->irq_coalesced, 7) + 1;
120 int64_t next_clock = qemu_clock_get_ns(rtc_clock) +
121 muldiv64(s->period / c, get_ticks_per_sec(), RTC_CLOCK_RATE);
122 timer_mod(s->coalesced_timer, next_clock);
126 static void rtc_coalesced_timer(void *opaque)
128 RTCState *s = opaque;
130 if (s->irq_coalesced != 0) {
131 apic_reset_irq_delivered();
132 s->cmos_data[RTC_REG_C] |= 0xc0;
133 DPRINTF_C("cmos: injecting from timer\n");
134 qemu_irq_raise(s->irq);
135 if (apic_get_irq_delivered()) {
137 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
142 rtc_coalesced_timer_update(s);
146 /* handle periodic timer */
147 static void periodic_timer_update(RTCState *s, int64_t current_time)
149 int period_code, period;
150 int64_t cur_clock, next_irq_clock;
152 period_code = s->cmos_data[RTC_REG_A] & 0x0f;
154 && (s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
155 if (period_code <= 2)
157 /* period in 32 Khz cycles */
158 period = 1 << (period_code - 1);
160 if (period != s->period) {
161 s->irq_coalesced = (s->irq_coalesced * s->period) / period;
162 DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s->irq_coalesced);
166 /* compute 32 khz clock */
167 cur_clock = muldiv64(current_time, RTC_CLOCK_RATE, get_ticks_per_sec());
168 next_irq_clock = (cur_clock & ~(period - 1)) + period;
169 s->next_periodic_time =
170 muldiv64(next_irq_clock, get_ticks_per_sec(), RTC_CLOCK_RATE) + 1;
171 timer_mod(s->periodic_timer, s->next_periodic_time);
174 s->irq_coalesced = 0;
176 timer_del(s->periodic_timer);
180 static void rtc_periodic_timer(void *opaque)
182 RTCState *s = opaque;
184 periodic_timer_update(s, s->next_periodic_time);
185 s->cmos_data[RTC_REG_C] |= REG_C_PF;
186 if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
187 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
189 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
190 if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT)
191 s->irq_reinject_on_ack_count = 0;
192 apic_reset_irq_delivered();
193 qemu_irq_raise(s->irq);
194 if (!apic_get_irq_delivered()) {
196 rtc_coalesced_timer_update(s);
197 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
202 qemu_irq_raise(s->irq);
206 /* handle update-ended timer */
207 static void check_update_timer(RTCState *s)
209 uint64_t next_update_time;
213 /* From the data sheet: "Holding the dividers in reset prevents
214 * interrupts from operating, while setting the SET bit allows"
215 * them to occur. However, it will prevent an alarm interrupt
216 * from occurring, because the time of day is not updated.
218 if ((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) {
219 timer_del(s->update_timer);
222 if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
223 (s->cmos_data[RTC_REG_B] & REG_B_SET)) {
224 timer_del(s->update_timer);
227 if ((s->cmos_data[RTC_REG_C] & REG_C_UF) &&
228 (s->cmos_data[RTC_REG_C] & REG_C_AF)) {
229 timer_del(s->update_timer);
233 guest_nsec = get_guest_rtc_ns(s) % NSEC_PER_SEC;
234 /* if UF is clear, reprogram to next second */
235 next_update_time = qemu_clock_get_ns(rtc_clock)
236 + NSEC_PER_SEC - guest_nsec;
238 /* Compute time of next alarm. One second is already accounted
239 * for in next_update_time.
241 next_alarm_sec = get_next_alarm(s);
242 s->next_alarm_time = next_update_time + (next_alarm_sec - 1) * NSEC_PER_SEC;
244 if (s->cmos_data[RTC_REG_C] & REG_C_UF) {
245 /* UF is set, but AF is clear. Program the timer to target
247 next_update_time = s->next_alarm_time;
249 if (next_update_time != timer_expire_time_ns(s->update_timer)) {
250 timer_mod(s->update_timer, next_update_time);
254 static inline uint8_t convert_hour(RTCState *s, uint8_t hour)
256 if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
258 if (s->cmos_data[RTC_HOURS] & 0x80) {
265 static uint64_t get_next_alarm(RTCState *s)
267 int32_t alarm_sec, alarm_min, alarm_hour, cur_hour, cur_min, cur_sec;
268 int32_t hour, min, sec;
272 alarm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]);
273 alarm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]);
274 alarm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]);
275 alarm_hour = alarm_hour == -1 ? -1 : convert_hour(s, alarm_hour);
277 cur_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
278 cur_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
279 cur_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS]);
280 cur_hour = convert_hour(s, cur_hour);
282 if (alarm_hour == -1) {
283 alarm_hour = cur_hour;
284 if (alarm_min == -1) {
286 if (alarm_sec == -1) {
287 alarm_sec = cur_sec + 1;
288 } else if (cur_sec > alarm_sec) {
291 } else if (cur_min == alarm_min) {
292 if (alarm_sec == -1) {
293 alarm_sec = cur_sec + 1;
295 if (cur_sec > alarm_sec) {
299 if (alarm_sec == SEC_PER_MIN) {
300 /* wrap to next hour, minutes is not in don't care mode */
304 } else if (cur_min > alarm_min) {
307 } else if (cur_hour == alarm_hour) {
308 if (alarm_min == -1) {
310 if (alarm_sec == -1) {
311 alarm_sec = cur_sec + 1;
312 } else if (cur_sec > alarm_sec) {
316 if (alarm_sec == SEC_PER_MIN) {
320 /* wrap to next day, hour is not in don't care mode */
321 alarm_min %= MIN_PER_HOUR;
322 } else if (cur_min == alarm_min) {
323 if (alarm_sec == -1) {
324 alarm_sec = cur_sec + 1;
326 /* wrap to next day, hours+minutes not in don't care mode */
327 alarm_sec %= SEC_PER_MIN;
331 /* values that are still don't care fire at the next min/sec */
332 if (alarm_min == -1) {
335 if (alarm_sec == -1) {
339 /* keep values in range */
340 if (alarm_sec == SEC_PER_MIN) {
344 if (alarm_min == MIN_PER_HOUR) {
348 alarm_hour %= HOUR_PER_DAY;
350 hour = alarm_hour - cur_hour;
351 min = hour * MIN_PER_HOUR + alarm_min - cur_min;
352 sec = min * SEC_PER_MIN + alarm_sec - cur_sec;
353 return sec <= 0 ? sec + SEC_PER_DAY : sec;
356 static void rtc_update_timer(void *opaque)
358 RTCState *s = opaque;
359 int32_t irqs = REG_C_UF;
362 assert((s->cmos_data[RTC_REG_A] & 0x60) != 0x60);
364 /* UIP might have been latched, update time and clear it. */
366 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
368 if (qemu_clock_get_ns(rtc_clock) >= s->next_alarm_time) {
370 if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
371 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC);
375 new_irqs = irqs & ~s->cmos_data[RTC_REG_C];
376 s->cmos_data[RTC_REG_C] |= irqs;
377 if ((new_irqs & s->cmos_data[RTC_REG_B]) != 0) {
378 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
379 qemu_irq_raise(s->irq);
381 check_update_timer(s);
384 static void cmos_ioport_write(void *opaque, hwaddr addr,
385 uint64_t data, unsigned size)
387 RTCState *s = opaque;
389 if ((addr & 1) == 0) {
390 s->cmos_index = data & 0x7f;
392 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n",
393 s->cmos_index, data);
394 switch(s->cmos_index) {
395 case RTC_SECONDS_ALARM:
396 case RTC_MINUTES_ALARM:
397 case RTC_HOURS_ALARM:
398 s->cmos_data[s->cmos_index] = data;
399 check_update_timer(s);
401 case RTC_IBM_PS2_CENTURY_BYTE:
402 s->cmos_index = RTC_CENTURY;
408 case RTC_DAY_OF_WEEK:
409 case RTC_DAY_OF_MONTH:
412 s->cmos_data[s->cmos_index] = data;
413 /* if in set mode, do not update the time */
414 if (rtc_running(s)) {
416 check_update_timer(s);
420 if ((data & 0x60) == 0x60) {
421 if (rtc_running(s)) {
424 /* What happens to UIP when divider reset is enabled is
425 * unclear from the datasheet. Shouldn't matter much
428 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
429 } else if (((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) &&
430 (data & 0x70) <= 0x20) {
431 /* when the divider reset is removed, the first update cycle
432 * begins one-half second later*/
433 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
434 s->offset = 500000000;
437 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
439 /* UIP bit is read only */
440 s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
441 (s->cmos_data[RTC_REG_A] & REG_A_UIP);
442 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock));
443 check_update_timer(s);
446 if (data & REG_B_SET) {
447 /* update cmos to when the rtc was stopping */
448 if (rtc_running(s)) {
451 /* set mode: reset UIP mode */
452 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
455 /* if disabling set mode, update the time */
456 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) &&
457 (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20) {
458 s->offset = get_guest_rtc_ns(s) % NSEC_PER_SEC;
462 /* if an interrupt flag is already set when the interrupt
463 * becomes enabled, raise an interrupt immediately. */
464 if (data & s->cmos_data[RTC_REG_C] & REG_C_MASK) {
465 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
466 qemu_irq_raise(s->irq);
468 s->cmos_data[RTC_REG_C] &= ~REG_C_IRQF;
469 qemu_irq_lower(s->irq);
471 s->cmos_data[RTC_REG_B] = data;
472 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock));
473 check_update_timer(s);
477 /* cannot write to them */
480 s->cmos_data[s->cmos_index] = data;
486 static inline int rtc_to_bcd(RTCState *s, int a)
488 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
491 return ((a / 10) << 4) | (a % 10);
495 static inline int rtc_from_bcd(RTCState *s, int a)
497 if ((a & 0xc0) == 0xc0) {
500 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
503 return ((a >> 4) * 10) + (a & 0x0f);
507 static void rtc_get_time(RTCState *s, struct tm *tm)
509 tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
510 tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
511 tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
512 if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
514 if (s->cmos_data[RTC_HOURS] & 0x80) {
518 tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
519 tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
520 tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
522 rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year +
523 rtc_from_bcd(s, s->cmos_data[RTC_CENTURY]) * 100 - 1900;
526 static void rtc_set_time(RTCState *s)
530 rtc_get_time(s, &tm);
531 s->base_rtc = mktimegm(&tm);
532 s->last_update = qemu_clock_get_ns(rtc_clock);
534 qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort);
537 static void rtc_set_cmos(RTCState *s, const struct tm *tm)
541 s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec);
542 s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min);
543 if (s->cmos_data[RTC_REG_B] & REG_B_24H) {
545 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
548 int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12;
549 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h);
550 if (tm->tm_hour >= 12)
551 s->cmos_data[RTC_HOURS] |= 0x80;
553 s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
554 s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday);
555 s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
556 year = tm->tm_year + 1900 - s->base_year;
557 s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year % 100);
558 s->cmos_data[RTC_CENTURY] = rtc_to_bcd(s, year / 100);
561 static void rtc_update_time(RTCState *s)
567 guest_nsec = get_guest_rtc_ns(s);
568 guest_sec = guest_nsec / NSEC_PER_SEC;
569 gmtime_r(&guest_sec, &ret);
571 /* Is SET flag of Register B disabled? */
572 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) == 0) {
573 rtc_set_cmos(s, &ret);
577 static int update_in_progress(RTCState *s)
581 if (!rtc_running(s)) {
584 if (timer_pending(s->update_timer)) {
585 int64_t next_update_time = timer_expire_time_ns(s->update_timer);
586 /* Latch UIP until the timer expires. */
587 if (qemu_clock_get_ns(rtc_clock) >=
588 (next_update_time - UIP_HOLD_LENGTH)) {
589 s->cmos_data[RTC_REG_A] |= REG_A_UIP;
594 guest_nsec = get_guest_rtc_ns(s);
595 /* UIP bit will be set at last 244us of every second. */
596 if ((guest_nsec % NSEC_PER_SEC) >= (NSEC_PER_SEC - UIP_HOLD_LENGTH)) {
602 static uint64_t cmos_ioport_read(void *opaque, hwaddr addr,
605 RTCState *s = opaque;
607 if ((addr & 1) == 0) {
610 switch(s->cmos_index) {
611 case RTC_IBM_PS2_CENTURY_BYTE:
612 s->cmos_index = RTC_CENTURY;
618 case RTC_DAY_OF_WEEK:
619 case RTC_DAY_OF_MONTH:
622 /* if not in set mode, calibrate cmos before
624 if (rtc_running(s)) {
627 ret = s->cmos_data[s->cmos_index];
630 if (update_in_progress(s)) {
631 s->cmos_data[s->cmos_index] |= REG_A_UIP;
633 s->cmos_data[s->cmos_index] &= ~REG_A_UIP;
635 ret = s->cmos_data[s->cmos_index];
638 ret = s->cmos_data[s->cmos_index];
639 qemu_irq_lower(s->irq);
640 s->cmos_data[RTC_REG_C] = 0x00;
641 if (ret & (REG_C_UF | REG_C_AF)) {
642 check_update_timer(s);
645 if(s->irq_coalesced &&
646 (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
647 s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
648 s->irq_reinject_on_ack_count++;
649 s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF;
650 apic_reset_irq_delivered();
651 DPRINTF_C("cmos: injecting on ack\n");
652 qemu_irq_raise(s->irq);
653 if (apic_get_irq_delivered()) {
655 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
662 ret = s->cmos_data[s->cmos_index];
665 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
671 void rtc_set_memory(ISADevice *dev, int addr, int val)
673 RTCState *s = MC146818_RTC(dev);
674 if (addr >= 0 && addr <= 127)
675 s->cmos_data[addr] = val;
678 int rtc_get_memory(ISADevice *dev, int addr)
680 RTCState *s = MC146818_RTC(dev);
681 assert(addr >= 0 && addr <= 127);
682 return s->cmos_data[addr];
685 static void rtc_set_date_from_host(ISADevice *dev)
687 RTCState *s = MC146818_RTC(dev);
690 qemu_get_timedate(&tm, 0);
692 s->base_rtc = mktimegm(&tm);
693 s->last_update = qemu_clock_get_ns(rtc_clock);
696 /* set the CMOS date */
697 rtc_set_cmos(s, &tm);
700 static int rtc_post_load(void *opaque, int version_id)
702 RTCState *s = opaque;
704 if (version_id <= 2) {
707 check_update_timer(s);
711 if (version_id >= 2) {
712 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
713 rtc_coalesced_timer_update(s);
720 static const VMStateDescription vmstate_rtc = {
721 .name = "mc146818rtc",
723 .minimum_version_id = 1,
724 .post_load = rtc_post_load,
725 .fields = (VMStateField[]) {
726 VMSTATE_BUFFER(cmos_data, RTCState),
727 VMSTATE_UINT8(cmos_index, RTCState),
729 VMSTATE_TIMER(periodic_timer, RTCState),
730 VMSTATE_INT64(next_periodic_time, RTCState),
732 VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
733 VMSTATE_UINT32_V(period, RTCState, 2),
734 VMSTATE_UINT64_V(base_rtc, RTCState, 3),
735 VMSTATE_UINT64_V(last_update, RTCState, 3),
736 VMSTATE_INT64_V(offset, RTCState, 3),
737 VMSTATE_TIMER_V(update_timer, RTCState, 3),
738 VMSTATE_UINT64_V(next_alarm_time, RTCState, 3),
739 VMSTATE_END_OF_LIST()
743 static void rtc_notify_clock_reset(Notifier *notifier, void *data)
745 RTCState *s = container_of(notifier, RTCState, clock_reset_notifier);
746 int64_t now = *(int64_t *)data;
748 rtc_set_date_from_host(ISA_DEVICE(s));
749 periodic_timer_update(s, now);
750 check_update_timer(s);
752 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
753 rtc_coalesced_timer_update(s);
758 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
759 BIOS will read it and start S3 resume at POST Entry */
760 static void rtc_notify_suspend(Notifier *notifier, void *data)
762 RTCState *s = container_of(notifier, RTCState, suspend_notifier);
763 rtc_set_memory(ISA_DEVICE(s), 0xF, 0xFE);
766 static void rtc_reset(void *opaque)
768 RTCState *s = opaque;
770 s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
771 s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
772 check_update_timer(s);
774 qemu_irq_lower(s->irq);
777 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
778 s->irq_coalesced = 0;
783 static const MemoryRegionOps cmos_ops = {
784 .read = cmos_ioport_read,
785 .write = cmos_ioport_write,
787 .min_access_size = 1,
788 .max_access_size = 1,
790 .endianness = DEVICE_LITTLE_ENDIAN,
793 static void rtc_get_date(Object *obj, Visitor *v, void *opaque,
794 const char *name, Error **errp)
797 RTCState *s = MC146818_RTC(obj);
798 struct tm current_tm;
801 rtc_get_time(s, ¤t_tm);
802 visit_start_struct(v, NULL, "struct tm", name, 0, &err);
806 visit_type_int32(v, ¤t_tm.tm_year, "tm_year", &err);
810 visit_type_int32(v, ¤t_tm.tm_mon, "tm_mon", &err);
814 visit_type_int32(v, ¤t_tm.tm_mday, "tm_mday", &err);
818 visit_type_int32(v, ¤t_tm.tm_hour, "tm_hour", &err);
822 visit_type_int32(v, ¤t_tm.tm_min, "tm_min", &err);
826 visit_type_int32(v, ¤t_tm.tm_sec, "tm_sec", &err);
831 error_propagate(errp, err);
833 visit_end_struct(v, errp);
835 error_propagate(errp, err);
838 static void rtc_realizefn(DeviceState *dev, Error **errp)
840 ISADevice *isadev = ISA_DEVICE(dev);
841 RTCState *s = MC146818_RTC(dev);
844 s->cmos_data[RTC_REG_A] = 0x26;
845 s->cmos_data[RTC_REG_B] = 0x02;
846 s->cmos_data[RTC_REG_C] = 0x00;
847 s->cmos_data[RTC_REG_D] = 0x80;
849 /* This is for historical reasons. The default base year qdev property
850 * was set to 2000 for most machine types before the century byte was
853 * This if statement means that the century byte will be always 0
854 * (at least until 2079...) for base_year = 1980, but will be set
855 * correctly for base_year = 2000.
857 if (s->base_year == 2000) {
861 rtc_set_date_from_host(isadev);
864 switch (s->lost_tick_policy) {
865 case LOST_TICK_POLICY_SLEW:
867 timer_new_ns(rtc_clock, rtc_coalesced_timer, s);
869 case LOST_TICK_POLICY_DISCARD:
872 error_setg(errp, "Invalid lost tick policy.");
877 s->periodic_timer = timer_new_ns(rtc_clock, rtc_periodic_timer, s);
878 s->update_timer = timer_new_ns(rtc_clock, rtc_update_timer, s);
879 check_update_timer(s);
881 s->clock_reset_notifier.notify = rtc_notify_clock_reset;
882 qemu_clock_register_reset_notifier(QEMU_CLOCK_REALTIME,
883 &s->clock_reset_notifier);
885 s->suspend_notifier.notify = rtc_notify_suspend;
886 qemu_register_suspend_notifier(&s->suspend_notifier);
888 memory_region_init_io(&s->io, OBJECT(s), &cmos_ops, s, "rtc", 2);
889 isa_register_ioport(isadev, &s->io, base);
891 qdev_set_legacy_instance_id(dev, base, 3);
892 qemu_register_reset(rtc_reset, s);
894 object_property_add(OBJECT(s), "date", "struct tm",
895 rtc_get_date, NULL, NULL, s, NULL);
898 ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq)
904 isadev = isa_create(bus, TYPE_MC146818_RTC);
905 dev = DEVICE(isadev);
906 s = MC146818_RTC(isadev);
907 qdev_prop_set_int32(dev, "base_year", base_year);
908 qdev_init_nofail(dev);
910 s->irq = intercept_irq;
912 isa_init_irq(isadev, &s->irq, RTC_ISA_IRQ);
917 static Property mc146818rtc_properties[] = {
918 DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
919 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState,
920 lost_tick_policy, LOST_TICK_POLICY_DISCARD),
921 DEFINE_PROP_END_OF_LIST(),
924 static void rtc_class_initfn(ObjectClass *klass, void *data)
926 DeviceClass *dc = DEVICE_CLASS(klass);
928 dc->realize = rtc_realizefn;
929 dc->vmsd = &vmstate_rtc;
930 dc->props = mc146818rtc_properties;
931 /* Reason: needs to be wired up by rtc_init() */
932 dc->cannot_instantiate_with_device_add_yet = true;
935 static const TypeInfo mc146818rtc_info = {
936 .name = TYPE_MC146818_RTC,
937 .parent = TYPE_ISA_DEVICE,
938 .instance_size = sizeof(RTCState),
939 .class_init = rtc_class_initfn,
942 static void mc146818rtc_register_types(void)
944 type_register_static(&mc146818rtc_info);
947 type_init(mc146818rtc_register_types)