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 "mc146818rtc.h"
36 //#define DEBUG_COALESCED
39 # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
41 # define CMOS_DPRINTF(format, ...) do { } while (0)
44 #ifdef DEBUG_COALESCED
45 # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
47 # define DPRINTF_C(format, ...) do { } while (0)
50 #define RTC_REINJECT_ON_ACK_COUNT 20
53 #define RTC_SECONDS_ALARM 1
55 #define RTC_MINUTES_ALARM 3
57 #define RTC_HOURS_ALARM 5
58 #define RTC_ALARM_DONT_CARE 0xC0
60 #define RTC_DAY_OF_WEEK 6
61 #define RTC_DAY_OF_MONTH 7
70 #define REG_A_UIP 0x80
72 #define REG_B_SET 0x80
73 #define REG_B_PIE 0x40
74 #define REG_B_AIE 0x20
75 #define REG_B_UIE 0x10
76 #define REG_B_SQWE 0x08
78 #define REG_B_24H 0x02
81 #define REG_C_IRQF 0x80
85 typedef struct RTCState
{
88 uint8_t cmos_data
[128];
96 QEMUTimer
*periodic_timer
;
97 int64_t next_periodic_time
;
99 int64_t next_second_time
;
100 uint16_t irq_reinject_on_ack_count
;
101 uint32_t irq_coalesced
;
103 QEMUTimer
*coalesced_timer
;
104 QEMUTimer
*second_timer
;
105 QEMUTimer
*second_timer2
;
106 Notifier clock_reset_notifier
;
107 LostTickPolicy lost_tick_policy
;
108 Notifier suspend_notifier
;
111 static void rtc_set_time(RTCState
*s
);
112 static void rtc_copy_date(RTCState
*s
);
115 static void rtc_coalesced_timer_update(RTCState
*s
)
117 if (s
->irq_coalesced
== 0) {
118 qemu_del_timer(s
->coalesced_timer
);
120 /* divide each RTC interval to 2 - 8 smaller intervals */
121 int c
= MIN(s
->irq_coalesced
, 7) + 1;
122 int64_t next_clock
= qemu_get_clock_ns(rtc_clock
) +
123 muldiv64(s
->period
/ c
, get_ticks_per_sec(), 32768);
124 qemu_mod_timer(s
->coalesced_timer
, next_clock
);
128 static void rtc_coalesced_timer(void *opaque
)
130 RTCState
*s
= opaque
;
132 if (s
->irq_coalesced
!= 0) {
133 apic_reset_irq_delivered();
134 s
->cmos_data
[RTC_REG_C
] |= 0xc0;
135 DPRINTF_C("cmos: injecting from timer\n");
136 qemu_irq_raise(s
->irq
);
137 if (apic_get_irq_delivered()) {
139 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
144 rtc_coalesced_timer_update(s
);
148 static void rtc_timer_update(RTCState
*s
, int64_t current_time
)
150 int period_code
, period
;
151 int64_t cur_clock
, next_irq_clock
;
153 period_code
= s
->cmos_data
[RTC_REG_A
] & 0x0f;
155 && ((s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
)
156 || ((s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) && s
->sqw_irq
))) {
157 if (period_code
<= 2)
159 /* period in 32 Khz cycles */
160 period
= 1 << (period_code
- 1);
162 if (period
!= s
->period
) {
163 s
->irq_coalesced
= (s
->irq_coalesced
* s
->period
) / period
;
164 DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s
->irq_coalesced
);
168 /* compute 32 khz clock */
169 cur_clock
= muldiv64(current_time
, 32768, get_ticks_per_sec());
170 next_irq_clock
= (cur_clock
& ~(period
- 1)) + period
;
171 s
->next_periodic_time
=
172 muldiv64(next_irq_clock
, get_ticks_per_sec(), 32768) + 1;
173 qemu_mod_timer(s
->periodic_timer
, s
->next_periodic_time
);
176 s
->irq_coalesced
= 0;
178 qemu_del_timer(s
->periodic_timer
);
182 static void rtc_periodic_timer(void *opaque
)
184 RTCState
*s
= opaque
;
186 rtc_timer_update(s
, s
->next_periodic_time
);
187 s
->cmos_data
[RTC_REG_C
] |= REG_C_PF
;
188 if (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) {
189 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
191 if (s
->lost_tick_policy
== LOST_TICK_SLEW
) {
192 if (s
->irq_reinject_on_ack_count
>= RTC_REINJECT_ON_ACK_COUNT
)
193 s
->irq_reinject_on_ack_count
= 0;
194 apic_reset_irq_delivered();
195 qemu_irq_raise(s
->irq
);
196 if (!apic_get_irq_delivered()) {
198 rtc_coalesced_timer_update(s
);
199 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
204 qemu_irq_raise(s
->irq
);
206 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SQWE
) {
207 /* Not square wave at all but we don't want 2048Hz interrupts!
208 Must be seen as a pulse. */
209 qemu_irq_raise(s
->sqw_irq
);
213 static void cmos_ioport_write(void *opaque
, uint32_t addr
, uint32_t data
)
215 RTCState
*s
= opaque
;
217 if ((addr
& 1) == 0) {
218 s
->cmos_index
= data
& 0x7f;
220 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n",
221 s
->cmos_index
, data
);
222 switch(s
->cmos_index
) {
223 case RTC_SECONDS_ALARM
:
224 case RTC_MINUTES_ALARM
:
225 case RTC_HOURS_ALARM
:
226 s
->cmos_data
[s
->cmos_index
] = data
;
231 case RTC_DAY_OF_WEEK
:
232 case RTC_DAY_OF_MONTH
:
235 s
->cmos_data
[s
->cmos_index
] = data
;
236 /* if in set mode, do not update the time */
237 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
242 /* UIP bit is read only */
243 s
->cmos_data
[RTC_REG_A
] = (data
& ~REG_A_UIP
) |
244 (s
->cmos_data
[RTC_REG_A
] & REG_A_UIP
);
245 rtc_timer_update(s
, qemu_get_clock_ns(rtc_clock
));
248 if (data
& REG_B_SET
) {
249 /* set mode: reset UIP mode */
250 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
253 /* if disabling set mode, update the time */
254 if (s
->cmos_data
[RTC_REG_B
] & REG_B_SET
) {
258 if (((s
->cmos_data
[RTC_REG_B
] ^ data
) & (REG_B_DM
| REG_B_24H
)) &&
259 !(data
& REG_B_SET
)) {
260 /* If the time format has changed and not in set mode,
261 update the registers immediately. */
262 s
->cmos_data
[RTC_REG_B
] = data
;
265 s
->cmos_data
[RTC_REG_B
] = data
;
267 rtc_timer_update(s
, qemu_get_clock_ns(rtc_clock
));
271 /* cannot write to them */
274 s
->cmos_data
[s
->cmos_index
] = data
;
280 static inline int rtc_to_bcd(RTCState
*s
, int a
)
282 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
285 return ((a
/ 10) << 4) | (a
% 10);
289 static inline int rtc_from_bcd(RTCState
*s
, int a
)
291 if (s
->cmos_data
[RTC_REG_B
] & REG_B_DM
) {
294 return ((a
>> 4) * 10) + (a
& 0x0f);
298 static void rtc_set_time(RTCState
*s
)
300 struct tm
*tm
= &s
->current_tm
;
302 tm
->tm_sec
= rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS
]);
303 tm
->tm_min
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES
]);
304 tm
->tm_hour
= rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS
] & 0x7f);
305 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_24H
)) {
307 if (s
->cmos_data
[RTC_HOURS
] & 0x80) {
311 tm
->tm_wday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_WEEK
]) - 1;
312 tm
->tm_mday
= rtc_from_bcd(s
, s
->cmos_data
[RTC_DAY_OF_MONTH
]);
313 tm
->tm_mon
= rtc_from_bcd(s
, s
->cmos_data
[RTC_MONTH
]) - 1;
314 tm
->tm_year
= rtc_from_bcd(s
, s
->cmos_data
[RTC_YEAR
]) + s
->base_year
- 1900;
316 rtc_change_mon_event(tm
);
319 static void rtc_copy_date(RTCState
*s
)
321 const struct tm
*tm
= &s
->current_tm
;
324 s
->cmos_data
[RTC_SECONDS
] = rtc_to_bcd(s
, tm
->tm_sec
);
325 s
->cmos_data
[RTC_MINUTES
] = rtc_to_bcd(s
, tm
->tm_min
);
326 if (s
->cmos_data
[RTC_REG_B
] & REG_B_24H
) {
328 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, tm
->tm_hour
);
331 int h
= (tm
->tm_hour
% 12) ? tm
->tm_hour
% 12 : 12;
332 s
->cmos_data
[RTC_HOURS
] = rtc_to_bcd(s
, h
);
333 if (tm
->tm_hour
>= 12)
334 s
->cmos_data
[RTC_HOURS
] |= 0x80;
336 s
->cmos_data
[RTC_DAY_OF_WEEK
] = rtc_to_bcd(s
, tm
->tm_wday
+ 1);
337 s
->cmos_data
[RTC_DAY_OF_MONTH
] = rtc_to_bcd(s
, tm
->tm_mday
);
338 s
->cmos_data
[RTC_MONTH
] = rtc_to_bcd(s
, tm
->tm_mon
+ 1);
339 year
= (tm
->tm_year
- s
->base_year
) % 100;
342 s
->cmos_data
[RTC_YEAR
] = rtc_to_bcd(s
, year
);
345 /* month is between 0 and 11. */
346 static int get_days_in_month(int month
, int year
)
348 static const int days_tab
[12] = {
349 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
352 if ((unsigned )month
>= 12)
356 if ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0))
362 /* update 'tm' to the next second */
363 static void rtc_next_second(struct tm
*tm
)
368 if ((unsigned)tm
->tm_sec
>= 60) {
371 if ((unsigned)tm
->tm_min
>= 60) {
374 if ((unsigned)tm
->tm_hour
>= 24) {
378 if ((unsigned)tm
->tm_wday
>= 7)
380 days_in_month
= get_days_in_month(tm
->tm_mon
,
383 if (tm
->tm_mday
< 1) {
385 } else if (tm
->tm_mday
> days_in_month
) {
388 if (tm
->tm_mon
>= 12) {
399 static void rtc_update_second(void *opaque
)
401 RTCState
*s
= opaque
;
404 /* if the oscillator is not in normal operation, we do not update */
405 if ((s
->cmos_data
[RTC_REG_A
] & 0x70) != 0x20) {
406 s
->next_second_time
+= get_ticks_per_sec();
407 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
409 rtc_next_second(&s
->current_tm
);
411 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
412 /* update in progress bit */
413 s
->cmos_data
[RTC_REG_A
] |= REG_A_UIP
;
415 /* should be 244 us = 8 / 32768 seconds, but currently the
416 timers do not have the necessary resolution. */
417 delay
= (get_ticks_per_sec() * 1) / 100;
420 qemu_mod_timer(s
->second_timer2
,
421 s
->next_second_time
+ delay
);
425 static void rtc_update_second2(void *opaque
)
427 RTCState
*s
= opaque
;
429 if (!(s
->cmos_data
[RTC_REG_B
] & REG_B_SET
)) {
434 if (((s
->cmos_data
[RTC_SECONDS_ALARM
] & 0xc0) == 0xc0 ||
435 rtc_from_bcd(s
, s
->cmos_data
[RTC_SECONDS_ALARM
]) == s
->current_tm
.tm_sec
) &&
436 ((s
->cmos_data
[RTC_MINUTES_ALARM
] & 0xc0) == 0xc0 ||
437 rtc_from_bcd(s
, s
->cmos_data
[RTC_MINUTES_ALARM
]) == s
->current_tm
.tm_min
) &&
438 ((s
->cmos_data
[RTC_HOURS_ALARM
] & 0xc0) == 0xc0 ||
439 rtc_from_bcd(s
, s
->cmos_data
[RTC_HOURS_ALARM
]) == s
->current_tm
.tm_hour
)) {
441 s
->cmos_data
[RTC_REG_C
] |= REG_C_AF
;
442 if (s
->cmos_data
[RTC_REG_B
] & REG_B_AIE
) {
443 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC
);
444 qemu_irq_raise(s
->irq
);
445 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
449 /* update ended interrupt */
450 s
->cmos_data
[RTC_REG_C
] |= REG_C_UF
;
451 if (s
->cmos_data
[RTC_REG_B
] & REG_B_UIE
) {
452 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
;
453 qemu_irq_raise(s
->irq
);
456 /* clear update in progress bit */
457 s
->cmos_data
[RTC_REG_A
] &= ~REG_A_UIP
;
459 s
->next_second_time
+= get_ticks_per_sec();
460 qemu_mod_timer(s
->second_timer
, s
->next_second_time
);
463 static uint32_t cmos_ioport_read(void *opaque
, uint32_t addr
)
465 RTCState
*s
= opaque
;
467 if ((addr
& 1) == 0) {
470 switch(s
->cmos_index
) {
474 case RTC_DAY_OF_WEEK
:
475 case RTC_DAY_OF_MONTH
:
478 ret
= s
->cmos_data
[s
->cmos_index
];
481 ret
= s
->cmos_data
[s
->cmos_index
];
484 ret
= s
->cmos_data
[s
->cmos_index
];
485 qemu_irq_lower(s
->irq
);
486 s
->cmos_data
[RTC_REG_C
] = 0x00;
488 if(s
->irq_coalesced
&&
489 (s
->cmos_data
[RTC_REG_B
] & REG_B_PIE
) &&
490 s
->irq_reinject_on_ack_count
< RTC_REINJECT_ON_ACK_COUNT
) {
491 s
->irq_reinject_on_ack_count
++;
492 s
->cmos_data
[RTC_REG_C
] |= REG_C_IRQF
| REG_C_PF
;
493 apic_reset_irq_delivered();
494 DPRINTF_C("cmos: injecting on ack\n");
495 qemu_irq_raise(s
->irq
);
496 if (apic_get_irq_delivered()) {
498 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
505 ret
= s
->cmos_data
[s
->cmos_index
];
508 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
514 void rtc_set_memory(ISADevice
*dev
, int addr
, int val
)
516 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
517 if (addr
>= 0 && addr
<= 127)
518 s
->cmos_data
[addr
] = val
;
521 void rtc_set_date(ISADevice
*dev
, const struct tm
*tm
)
523 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
528 /* PC cmos mappings */
529 #define REG_IBM_CENTURY_BYTE 0x32
530 #define REG_IBM_PS2_CENTURY_BYTE 0x37
532 static void rtc_set_date_from_host(ISADevice
*dev
)
534 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
538 /* set the CMOS date */
539 qemu_get_timedate(&tm
, 0);
540 rtc_set_date(dev
, &tm
);
542 val
= rtc_to_bcd(s
, (tm
.tm_year
/ 100) + 19);
543 rtc_set_memory(dev
, REG_IBM_CENTURY_BYTE
, val
);
544 rtc_set_memory(dev
, REG_IBM_PS2_CENTURY_BYTE
, val
);
547 static int rtc_post_load(void *opaque
, int version_id
)
550 RTCState
*s
= opaque
;
552 if (version_id
>= 2) {
553 if (s
->lost_tick_policy
== LOST_TICK_SLEW
) {
554 rtc_coalesced_timer_update(s
);
561 static const VMStateDescription vmstate_rtc
= {
562 .name
= "mc146818rtc",
564 .minimum_version_id
= 1,
565 .minimum_version_id_old
= 1,
566 .post_load
= rtc_post_load
,
567 .fields
= (VMStateField
[]) {
568 VMSTATE_BUFFER(cmos_data
, RTCState
),
569 VMSTATE_UINT8(cmos_index
, RTCState
),
570 VMSTATE_INT32(current_tm
.tm_sec
, RTCState
),
571 VMSTATE_INT32(current_tm
.tm_min
, RTCState
),
572 VMSTATE_INT32(current_tm
.tm_hour
, RTCState
),
573 VMSTATE_INT32(current_tm
.tm_wday
, RTCState
),
574 VMSTATE_INT32(current_tm
.tm_mday
, RTCState
),
575 VMSTATE_INT32(current_tm
.tm_mon
, RTCState
),
576 VMSTATE_INT32(current_tm
.tm_year
, RTCState
),
577 VMSTATE_TIMER(periodic_timer
, RTCState
),
578 VMSTATE_INT64(next_periodic_time
, RTCState
),
579 VMSTATE_INT64(next_second_time
, RTCState
),
580 VMSTATE_TIMER(second_timer
, RTCState
),
581 VMSTATE_TIMER(second_timer2
, RTCState
),
582 VMSTATE_UINT32_V(irq_coalesced
, RTCState
, 2),
583 VMSTATE_UINT32_V(period
, RTCState
, 2),
584 VMSTATE_END_OF_LIST()
588 static void rtc_notify_clock_reset(Notifier
*notifier
, void *data
)
590 RTCState
*s
= container_of(notifier
, RTCState
, clock_reset_notifier
);
591 int64_t now
= *(int64_t *)data
;
593 rtc_set_date_from_host(&s
->dev
);
594 s
->next_second_time
= now
+ (get_ticks_per_sec() * 99) / 100;
595 qemu_mod_timer(s
->second_timer2
, s
->next_second_time
);
596 rtc_timer_update(s
, now
);
598 if (s
->lost_tick_policy
== LOST_TICK_SLEW
) {
599 rtc_coalesced_timer_update(s
);
604 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
605 BIOS will read it and start S3 resume at POST Entry */
606 static void rtc_notify_suspend(Notifier
*notifier
, void *data
)
608 RTCState
*s
= container_of(notifier
, RTCState
, suspend_notifier
);
609 rtc_set_memory(&s
->dev
, 0xF, 0xFE);
612 static void rtc_reset(void *opaque
)
614 RTCState
*s
= opaque
;
616 s
->cmos_data
[RTC_REG_B
] &= ~(REG_B_PIE
| REG_B_AIE
| REG_B_SQWE
);
617 s
->cmos_data
[RTC_REG_C
] &= ~(REG_C_UF
| REG_C_IRQF
| REG_C_PF
| REG_C_AF
);
619 qemu_irq_lower(s
->irq
);
622 if (s
->lost_tick_policy
== LOST_TICK_SLEW
) {
623 s
->irq_coalesced
= 0;
628 static const MemoryRegionPortio cmos_portio
[] = {
629 {0, 2, 1, .read
= cmos_ioport_read
, .write
= cmos_ioport_write
},
630 PORTIO_END_OF_LIST(),
633 static const MemoryRegionOps cmos_ops
= {
634 .old_portio
= cmos_portio
637 // FIXME add int32 visitor
638 static void visit_type_int32(Visitor
*v
, int *value
, const char *name
, Error
**errp
)
640 int64_t val
= *value
;
641 visit_type_int(v
, &val
, name
, errp
);
644 static void rtc_get_date(Object
*obj
, Visitor
*v
, void *opaque
,
645 const char *name
, Error
**errp
)
647 ISADevice
*isa
= ISA_DEVICE(obj
);
648 RTCState
*s
= DO_UPCAST(RTCState
, dev
, isa
);
650 visit_start_struct(v
, NULL
, "struct tm", name
, 0, errp
);
651 visit_type_int32(v
, &s
->current_tm
.tm_year
, "tm_year", errp
);
652 visit_type_int32(v
, &s
->current_tm
.tm_mon
, "tm_mon", errp
);
653 visit_type_int32(v
, &s
->current_tm
.tm_mday
, "tm_mday", errp
);
654 visit_type_int32(v
, &s
->current_tm
.tm_hour
, "tm_hour", errp
);
655 visit_type_int32(v
, &s
->current_tm
.tm_min
, "tm_min", errp
);
656 visit_type_int32(v
, &s
->current_tm
.tm_sec
, "tm_sec", errp
);
657 visit_end_struct(v
, errp
);
660 static int rtc_initfn(ISADevice
*dev
)
662 RTCState
*s
= DO_UPCAST(RTCState
, dev
, dev
);
665 s
->cmos_data
[RTC_REG_A
] = 0x26;
666 s
->cmos_data
[RTC_REG_B
] = 0x02;
667 s
->cmos_data
[RTC_REG_C
] = 0x00;
668 s
->cmos_data
[RTC_REG_D
] = 0x80;
670 rtc_set_date_from_host(dev
);
673 switch (s
->lost_tick_policy
) {
676 qemu_new_timer_ns(rtc_clock
, rtc_coalesced_timer
, s
);
678 case LOST_TICK_DISCARD
:
685 s
->periodic_timer
= qemu_new_timer_ns(rtc_clock
, rtc_periodic_timer
, s
);
686 s
->second_timer
= qemu_new_timer_ns(rtc_clock
, rtc_update_second
, s
);
687 s
->second_timer2
= qemu_new_timer_ns(rtc_clock
, rtc_update_second2
, s
);
689 s
->clock_reset_notifier
.notify
= rtc_notify_clock_reset
;
690 qemu_register_clock_reset_notifier(rtc_clock
, &s
->clock_reset_notifier
);
692 s
->suspend_notifier
.notify
= rtc_notify_suspend
;
693 qemu_register_suspend_notifier(&s
->suspend_notifier
);
695 s
->next_second_time
=
696 qemu_get_clock_ns(rtc_clock
) + (get_ticks_per_sec() * 99) / 100;
697 qemu_mod_timer(s
->second_timer2
, s
->next_second_time
);
699 memory_region_init_io(&s
->io
, &cmos_ops
, s
, "rtc", 2);
700 isa_register_ioport(dev
, &s
->io
, base
);
702 qdev_set_legacy_instance_id(&dev
->qdev
, base
, 2);
703 qemu_register_reset(rtc_reset
, s
);
705 object_property_add(OBJECT(s
), "date", "struct tm",
706 rtc_get_date
, NULL
, NULL
, s
, NULL
);
711 ISADevice
*rtc_init(ISABus
*bus
, int base_year
, qemu_irq intercept_irq
)
716 dev
= isa_create(bus
, "mc146818rtc");
717 s
= DO_UPCAST(RTCState
, dev
, dev
);
718 qdev_prop_set_int32(&dev
->qdev
, "base_year", base_year
);
719 qdev_init_nofail(&dev
->qdev
);
721 s
->irq
= intercept_irq
;
723 isa_init_irq(dev
, &s
->irq
, RTC_ISA_IRQ
);
728 static Property mc146818rtc_properties
[] = {
729 DEFINE_PROP_INT32("base_year", RTCState
, base_year
, 1980),
730 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState
,
731 lost_tick_policy
, LOST_TICK_DISCARD
),
732 DEFINE_PROP_END_OF_LIST(),
735 static void rtc_class_initfn(ObjectClass
*klass
, void *data
)
737 DeviceClass
*dc
= DEVICE_CLASS(klass
);
738 ISADeviceClass
*ic
= ISA_DEVICE_CLASS(klass
);
739 ic
->init
= rtc_initfn
;
741 dc
->vmsd
= &vmstate_rtc
;
742 dc
->props
= mc146818rtc_properties
;
745 static TypeInfo mc146818rtc_info
= {
746 .name
= "mc146818rtc",
747 .parent
= TYPE_ISA_DEVICE
,
748 .instance_size
= sizeof(RTCState
),
749 .class_init
= rtc_class_initfn
,
752 static void mc146818rtc_register_types(void)
754 type_register_static(&mc146818rtc_info
);
757 type_init(mc146818rtc_register_types
)