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
{
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_set_time(RTCState
*s
);
105 static void rtc_copy_date(RTCState
*s
);
108 static void rtc_coalesced_timer_update(RTCState
*s
)
110 if (s
->irq_coalesced
== 0) {
111 qemu_del_timer(s
->coalesced_timer
);
113 /* divide each RTC interval to 2 - 8 smaller intervals */
114 int c
= MIN(s
->irq_coalesced
, 7) + 1;
115 int64_t next_clock
= qemu_get_clock(rtc_clock
) +
116 muldiv64(s
->period
/ c
, get_ticks_per_sec(), 32768);
117 qemu_mod_timer(s
->coalesced_timer
, next_clock
);
121 static void rtc_coalesced_timer(void *opaque
)
123 RTCState
*s
= opaque
;
125 if (s
->irq_coalesced
!= 0) {
126 apic_reset_irq_delivered();
127 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
128 DPRINTF_C("cmos: injecting from timer\n");
129 qemu_irq_raise(s
->irq
);
130 if (apic_get_irq_delivered()) {
132 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
137 rtc_coalesced_timer_update(s
);
141 static void rtc_timer_update(RTCState
*s
, int64_t current_time
)
143 int period_code
, period
;
144 int64_t cur_clock
, next_irq_clock
;
146 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
148 && ((s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
)
149 || ((s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) && s
->sqw_irq
))) {
150 if (period_code
<= 2)
152 /* period in 32 Khz cycles */
153 period
= 1 << (period_code
- 1);
155 if (period
!= s
->period
) {
156 s
->irq_coalesced
= (s
->irq_coalesced
* s
->period
) / period
;
157 DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s
->irq_coalesced
);
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 if (s
->irq_reinject_on_ack_count
>= RTC_REINJECT_ON_ACK_COUNT
)
185 s
->irq_reinject_on_ack_count
= 0;
186 apic_reset_irq_delivered();
187 qemu_irq_raise(s
->irq
);
188 if (!apic_get_irq_delivered()) {
190 rtc_coalesced_timer_update(s
);
191 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
196 qemu_irq_raise(s
->irq
);
198 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) {
199 /* Not square wave at all but we don't want 2048Hz interrupts!
200 Must be seen as a pulse. */
201 qemu_irq_raise(s
->sqw_irq
);
205 static void cmos_ioport_write(void *opaque
, uint32_t addr
, uint32_t data
)
207 RTCState
*s
= opaque
;
209 if ((addr
& 1) == 0) {
210 s
->cmos_index
= data
& 0x7f;
212 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n",
213 s
->cmos_index
, data
);
214 switch(s
->cmos_index
) {
215 case RTC_SECONDS_ALARM
:
216 case RTC_MINUTES_ALARM
:
217 case RTC_HOURS_ALARM
:
218 s
->cmos_data
[s
->cmos_index
] = data
;
223 case RTC_DAY_OF_WEEK
:
224 case RTC_DAY_OF_MONTH
:
227 s
->cmos_data
[s
->cmos_index
] = data
;
228 /* if in set mode, do not update the time */
229 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
234 /* UIP bit is read only */
235 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
236 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
237 rtc_timer_update(s
, qemu_get_clock(rtc_clock
));
240 if (data
& REG_B_SET
) {
241 /* set mode: reset UIP mode */
242 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
245 /* if disabling set mode, update the time */
246 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) {
250 if (((s
->cmos_data
[RTC_REG_B
] ^ data
) & (REG_B_DM
| REG_B_24H
)) &&
251 !(data
& REG_B_SET
)) {
252 /* If the time format has changed and not in set mode,
253 update the registers immediately. */
254 s
->cmos_data
[RTC_REG_B
] = data
;
257 s
->cmos_data
[RTC_REG_B
] = data
;
259 rtc_timer_update(s
, qemu_get_clock(rtc_clock
));
263 /* cannot write to them */
266 s
->cmos_data
[s
->cmos_index
] = data
;
272 static inline int rtc_to_bcd(RTCState
*s
, int a
)
274 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
277 return ((a
/ 10) << 4) | (a
% 10);
281 static inline int rtc_from_bcd(RTCState
*s
, int a
)
283 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
286 return ((a
>> 4) * 10) + (a
& 0x0f);
290 static void rtc_set_time(RTCState
*s
)
292 struct tm
*tm
= &s
->current_tm
;
294 tm
->tm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
295 tm
->tm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
296 tm
->tm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
297 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
) &&
298 (s
->cmos_data
[RTC_HOURS
] & 0x80)) {
301 tm
->tm_wday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
302 tm
->tm_mday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
303 tm
->tm_mon
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
304 tm
->tm_year
= rtc_from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + s
->base_year
- 1900;
306 rtc_change_mon_event(tm
);
309 static void rtc_copy_date(RTCState
*s
)
311 const struct tm
*tm
= &s
->current_tm
;
314 s
->cmos_data
[RTC_SECONDS
] = rtc_to_bcd(s
, tm
->tm_sec
);
315 s
->cmos_data
[RTC_MINUTES
] = rtc_to_bcd(s
, tm
->tm_min
);
316 if (s
->cmos_data
[RTC_REG_B
] & REG_B_24H
) {
318 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
);
321 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
% 12);
322 if (tm
->tm_hour
>= 12)
323 s
->cmos_data
[RTC_HOURS
] |= 0x80;
325 s
->cmos_data
[RTC_DAY_OF_WEEK
] = rtc_to_bcd(s
, tm
->tm_wday
+ 1);
326 s
->cmos_data
[RTC_DAY_OF_MONTH
] = rtc_to_bcd(s
, tm
->tm_mday
);
327 s
->cmos_data
[RTC_MONTH
] = rtc_to_bcd(s
, tm
->tm_mon
+ 1);
328 year
= (tm
->tm_year
- s
->base_year
) % 100;
331 s
->cmos_data
[RTC_YEAR
] = rtc_to_bcd(s
, year
);
334 /* month is between 0 and 11. */
335 static int get_days_in_month(int month
, int year
)
337 static const int days_tab
[12] = {
338 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
341 if ((unsigned )month
>= 12)
345 if ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
351 /* update 'tm' to the next second */
352 static void rtc_next_second(struct tm
*tm
)
357 if ((unsigned)tm
->tm_sec
>= 60) {
360 if ((unsigned)tm
->tm_min
>= 60) {
363 if ((unsigned)tm
->tm_hour
>= 24) {
367 if ((unsigned)tm
->tm_wday
>= 7)
369 days_in_month
= get_days_in_month(tm
->tm_mon
,
372 if (tm
->tm_mday
< 1) {
374 } else if (tm
->tm_mday
> days_in_month
) {
377 if (tm
->tm_mon
>= 12) {
388 static void rtc_update_second(void *opaque
)
390 RTCState
*s
= opaque
;
393 /* if the oscillator is not in normal operation, we do not update */
394 if ((s
->cmos_data
[RTC_REG_A
] & 0x70) != 0x20) {
395 s
->next_second_time
+= get_ticks_per_sec();
396 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
398 rtc_next_second(&s
->current_tm
);
400 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
401 /* update in progress bit */
402 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
404 /* should be 244 us = 8 / 32768 seconds, but currently the
405 timers do not have the necessary resolution. */
406 delay
= (get_ticks_per_sec() * 1) / 100;
409 qemu_mod_timer(s
->second_timer2
,
410 s
->next_second_time
+ delay
);
414 static void rtc_update_second2(void *opaque
)
416 RTCState
*s
= opaque
;
418 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
423 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
424 if (((s
->cmos_data
[RTC_SECONDS_ALARM
] & 0xc0) == 0xc0 ||
425 rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS_ALARM
]) == s
->current_tm
.tm_sec
) &&
426 ((s
->cmos_data
[RTC_MINUTES_ALARM
] & 0xc0) == 0xc0 ||
427 rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES_ALARM
]) == s
->current_tm
.tm_min
) &&
428 ((s
->cmos_data
[RTC_HOURS_ALARM
] & 0xc0) == 0xc0 ||
429 rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS_ALARM
]) == s
->current_tm
.tm_hour
)) {
431 s
->cmos_data
[RTC_REG_C
] |= 0xa0;
432 qemu_irq_raise(s
->irq
);
436 /* update ended interrupt */
437 s
->cmos_data
[RTC_REG_C
] |= REG_C_UF
;
438 if (s
->cmos_data
[RTC_REG_B
] & REG_B_UIE
) {
439 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
440 qemu_irq_raise(s
->irq
);
443 /* clear update in progress bit */
444 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
446 s
->next_second_time
+= get_ticks_per_sec();
447 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
450 static uint32_t cmos_ioport_read(void *opaque
, uint32_t addr
)
452 RTCState
*s
= opaque
;
454 if ((addr
& 1) == 0) {
457 switch(s
->cmos_index
) {
461 case RTC_DAY_OF_WEEK
:
462 case RTC_DAY_OF_MONTH
:
465 ret
= s
->cmos_data
[s
->cmos_index
];
468 ret
= s
->cmos_data
[s
->cmos_index
];
471 ret
= s
->cmos_data
[s
->cmos_index
];
472 qemu_irq_lower(s
->irq
);
474 if(s
->irq_coalesced
&&
475 s
->irq_reinject_on_ack_count
< RTC_REINJECT_ON_ACK_COUNT
) {
476 s
->irq_reinject_on_ack_count
++;
477 apic_reset_irq_delivered();
478 DPRINTF_C("cmos: injecting on ack\n");
479 qemu_irq_raise(s
->irq
);
480 if (apic_get_irq_delivered()) {
482 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
489 s
->cmos_data
[RTC_REG_C
] = 0x00;
492 ret
= s
->cmos_data
[s
->cmos_index
];
495 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
501 void rtc_set_memory(ISADevice
*dev
, int addr
, int val
)
503 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
504 if (addr
>= 0 && addr
<= 127)
505 s
->cmos_data
[addr
] = val
;
508 void rtc_set_date(ISADevice
*dev
, const struct tm
*tm
)
510 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
515 /* PC cmos mappings */
516 #define REG_IBM_CENTURY_BYTE 0x32
517 #define REG_IBM_PS2_CENTURY_BYTE 0x37
519 static void rtc_set_date_from_host(ISADevice
*dev
)
521 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
525 /* set the CMOS date */
526 qemu_get_timedate(&tm
, 0);
527 rtc_set_date(dev
, &tm
);
529 val
= rtc_to_bcd(s
, (tm
.tm_year
/ 100) + 19);
530 rtc_set_memory(dev
, REG_IBM_CENTURY_BYTE
, val
);
531 rtc_set_memory(dev
, REG_IBM_PS2_CENTURY_BYTE
, val
);
534 static int rtc_post_load(void *opaque
, int version_id
)
537 RTCState
*s
= opaque
;
539 if (version_id
>= 2) {
541 rtc_coalesced_timer_update(s
);
548 static const VMStateDescription vmstate_rtc
= {
549 .name
= "mc146818rtc",
551 .minimum_version_id
= 1,
552 .minimum_version_id_old
= 1,
553 .post_load
= rtc_post_load
,
554 .fields
= (VMStateField
[]) {
555 VMSTATE_BUFFER(cmos_data
, RTCState
),
556 VMSTATE_UINT8(cmos_index
, RTCState
),
557 VMSTATE_INT32(current_tm
.tm_sec
, RTCState
),
558 VMSTATE_INT32(current_tm
.tm_min
, RTCState
),
559 VMSTATE_INT32(current_tm
.tm_hour
, RTCState
),
560 VMSTATE_INT32(current_tm
.tm_wday
, RTCState
),
561 VMSTATE_INT32(current_tm
.tm_mday
, RTCState
),
562 VMSTATE_INT32(current_tm
.tm_mon
, RTCState
),
563 VMSTATE_INT32(current_tm
.tm_year
, RTCState
),
564 VMSTATE_TIMER(periodic_timer
, RTCState
),
565 VMSTATE_INT64(next_periodic_time
, RTCState
),
566 VMSTATE_INT64(next_second_time
, RTCState
),
567 VMSTATE_TIMER(second_timer
, RTCState
),
568 VMSTATE_TIMER(second_timer2
, RTCState
),
569 VMSTATE_UINT32_V(irq_coalesced
, RTCState
, 2),
570 VMSTATE_UINT32_V(period
, RTCState
, 2),
571 VMSTATE_END_OF_LIST()
575 static void rtc_reset(void *opaque
)
577 RTCState
*s
= opaque
;
579 s
->cmos_data
[RTC_REG_B
] &= ~(REG_B_PIE
| REG_B_AIE
| REG_B_SQWE
);
580 s
->cmos_data
[RTC_REG_C
] &= ~(REG_C_UF
| REG_C_IRQF
| REG_C_PF
| REG_C_AF
);
582 qemu_irq_lower(s
->irq
);
586 s
->irq_coalesced
= 0;
590 static int rtc_initfn(ISADevice
*dev
)
592 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
595 s
->cmos_data
[RTC_REG_A
] = 0x26;
596 s
->cmos_data
[RTC_REG_B
] = 0x02;
597 s
->cmos_data
[RTC_REG_C
] = 0x00;
598 s
->cmos_data
[RTC_REG_D
] = 0x80;
600 rtc_set_date_from_host(dev
);
602 s
->periodic_timer
= qemu_new_timer(rtc_clock
, rtc_periodic_timer
, s
);
606 qemu_new_timer(rtc_clock
, rtc_coalesced_timer
, s
);
608 s
->second_timer
= qemu_new_timer(rtc_clock
, rtc_update_second
, s
);
609 s
->second_timer2
= qemu_new_timer(rtc_clock
, rtc_update_second2
, s
);
611 s
->next_second_time
=
612 qemu_get_clock(rtc_clock
) + (get_ticks_per_sec() * 99) / 100;
613 qemu_mod_timer(s
->second_timer2
, s
->next_second_time
);
615 register_ioport_write(base
, 2, 1, cmos_ioport_write
, s
);
616 register_ioport_read(base
, 2, 1, cmos_ioport_read
, s
);
617 isa_init_ioport_range(dev
, base
, 2);
619 qdev_set_legacy_instance_id(&dev
->qdev
, base
, 2);
620 qemu_register_reset(rtc_reset
, s
);
624 ISADevice
*rtc_init(int base_year
, qemu_irq intercept_irq
)
629 dev
= isa_create("mc146818rtc");
630 s
= DO_UPCAST(RTCState
, dev
, dev
);
631 qdev_prop_set_int32(&dev
->qdev
, "base_year", base_year
);
632 qdev_init_nofail(&dev
->qdev
);
634 s
->irq
= intercept_irq
;
636 isa_init_irq(dev
, &s
->irq
, RTC_ISA_IRQ
);
641 static ISADeviceInfo mc146818rtc_info
= {
642 .qdev
.name
= "mc146818rtc",
643 .qdev
.size
= sizeof(RTCState
),
645 .qdev
.vmsd
= &vmstate_rtc
,
647 .qdev
.props
= (Property
[]) {
648 DEFINE_PROP_INT32("base_year", RTCState
, base_year
, 1980),
649 DEFINE_PROP_END_OF_LIST(),
653 static void mc146818rtc_register(void)
655 isa_qdev_register(&mc146818rtc_info
);
657 device_init(mc146818rtc_register
)