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 uint8_t cmos_data
[128];
69 QEMUTimer
*periodic_timer
;
70 int64_t next_periodic_time
;
72 int64_t next_second_time
;
74 uint32_t irq_coalesced
;
77 QEMUTimer
*second_timer
;
78 QEMUTimer
*second_timer2
;
81 static void rtc_irq_raise(qemu_irq irq
) {
82 /* When HPET is operating in legacy mode, RTC interrupts are disabled
83 * We block qemu_irq_raise, but not qemu_irq_lower, in case legacy
84 * mode is established while interrupt is raised. We want it to
85 * be lowered in any case
87 #if defined TARGET_I386 || defined TARGET_X86_64
88 if (!hpet_in_legacy_mode())
93 static void rtc_set_time(RTCState
*s
);
94 static void rtc_copy_date(RTCState
*s
);
96 static void rtc_timer_update(RTCState
*s
, int64_t current_time
)
98 int period_code
, period
;
99 int64_t cur_clock
, next_irq_clock
;
102 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
103 #if defined TARGET_I386 || defined TARGET_X86_64
104 /* disable periodic timer if hpet is in legacy mode, since interrupts are
107 enable_pie
= !hpet_in_legacy_mode();
112 && (((s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) && enable_pie
)
113 || ((s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) && s
->sqw_irq
))) {
114 if (period_code
<= 2)
116 /* period in 32 Khz cycles */
117 period
= 1 << (period_code
- 1);
119 if(period
!= s
->period
)
120 s
->irq_coalesced
= (s
->irq_coalesced
* s
->period
) / period
;
123 /* compute 32 khz clock */
124 cur_clock
= muldiv64(current_time
, 32768, ticks_per_sec
);
125 next_irq_clock
= (cur_clock
& ~(period
- 1)) + period
;
126 s
->next_periodic_time
= muldiv64(next_irq_clock
, ticks_per_sec
, 32768) + 1;
127 qemu_mod_timer(s
->periodic_timer
, s
->next_periodic_time
);
130 s
->irq_coalesced
= 0;
132 qemu_del_timer(s
->periodic_timer
);
136 static void rtc_periodic_timer(void *opaque
)
138 RTCState
*s
= opaque
;
140 rtc_timer_update(s
, s
->next_periodic_time
);
142 if ((s
->cmos_data
[RTC_REG_C
] & 0xc0) && rtc_td_hack
) {
147 if (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) {
148 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
149 rtc_irq_raise(s
->irq
);
151 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) {
152 /* Not square wave at all but we don't want 2048Hz interrupts!
153 Must be seen as a pulse. */
154 qemu_irq_raise(s
->sqw_irq
);
158 static void cmos_ioport_write(void *opaque
, uint32_t addr
, uint32_t data
)
160 RTCState
*s
= opaque
;
162 if ((addr
& 1) == 0) {
163 s
->cmos_index
= data
& 0x7f;
166 printf("cmos: write index=0x%02x val=0x%02x\n",
167 s
->cmos_index
, data
);
169 switch(s
->cmos_index
) {
170 case RTC_SECONDS_ALARM
:
171 case RTC_MINUTES_ALARM
:
172 case RTC_HOURS_ALARM
:
173 /* XXX: not supported */
174 s
->cmos_data
[s
->cmos_index
] = data
;
179 case RTC_DAY_OF_WEEK
:
180 case RTC_DAY_OF_MONTH
:
183 s
->cmos_data
[s
->cmos_index
] = data
;
184 /* if in set mode, do not update the time */
185 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
190 /* UIP bit is read only */
191 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
192 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
193 rtc_timer_update(s
, qemu_get_clock(vm_clock
));
196 if (data
& REG_B_SET
) {
197 /* set mode: reset UIP mode */
198 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
201 /* if disabling set mode, update the time */
202 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) {
206 s
->cmos_data
[RTC_REG_B
] = data
;
207 rtc_timer_update(s
, qemu_get_clock(vm_clock
));
211 /* cannot write to them */
214 s
->cmos_data
[s
->cmos_index
] = data
;
220 static inline int to_bcd(RTCState
*s
, int a
)
222 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
225 return ((a
/ 10) << 4) | (a
% 10);
229 static inline int from_bcd(RTCState
*s
, int a
)
231 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
234 return ((a
>> 4) * 10) + (a
& 0x0f);
238 static void rtc_set_time(RTCState
*s
)
240 struct tm
*tm
= &s
->current_tm
;
242 tm
->tm_sec
= from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
243 tm
->tm_min
= from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
244 tm
->tm_hour
= from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
245 if (!(s
->cmos_data
[RTC_REG_B
] & 0x02) &&
246 (s
->cmos_data
[RTC_HOURS
] & 0x80)) {
249 tm
->tm_wday
= from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
250 tm
->tm_mday
= from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
251 tm
->tm_mon
= from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
252 tm
->tm_year
= from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + s
->base_year
- 1900;
255 static void rtc_copy_date(RTCState
*s
)
257 const struct tm
*tm
= &s
->current_tm
;
260 s
->cmos_data
[RTC_SECONDS
] = to_bcd(s
, tm
->tm_sec
);
261 s
->cmos_data
[RTC_MINUTES
] = to_bcd(s
, tm
->tm_min
);
262 if (s
->cmos_data
[RTC_REG_B
] & 0x02) {
264 s
->cmos_data
[RTC_HOURS
] = to_bcd(s
, tm
->tm_hour
);
267 s
->cmos_data
[RTC_HOURS
] = to_bcd(s
, tm
->tm_hour
% 12);
268 if (tm
->tm_hour
>= 12)
269 s
->cmos_data
[RTC_HOURS
] |= 0x80;
271 s
->cmos_data
[RTC_DAY_OF_WEEK
] = to_bcd(s
, tm
->tm_wday
+ 1);
272 s
->cmos_data
[RTC_DAY_OF_MONTH
] = to_bcd(s
, tm
->tm_mday
);
273 s
->cmos_data
[RTC_MONTH
] = to_bcd(s
, tm
->tm_mon
+ 1);
274 year
= (tm
->tm_year
- s
->base_year
) % 100;
277 s
->cmos_data
[RTC_YEAR
] = to_bcd(s
, year
);
280 /* month is between 0 and 11. */
281 static int get_days_in_month(int month
, int year
)
283 static const int days_tab
[12] = {
284 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
287 if ((unsigned )month
>= 12)
291 if ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
297 /* update 'tm' to the next second */
298 static void rtc_next_second(struct tm
*tm
)
303 if ((unsigned)tm
->tm_sec
>= 60) {
306 if ((unsigned)tm
->tm_min
>= 60) {
309 if ((unsigned)tm
->tm_hour
>= 24) {
313 if ((unsigned)tm
->tm_wday
>= 7)
315 days_in_month
= get_days_in_month(tm
->tm_mon
,
318 if (tm
->tm_mday
< 1) {
320 } else if (tm
->tm_mday
> days_in_month
) {
323 if (tm
->tm_mon
>= 12) {
334 static void rtc_update_second(void *opaque
)
336 RTCState
*s
= opaque
;
339 /* if the oscillator is not in normal operation, we do not update */
340 if ((s
->cmos_data
[RTC_REG_A
] & 0x70) != 0x20) {
341 s
->next_second_time
+= ticks_per_sec
;
342 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
344 rtc_next_second(&s
->current_tm
);
346 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
347 /* update in progress bit */
348 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
350 /* should be 244 us = 8 / 32768 seconds, but currently the
351 timers do not have the necessary resolution. */
352 delay
= (ticks_per_sec
* 1) / 100;
355 qemu_mod_timer(s
->second_timer2
,
356 s
->next_second_time
+ delay
);
360 static void rtc_update_second2(void *opaque
)
362 RTCState
*s
= opaque
;
364 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
369 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
370 if (((s
->cmos_data
[RTC_SECONDS_ALARM
] & 0xc0) == 0xc0 ||
371 s
->cmos_data
[RTC_SECONDS_ALARM
] == s
->current_tm
.tm_sec
) &&
372 ((s
->cmos_data
[RTC_MINUTES_ALARM
] & 0xc0) == 0xc0 ||
373 s
->cmos_data
[RTC_MINUTES_ALARM
] == s
->current_tm
.tm_mon
) &&
374 ((s
->cmos_data
[RTC_HOURS_ALARM
] & 0xc0) == 0xc0 ||
375 s
->cmos_data
[RTC_HOURS_ALARM
] == s
->current_tm
.tm_hour
)) {
377 s
->cmos_data
[RTC_REG_C
] |= 0xa0;
378 rtc_irq_raise(s
->irq
);
382 /* update ended interrupt */
383 if (s
->cmos_data
[RTC_REG_B
] & REG_B_UIE
) {
384 s
->cmos_data
[RTC_REG_C
] |= 0x90;
385 rtc_irq_raise(s
->irq
);
388 /* clear update in progress bit */
389 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
391 s
->next_second_time
+= ticks_per_sec
;
392 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
395 static uint32_t cmos_ioport_read(void *opaque
, uint32_t addr
)
397 RTCState
*s
= opaque
;
399 if ((addr
& 1) == 0) {
402 switch(s
->cmos_index
) {
406 case RTC_DAY_OF_WEEK
:
407 case RTC_DAY_OF_MONTH
:
410 ret
= s
->cmos_data
[s
->cmos_index
];
413 ret
= s
->cmos_data
[s
->cmos_index
];
416 ret
= s
->cmos_data
[s
->cmos_index
];
417 qemu_irq_lower(s
->irq
);
419 if(s
->irq_coalesced
) {
420 apic_reset_irq_delivered();
421 qemu_irq_raise(s
->irq
);
422 if (apic_get_irq_delivered())
427 s
->cmos_data
[RTC_REG_C
] = 0x00;
430 ret
= s
->cmos_data
[s
->cmos_index
];
434 printf("cmos: read index=0x%02x val=0x%02x\n",
441 void rtc_set_memory(RTCState
*s
, int addr
, int val
)
443 if (addr
>= 0 && addr
<= 127)
444 s
->cmos_data
[addr
] = val
;
447 void rtc_set_date(RTCState
*s
, const struct tm
*tm
)
453 /* PC cmos mappings */
454 #define REG_IBM_CENTURY_BYTE 0x32
455 #define REG_IBM_PS2_CENTURY_BYTE 0x37
457 static void rtc_set_date_from_host(RTCState
*s
)
462 /* set the CMOS date */
463 qemu_get_timedate(&tm
, 0);
464 rtc_set_date(s
, &tm
);
466 val
= to_bcd(s
, (tm
.tm_year
/ 100) + 19);
467 rtc_set_memory(s
, REG_IBM_CENTURY_BYTE
, val
);
468 rtc_set_memory(s
, REG_IBM_PS2_CENTURY_BYTE
, val
);
471 static void rtc_save(QEMUFile
*f
, void *opaque
)
473 RTCState
*s
= opaque
;
475 qemu_put_buffer(f
, s
->cmos_data
, 128);
476 qemu_put_8s(f
, &s
->cmos_index
);
478 qemu_put_be32(f
, s
->current_tm
.tm_sec
);
479 qemu_put_be32(f
, s
->current_tm
.tm_min
);
480 qemu_put_be32(f
, s
->current_tm
.tm_hour
);
481 qemu_put_be32(f
, s
->current_tm
.tm_wday
);
482 qemu_put_be32(f
, s
->current_tm
.tm_mday
);
483 qemu_put_be32(f
, s
->current_tm
.tm_mon
);
484 qemu_put_be32(f
, s
->current_tm
.tm_year
);
486 qemu_put_timer(f
, s
->periodic_timer
);
487 qemu_put_be64(f
, s
->next_periodic_time
);
489 qemu_put_be64(f
, s
->next_second_time
);
490 qemu_put_timer(f
, s
->second_timer
);
491 qemu_put_timer(f
, s
->second_timer2
);
494 static int rtc_load(QEMUFile
*f
, void *opaque
, int version_id
)
496 RTCState
*s
= opaque
;
501 qemu_get_buffer(f
, s
->cmos_data
, 128);
502 qemu_get_8s(f
, &s
->cmos_index
);
504 s
->current_tm
.tm_sec
=qemu_get_be32(f
);
505 s
->current_tm
.tm_min
=qemu_get_be32(f
);
506 s
->current_tm
.tm_hour
=qemu_get_be32(f
);
507 s
->current_tm
.tm_wday
=qemu_get_be32(f
);
508 s
->current_tm
.tm_mday
=qemu_get_be32(f
);
509 s
->current_tm
.tm_mon
=qemu_get_be32(f
);
510 s
->current_tm
.tm_year
=qemu_get_be32(f
);
512 qemu_get_timer(f
, s
->periodic_timer
);
513 s
->next_periodic_time
=qemu_get_be64(f
);
515 s
->next_second_time
=qemu_get_be64(f
);
516 qemu_get_timer(f
, s
->second_timer
);
517 qemu_get_timer(f
, s
->second_timer2
);
522 static void rtc_save_td(QEMUFile
*f
, void *opaque
)
524 RTCState
*s
= opaque
;
526 qemu_put_be32(f
, s
->irq_coalesced
);
527 qemu_put_be32(f
, s
->period
);
530 static int rtc_load_td(QEMUFile
*f
, void *opaque
, int version_id
)
532 RTCState
*s
= opaque
;
537 s
->irq_coalesced
= qemu_get_be32(f
);
538 s
->period
= qemu_get_be32(f
);
543 RTCState
*rtc_init_sqw(int base
, qemu_irq irq
, qemu_irq sqw_irq
, int base_year
)
547 s
= qemu_mallocz(sizeof(RTCState
));
550 s
->sqw_irq
= sqw_irq
;
551 s
->cmos_data
[RTC_REG_A
] = 0x26;
552 s
->cmos_data
[RTC_REG_B
] = 0x02;
553 s
->cmos_data
[RTC_REG_C
] = 0x00;
554 s
->cmos_data
[RTC_REG_D
] = 0x80;
556 s
->base_year
= base_year
;
557 rtc_set_date_from_host(s
);
559 s
->periodic_timer
= qemu_new_timer(vm_clock
,
560 rtc_periodic_timer
, s
);
561 s
->second_timer
= qemu_new_timer(vm_clock
,
562 rtc_update_second
, s
);
563 s
->second_timer2
= qemu_new_timer(vm_clock
,
564 rtc_update_second2
, s
);
566 s
->next_second_time
= qemu_get_clock(vm_clock
) + (ticks_per_sec
* 99) / 100;
567 qemu_mod_timer(s
->second_timer2
, s
->next_second_time
);
569 register_ioport_write(base
, 2, 1, cmos_ioport_write
, s
);
570 register_ioport_read(base
, 2, 1, cmos_ioport_read
, s
);
572 register_savevm("mc146818rtc", base
, 1, rtc_save
, rtc_load
, s
);
575 register_savevm("mc146818rtc-td", base
, 1, rtc_save_td
, rtc_load_td
, s
);
580 RTCState
*rtc_init(int base
, qemu_irq irq
, int base_year
)
582 return rtc_init_sqw(base
, irq
, NULL
, base_year
);
585 /* Memory mapped interface */
586 static uint32_t cmos_mm_readb (void *opaque
, target_phys_addr_t addr
)
588 RTCState
*s
= opaque
;
590 return cmos_ioport_read(s
, addr
>> s
->it_shift
) & 0xFF;
593 static void cmos_mm_writeb (void *opaque
,
594 target_phys_addr_t addr
, uint32_t value
)
596 RTCState
*s
= opaque
;
598 cmos_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFF);
601 static uint32_t cmos_mm_readw (void *opaque
, target_phys_addr_t addr
)
603 RTCState
*s
= opaque
;
606 val
= cmos_ioport_read(s
, addr
>> s
->it_shift
) & 0xFFFF;
607 #ifdef TARGET_WORDS_BIGENDIAN
613 static void cmos_mm_writew (void *opaque
,
614 target_phys_addr_t addr
, uint32_t value
)
616 RTCState
*s
= opaque
;
617 #ifdef TARGET_WORDS_BIGENDIAN
618 value
= bswap16(value
);
620 cmos_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
623 static uint32_t cmos_mm_readl (void *opaque
, target_phys_addr_t addr
)
625 RTCState
*s
= opaque
;
628 val
= cmos_ioport_read(s
, addr
>> s
->it_shift
);
629 #ifdef TARGET_WORDS_BIGENDIAN
635 static void cmos_mm_writel (void *opaque
,
636 target_phys_addr_t addr
, uint32_t value
)
638 RTCState
*s
= opaque
;
639 #ifdef TARGET_WORDS_BIGENDIAN
640 value
= bswap32(value
);
642 cmos_ioport_write(s
, addr
>> s
->it_shift
, value
);
645 static CPUReadMemoryFunc
*rtc_mm_read
[] = {
651 static CPUWriteMemoryFunc
*rtc_mm_write
[] = {
657 RTCState
*rtc_mm_init(target_phys_addr_t base
, int it_shift
, qemu_irq irq
,
663 s
= qemu_mallocz(sizeof(RTCState
));
666 s
->cmos_data
[RTC_REG_A
] = 0x26;
667 s
->cmos_data
[RTC_REG_B
] = 0x02;
668 s
->cmos_data
[RTC_REG_C
] = 0x00;
669 s
->cmos_data
[RTC_REG_D
] = 0x80;
671 s
->base_year
= base_year
;
672 rtc_set_date_from_host(s
);
674 s
->periodic_timer
= qemu_new_timer(vm_clock
,
675 rtc_periodic_timer
, s
);
676 s
->second_timer
= qemu_new_timer(vm_clock
,
677 rtc_update_second
, s
);
678 s
->second_timer2
= qemu_new_timer(vm_clock
,
679 rtc_update_second2
, s
);
681 s
->next_second_time
= qemu_get_clock(vm_clock
) + (ticks_per_sec
* 99) / 100;
682 qemu_mod_timer(s
->second_timer2
, s
->next_second_time
);
684 io_memory
= cpu_register_io_memory(0, rtc_mm_read
, rtc_mm_write
, s
);
685 cpu_register_physical_memory(base
, 2 << it_shift
, io_memory
);
687 register_savevm("mc146818rtc", base
, 1, rtc_save
, rtc_load
, s
);
690 register_savevm("mc146818rtc-td", base
, 1, rtc_save_td
, rtc_load_td
, s
);