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
;
106 static void rtc_set_time(RTCState
*s
);
107 static void rtc_copy_date(RTCState
*s
);
110 static void rtc_coalesced_timer_update(RTCState
*s
)
112 if (s
->irq_coalesced
== 0) {
113 qemu_del_timer(s
->coalesced_timer
);
115 /* divide each RTC interval to 2 - 8 smaller intervals */
116 int c
= MIN(s
->irq_coalesced
, 7) + 1;
117 int64_t next_clock
= qemu_get_clock_ns(rtc_clock
) +
118 muldiv64(s
->period
/ c
, get_ticks_per_sec(), 32768);
119 qemu_mod_timer(s
->coalesced_timer
, next_clock
);
123 static void rtc_coalesced_timer(void *opaque
)
125 RTCState
*s
= opaque
;
127 if (s
->irq_coalesced
!= 0) {
128 apic_reset_irq_delivered();
129 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
130 DPRINTF_C("cmos: injecting from timer\n");
131 qemu_irq_raise(s
->irq
);
132 if (apic_get_irq_delivered()) {
134 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
139 rtc_coalesced_timer_update(s
);
143 static void rtc_timer_update(RTCState
*s
, int64_t current_time
)
145 int period_code
, period
;
146 int64_t cur_clock
, next_irq_clock
;
148 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
150 && ((s
->cmos_data
[RTC_REG_B
] & REG_B_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
;
159 DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s
->irq_coalesced
);
163 /* compute 32 khz clock */
164 cur_clock
= muldiv64(current_time
, 32768, get_ticks_per_sec());
165 next_irq_clock
= (cur_clock
& ~(period
- 1)) + period
;
166 s
->next_periodic_time
=
167 muldiv64(next_irq_clock
, get_ticks_per_sec(), 32768) + 1;
168 qemu_mod_timer(s
->periodic_timer
, s
->next_periodic_time
);
171 s
->irq_coalesced
= 0;
173 qemu_del_timer(s
->periodic_timer
);
177 static void rtc_periodic_timer(void *opaque
)
179 RTCState
*s
= opaque
;
181 rtc_timer_update(s
, s
->next_periodic_time
);
182 if (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) {
183 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
186 if (s
->irq_reinject_on_ack_count
>= RTC_REINJECT_ON_ACK_COUNT
)
187 s
->irq_reinject_on_ack_count
= 0;
188 apic_reset_irq_delivered();
189 qemu_irq_raise(s
->irq
);
190 if (!apic_get_irq_delivered()) {
192 rtc_coalesced_timer_update(s
);
193 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
198 qemu_irq_raise(s
->irq
);
200 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) {
201 /* Not square wave at all but we don't want 2048Hz interrupts!
202 Must be seen as a pulse. */
203 qemu_irq_raise(s
->sqw_irq
);
207 static void cmos_ioport_write(void *opaque
, uint32_t addr
, uint32_t data
)
209 RTCState
*s
= opaque
;
211 if ((addr
& 1) == 0) {
212 s
->cmos_index
= data
& 0x7f;
214 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n",
215 s
->cmos_index
, data
);
216 switch(s
->cmos_index
) {
217 case RTC_SECONDS_ALARM
:
218 case RTC_MINUTES_ALARM
:
219 case RTC_HOURS_ALARM
:
220 s
->cmos_data
[s
->cmos_index
] = data
;
225 case RTC_DAY_OF_WEEK
:
226 case RTC_DAY_OF_MONTH
:
229 s
->cmos_data
[s
->cmos_index
] = data
;
230 /* if in set mode, do not update the time */
231 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
236 /* UIP bit is read only */
237 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
238 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
239 rtc_timer_update(s
, qemu_get_clock_ns(rtc_clock
));
242 if (data
& REG_B_SET
) {
243 /* set mode: reset UIP mode */
244 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
247 /* if disabling set mode, update the time */
248 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) {
252 if (((s
->cmos_data
[RTC_REG_B
] ^ data
) & (REG_B_DM
| REG_B_24H
)) &&
253 !(data
& REG_B_SET
)) {
254 /* If the time format has changed and not in set mode,
255 update the registers immediately. */
256 s
->cmos_data
[RTC_REG_B
] = data
;
259 s
->cmos_data
[RTC_REG_B
] = data
;
261 rtc_timer_update(s
, qemu_get_clock_ns(rtc_clock
));
265 /* cannot write to them */
268 s
->cmos_data
[s
->cmos_index
] = data
;
274 static inline int rtc_to_bcd(RTCState
*s
, int a
)
276 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
279 return ((a
/ 10) << 4) | (a
% 10);
283 static inline int rtc_from_bcd(RTCState
*s
, int a
)
285 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
288 return ((a
>> 4) * 10) + (a
& 0x0f);
292 static void rtc_set_time(RTCState
*s
)
294 struct tm
*tm
= &s
->current_tm
;
296 tm
->tm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
297 tm
->tm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
298 tm
->tm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
299 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
) &&
300 (s
->cmos_data
[RTC_HOURS
] & 0x80)) {
303 tm
->tm_wday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
304 tm
->tm_mday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
305 tm
->tm_mon
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
306 tm
->tm_year
= rtc_from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + s
->base_year
- 1900;
308 rtc_change_mon_event(tm
);
311 static void rtc_copy_date(RTCState
*s
)
313 const struct tm
*tm
= &s
->current_tm
;
316 s
->cmos_data
[RTC_SECONDS
] = rtc_to_bcd(s
, tm
->tm_sec
);
317 s
->cmos_data
[RTC_MINUTES
] = rtc_to_bcd(s
, tm
->tm_min
);
318 if (s
->cmos_data
[RTC_REG_B
] & REG_B_24H
) {
320 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
);
323 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
% 12);
324 if (tm
->tm_hour
>= 12)
325 s
->cmos_data
[RTC_HOURS
] |= 0x80;
327 s
->cmos_data
[RTC_DAY_OF_WEEK
] = rtc_to_bcd(s
, tm
->tm_wday
+ 1);
328 s
->cmos_data
[RTC_DAY_OF_MONTH
] = rtc_to_bcd(s
, tm
->tm_mday
);
329 s
->cmos_data
[RTC_MONTH
] = rtc_to_bcd(s
, tm
->tm_mon
+ 1);
330 year
= (tm
->tm_year
- s
->base_year
) % 100;
333 s
->cmos_data
[RTC_YEAR
] = rtc_to_bcd(s
, year
);
336 /* month is between 0 and 11. */
337 static int get_days_in_month(int month
, int year
)
339 static const int days_tab
[12] = {
340 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
343 if ((unsigned )month
>= 12)
347 if ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
353 /* update 'tm' to the next second */
354 static void rtc_next_second(struct tm
*tm
)
359 if ((unsigned)tm
->tm_sec
>= 60) {
362 if ((unsigned)tm
->tm_min
>= 60) {
365 if ((unsigned)tm
->tm_hour
>= 24) {
369 if ((unsigned)tm
->tm_wday
>= 7)
371 days_in_month
= get_days_in_month(tm
->tm_mon
,
374 if (tm
->tm_mday
< 1) {
376 } else if (tm
->tm_mday
> days_in_month
) {
379 if (tm
->tm_mon
>= 12) {
390 static void rtc_update_second(void *opaque
)
392 RTCState
*s
= opaque
;
395 /* if the oscillator is not in normal operation, we do not update */
396 if ((s
->cmos_data
[RTC_REG_A
] & 0x70) != 0x20) {
397 s
->next_second_time
+= get_ticks_per_sec();
398 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
400 rtc_next_second(&s
->current_tm
);
402 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
403 /* update in progress bit */
404 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
406 /* should be 244 us = 8 / 32768 seconds, but currently the
407 timers do not have the necessary resolution. */
408 delay
= (get_ticks_per_sec() * 1) / 100;
411 qemu_mod_timer(s
->second_timer2
,
412 s
->next_second_time
+ delay
);
416 static void rtc_update_second2(void *opaque
)
418 RTCState
*s
= opaque
;
420 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
425 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
426 if (((s
->cmos_data
[RTC_SECONDS_ALARM
] & 0xc0) == 0xc0 ||
427 rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS_ALARM
]) == s
->current_tm
.tm_sec
) &&
428 ((s
->cmos_data
[RTC_MINUTES_ALARM
] & 0xc0) == 0xc0 ||
429 rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES_ALARM
]) == s
->current_tm
.tm_min
) &&
430 ((s
->cmos_data
[RTC_HOURS_ALARM
] & 0xc0) == 0xc0 ||
431 rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS_ALARM
]) == s
->current_tm
.tm_hour
)) {
433 s
->cmos_data
[RTC_REG_C
] |= 0xa0;
434 qemu_irq_raise(s
->irq
);
438 /* update ended interrupt */
439 s
->cmos_data
[RTC_REG_C
] |= REG_C_UF
;
440 if (s
->cmos_data
[RTC_REG_B
] & REG_B_UIE
) {
441 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
442 qemu_irq_raise(s
->irq
);
445 /* clear update in progress bit */
446 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
448 s
->next_second_time
+= get_ticks_per_sec();
449 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
452 static uint32_t cmos_ioport_read(void *opaque
, uint32_t addr
)
454 RTCState
*s
= opaque
;
456 if ((addr
& 1) == 0) {
459 switch(s
->cmos_index
) {
463 case RTC_DAY_OF_WEEK
:
464 case RTC_DAY_OF_MONTH
:
467 ret
= s
->cmos_data
[s
->cmos_index
];
470 ret
= s
->cmos_data
[s
->cmos_index
];
473 ret
= s
->cmos_data
[s
->cmos_index
];
474 qemu_irq_lower(s
->irq
);
476 if(s
->irq_coalesced
&&
477 s
->irq_reinject_on_ack_count
< RTC_REINJECT_ON_ACK_COUNT
) {
478 s
->irq_reinject_on_ack_count
++;
479 apic_reset_irq_delivered();
480 DPRINTF_C("cmos: injecting on ack\n");
481 qemu_irq_raise(s
->irq
);
482 if (apic_get_irq_delivered()) {
484 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
491 s
->cmos_data
[RTC_REG_C
] = 0x00;
494 ret
= s
->cmos_data
[s
->cmos_index
];
497 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
503 void rtc_set_memory(ISADevice
*dev
, int addr
, int val
)
505 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
506 if (addr
>= 0 && addr
<= 127)
507 s
->cmos_data
[addr
] = val
;
510 void rtc_set_date(ISADevice
*dev
, const struct tm
*tm
)
512 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
517 /* PC cmos mappings */
518 #define REG_IBM_CENTURY_BYTE 0x32
519 #define REG_IBM_PS2_CENTURY_BYTE 0x37
521 static void rtc_set_date_from_host(ISADevice
*dev
)
523 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
527 /* set the CMOS date */
528 qemu_get_timedate(&tm
, 0);
529 rtc_set_date(dev
, &tm
);
531 val
= rtc_to_bcd(s
, (tm
.tm_year
/ 100) + 19);
532 rtc_set_memory(dev
, REG_IBM_CENTURY_BYTE
, val
);
533 rtc_set_memory(dev
, REG_IBM_PS2_CENTURY_BYTE
, val
);
536 static int rtc_post_load(void *opaque
, int version_id
)
539 RTCState
*s
= opaque
;
541 if (version_id
>= 2) {
543 rtc_coalesced_timer_update(s
);
550 static const VMStateDescription vmstate_rtc
= {
551 .name
= "mc146818rtc",
553 .minimum_version_id
= 1,
554 .minimum_version_id_old
= 1,
555 .post_load
= rtc_post_load
,
556 .fields
= (VMStateField
[]) {
557 VMSTATE_BUFFER(cmos_data
, RTCState
),
558 VMSTATE_UINT8(cmos_index
, RTCState
),
559 VMSTATE_INT32(current_tm
.tm_sec
, RTCState
),
560 VMSTATE_INT32(current_tm
.tm_min
, RTCState
),
561 VMSTATE_INT32(current_tm
.tm_hour
, RTCState
),
562 VMSTATE_INT32(current_tm
.tm_wday
, RTCState
),
563 VMSTATE_INT32(current_tm
.tm_mday
, RTCState
),
564 VMSTATE_INT32(current_tm
.tm_mon
, RTCState
),
565 VMSTATE_INT32(current_tm
.tm_year
, RTCState
),
566 VMSTATE_TIMER(periodic_timer
, RTCState
),
567 VMSTATE_INT64(next_periodic_time
, RTCState
),
568 VMSTATE_INT64(next_second_time
, RTCState
),
569 VMSTATE_TIMER(second_timer
, RTCState
),
570 VMSTATE_TIMER(second_timer2
, RTCState
),
571 VMSTATE_UINT32_V(irq_coalesced
, RTCState
, 2),
572 VMSTATE_UINT32_V(period
, RTCState
, 2),
573 VMSTATE_END_OF_LIST()
577 static void rtc_notify_clock_reset(Notifier
*notifier
, void *data
)
579 RTCState
*s
= container_of(notifier
, RTCState
, clock_reset_notifier
);
580 int64_t now
= *(int64_t *)data
;
582 rtc_set_date_from_host(&s
->dev
);
583 s
->next_second_time
= now
+ (get_ticks_per_sec() * 99) / 100;
584 qemu_mod_timer(s
->second_timer2
, s
->next_second_time
);
585 rtc_timer_update(s
, now
);
588 rtc_coalesced_timer_update(s
);
593 static void rtc_reset(void *opaque
)
595 RTCState
*s
= opaque
;
597 s
->cmos_data
[RTC_REG_B
] &= ~(REG_B_PIE
| REG_B_AIE
| REG_B_SQWE
);
598 s
->cmos_data
[RTC_REG_C
] &= ~(REG_C_UF
| REG_C_IRQF
| REG_C_PF
| REG_C_AF
);
600 qemu_irq_lower(s
->irq
);
604 s
->irq_coalesced
= 0;
608 static const MemoryRegionPortio cmos_portio
[] = {
609 {0, 2, 1, .read
= cmos_ioport_read
, .write
= cmos_ioport_write
},
610 PORTIO_END_OF_LIST(),
613 static const MemoryRegionOps cmos_ops
= {
614 .old_portio
= cmos_portio
617 static int rtc_initfn(ISADevice
*dev
)
619 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
622 s
->cmos_data
[RTC_REG_A
] = 0x26;
623 s
->cmos_data
[RTC_REG_B
] = 0x02;
624 s
->cmos_data
[RTC_REG_C
] = 0x00;
625 s
->cmos_data
[RTC_REG_D
] = 0x80;
627 rtc_set_date_from_host(dev
);
629 s
->periodic_timer
= qemu_new_timer_ns(rtc_clock
, rtc_periodic_timer
, s
);
633 qemu_new_timer_ns(rtc_clock
, rtc_coalesced_timer
, s
);
635 s
->second_timer
= qemu_new_timer_ns(rtc_clock
, rtc_update_second
, s
);
636 s
->second_timer2
= qemu_new_timer_ns(rtc_clock
, rtc_update_second2
, s
);
638 s
->clock_reset_notifier
.notify
= rtc_notify_clock_reset
;
639 qemu_register_clock_reset_notifier(rtc_clock
, &s
->clock_reset_notifier
);
641 s
->next_second_time
=
642 qemu_get_clock_ns(rtc_clock
) + (get_ticks_per_sec() * 99) / 100;
643 qemu_mod_timer(s
->second_timer2
, s
->next_second_time
);
645 memory_region_init_io(&s
->io
, &cmos_ops
, s
, "rtc", 2);
646 isa_register_ioport(dev
, &s
->io
, base
);
648 qdev_set_legacy_instance_id(&dev
->qdev
, base
, 2);
649 qemu_register_reset(rtc_reset
, s
);
653 ISADevice
*rtc_init(int base_year
, qemu_irq intercept_irq
)
658 dev
= isa_create("mc146818rtc");
659 s
= DO_UPCAST(RTCState
, dev
, dev
);
660 qdev_prop_set_int32(&dev
->qdev
, "base_year", base_year
);
661 qdev_init_nofail(&dev
->qdev
);
663 s
->irq
= intercept_irq
;
665 isa_init_irq(dev
, &s
->irq
, RTC_ISA_IRQ
);
670 static ISADeviceInfo mc146818rtc_info
= {
671 .qdev
.name
= "mc146818rtc",
672 .qdev
.size
= sizeof(RTCState
),
674 .qdev
.vmsd
= &vmstate_rtc
,
676 .qdev
.props
= (Property
[]) {
677 DEFINE_PROP_INT32("base_year", RTCState
, base_year
, 1980),
678 DEFINE_PROP_END_OF_LIST(),
682 static void mc146818rtc_register(void)
684 isa_qdev_register(&mc146818rtc_info
);
686 device_init(mc146818rtc_register
)