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"
30 #include "mc146818rtc.h"
33 //#define DEBUG_COALESCED
36 # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
38 # define CMOS_DPRINTF(format, ...) do { } while (0)
41 #ifdef DEBUG_COALESCED
42 # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
44 # define DPRINTF_C(format, ...) do { } while (0)
47 #define RTC_REINJECT_ON_ACK_COUNT 20
50 #define RTC_SECONDS_ALARM 1
52 #define RTC_MINUTES_ALARM 3
54 #define RTC_HOURS_ALARM 5
55 #define RTC_ALARM_DONT_CARE 0xC0
57 #define RTC_DAY_OF_WEEK 6
58 #define RTC_DAY_OF_MONTH 7
67 #define REG_A_UIP 0x80
69 #define REG_B_SET 0x80
70 #define REG_B_PIE 0x40
71 #define REG_B_AIE 0x20
72 #define REG_B_UIE 0x10
73 #define REG_B_SQWE 0x08
75 #define REG_B_24H 0x02
78 #define REG_C_IRQF 0x80
82 typedef struct RTCState {
85 uint8_t cmos_data[128];
93 QEMUTimer *periodic_timer;
94 int64_t next_periodic_time;
96 int64_t next_second_time;
97 uint16_t irq_reinject_on_ack_count;
98 uint32_t irq_coalesced;
100 QEMUTimer *coalesced_timer;
101 QEMUTimer *second_timer;
102 QEMUTimer *second_timer2;
103 Notifier clock_reset_notifier;
104 LostTickPolicy lost_tick_policy;
107 static void rtc_set_time(RTCState *s);
108 static void rtc_copy_date(RTCState *s);
111 static void rtc_coalesced_timer_update(RTCState *s)
113 if (s->irq_coalesced == 0) {
114 qemu_del_timer(s->coalesced_timer);
116 /* divide each RTC interval to 2 - 8 smaller intervals */
117 int c = MIN(s->irq_coalesced, 7) + 1;
118 int64_t next_clock = qemu_get_clock_ns(rtc_clock) +
119 muldiv64(s->period / c, get_ticks_per_sec(), 32768);
120 qemu_mod_timer(s->coalesced_timer, next_clock);
124 static void rtc_coalesced_timer(void *opaque)
126 RTCState *s = opaque;
128 if (s->irq_coalesced != 0) {
129 apic_reset_irq_delivered();
130 s->cmos_data[RTC_REG_C] |= 0xc0;
131 DPRINTF_C("cmos: injecting from timer\n");
132 qemu_irq_raise(s->irq);
133 if (apic_get_irq_delivered()) {
135 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
140 rtc_coalesced_timer_update(s);
144 static void rtc_timer_update(RTCState *s, int64_t current_time)
146 int period_code, period;
147 int64_t cur_clock, next_irq_clock;
149 period_code = s->cmos_data[RTC_REG_A] & 0x0f;
151 && ((s->cmos_data[RTC_REG_B] & REG_B_PIE)
152 || ((s->cmos_data[RTC_REG_B] & REG_B_SQWE) && s->sqw_irq))) {
153 if (period_code <= 2)
155 /* period in 32 Khz cycles */
156 period = 1 << (period_code - 1);
158 if (period != s->period) {
159 s->irq_coalesced = (s->irq_coalesced * s->period) / period;
160 DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s->irq_coalesced);
164 /* compute 32 khz clock */
165 cur_clock = muldiv64(current_time, 32768, get_ticks_per_sec());
166 next_irq_clock = (cur_clock & ~(period - 1)) + period;
167 s->next_periodic_time =
168 muldiv64(next_irq_clock, get_ticks_per_sec(), 32768) + 1;
169 qemu_mod_timer(s->periodic_timer, s->next_periodic_time);
172 s->irq_coalesced = 0;
174 qemu_del_timer(s->periodic_timer);
178 static void rtc_periodic_timer(void *opaque)
180 RTCState *s = opaque;
182 rtc_timer_update(s, s->next_periodic_time);
183 s->cmos_data[RTC_REG_C] |= REG_C_PF;
184 if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
185 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
187 if (s->lost_tick_policy == LOST_TICK_SLEW) {
188 if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT)
189 s->irq_reinject_on_ack_count = 0;
190 apic_reset_irq_delivered();
191 qemu_irq_raise(s->irq);
192 if (!apic_get_irq_delivered()) {
194 rtc_coalesced_timer_update(s);
195 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
200 qemu_irq_raise(s->irq);
202 if (s->cmos_data[RTC_REG_B] & REG_B_SQWE) {
203 /* Not square wave at all but we don't want 2048Hz interrupts!
204 Must be seen as a pulse. */
205 qemu_irq_raise(s->sqw_irq);
209 static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data)
211 RTCState *s = opaque;
213 if ((addr & 1) == 0) {
214 s->cmos_index = data & 0x7f;
216 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n",
217 s->cmos_index, data);
218 switch(s->cmos_index) {
219 case RTC_SECONDS_ALARM:
220 case RTC_MINUTES_ALARM:
221 case RTC_HOURS_ALARM:
222 s->cmos_data[s->cmos_index] = data;
227 case RTC_DAY_OF_WEEK:
228 case RTC_DAY_OF_MONTH:
231 s->cmos_data[s->cmos_index] = data;
232 /* if in set mode, do not update the time */
233 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
238 /* UIP bit is read only */
239 s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
240 (s->cmos_data[RTC_REG_A] & REG_A_UIP);
241 rtc_timer_update(s, qemu_get_clock_ns(rtc_clock));
244 if (data & REG_B_SET) {
245 /* set mode: reset UIP mode */
246 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
249 /* if disabling set mode, update the time */
250 if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
254 if (((s->cmos_data[RTC_REG_B] ^ data) & (REG_B_DM | REG_B_24H)) &&
255 !(data & REG_B_SET)) {
256 /* If the time format has changed and not in set mode,
257 update the registers immediately. */
258 s->cmos_data[RTC_REG_B] = data;
261 s->cmos_data[RTC_REG_B] = data;
263 rtc_timer_update(s, qemu_get_clock_ns(rtc_clock));
267 /* cannot write to them */
270 s->cmos_data[s->cmos_index] = data;
276 static inline int rtc_to_bcd(RTCState *s, int a)
278 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
281 return ((a / 10) << 4) | (a % 10);
285 static inline int rtc_from_bcd(RTCState *s, int a)
287 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
290 return ((a >> 4) * 10) + (a & 0x0f);
294 static void rtc_set_time(RTCState *s)
296 struct tm *tm = &s->current_tm;
298 tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
299 tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
300 tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
301 if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
303 if (s->cmos_data[RTC_HOURS] & 0x80) {
307 tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
308 tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
309 tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
310 tm->tm_year = rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year - 1900;
312 rtc_change_mon_event(tm);
315 static void rtc_copy_date(RTCState *s)
317 const struct tm *tm = &s->current_tm;
320 s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec);
321 s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min);
322 if (s->cmos_data[RTC_REG_B] & REG_B_24H) {
324 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
327 int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12;
328 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h);
329 if (tm->tm_hour >= 12)
330 s->cmos_data[RTC_HOURS] |= 0x80;
332 s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
333 s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday);
334 s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
335 year = (tm->tm_year - s->base_year) % 100;
338 s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year);
341 /* month is between 0 and 11. */
342 static int get_days_in_month(int month, int year)
344 static const int days_tab[12] = {
345 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
348 if ((unsigned )month >= 12)
352 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0))
358 /* update 'tm' to the next second */
359 static void rtc_next_second(struct tm *tm)
364 if ((unsigned)tm->tm_sec >= 60) {
367 if ((unsigned)tm->tm_min >= 60) {
370 if ((unsigned)tm->tm_hour >= 24) {
374 if ((unsigned)tm->tm_wday >= 7)
376 days_in_month = get_days_in_month(tm->tm_mon,
379 if (tm->tm_mday < 1) {
381 } else if (tm->tm_mday > days_in_month) {
384 if (tm->tm_mon >= 12) {
395 static void rtc_update_second(void *opaque)
397 RTCState *s = opaque;
400 /* if the oscillator is not in normal operation, we do not update */
401 if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) {
402 s->next_second_time += get_ticks_per_sec();
403 qemu_mod_timer(s->second_timer, s->next_second_time);
405 rtc_next_second(&s->current_tm);
407 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
408 /* update in progress bit */
409 s->cmos_data[RTC_REG_A] |= REG_A_UIP;
411 /* should be 244 us = 8 / 32768 seconds, but currently the
412 timers do not have the necessary resolution. */
413 delay = (get_ticks_per_sec() * 1) / 100;
416 qemu_mod_timer(s->second_timer2,
417 s->next_second_time + delay);
421 static void rtc_update_second2(void *opaque)
423 RTCState *s = opaque;
425 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
430 if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 ||
431 rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]) == s->current_tm.tm_sec) &&
432 ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 ||
433 rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]) == s->current_tm.tm_min) &&
434 ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 ||
435 rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]) == s->current_tm.tm_hour)) {
437 s->cmos_data[RTC_REG_C] |= REG_C_AF;
438 if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
439 qemu_irq_raise(s->irq);
440 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
444 /* update ended interrupt */
445 s->cmos_data[RTC_REG_C] |= REG_C_UF;
446 if (s->cmos_data[RTC_REG_B] & REG_B_UIE) {
447 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
448 qemu_irq_raise(s->irq);
451 /* clear update in progress bit */
452 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
454 s->next_second_time += get_ticks_per_sec();
455 qemu_mod_timer(s->second_timer, s->next_second_time);
458 static uint32_t cmos_ioport_read(void *opaque, uint32_t addr)
460 RTCState *s = opaque;
462 if ((addr & 1) == 0) {
465 switch(s->cmos_index) {
469 case RTC_DAY_OF_WEEK:
470 case RTC_DAY_OF_MONTH:
473 ret = s->cmos_data[s->cmos_index];
476 ret = s->cmos_data[s->cmos_index];
479 ret = s->cmos_data[s->cmos_index];
480 qemu_irq_lower(s->irq);
481 s->cmos_data[RTC_REG_C] = 0x00;
483 if(s->irq_coalesced &&
484 (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
485 s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
486 s->irq_reinject_on_ack_count++;
487 s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF;
488 apic_reset_irq_delivered();
489 DPRINTF_C("cmos: injecting on ack\n");
490 qemu_irq_raise(s->irq);
491 if (apic_get_irq_delivered()) {
493 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
500 ret = s->cmos_data[s->cmos_index];
503 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
509 void rtc_set_memory(ISADevice *dev, int addr, int val)
511 RTCState *s = DO_UPCAST(RTCState, dev, dev);
512 if (addr >= 0 && addr <= 127)
513 s->cmos_data[addr] = val;
516 void rtc_set_date(ISADevice *dev, const struct tm *tm)
518 RTCState *s = DO_UPCAST(RTCState, dev, dev);
523 /* PC cmos mappings */
524 #define REG_IBM_CENTURY_BYTE 0x32
525 #define REG_IBM_PS2_CENTURY_BYTE 0x37
527 static void rtc_set_date_from_host(ISADevice *dev)
529 RTCState *s = DO_UPCAST(RTCState, dev, dev);
533 /* set the CMOS date */
534 qemu_get_timedate(&tm, 0);
535 rtc_set_date(dev, &tm);
537 val = rtc_to_bcd(s, (tm.tm_year / 100) + 19);
538 rtc_set_memory(dev, REG_IBM_CENTURY_BYTE, val);
539 rtc_set_memory(dev, REG_IBM_PS2_CENTURY_BYTE, val);
542 static int rtc_post_load(void *opaque, int version_id)
545 RTCState *s = opaque;
547 if (version_id >= 2) {
548 if (s->lost_tick_policy == LOST_TICK_SLEW) {
549 rtc_coalesced_timer_update(s);
556 static const VMStateDescription vmstate_rtc = {
557 .name = "mc146818rtc",
559 .minimum_version_id = 1,
560 .minimum_version_id_old = 1,
561 .post_load = rtc_post_load,
562 .fields = (VMStateField []) {
563 VMSTATE_BUFFER(cmos_data, RTCState),
564 VMSTATE_UINT8(cmos_index, RTCState),
565 VMSTATE_INT32(current_tm.tm_sec, RTCState),
566 VMSTATE_INT32(current_tm.tm_min, RTCState),
567 VMSTATE_INT32(current_tm.tm_hour, RTCState),
568 VMSTATE_INT32(current_tm.tm_wday, RTCState),
569 VMSTATE_INT32(current_tm.tm_mday, RTCState),
570 VMSTATE_INT32(current_tm.tm_mon, RTCState),
571 VMSTATE_INT32(current_tm.tm_year, RTCState),
572 VMSTATE_TIMER(periodic_timer, RTCState),
573 VMSTATE_INT64(next_periodic_time, RTCState),
574 VMSTATE_INT64(next_second_time, RTCState),
575 VMSTATE_TIMER(second_timer, RTCState),
576 VMSTATE_TIMER(second_timer2, RTCState),
577 VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
578 VMSTATE_UINT32_V(period, RTCState, 2),
579 VMSTATE_END_OF_LIST()
583 static void rtc_notify_clock_reset(Notifier *notifier, void *data)
585 RTCState *s = container_of(notifier, RTCState, clock_reset_notifier);
586 int64_t now = *(int64_t *)data;
588 rtc_set_date_from_host(&s->dev);
589 s->next_second_time = now + (get_ticks_per_sec() * 99) / 100;
590 qemu_mod_timer(s->second_timer2, s->next_second_time);
591 rtc_timer_update(s, now);
593 if (s->lost_tick_policy == LOST_TICK_SLEW) {
594 rtc_coalesced_timer_update(s);
599 static void rtc_reset(void *opaque)
601 RTCState *s = opaque;
603 s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
604 s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
606 qemu_irq_lower(s->irq);
609 if (s->lost_tick_policy == LOST_TICK_SLEW) {
610 s->irq_coalesced = 0;
615 static const MemoryRegionPortio cmos_portio[] = {
616 {0, 2, 1, .read = cmos_ioport_read, .write = cmos_ioport_write },
617 PORTIO_END_OF_LIST(),
620 static const MemoryRegionOps cmos_ops = {
621 .old_portio = cmos_portio
624 // FIXME add int32 visitor
625 static void visit_type_int32(Visitor *v, int *value, const char *name, Error **errp)
627 int64_t val = *value;
628 visit_type_int(v, &val, name, errp);
631 static void rtc_get_date(Object *obj, Visitor *v, void *opaque,
632 const char *name, Error **errp)
634 ISADevice *isa = ISA_DEVICE(obj);
635 RTCState *s = DO_UPCAST(RTCState, dev, isa);
637 visit_start_struct(v, NULL, "struct tm", name, 0, errp);
638 visit_type_int32(v, &s->current_tm.tm_year, "tm_year", errp);
639 visit_type_int32(v, &s->current_tm.tm_mon, "tm_mon", errp);
640 visit_type_int32(v, &s->current_tm.tm_mday, "tm_mday", errp);
641 visit_type_int32(v, &s->current_tm.tm_hour, "tm_hour", errp);
642 visit_type_int32(v, &s->current_tm.tm_min, "tm_min", errp);
643 visit_type_int32(v, &s->current_tm.tm_sec, "tm_sec", errp);
644 visit_end_struct(v, errp);
647 static int rtc_initfn(ISADevice *dev)
649 RTCState *s = DO_UPCAST(RTCState, dev, dev);
652 s->cmos_data[RTC_REG_A] = 0x26;
653 s->cmos_data[RTC_REG_B] = 0x02;
654 s->cmos_data[RTC_REG_C] = 0x00;
655 s->cmos_data[RTC_REG_D] = 0x80;
657 rtc_set_date_from_host(dev);
660 switch (s->lost_tick_policy) {
663 qemu_new_timer_ns(rtc_clock, rtc_coalesced_timer, s);
665 case LOST_TICK_DISCARD:
672 s->periodic_timer = qemu_new_timer_ns(rtc_clock, rtc_periodic_timer, s);
673 s->second_timer = qemu_new_timer_ns(rtc_clock, rtc_update_second, s);
674 s->second_timer2 = qemu_new_timer_ns(rtc_clock, rtc_update_second2, s);
676 s->clock_reset_notifier.notify = rtc_notify_clock_reset;
677 qemu_register_clock_reset_notifier(rtc_clock, &s->clock_reset_notifier);
679 s->next_second_time =
680 qemu_get_clock_ns(rtc_clock) + (get_ticks_per_sec() * 99) / 100;
681 qemu_mod_timer(s->second_timer2, s->next_second_time);
683 memory_region_init_io(&s->io, &cmos_ops, s, "rtc", 2);
684 isa_register_ioport(dev, &s->io, base);
686 qdev_set_legacy_instance_id(&dev->qdev, base, 2);
687 qemu_register_reset(rtc_reset, s);
689 object_property_add(OBJECT(s), "date", "struct tm",
690 rtc_get_date, NULL, NULL, s, NULL);
695 ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq)
700 dev = isa_create(bus, "mc146818rtc");
701 s = DO_UPCAST(RTCState, dev, dev);
702 qdev_prop_set_int32(&dev->qdev, "base_year", base_year);
703 qdev_init_nofail(&dev->qdev);
705 s->irq = intercept_irq;
707 isa_init_irq(dev, &s->irq, RTC_ISA_IRQ);
712 static Property mc146818rtc_properties[] = {
713 DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
714 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState,
715 lost_tick_policy, LOST_TICK_DISCARD),
716 DEFINE_PROP_END_OF_LIST(),
719 static void rtc_class_initfn(ObjectClass *klass, void *data)
721 DeviceClass *dc = DEVICE_CLASS(klass);
722 ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
723 ic->init = rtc_initfn;
725 dc->vmsd = &vmstate_rtc;
726 dc->props = mc146818rtc_properties;
729 static TypeInfo mc146818rtc_info = {
730 .name = "mc146818rtc",
731 .parent = TYPE_ISA_DEVICE,
732 .instance_size = sizeof(RTCState),
733 .class_init = rtc_class_initfn,
736 static void mc146818rtc_register(void)
738 type_register_static(&mc146818rtc_info);
740 device_init(mc146818rtc_register)