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"
33 #define RTC_REINJECT_ON_ACK_COUNT 1000
36 #define RTC_SECONDS_ALARM 1
38 #define RTC_MINUTES_ALARM 3
40 #define RTC_HOURS_ALARM 5
41 #define RTC_ALARM_DONT_CARE 0xC0
43 #define RTC_DAY_OF_WEEK 6
44 #define RTC_DAY_OF_MONTH 7
53 #define REG_A_UIP 0x80
55 #define REG_B_SET 0x80
56 #define REG_B_PIE 0x40
57 #define REG_B_AIE 0x20
58 #define REG_B_UIE 0x10
59 #define REG_B_SQWE 0x08
63 #define REG_C_IRQF 0x80
69 uint8_t cmos_data
[128];
77 QEMUTimer
*periodic_timer
;
78 int64_t next_periodic_time
;
80 int64_t next_second_time
;
81 uint16_t irq_reinject_on_ack_count
;
82 uint32_t irq_coalesced
;
84 QEMUTimer
*coalesced_timer
;
85 QEMUTimer
*second_timer
;
86 QEMUTimer
*second_timer2
;
89 static void rtc_irq_raise(qemu_irq irq
)
91 /* When HPET is operating in legacy mode, RTC interrupts are disabled
92 * We block qemu_irq_raise, but not qemu_irq_lower, in case legacy
93 * mode is established while interrupt is raised. We want it to
94 * be lowered in any case
96 #if defined TARGET_I386
97 if (!hpet_in_legacy_mode())
102 static void rtc_set_time(RTCState
*s
);
103 static void rtc_copy_date(RTCState
*s
);
106 static void rtc_coalesced_timer_update(RTCState
*s
)
108 if (s
->irq_coalesced
== 0) {
109 qemu_del_timer(s
->coalesced_timer
);
111 /* divide each RTC interval to 2 - 8 smaller intervals */
112 int c
= MIN(s
->irq_coalesced
, 7) + 1;
113 int64_t next_clock
= qemu_get_clock(rtc_clock
) +
114 muldiv64(s
->period
/ c
, get_ticks_per_sec(), 32768);
115 qemu_mod_timer(s
->coalesced_timer
, next_clock
);
119 static void rtc_coalesced_timer(void *opaque
)
121 RTCState
*s
= opaque
;
123 if (s
->irq_coalesced
!= 0) {
124 apic_reset_irq_delivered();
125 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
126 rtc_irq_raise(s
->irq
);
127 if (apic_get_irq_delivered()) {
132 rtc_coalesced_timer_update(s
);
136 static void rtc_timer_update(RTCState
*s
, int64_t current_time
)
138 int period_code
, period
;
139 int64_t cur_clock
, next_irq_clock
;
142 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
143 #if defined TARGET_I386
144 /* disable periodic timer if hpet is in legacy mode, since interrupts are
147 enable_pie
= !hpet_in_legacy_mode();
152 && (((s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) && enable_pie
)
153 || ((s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) && s
->sqw_irq
))) {
154 if (period_code
<= 2)
156 /* period in 32 Khz cycles */
157 period
= 1 << (period_code
- 1);
159 if(period
!= s
->period
)
160 s
->irq_coalesced
= (s
->irq_coalesced
* s
->period
) / period
;
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 rtc_irq_raise(s
->irq
);
190 if (!apic_get_irq_delivered()) {
192 rtc_coalesced_timer_update(s
);
196 rtc_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;
213 printf("cmos: write index=0x%02x val=0x%02x\n",
214 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 /* XXX: not supported */
221 s
->cmos_data
[s
->cmos_index
] = data
;
226 case RTC_DAY_OF_WEEK
:
227 case RTC_DAY_OF_MONTH
:
230 s
->cmos_data
[s
->cmos_index
] = data
;
231 /* if in set mode, do not update the time */
232 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
237 /* UIP bit is read only */
238 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
239 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
240 rtc_timer_update(s
, qemu_get_clock(rtc_clock
));
243 if (data
& REG_B_SET
) {
244 /* set mode: reset UIP mode */
245 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
248 /* if disabling set mode, update the time */
249 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) {
253 s
->cmos_data
[RTC_REG_B
] = data
;
254 rtc_timer_update(s
, qemu_get_clock(rtc_clock
));
258 /* cannot write to them */
261 s
->cmos_data
[s
->cmos_index
] = data
;
267 static inline int rtc_to_bcd(RTCState
*s
, int a
)
269 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
272 return ((a
/ 10) << 4) | (a
% 10);
276 static inline int rtc_from_bcd(RTCState
*s
, int a
)
278 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
281 return ((a
>> 4) * 10) + (a
& 0x0f);
285 static void rtc_set_time(RTCState
*s
)
287 struct tm
*tm
= &s
->current_tm
;
289 tm
->tm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
290 tm
->tm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
291 tm
->tm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
292 if (!(s
->cmos_data
[RTC_REG_B
] & 0x02) &&
293 (s
->cmos_data
[RTC_HOURS
] & 0x80)) {
296 tm
->tm_wday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
297 tm
->tm_mday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
298 tm
->tm_mon
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
299 tm
->tm_year
= rtc_from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + s
->base_year
- 1900;
302 static void rtc_copy_date(RTCState
*s
)
304 const struct tm
*tm
= &s
->current_tm
;
307 s
->cmos_data
[RTC_SECONDS
] = rtc_to_bcd(s
, tm
->tm_sec
);
308 s
->cmos_data
[RTC_MINUTES
] = rtc_to_bcd(s
, tm
->tm_min
);
309 if (s
->cmos_data
[RTC_REG_B
] & 0x02) {
311 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
);
314 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
% 12);
315 if (tm
->tm_hour
>= 12)
316 s
->cmos_data
[RTC_HOURS
] |= 0x80;
318 s
->cmos_data
[RTC_DAY_OF_WEEK
] = rtc_to_bcd(s
, tm
->tm_wday
+ 1);
319 s
->cmos_data
[RTC_DAY_OF_MONTH
] = rtc_to_bcd(s
, tm
->tm_mday
);
320 s
->cmos_data
[RTC_MONTH
] = rtc_to_bcd(s
, tm
->tm_mon
+ 1);
321 year
= (tm
->tm_year
- s
->base_year
) % 100;
324 s
->cmos_data
[RTC_YEAR
] = rtc_to_bcd(s
, year
);
327 /* month is between 0 and 11. */
328 static int get_days_in_month(int month
, int year
)
330 static const int days_tab
[12] = {
331 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
334 if ((unsigned )month
>= 12)
338 if ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
344 /* update 'tm' to the next second */
345 static void rtc_next_second(struct tm
*tm
)
350 if ((unsigned)tm
->tm_sec
>= 60) {
353 if ((unsigned)tm
->tm_min
>= 60) {
356 if ((unsigned)tm
->tm_hour
>= 24) {
360 if ((unsigned)tm
->tm_wday
>= 7)
362 days_in_month
= get_days_in_month(tm
->tm_mon
,
365 if (tm
->tm_mday
< 1) {
367 } else if (tm
->tm_mday
> days_in_month
) {
370 if (tm
->tm_mon
>= 12) {
381 static void rtc_update_second(void *opaque
)
383 RTCState
*s
= opaque
;
386 /* if the oscillator is not in normal operation, we do not update */
387 if ((s
->cmos_data
[RTC_REG_A
] & 0x70) != 0x20) {
388 s
->next_second_time
+= get_ticks_per_sec();
389 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
391 rtc_next_second(&s
->current_tm
);
393 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
394 /* update in progress bit */
395 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
397 /* should be 244 us = 8 / 32768 seconds, but currently the
398 timers do not have the necessary resolution. */
399 delay
= (get_ticks_per_sec() * 1) / 100;
402 qemu_mod_timer(s
->second_timer2
,
403 s
->next_second_time
+ delay
);
407 static void rtc_update_second2(void *opaque
)
409 RTCState
*s
= opaque
;
411 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
416 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
417 if (((s
->cmos_data
[RTC_SECONDS_ALARM
] & 0xc0) == 0xc0 ||
418 s
->cmos_data
[RTC_SECONDS_ALARM
] == s
->current_tm
.tm_sec
) &&
419 ((s
->cmos_data
[RTC_MINUTES_ALARM
] & 0xc0) == 0xc0 ||
420 s
->cmos_data
[RTC_MINUTES_ALARM
] == s
->current_tm
.tm_mon
) &&
421 ((s
->cmos_data
[RTC_HOURS_ALARM
] & 0xc0) == 0xc0 ||
422 s
->cmos_data
[RTC_HOURS_ALARM
] == s
->current_tm
.tm_hour
)) {
424 s
->cmos_data
[RTC_REG_C
] |= 0xa0;
425 rtc_irq_raise(s
->irq
);
429 /* update ended interrupt */
430 s
->cmos_data
[RTC_REG_C
] |= REG_C_UF
;
431 if (s
->cmos_data
[RTC_REG_B
] & REG_B_UIE
) {
432 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
433 rtc_irq_raise(s
->irq
);
436 /* clear update in progress bit */
437 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
439 s
->next_second_time
+= get_ticks_per_sec();
440 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
443 static uint32_t cmos_ioport_read(void *opaque
, uint32_t addr
)
445 RTCState
*s
= opaque
;
447 if ((addr
& 1) == 0) {
450 switch(s
->cmos_index
) {
454 case RTC_DAY_OF_WEEK
:
455 case RTC_DAY_OF_MONTH
:
458 ret
= s
->cmos_data
[s
->cmos_index
];
461 ret
= s
->cmos_data
[s
->cmos_index
];
464 ret
= s
->cmos_data
[s
->cmos_index
];
465 qemu_irq_lower(s
->irq
);
467 if(s
->irq_coalesced
&&
468 s
->irq_reinject_on_ack_count
< RTC_REINJECT_ON_ACK_COUNT
) {
469 s
->irq_reinject_on_ack_count
++;
470 apic_reset_irq_delivered();
471 qemu_irq_raise(s
->irq
);
472 if (apic_get_irq_delivered())
478 s
->cmos_data
[RTC_REG_C
] = 0x00;
481 ret
= s
->cmos_data
[s
->cmos_index
];
485 printf("cmos: read index=0x%02x val=0x%02x\n",
492 void rtc_set_memory(RTCState
*s
, int addr
, int val
)
494 if (addr
>= 0 && addr
<= 127)
495 s
->cmos_data
[addr
] = val
;
498 void rtc_set_date(RTCState
*s
, const struct tm
*tm
)
504 /* PC cmos mappings */
505 #define REG_IBM_CENTURY_BYTE 0x32
506 #define REG_IBM_PS2_CENTURY_BYTE 0x37
508 static void rtc_set_date_from_host(RTCState
*s
)
513 /* set the CMOS date */
514 qemu_get_timedate(&tm
, 0);
515 rtc_set_date(s
, &tm
);
517 val
= rtc_to_bcd(s
, (tm
.tm_year
/ 100) + 19);
518 rtc_set_memory(s
, REG_IBM_CENTURY_BYTE
, val
);
519 rtc_set_memory(s
, REG_IBM_PS2_CENTURY_BYTE
, val
);
522 static int rtc_post_load(void *opaque
, int version_id
)
525 RTCState
*s
= opaque
;
527 if (version_id
>= 2) {
529 rtc_coalesced_timer_update(s
);
536 static const VMStateDescription vmstate_rtc
= {
537 .name
= "mc146818rtc",
539 .minimum_version_id
= 1,
540 .minimum_version_id_old
= 1,
541 .post_load
= rtc_post_load
,
542 .fields
= (VMStateField
[]) {
543 VMSTATE_BUFFER(cmos_data
, RTCState
),
544 VMSTATE_UINT8(cmos_index
, RTCState
),
545 VMSTATE_INT32(current_tm
.tm_sec
, RTCState
),
546 VMSTATE_INT32(current_tm
.tm_min
, RTCState
),
547 VMSTATE_INT32(current_tm
.tm_hour
, RTCState
),
548 VMSTATE_INT32(current_tm
.tm_wday
, RTCState
),
549 VMSTATE_INT32(current_tm
.tm_mday
, RTCState
),
550 VMSTATE_INT32(current_tm
.tm_mon
, RTCState
),
551 VMSTATE_INT32(current_tm
.tm_year
, RTCState
),
552 VMSTATE_TIMER(periodic_timer
, RTCState
),
553 VMSTATE_INT64(next_periodic_time
, RTCState
),
554 VMSTATE_INT64(next_second_time
, RTCState
),
555 VMSTATE_TIMER(second_timer
, RTCState
),
556 VMSTATE_TIMER(second_timer2
, RTCState
),
557 VMSTATE_UINT32_V(irq_coalesced
, RTCState
, 2),
558 VMSTATE_UINT32_V(period
, RTCState
, 2),
559 VMSTATE_END_OF_LIST()
563 static void rtc_reset(void *opaque
)
565 RTCState
*s
= opaque
;
567 s
->cmos_data
[RTC_REG_B
] &= ~(REG_B_PIE
| REG_B_AIE
| REG_B_SQWE
);
568 s
->cmos_data
[RTC_REG_C
] &= ~(REG_C_UF
| REG_C_IRQF
| REG_C_PF
| REG_C_AF
);
570 qemu_irq_lower(s
->irq
);
574 s
->irq_coalesced
= 0;
578 static int rtc_initfn(ISADevice
*dev
)
580 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
584 isa_init_irq(dev
, &s
->irq
, isairq
);
586 s
->cmos_data
[RTC_REG_A
] = 0x26;
587 s
->cmos_data
[RTC_REG_B
] = 0x02;
588 s
->cmos_data
[RTC_REG_C
] = 0x00;
589 s
->cmos_data
[RTC_REG_D
] = 0x80;
591 rtc_set_date_from_host(s
);
593 s
->periodic_timer
= qemu_new_timer(rtc_clock
, rtc_periodic_timer
, s
);
597 qemu_new_timer(rtc_clock
, rtc_coalesced_timer
, s
);
599 s
->second_timer
= qemu_new_timer(rtc_clock
, rtc_update_second
, s
);
600 s
->second_timer2
= qemu_new_timer(rtc_clock
, rtc_update_second2
, s
);
602 s
->next_second_time
=
603 qemu_get_clock(rtc_clock
) + (get_ticks_per_sec() * 99) / 100;
604 qemu_mod_timer(s
->second_timer2
, s
->next_second_time
);
606 register_ioport_write(base
, 2, 1, cmos_ioport_write
, s
);
607 register_ioport_read(base
, 2, 1, cmos_ioport_read
, s
);
609 vmstate_register(base
, &vmstate_rtc
, s
);
610 qemu_register_reset(rtc_reset
, s
);
614 RTCState
*rtc_init(int base_year
)
618 dev
= isa_create("mc146818rtc");
619 qdev_prop_set_int32(&dev
->qdev
, "base_year", base_year
);
620 qdev_init_nofail(&dev
->qdev
);
621 return DO_UPCAST(RTCState
, dev
, dev
);
624 static ISADeviceInfo mc146818rtc_info
= {
625 .qdev
.name
= "mc146818rtc",
626 .qdev
.size
= sizeof(RTCState
),
629 .qdev
.props
= (Property
[]) {
630 DEFINE_PROP_INT32("base_year", RTCState
, base_year
, 1980),
631 DEFINE_PROP_END_OF_LIST(),
635 static void mc146818rtc_register(void)
637 isa_qdev_register(&mc146818rtc_info
);
639 device_init(mc146818rtc_register
)