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"
29 #include "hpet_emul.h"
34 #define RTC_SECONDS_ALARM 1
36 #define RTC_MINUTES_ALARM 3
38 #define RTC_HOURS_ALARM 5
39 #define RTC_ALARM_DONT_CARE 0xC0
41 #define RTC_DAY_OF_WEEK 6
42 #define RTC_DAY_OF_MONTH 7
51 #define REG_A_UIP 0x80
53 #define REG_B_SET 0x80
54 #define REG_B_PIE 0x40
55 #define REG_B_AIE 0x20
56 #define REG_B_UIE 0x10
57 #define REG_B_SQWE 0x08
61 #define REG_C_IRQF 0x80
67 uint8_t cmos_data[128];
75 QEMUTimer *periodic_timer;
76 int64_t next_periodic_time;
78 int64_t next_second_time;
80 uint32_t irq_coalesced;
82 QEMUTimer *coalesced_timer;
84 QEMUTimer *second_timer;
85 QEMUTimer *second_timer2;
88 static void rtc_irq_raise(qemu_irq irq) {
89 /* When HPET is operating in legacy mode, RTC interrupts are disabled
90 * We block qemu_irq_raise, but not qemu_irq_lower, in case legacy
91 * mode is established while interrupt is raised. We want it to
92 * be lowered in any case
94 #if defined TARGET_I386
95 if (!hpet_in_legacy_mode())
100 static void rtc_set_time(RTCState *s);
101 static void rtc_copy_date(RTCState *s);
104 static void rtc_coalesced_timer_update(RTCState *s)
106 if (s->irq_coalesced == 0) {
107 qemu_del_timer(s->coalesced_timer);
109 /* divide each RTC interval to 2 - 8 smaller intervals */
110 int c = MIN(s->irq_coalesced, 7) + 1;
111 int64_t next_clock = qemu_get_clock(rtc_clock) +
112 muldiv64(s->period / c, get_ticks_per_sec(), 32768);
113 qemu_mod_timer(s->coalesced_timer, next_clock);
117 static void rtc_coalesced_timer(void *opaque)
119 RTCState *s = opaque;
121 if (s->irq_coalesced != 0) {
122 apic_reset_irq_delivered();
123 s->cmos_data[RTC_REG_C] |= 0xc0;
124 rtc_irq_raise(s->irq);
125 if (apic_get_irq_delivered()) {
130 rtc_coalesced_timer_update(s);
134 static void rtc_timer_update(RTCState *s, int64_t current_time)
136 int period_code, period;
137 int64_t cur_clock, next_irq_clock;
140 period_code = s->cmos_data[RTC_REG_A] & 0x0f;
141 #if defined TARGET_I386
142 /* disable periodic timer if hpet is in legacy mode, since interrupts are
145 enable_pie = !hpet_in_legacy_mode();
150 && (((s->cmos_data[RTC_REG_B] & REG_B_PIE) && enable_pie)
151 || ((s->cmos_data[RTC_REG_B] & REG_B_SQWE) && s->sqw_irq))) {
152 if (period_code <= 2)
154 /* period in 32 Khz cycles */
155 period = 1 << (period_code - 1);
157 if(period != s->period)
158 s->irq_coalesced = (s->irq_coalesced * s->period) / period;
161 /* compute 32 khz clock */
162 cur_clock = muldiv64(current_time, 32768, get_ticks_per_sec());
163 next_irq_clock = (cur_clock & ~(period - 1)) + period;
164 s->next_periodic_time =
165 muldiv64(next_irq_clock, get_ticks_per_sec(), 32768) + 1;
166 qemu_mod_timer(s->periodic_timer, s->next_periodic_time);
169 s->irq_coalesced = 0;
171 qemu_del_timer(s->periodic_timer);
175 static void rtc_periodic_timer(void *opaque)
177 RTCState *s = opaque;
179 rtc_timer_update(s, s->next_periodic_time);
180 if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
181 s->cmos_data[RTC_REG_C] |= 0xc0;
184 apic_reset_irq_delivered();
185 rtc_irq_raise(s->irq);
186 if (!apic_get_irq_delivered()) {
188 rtc_coalesced_timer_update(s);
192 rtc_irq_raise(s->irq);
194 if (s->cmos_data[RTC_REG_B] & REG_B_SQWE) {
195 /* Not square wave at all but we don't want 2048Hz interrupts!
196 Must be seen as a pulse. */
197 qemu_irq_raise(s->sqw_irq);
201 static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data)
203 RTCState *s = opaque;
205 if ((addr & 1) == 0) {
206 s->cmos_index = data & 0x7f;
209 printf("cmos: write index=0x%02x val=0x%02x\n",
210 s->cmos_index, data);
212 switch(s->cmos_index) {
213 case RTC_SECONDS_ALARM:
214 case RTC_MINUTES_ALARM:
215 case RTC_HOURS_ALARM:
216 /* XXX: not supported */
217 s->cmos_data[s->cmos_index] = data;
222 case RTC_DAY_OF_WEEK:
223 case RTC_DAY_OF_MONTH:
226 s->cmos_data[s->cmos_index] = data;
227 /* if in set mode, do not update the time */
228 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
233 /* UIP bit is read only */
234 s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
235 (s->cmos_data[RTC_REG_A] & REG_A_UIP);
236 rtc_timer_update(s, qemu_get_clock(rtc_clock));
239 if (data & REG_B_SET) {
240 /* set mode: reset UIP mode */
241 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
244 /* if disabling set mode, update the time */
245 if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
249 s->cmos_data[RTC_REG_B] = data;
250 rtc_timer_update(s, qemu_get_clock(rtc_clock));
254 /* cannot write to them */
257 s->cmos_data[s->cmos_index] = data;
263 static inline int to_bcd(RTCState *s, int a)
265 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
268 return ((a / 10) << 4) | (a % 10);
272 static inline int from_bcd(RTCState *s, int a)
274 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
277 return ((a >> 4) * 10) + (a & 0x0f);
281 static void rtc_set_time(RTCState *s)
283 struct tm *tm = &s->current_tm;
285 tm->tm_sec = from_bcd(s, s->cmos_data[RTC_SECONDS]);
286 tm->tm_min = from_bcd(s, s->cmos_data[RTC_MINUTES]);
287 tm->tm_hour = from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
288 if (!(s->cmos_data[RTC_REG_B] & 0x02) &&
289 (s->cmos_data[RTC_HOURS] & 0x80)) {
292 tm->tm_wday = from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
293 tm->tm_mday = from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
294 tm->tm_mon = from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
295 tm->tm_year = from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year - 1900;
298 static void rtc_copy_date(RTCState *s)
300 const struct tm *tm = &s->current_tm;
303 s->cmos_data[RTC_SECONDS] = to_bcd(s, tm->tm_sec);
304 s->cmos_data[RTC_MINUTES] = to_bcd(s, tm->tm_min);
305 if (s->cmos_data[RTC_REG_B] & 0x02) {
307 s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour);
310 s->cmos_data[RTC_HOURS] = to_bcd(s, tm->tm_hour % 12);
311 if (tm->tm_hour >= 12)
312 s->cmos_data[RTC_HOURS] |= 0x80;
314 s->cmos_data[RTC_DAY_OF_WEEK] = to_bcd(s, tm->tm_wday + 1);
315 s->cmos_data[RTC_DAY_OF_MONTH] = to_bcd(s, tm->tm_mday);
316 s->cmos_data[RTC_MONTH] = to_bcd(s, tm->tm_mon + 1);
317 year = (tm->tm_year - s->base_year) % 100;
320 s->cmos_data[RTC_YEAR] = to_bcd(s, year);
323 /* month is between 0 and 11. */
324 static int get_days_in_month(int month, int year)
326 static const int days_tab[12] = {
327 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
330 if ((unsigned )month >= 12)
334 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0))
340 /* update 'tm' to the next second */
341 static void rtc_next_second(struct tm *tm)
346 if ((unsigned)tm->tm_sec >= 60) {
349 if ((unsigned)tm->tm_min >= 60) {
352 if ((unsigned)tm->tm_hour >= 24) {
356 if ((unsigned)tm->tm_wday >= 7)
358 days_in_month = get_days_in_month(tm->tm_mon,
361 if (tm->tm_mday < 1) {
363 } else if (tm->tm_mday > days_in_month) {
366 if (tm->tm_mon >= 12) {
377 static void rtc_update_second(void *opaque)
379 RTCState *s = opaque;
382 /* if the oscillator is not in normal operation, we do not update */
383 if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) {
384 s->next_second_time += get_ticks_per_sec();
385 qemu_mod_timer(s->second_timer, s->next_second_time);
387 rtc_next_second(&s->current_tm);
389 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
390 /* update in progress bit */
391 s->cmos_data[RTC_REG_A] |= REG_A_UIP;
393 /* should be 244 us = 8 / 32768 seconds, but currently the
394 timers do not have the necessary resolution. */
395 delay = (get_ticks_per_sec() * 1) / 100;
398 qemu_mod_timer(s->second_timer2,
399 s->next_second_time + delay);
403 static void rtc_update_second2(void *opaque)
405 RTCState *s = opaque;
407 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
412 if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
413 if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 ||
414 s->cmos_data[RTC_SECONDS_ALARM] == s->current_tm.tm_sec) &&
415 ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 ||
416 s->cmos_data[RTC_MINUTES_ALARM] == s->current_tm.tm_mon) &&
417 ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 ||
418 s->cmos_data[RTC_HOURS_ALARM] == s->current_tm.tm_hour)) {
420 s->cmos_data[RTC_REG_C] |= 0xa0;
421 rtc_irq_raise(s->irq);
425 /* update ended interrupt */
426 s->cmos_data[RTC_REG_C] |= REG_C_UF;
427 if (s->cmos_data[RTC_REG_B] & REG_B_UIE) {
428 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
429 rtc_irq_raise(s->irq);
432 /* clear update in progress bit */
433 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
435 s->next_second_time += get_ticks_per_sec();
436 qemu_mod_timer(s->second_timer, s->next_second_time);
439 static uint32_t cmos_ioport_read(void *opaque, uint32_t addr)
441 RTCState *s = opaque;
443 if ((addr & 1) == 0) {
446 switch(s->cmos_index) {
450 case RTC_DAY_OF_WEEK:
451 case RTC_DAY_OF_MONTH:
454 ret = s->cmos_data[s->cmos_index];
457 ret = s->cmos_data[s->cmos_index];
460 ret = s->cmos_data[s->cmos_index];
461 qemu_irq_lower(s->irq);
462 s->cmos_data[RTC_REG_C] = 0x00;
465 ret = s->cmos_data[s->cmos_index];
469 printf("cmos: read index=0x%02x val=0x%02x\n",
476 void rtc_set_memory(RTCState *s, int addr, int val)
478 if (addr >= 0 && addr <= 127)
479 s->cmos_data[addr] = val;
482 void rtc_set_date(RTCState *s, const struct tm *tm)
488 /* PC cmos mappings */
489 #define REG_IBM_CENTURY_BYTE 0x32
490 #define REG_IBM_PS2_CENTURY_BYTE 0x37
492 static void rtc_set_date_from_host(RTCState *s)
497 /* set the CMOS date */
498 qemu_get_timedate(&tm, 0);
499 rtc_set_date(s, &tm);
501 val = to_bcd(s, (tm.tm_year / 100) + 19);
502 rtc_set_memory(s, REG_IBM_CENTURY_BYTE, val);
503 rtc_set_memory(s, REG_IBM_PS2_CENTURY_BYTE, val);
506 static void rtc_save(QEMUFile *f, void *opaque)
508 RTCState *s = opaque;
510 qemu_put_buffer(f, s->cmos_data, 128);
511 qemu_put_8s(f, &s->cmos_index);
513 qemu_put_be32(f, s->current_tm.tm_sec);
514 qemu_put_be32(f, s->current_tm.tm_min);
515 qemu_put_be32(f, s->current_tm.tm_hour);
516 qemu_put_be32(f, s->current_tm.tm_wday);
517 qemu_put_be32(f, s->current_tm.tm_mday);
518 qemu_put_be32(f, s->current_tm.tm_mon);
519 qemu_put_be32(f, s->current_tm.tm_year);
521 qemu_put_timer(f, s->periodic_timer);
522 qemu_put_be64(f, s->next_periodic_time);
524 qemu_put_be64(f, s->next_second_time);
525 qemu_put_timer(f, s->second_timer);
526 qemu_put_timer(f, s->second_timer2);
529 static int rtc_load(QEMUFile *f, void *opaque, int version_id)
531 RTCState *s = opaque;
536 qemu_get_buffer(f, s->cmos_data, 128);
537 qemu_get_8s(f, &s->cmos_index);
539 s->current_tm.tm_sec=qemu_get_be32(f);
540 s->current_tm.tm_min=qemu_get_be32(f);
541 s->current_tm.tm_hour=qemu_get_be32(f);
542 s->current_tm.tm_wday=qemu_get_be32(f);
543 s->current_tm.tm_mday=qemu_get_be32(f);
544 s->current_tm.tm_mon=qemu_get_be32(f);
545 s->current_tm.tm_year=qemu_get_be32(f);
547 qemu_get_timer(f, s->periodic_timer);
548 s->next_periodic_time=qemu_get_be64(f);
550 s->next_second_time=qemu_get_be64(f);
551 qemu_get_timer(f, s->second_timer);
552 qemu_get_timer(f, s->second_timer2);
557 static void rtc_save_td(QEMUFile *f, void *opaque)
559 RTCState *s = opaque;
561 qemu_put_be32(f, s->irq_coalesced);
562 qemu_put_be32(f, s->period);
565 static int rtc_load_td(QEMUFile *f, void *opaque, int version_id)
567 RTCState *s = opaque;
572 s->irq_coalesced = qemu_get_be32(f);
573 s->period = qemu_get_be32(f);
574 rtc_coalesced_timer_update(s);
579 static void rtc_reset(void *opaque)
581 RTCState *s = opaque;
583 s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
584 s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
586 qemu_irq_lower(s->irq);
590 s->irq_coalesced = 0;
594 static int rtc_initfn(ISADevice *dev)
596 RTCState *s = DO_UPCAST(RTCState, dev, dev);
600 isa_init_irq(dev, &s->irq, isairq);
602 s->cmos_data[RTC_REG_A] = 0x26;
603 s->cmos_data[RTC_REG_B] = 0x02;
604 s->cmos_data[RTC_REG_C] = 0x00;
605 s->cmos_data[RTC_REG_D] = 0x80;
607 rtc_set_date_from_host(s);
609 s->periodic_timer = qemu_new_timer(rtc_clock, rtc_periodic_timer, s);
613 qemu_new_timer(rtc_clock, rtc_coalesced_timer, s);
615 s->second_timer = qemu_new_timer(rtc_clock, rtc_update_second, s);
616 s->second_timer2 = qemu_new_timer(rtc_clock, rtc_update_second2, s);
618 s->next_second_time =
619 qemu_get_clock(rtc_clock) + (get_ticks_per_sec() * 99) / 100;
620 qemu_mod_timer(s->second_timer2, s->next_second_time);
622 register_ioport_write(base, 2, 1, cmos_ioport_write, s);
623 register_ioport_read(base, 2, 1, cmos_ioport_read, s);
625 register_savevm("mc146818rtc", base, 1, rtc_save, rtc_load, s);
628 register_savevm("mc146818rtc-td", base, 1, rtc_save_td, rtc_load_td, s);
630 qemu_register_reset(rtc_reset, s);
634 RTCState *rtc_init(int base_year)
638 dev = isa_create("mc146818rtc");
639 qdev_prop_set_int32(&dev->qdev, "base_year", base_year);
640 qdev_init_nofail(&dev->qdev);
641 return DO_UPCAST(RTCState, dev, dev);
644 static ISADeviceInfo mc146818rtc_info = {
645 .qdev.name = "mc146818rtc",
646 .qdev.size = sizeof(RTCState),
649 .qdev.props = (Property[]) {
650 DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
651 DEFINE_PROP_END_OF_LIST(),
655 static void mc146818rtc_register(void)
657 isa_qdev_register(&mc146818rtc_info);
659 device_init(mc146818rtc_register)
661 /* Memory mapped interface */
662 static uint32_t cmos_mm_readb (void *opaque, target_phys_addr_t addr)
664 RTCState *s = opaque;
666 return cmos_ioport_read(s, addr >> s->it_shift) & 0xFF;
669 static void cmos_mm_writeb (void *opaque,
670 target_phys_addr_t addr, uint32_t value)
672 RTCState *s = opaque;
674 cmos_ioport_write(s, addr >> s->it_shift, value & 0xFF);
677 static uint32_t cmos_mm_readw (void *opaque, target_phys_addr_t addr)
679 RTCState *s = opaque;
682 val = cmos_ioport_read(s, addr >> s->it_shift) & 0xFFFF;
683 #ifdef TARGET_WORDS_BIGENDIAN
689 static void cmos_mm_writew (void *opaque,
690 target_phys_addr_t addr, uint32_t value)
692 RTCState *s = opaque;
693 #ifdef TARGET_WORDS_BIGENDIAN
694 value = bswap16(value);
696 cmos_ioport_write(s, addr >> s->it_shift, value & 0xFFFF);
699 static uint32_t cmos_mm_readl (void *opaque, target_phys_addr_t addr)
701 RTCState *s = opaque;
704 val = cmos_ioport_read(s, addr >> s->it_shift);
705 #ifdef TARGET_WORDS_BIGENDIAN
711 static void cmos_mm_writel (void *opaque,
712 target_phys_addr_t addr, uint32_t value)
714 RTCState *s = opaque;
715 #ifdef TARGET_WORDS_BIGENDIAN
716 value = bswap32(value);
718 cmos_ioport_write(s, addr >> s->it_shift, value);
721 static CPUReadMemoryFunc * const rtc_mm_read[] = {
727 static CPUWriteMemoryFunc * const rtc_mm_write[] = {
733 RTCState *rtc_mm_init(target_phys_addr_t base, int it_shift, qemu_irq irq,
739 s = qemu_mallocz(sizeof(RTCState));
742 s->cmos_data[RTC_REG_A] = 0x26;
743 s->cmos_data[RTC_REG_B] = 0x02;
744 s->cmos_data[RTC_REG_C] = 0x00;
745 s->cmos_data[RTC_REG_D] = 0x80;
747 s->base_year = base_year;
748 rtc_set_date_from_host(s);
750 s->periodic_timer = qemu_new_timer(rtc_clock, rtc_periodic_timer, s);
751 s->second_timer = qemu_new_timer(rtc_clock, rtc_update_second, s);
752 s->second_timer2 = qemu_new_timer(rtc_clock, rtc_update_second2, s);
754 s->next_second_time =
755 qemu_get_clock(rtc_clock) + (get_ticks_per_sec() * 99) / 100;
756 qemu_mod_timer(s->second_timer2, s->next_second_time);
758 io_memory = cpu_register_io_memory(rtc_mm_read, rtc_mm_write, s);
759 cpu_register_physical_memory(base, 2 << it_shift, io_memory);
761 register_savevm("mc146818rtc", base, 1, rtc_save, rtc_load, s);
764 register_savevm("mc146818rtc-td", base, 1, rtc_save_td, rtc_load_td, s);
766 qemu_register_reset(rtc_reset, s);