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 "hpet_emul.h"
31 #include "mc146818rtc.h"
34 //#define DEBUG_COALESCED
37 # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
39 # define CMOS_DPRINTF(format, ...) do { } while (0)
42 #ifdef DEBUG_COALESCED
43 # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
45 # define DPRINTF_C(format, ...) do { } while (0)
48 #define RTC_REINJECT_ON_ACK_COUNT 20
51 #define RTC_SECONDS_ALARM 1
53 #define RTC_MINUTES_ALARM 3
55 #define RTC_HOURS_ALARM 5
56 #define RTC_ALARM_DONT_CARE 0xC0
58 #define RTC_DAY_OF_WEEK 6
59 #define RTC_DAY_OF_MONTH 7
68 #define REG_A_UIP 0x80
70 #define REG_B_SET 0x80
71 #define REG_B_PIE 0x40
72 #define REG_B_AIE 0x20
73 #define REG_B_UIE 0x10
74 #define REG_B_SQWE 0x08
78 #define REG_C_IRQF 0x80
82 typedef struct RTCState
{
84 uint8_t cmos_data
[128];
92 QEMUTimer
*periodic_timer
;
93 int64_t next_periodic_time
;
95 int64_t next_second_time
;
96 uint16_t irq_reinject_on_ack_count
;
97 uint32_t irq_coalesced
;
99 QEMUTimer
*coalesced_timer
;
100 QEMUTimer
*second_timer
;
101 QEMUTimer
*second_timer2
;
104 static void rtc_irq_raise(qemu_irq irq
)
106 /* When HPET is operating in legacy mode, RTC interrupts are disabled
107 * We block qemu_irq_raise, but not qemu_irq_lower, in case legacy
108 * mode is established while interrupt is raised. We want it to
109 * be lowered in any case
111 #if defined TARGET_I386
112 if (!hpet_in_legacy_mode())
117 static void rtc_set_time(RTCState
*s
);
118 static void rtc_copy_date(RTCState
*s
);
121 static void rtc_coalesced_timer_update(RTCState
*s
)
123 if (s
->irq_coalesced
== 0) {
124 qemu_del_timer(s
->coalesced_timer
);
126 /* divide each RTC interval to 2 - 8 smaller intervals */
127 int c
= MIN(s
->irq_coalesced
, 7) + 1;
128 int64_t next_clock
= qemu_get_clock(rtc_clock
) +
129 muldiv64(s
->period
/ c
, get_ticks_per_sec(), 32768);
130 qemu_mod_timer(s
->coalesced_timer
, next_clock
);
134 static void rtc_coalesced_timer(void *opaque
)
136 RTCState
*s
= opaque
;
138 if (s
->irq_coalesced
!= 0) {
139 apic_reset_irq_delivered();
140 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
141 DPRINTF_C("cmos: injecting from timer\n");
142 rtc_irq_raise(s
->irq
);
143 if (apic_get_irq_delivered()) {
145 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
150 rtc_coalesced_timer_update(s
);
154 static void rtc_timer_update(RTCState
*s
, int64_t current_time
)
156 int period_code
, period
;
157 int64_t cur_clock
, next_irq_clock
;
160 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
161 #if defined TARGET_I386
162 /* disable periodic timer if hpet is in legacy mode, since interrupts are
165 enable_pie
= !hpet_in_legacy_mode();
170 && (((s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) && enable_pie
)
171 || ((s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) && s
->sqw_irq
))) {
172 if (period_code
<= 2)
174 /* period in 32 Khz cycles */
175 period
= 1 << (period_code
- 1);
177 if (period
!= s
->period
) {
178 s
->irq_coalesced
= (s
->irq_coalesced
* s
->period
) / period
;
179 DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s
->irq_coalesced
);
183 /* compute 32 khz clock */
184 cur_clock
= muldiv64(current_time
, 32768, get_ticks_per_sec());
185 next_irq_clock
= (cur_clock
& ~(period
- 1)) + period
;
186 s
->next_periodic_time
=
187 muldiv64(next_irq_clock
, get_ticks_per_sec(), 32768) + 1;
188 qemu_mod_timer(s
->periodic_timer
, s
->next_periodic_time
);
191 s
->irq_coalesced
= 0;
193 qemu_del_timer(s
->periodic_timer
);
197 static void rtc_periodic_timer(void *opaque
)
199 RTCState
*s
= opaque
;
201 rtc_timer_update(s
, s
->next_periodic_time
);
202 if (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) {
203 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
206 if (s
->irq_reinject_on_ack_count
>= RTC_REINJECT_ON_ACK_COUNT
)
207 s
->irq_reinject_on_ack_count
= 0;
208 apic_reset_irq_delivered();
209 rtc_irq_raise(s
->irq
);
210 if (!apic_get_irq_delivered()) {
212 rtc_coalesced_timer_update(s
);
213 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
218 rtc_irq_raise(s
->irq
);
220 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) {
221 /* Not square wave at all but we don't want 2048Hz interrupts!
222 Must be seen as a pulse. */
223 qemu_irq_raise(s
->sqw_irq
);
227 static void cmos_ioport_write(void *opaque
, uint32_t addr
, uint32_t data
)
229 RTCState
*s
= opaque
;
231 if ((addr
& 1) == 0) {
232 s
->cmos_index
= data
& 0x7f;
234 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n",
235 s
->cmos_index
, data
);
236 switch(s
->cmos_index
) {
237 case RTC_SECONDS_ALARM
:
238 case RTC_MINUTES_ALARM
:
239 case RTC_HOURS_ALARM
:
240 /* XXX: not supported */
241 s
->cmos_data
[s
->cmos_index
] = data
;
246 case RTC_DAY_OF_WEEK
:
247 case RTC_DAY_OF_MONTH
:
250 s
->cmos_data
[s
->cmos_index
] = data
;
251 /* if in set mode, do not update the time */
252 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
257 /* UIP bit is read only */
258 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
259 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
260 rtc_timer_update(s
, qemu_get_clock(rtc_clock
));
263 if (data
& REG_B_SET
) {
264 /* set mode: reset UIP mode */
265 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
268 /* if disabling set mode, update the time */
269 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) {
273 s
->cmos_data
[RTC_REG_B
] = data
;
274 rtc_timer_update(s
, qemu_get_clock(rtc_clock
));
278 /* cannot write to them */
281 s
->cmos_data
[s
->cmos_index
] = data
;
287 static inline int rtc_to_bcd(RTCState
*s
, int a
)
289 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
292 return ((a
/ 10) << 4) | (a
% 10);
296 static inline int rtc_from_bcd(RTCState
*s
, int a
)
298 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
301 return ((a
>> 4) * 10) + (a
& 0x0f);
305 static void rtc_set_time(RTCState
*s
)
307 struct tm
*tm
= &s
->current_tm
;
309 tm
->tm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
310 tm
->tm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
311 tm
->tm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
312 if (!(s
->cmos_data
[RTC_REG_B
] & 0x02) &&
313 (s
->cmos_data
[RTC_HOURS
] & 0x80)) {
316 tm
->tm_wday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
317 tm
->tm_mday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
318 tm
->tm_mon
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
319 tm
->tm_year
= rtc_from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + s
->base_year
- 1900;
321 rtc_change_mon_event(tm
);
324 static void rtc_copy_date(RTCState
*s
)
326 const struct tm
*tm
= &s
->current_tm
;
329 s
->cmos_data
[RTC_SECONDS
] = rtc_to_bcd(s
, tm
->tm_sec
);
330 s
->cmos_data
[RTC_MINUTES
] = rtc_to_bcd(s
, tm
->tm_min
);
331 if (s
->cmos_data
[RTC_REG_B
] & 0x02) {
333 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
);
336 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
% 12);
337 if (tm
->tm_hour
>= 12)
338 s
->cmos_data
[RTC_HOURS
] |= 0x80;
340 s
->cmos_data
[RTC_DAY_OF_WEEK
] = rtc_to_bcd(s
, tm
->tm_wday
+ 1);
341 s
->cmos_data
[RTC_DAY_OF_MONTH
] = rtc_to_bcd(s
, tm
->tm_mday
);
342 s
->cmos_data
[RTC_MONTH
] = rtc_to_bcd(s
, tm
->tm_mon
+ 1);
343 year
= (tm
->tm_year
- s
->base_year
) % 100;
346 s
->cmos_data
[RTC_YEAR
] = rtc_to_bcd(s
, year
);
349 /* month is between 0 and 11. */
350 static int get_days_in_month(int month
, int year
)
352 static const int days_tab
[12] = {
353 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
356 if ((unsigned )month
>= 12)
360 if ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
366 /* update 'tm' to the next second */
367 static void rtc_next_second(struct tm
*tm
)
372 if ((unsigned)tm
->tm_sec
>= 60) {
375 if ((unsigned)tm
->tm_min
>= 60) {
378 if ((unsigned)tm
->tm_hour
>= 24) {
382 if ((unsigned)tm
->tm_wday
>= 7)
384 days_in_month
= get_days_in_month(tm
->tm_mon
,
387 if (tm
->tm_mday
< 1) {
389 } else if (tm
->tm_mday
> days_in_month
) {
392 if (tm
->tm_mon
>= 12) {
403 static void rtc_update_second(void *opaque
)
405 RTCState
*s
= opaque
;
408 /* if the oscillator is not in normal operation, we do not update */
409 if ((s
->cmos_data
[RTC_REG_A
] & 0x70) != 0x20) {
410 s
->next_second_time
+= get_ticks_per_sec();
411 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
413 rtc_next_second(&s
->current_tm
);
415 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
416 /* update in progress bit */
417 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
419 /* should be 244 us = 8 / 32768 seconds, but currently the
420 timers do not have the necessary resolution. */
421 delay
= (get_ticks_per_sec() * 1) / 100;
424 qemu_mod_timer(s
->second_timer2
,
425 s
->next_second_time
+ delay
);
429 static void rtc_update_second2(void *opaque
)
431 RTCState
*s
= opaque
;
433 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
438 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
439 if (((s
->cmos_data
[RTC_SECONDS_ALARM
] & 0xc0) == 0xc0 ||
440 s
->cmos_data
[RTC_SECONDS_ALARM
] == s
->current_tm
.tm_sec
) &&
441 ((s
->cmos_data
[RTC_MINUTES_ALARM
] & 0xc0) == 0xc0 ||
442 s
->cmos_data
[RTC_MINUTES_ALARM
] == s
->current_tm
.tm_mon
) &&
443 ((s
->cmos_data
[RTC_HOURS_ALARM
] & 0xc0) == 0xc0 ||
444 s
->cmos_data
[RTC_HOURS_ALARM
] == s
->current_tm
.tm_hour
)) {
446 s
->cmos_data
[RTC_REG_C
] |= 0xa0;
447 rtc_irq_raise(s
->irq
);
451 /* update ended interrupt */
452 s
->cmos_data
[RTC_REG_C
] |= REG_C_UF
;
453 if (s
->cmos_data
[RTC_REG_B
] & REG_B_UIE
) {
454 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
455 rtc_irq_raise(s
->irq
);
458 /* clear update in progress bit */
459 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
461 s
->next_second_time
+= get_ticks_per_sec();
462 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
465 static uint32_t cmos_ioport_read(void *opaque
, uint32_t addr
)
467 RTCState
*s
= opaque
;
469 if ((addr
& 1) == 0) {
472 switch(s
->cmos_index
) {
476 case RTC_DAY_OF_WEEK
:
477 case RTC_DAY_OF_MONTH
:
480 ret
= s
->cmos_data
[s
->cmos_index
];
483 ret
= s
->cmos_data
[s
->cmos_index
];
486 ret
= s
->cmos_data
[s
->cmos_index
];
487 qemu_irq_lower(s
->irq
);
489 if(s
->irq_coalesced
&&
490 s
->irq_reinject_on_ack_count
< RTC_REINJECT_ON_ACK_COUNT
) {
491 s
->irq_reinject_on_ack_count
++;
492 apic_reset_irq_delivered();
493 DPRINTF_C("cmos: injecting on ack\n");
494 qemu_irq_raise(s
->irq
);
495 if (apic_get_irq_delivered()) {
497 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
504 s
->cmos_data
[RTC_REG_C
] = 0x00;
507 ret
= s
->cmos_data
[s
->cmos_index
];
510 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
516 void rtc_set_memory(ISADevice
*dev
, int addr
, int val
)
518 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
519 if (addr
>= 0 && addr
<= 127)
520 s
->cmos_data
[addr
] = val
;
523 void rtc_set_date(ISADevice
*dev
, const struct tm
*tm
)
525 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
530 /* PC cmos mappings */
531 #define REG_IBM_CENTURY_BYTE 0x32
532 #define REG_IBM_PS2_CENTURY_BYTE 0x37
534 static void rtc_set_date_from_host(ISADevice
*dev
)
536 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
540 /* set the CMOS date */
541 qemu_get_timedate(&tm
, 0);
542 rtc_set_date(dev
, &tm
);
544 val
= rtc_to_bcd(s
, (tm
.tm_year
/ 100) + 19);
545 rtc_set_memory(dev
, REG_IBM_CENTURY_BYTE
, val
);
546 rtc_set_memory(dev
, REG_IBM_PS2_CENTURY_BYTE
, val
);
549 static int rtc_post_load(void *opaque
, int version_id
)
552 RTCState
*s
= opaque
;
554 if (version_id
>= 2) {
556 rtc_coalesced_timer_update(s
);
563 static const VMStateDescription vmstate_rtc
= {
564 .name
= "mc146818rtc",
566 .minimum_version_id
= 1,
567 .minimum_version_id_old
= 1,
568 .post_load
= rtc_post_load
,
569 .fields
= (VMStateField
[]) {
570 VMSTATE_BUFFER(cmos_data
, RTCState
),
571 VMSTATE_UINT8(cmos_index
, RTCState
),
572 VMSTATE_INT32(current_tm
.tm_sec
, RTCState
),
573 VMSTATE_INT32(current_tm
.tm_min
, RTCState
),
574 VMSTATE_INT32(current_tm
.tm_hour
, RTCState
),
575 VMSTATE_INT32(current_tm
.tm_wday
, RTCState
),
576 VMSTATE_INT32(current_tm
.tm_mday
, RTCState
),
577 VMSTATE_INT32(current_tm
.tm_mon
, RTCState
),
578 VMSTATE_INT32(current_tm
.tm_year
, RTCState
),
579 VMSTATE_TIMER(periodic_timer
, RTCState
),
580 VMSTATE_INT64(next_periodic_time
, RTCState
),
581 VMSTATE_INT64(next_second_time
, RTCState
),
582 VMSTATE_TIMER(second_timer
, RTCState
),
583 VMSTATE_TIMER(second_timer2
, RTCState
),
584 VMSTATE_UINT32_V(irq_coalesced
, RTCState
, 2),
585 VMSTATE_UINT32_V(period
, RTCState
, 2),
586 VMSTATE_END_OF_LIST()
590 static void rtc_reset(void *opaque
)
592 RTCState
*s
= opaque
;
594 s
->cmos_data
[RTC_REG_B
] &= ~(REG_B_PIE
| REG_B_AIE
| REG_B_SQWE
);
595 s
->cmos_data
[RTC_REG_C
] &= ~(REG_C_UF
| REG_C_IRQF
| REG_C_PF
| REG_C_AF
);
597 qemu_irq_lower(s
->irq
);
601 s
->irq_coalesced
= 0;
605 static int rtc_initfn(ISADevice
*dev
)
607 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
611 isa_init_irq(dev
, &s
->irq
, isairq
);
613 s
->cmos_data
[RTC_REG_A
] = 0x26;
614 s
->cmos_data
[RTC_REG_B
] = 0x02;
615 s
->cmos_data
[RTC_REG_C
] = 0x00;
616 s
->cmos_data
[RTC_REG_D
] = 0x80;
618 rtc_set_date_from_host(dev
);
620 s
->periodic_timer
= qemu_new_timer(rtc_clock
, rtc_periodic_timer
, s
);
624 qemu_new_timer(rtc_clock
, rtc_coalesced_timer
, s
);
626 s
->second_timer
= qemu_new_timer(rtc_clock
, rtc_update_second
, s
);
627 s
->second_timer2
= qemu_new_timer(rtc_clock
, rtc_update_second2
, s
);
629 s
->next_second_time
=
630 qemu_get_clock(rtc_clock
) + (get_ticks_per_sec() * 99) / 100;
631 qemu_mod_timer(s
->second_timer2
, s
->next_second_time
);
633 register_ioport_write(base
, 2, 1, cmos_ioport_write
, s
);
634 register_ioport_read(base
, 2, 1, cmos_ioport_read
, s
);
636 qdev_set_legacy_instance_id(&dev
->qdev
, base
, 2);
637 qemu_register_reset(rtc_reset
, s
);
641 ISADevice
*rtc_init(int base_year
)
645 dev
= isa_create("mc146818rtc");
646 qdev_prop_set_int32(&dev
->qdev
, "base_year", base_year
);
647 qdev_init_nofail(&dev
->qdev
);
651 static ISADeviceInfo mc146818rtc_info
= {
652 .qdev
.name
= "mc146818rtc",
653 .qdev
.size
= sizeof(RTCState
),
655 .qdev
.vmsd
= &vmstate_rtc
,
657 .qdev
.props
= (Property
[]) {
658 DEFINE_PROP_INT32("base_year", RTCState
, base_year
, 1980),
659 DEFINE_PROP_END_OF_LIST(),
663 static void mc146818rtc_register(void)
665 isa_qdev_register(&mc146818rtc_info
);
667 device_init(mc146818rtc_register
)