]>
git.proxmox.com Git - qemu.git/blob - hw/mc146818rtc.c
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
60 uint8_t cmos_data
[128];
66 QEMUTimer
*periodic_timer
;
67 int64_t next_periodic_time
;
69 int64_t next_second_time
;
71 uint32_t irq_coalesced
;
74 QEMUTimer
*second_timer
;
75 QEMUTimer
*second_timer2
;
78 static void rtc_irq_raise(qemu_irq irq
) {
79 /* When HPET is operating in legacy mode, RTC interrupts are disabled
80 * We block qemu_irq_raise, but not qemu_irq_lower, in case legacy
81 * mode is established while interrupt is raised. We want it to
82 * be lowered in any case
84 #if defined TARGET_I386 || defined TARGET_X86_64
85 if (!hpet_in_legacy_mode())
90 static void rtc_set_time(RTCState
*s
);
91 static void rtc_copy_date(RTCState
*s
);
93 static void rtc_timer_update(RTCState
*s
, int64_t current_time
)
95 int period_code
, period
;
96 int64_t cur_clock
, next_irq_clock
;
98 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
99 #if defined TARGET_I386 || defined TARGET_X86_64
100 /* disable periodic timer if hpet is in legacy mode, since interrupts are
103 if (period_code
!= 0 && (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) && !hpet_in_legacy_mode()) {
105 if (period_code
!= 0 && (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
)) {
107 if (period_code
<= 2)
109 /* period in 32 Khz cycles */
110 period
= 1 << (period_code
- 1);
112 if(period
!= s
->period
)
113 s
->irq_coalesced
= (s
->irq_coalesced
* s
->period
) / period
;
116 /* compute 32 khz clock */
117 cur_clock
= muldiv64(current_time
, 32768, ticks_per_sec
);
118 next_irq_clock
= (cur_clock
& ~(period
- 1)) + period
;
119 s
->next_periodic_time
= muldiv64(next_irq_clock
, ticks_per_sec
, 32768) + 1;
120 qemu_mod_timer(s
->periodic_timer
, s
->next_periodic_time
);
123 s
->irq_coalesced
= 0;
125 qemu_del_timer(s
->periodic_timer
);
129 static void rtc_periodic_timer(void *opaque
)
131 RTCState
*s
= opaque
;
133 rtc_timer_update(s
, s
->next_periodic_time
);
135 if ((s
->cmos_data
[RTC_REG_C
] & 0xc0) && rtc_td_hack
) {
140 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
141 rtc_irq_raise(s
->irq
);
144 static void cmos_ioport_write(void *opaque
, uint32_t addr
, uint32_t data
)
146 RTCState
*s
= opaque
;
148 if ((addr
& 1) == 0) {
149 s
->cmos_index
= data
& 0x7f;
152 printf("cmos: write index=0x%02x val=0x%02x\n",
153 s
->cmos_index
, data
);
155 switch(s
->cmos_index
) {
156 case RTC_SECONDS_ALARM
:
157 case RTC_MINUTES_ALARM
:
158 case RTC_HOURS_ALARM
:
159 /* XXX: not supported */
160 s
->cmos_data
[s
->cmos_index
] = data
;
165 case RTC_DAY_OF_WEEK
:
166 case RTC_DAY_OF_MONTH
:
169 s
->cmos_data
[s
->cmos_index
] = data
;
170 /* if in set mode, do not update the time */
171 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
176 /* UIP bit is read only */
177 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
178 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
179 rtc_timer_update(s
, qemu_get_clock(vm_clock
));
182 if (data
& REG_B_SET
) {
183 /* set mode: reset UIP mode */
184 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
187 /* if disabling set mode, update the time */
188 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) {
192 s
->cmos_data
[RTC_REG_B
] = data
;
193 rtc_timer_update(s
, qemu_get_clock(vm_clock
));
197 /* cannot write to them */
200 s
->cmos_data
[s
->cmos_index
] = data
;
206 static inline int to_bcd(RTCState
*s
, int a
)
208 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
211 return ((a
/ 10) << 4) | (a
% 10);
215 static inline int from_bcd(RTCState
*s
, int a
)
217 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
220 return ((a
>> 4) * 10) + (a
& 0x0f);
224 static void rtc_set_time(RTCState
*s
)
226 struct tm
*tm
= &s
->current_tm
;
228 tm
->tm_sec
= from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
229 tm
->tm_min
= from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
230 tm
->tm_hour
= from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
231 if (!(s
->cmos_data
[RTC_REG_B
] & 0x02) &&
232 (s
->cmos_data
[RTC_HOURS
] & 0x80)) {
235 tm
->tm_wday
= from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
236 tm
->tm_mday
= from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
237 tm
->tm_mon
= from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
238 tm
->tm_year
= from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + 100;
241 static void rtc_copy_date(RTCState
*s
)
243 const struct tm
*tm
= &s
->current_tm
;
245 s
->cmos_data
[RTC_SECONDS
] = to_bcd(s
, tm
->tm_sec
);
246 s
->cmos_data
[RTC_MINUTES
] = to_bcd(s
, tm
->tm_min
);
247 if (s
->cmos_data
[RTC_REG_B
] & 0x02) {
249 s
->cmos_data
[RTC_HOURS
] = to_bcd(s
, tm
->tm_hour
);
252 s
->cmos_data
[RTC_HOURS
] = to_bcd(s
, tm
->tm_hour
% 12);
253 if (tm
->tm_hour
>= 12)
254 s
->cmos_data
[RTC_HOURS
] |= 0x80;
256 s
->cmos_data
[RTC_DAY_OF_WEEK
] = to_bcd(s
, tm
->tm_wday
+ 1);
257 s
->cmos_data
[RTC_DAY_OF_MONTH
] = to_bcd(s
, tm
->tm_mday
);
258 s
->cmos_data
[RTC_MONTH
] = to_bcd(s
, tm
->tm_mon
+ 1);
259 s
->cmos_data
[RTC_YEAR
] = to_bcd(s
, tm
->tm_year
% 100);
262 /* month is between 0 and 11. */
263 static int get_days_in_month(int month
, int year
)
265 static const int days_tab
[12] = {
266 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
269 if ((unsigned )month
>= 12)
273 if ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
279 /* update 'tm' to the next second */
280 static void rtc_next_second(struct tm
*tm
)
285 if ((unsigned)tm
->tm_sec
>= 60) {
288 if ((unsigned)tm
->tm_min
>= 60) {
291 if ((unsigned)tm
->tm_hour
>= 24) {
295 if ((unsigned)tm
->tm_wday
>= 7)
297 days_in_month
= get_days_in_month(tm
->tm_mon
,
300 if (tm
->tm_mday
< 1) {
302 } else if (tm
->tm_mday
> days_in_month
) {
305 if (tm
->tm_mon
>= 12) {
316 static void rtc_update_second(void *opaque
)
318 RTCState
*s
= opaque
;
321 /* if the oscillator is not in normal operation, we do not update */
322 if ((s
->cmos_data
[RTC_REG_A
] & 0x70) != 0x20) {
323 s
->next_second_time
+= ticks_per_sec
;
324 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
326 rtc_next_second(&s
->current_tm
);
328 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
329 /* update in progress bit */
330 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
332 /* should be 244 us = 8 / 32768 seconds, but currently the
333 timers do not have the necessary resolution. */
334 delay
= (ticks_per_sec
* 1) / 100;
337 qemu_mod_timer(s
->second_timer2
,
338 s
->next_second_time
+ delay
);
342 static void rtc_update_second2(void *opaque
)
344 RTCState
*s
= opaque
;
346 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
351 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
352 if (((s
->cmos_data
[RTC_SECONDS_ALARM
] & 0xc0) == 0xc0 ||
353 s
->cmos_data
[RTC_SECONDS_ALARM
] == s
->current_tm
.tm_sec
) &&
354 ((s
->cmos_data
[RTC_MINUTES_ALARM
] & 0xc0) == 0xc0 ||
355 s
->cmos_data
[RTC_MINUTES_ALARM
] == s
->current_tm
.tm_mon
) &&
356 ((s
->cmos_data
[RTC_HOURS_ALARM
] & 0xc0) == 0xc0 ||
357 s
->cmos_data
[RTC_HOURS_ALARM
] == s
->current_tm
.tm_hour
)) {
359 s
->cmos_data
[RTC_REG_C
] |= 0xa0;
360 rtc_irq_raise(s
->irq
);
364 /* update ended interrupt */
365 if (s
->cmos_data
[RTC_REG_B
] & REG_B_UIE
) {
366 s
->cmos_data
[RTC_REG_C
] |= 0x90;
367 rtc_irq_raise(s
->irq
);
370 /* clear update in progress bit */
371 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
373 s
->next_second_time
+= ticks_per_sec
;
374 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
377 static uint32_t cmos_ioport_read(void *opaque
, uint32_t addr
)
379 RTCState
*s
= opaque
;
381 if ((addr
& 1) == 0) {
384 switch(s
->cmos_index
) {
388 case RTC_DAY_OF_WEEK
:
389 case RTC_DAY_OF_MONTH
:
392 ret
= s
->cmos_data
[s
->cmos_index
];
395 ret
= s
->cmos_data
[s
->cmos_index
];
398 ret
= s
->cmos_data
[s
->cmos_index
];
399 qemu_irq_lower(s
->irq
);
401 if(s
->irq_coalesced
) {
402 apic_reset_irq_delivered();
403 qemu_irq_raise(s
->irq
);
404 if (apic_get_irq_delivered())
409 s
->cmos_data
[RTC_REG_C
] = 0x00;
412 ret
= s
->cmos_data
[s
->cmos_index
];
416 printf("cmos: read index=0x%02x val=0x%02x\n",
423 void rtc_set_memory(RTCState
*s
, int addr
, int val
)
425 if (addr
>= 0 && addr
<= 127)
426 s
->cmos_data
[addr
] = val
;
429 void rtc_set_date(RTCState
*s
, const struct tm
*tm
)
435 /* PC cmos mappings */
436 #define REG_IBM_CENTURY_BYTE 0x32
437 #define REG_IBM_PS2_CENTURY_BYTE 0x37
439 static void rtc_set_date_from_host(RTCState
*s
)
444 /* set the CMOS date */
445 qemu_get_timedate(&tm
, 0);
446 rtc_set_date(s
, &tm
);
448 val
= to_bcd(s
, (tm
.tm_year
/ 100) + 19);
449 rtc_set_memory(s
, REG_IBM_CENTURY_BYTE
, val
);
450 rtc_set_memory(s
, REG_IBM_PS2_CENTURY_BYTE
, val
);
453 static void rtc_save(QEMUFile
*f
, void *opaque
)
455 RTCState
*s
= opaque
;
457 qemu_put_buffer(f
, s
->cmos_data
, 128);
458 qemu_put_8s(f
, &s
->cmos_index
);
460 qemu_put_be32(f
, s
->current_tm
.tm_sec
);
461 qemu_put_be32(f
, s
->current_tm
.tm_min
);
462 qemu_put_be32(f
, s
->current_tm
.tm_hour
);
463 qemu_put_be32(f
, s
->current_tm
.tm_wday
);
464 qemu_put_be32(f
, s
->current_tm
.tm_mday
);
465 qemu_put_be32(f
, s
->current_tm
.tm_mon
);
466 qemu_put_be32(f
, s
->current_tm
.tm_year
);
468 qemu_put_timer(f
, s
->periodic_timer
);
469 qemu_put_be64(f
, s
->next_periodic_time
);
471 qemu_put_be64(f
, s
->next_second_time
);
472 qemu_put_timer(f
, s
->second_timer
);
473 qemu_put_timer(f
, s
->second_timer2
);
476 static int rtc_load(QEMUFile
*f
, void *opaque
, int version_id
)
478 RTCState
*s
= opaque
;
483 qemu_get_buffer(f
, s
->cmos_data
, 128);
484 qemu_get_8s(f
, &s
->cmos_index
);
486 s
->current_tm
.tm_sec
=qemu_get_be32(f
);
487 s
->current_tm
.tm_min
=qemu_get_be32(f
);
488 s
->current_tm
.tm_hour
=qemu_get_be32(f
);
489 s
->current_tm
.tm_wday
=qemu_get_be32(f
);
490 s
->current_tm
.tm_mday
=qemu_get_be32(f
);
491 s
->current_tm
.tm_mon
=qemu_get_be32(f
);
492 s
->current_tm
.tm_year
=qemu_get_be32(f
);
494 qemu_get_timer(f
, s
->periodic_timer
);
495 s
->next_periodic_time
=qemu_get_be64(f
);
497 s
->next_second_time
=qemu_get_be64(f
);
498 qemu_get_timer(f
, s
->second_timer
);
499 qemu_get_timer(f
, s
->second_timer2
);
504 static void rtc_save_td(QEMUFile
*f
, void *opaque
)
506 RTCState
*s
= opaque
;
508 qemu_put_be32(f
, s
->irq_coalesced
);
509 qemu_put_be32(f
, s
->period
);
512 static int rtc_load_td(QEMUFile
*f
, void *opaque
, int version_id
)
514 RTCState
*s
= opaque
;
519 s
->irq_coalesced
= qemu_get_be32(f
);
520 s
->period
= qemu_get_be32(f
);
525 RTCState
*rtc_init(int base
, qemu_irq irq
)
529 s
= qemu_mallocz(sizeof(RTCState
));
534 s
->cmos_data
[RTC_REG_A
] = 0x26;
535 s
->cmos_data
[RTC_REG_B
] = 0x02;
536 s
->cmos_data
[RTC_REG_C
] = 0x00;
537 s
->cmos_data
[RTC_REG_D
] = 0x80;
539 rtc_set_date_from_host(s
);
541 s
->periodic_timer
= qemu_new_timer(vm_clock
,
542 rtc_periodic_timer
, s
);
543 s
->second_timer
= qemu_new_timer(vm_clock
,
544 rtc_update_second
, s
);
545 s
->second_timer2
= qemu_new_timer(vm_clock
,
546 rtc_update_second2
, s
);
548 s
->next_second_time
= qemu_get_clock(vm_clock
) + (ticks_per_sec
* 99) / 100;
549 qemu_mod_timer(s
->second_timer2
, s
->next_second_time
);
551 register_ioport_write(base
, 2, 1, cmos_ioport_write
, s
);
552 register_ioport_read(base
, 2, 1, cmos_ioport_read
, s
);
554 register_savevm("mc146818rtc", base
, 1, rtc_save
, rtc_load
, s
);
557 register_savevm("mc146818rtc-td", base
, 1, rtc_save_td
, rtc_load_td
, s
);
562 /* Memory mapped interface */
563 static uint32_t cmos_mm_readb (void *opaque
, target_phys_addr_t addr
)
565 RTCState
*s
= opaque
;
567 return cmos_ioport_read(s
, addr
>> s
->it_shift
) & 0xFF;
570 static void cmos_mm_writeb (void *opaque
,
571 target_phys_addr_t addr
, uint32_t value
)
573 RTCState
*s
= opaque
;
575 cmos_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFF);
578 static uint32_t cmos_mm_readw (void *opaque
, target_phys_addr_t addr
)
580 RTCState
*s
= opaque
;
583 val
= cmos_ioport_read(s
, addr
>> s
->it_shift
) & 0xFFFF;
584 #ifdef TARGET_WORDS_BIGENDIAN
590 static void cmos_mm_writew (void *opaque
,
591 target_phys_addr_t addr
, uint32_t value
)
593 RTCState
*s
= opaque
;
594 #ifdef TARGET_WORDS_BIGENDIAN
595 value
= bswap16(value
);
597 cmos_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
600 static uint32_t cmos_mm_readl (void *opaque
, target_phys_addr_t addr
)
602 RTCState
*s
= opaque
;
605 val
= cmos_ioport_read(s
, addr
>> s
->it_shift
);
606 #ifdef TARGET_WORDS_BIGENDIAN
612 static void cmos_mm_writel (void *opaque
,
613 target_phys_addr_t addr
, uint32_t value
)
615 RTCState
*s
= opaque
;
616 #ifdef TARGET_WORDS_BIGENDIAN
617 value
= bswap32(value
);
619 cmos_ioport_write(s
, addr
>> s
->it_shift
, value
);
622 static CPUReadMemoryFunc
*rtc_mm_read
[] = {
628 static CPUWriteMemoryFunc
*rtc_mm_write
[] = {
634 RTCState
*rtc_mm_init(target_phys_addr_t base
, int it_shift
, qemu_irq irq
)
639 s
= qemu_mallocz(sizeof(RTCState
));
644 s
->cmos_data
[RTC_REG_A
] = 0x26;
645 s
->cmos_data
[RTC_REG_B
] = 0x02;
646 s
->cmos_data
[RTC_REG_C
] = 0x00;
647 s
->cmos_data
[RTC_REG_D
] = 0x80;
649 rtc_set_date_from_host(s
);
651 s
->periodic_timer
= qemu_new_timer(vm_clock
,
652 rtc_periodic_timer
, s
);
653 s
->second_timer
= qemu_new_timer(vm_clock
,
654 rtc_update_second
, s
);
655 s
->second_timer2
= qemu_new_timer(vm_clock
,
656 rtc_update_second2
, s
);
658 s
->next_second_time
= qemu_get_clock(vm_clock
) + (ticks_per_sec
* 99) / 100;
659 qemu_mod_timer(s
->second_timer2
, s
->next_second_time
);
661 io_memory
= cpu_register_io_memory(0, rtc_mm_read
, rtc_mm_write
, s
);
662 cpu_register_physical_memory(base
, 2 << it_shift
, io_memory
);
664 register_savevm("mc146818rtc", base
, 1, rtc_save
, rtc_load
, s
);
667 register_savevm("mc146818rtc-td", base
, 1, rtc_save_td
, rtc_load_td
, s
);