]> git.proxmox.com Git - mirror_qemu.git/blame - hw/timer/mc146818rtc.c
Include hw/qdev-properties.h less
[mirror_qemu.git] / hw / timer / mc146818rtc.c
CommitLineData
80cabfad
FB
1/*
2 * QEMU MC146818 RTC emulation
5fafdf24 3 *
80cabfad 4 * Copyright (c) 2003-2004 Fabrice Bellard
5fafdf24 5 *
80cabfad
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
e688df6b 24
b6a0aa05 25#include "qemu/osdep.h"
a8d25326 26#include "qemu-common.h"
f348b6d1 27#include "qemu/cutils.h"
0b8fa32f 28#include "qemu/module.h"
f348b6d1 29#include "qemu/bcd.h"
64552b6b 30#include "hw/irq.h"
a27bd6c7 31#include "hw/qdev-properties.h"
1de7afc9 32#include "qemu/timer.h"
9c17d615 33#include "sysemu/sysemu.h"
1dfb1b2d 34#include "sysemu/replay.h"
71e8a915 35#include "sysemu/reset.h"
0d09e41a 36#include "hw/timer/mc146818rtc.h"
d6454270 37#include "migration/vmstate.h"
e688df6b 38#include "qapi/error.h"
b0227cdb
MA
39#include "qapi/qapi-commands-misc-target.h"
40#include "qapi/qapi-events-misc-target.h"
7b1b5d19 41#include "qapi/visitor.h"
f98167ea 42#include "exec/address-spaces.h"
80cabfad 43
d362e757 44#ifdef TARGET_I386
0d09e41a 45#include "hw/i386/apic.h"
d362e757
JK
46#endif
47
80cabfad 48//#define DEBUG_CMOS
aa6f63ff 49//#define DEBUG_COALESCED
80cabfad 50
ec51e364
IY
51#ifdef DEBUG_CMOS
52# define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
53#else
54# define CMOS_DPRINTF(format, ...) do { } while (0)
55#endif
56
aa6f63ff
BS
57#ifdef DEBUG_COALESCED
58# define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
59#else
60# define DPRINTF_C(format, ...) do { } while (0)
61#endif
62
00cf5774
PB
63#define SEC_PER_MIN 60
64#define MIN_PER_HOUR 60
65#define SEC_PER_HOUR 3600
66#define HOUR_PER_DAY 24
67#define SEC_PER_DAY 86400
56038ef6 68
dd17765b 69#define RTC_REINJECT_ON_ACK_COUNT 20
e46deaba 70#define RTC_CLOCK_RATE 32768
13566fe3 71#define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768)
ba32edab 72
0e41271e
AF
73#define MC146818_RTC(obj) OBJECT_CHECK(RTCState, (obj), TYPE_MC146818_RTC)
74
1d914fa0 75typedef struct RTCState {
0e41271e
AF
76 ISADevice parent_obj;
77
b2c5009b 78 MemoryRegion io;
f98167ea 79 MemoryRegion coalesced_io;
dff38e7b
FB
80 uint8_t cmos_data[128];
81 uint8_t cmos_index;
32e0c826 82 int32_t base_year;
56038ef6
YZ
83 uint64_t base_rtc;
84 uint64_t last_update;
85 int64_t offset;
d537cf6c 86 qemu_irq irq;
18c6e2ff 87 int it_shift;
dff38e7b
FB
88 /* periodic timer */
89 QEMUTimer *periodic_timer;
90 int64_t next_periodic_time;
56038ef6
YZ
91 /* update-ended timer */
92 QEMUTimer *update_timer;
00cf5774 93 uint64_t next_alarm_time;
ba32edab 94 uint16_t irq_reinject_on_ack_count;
73822ec8
AL
95 uint32_t irq_coalesced;
96 uint32_t period;
93b66569 97 QEMUTimer *coalesced_timer;
17604dac 98 Notifier clock_reset_notifier;
433acf0d 99 LostTickPolicy lost_tick_policy;
da98c8eb 100 Notifier suspend_notifier;
f2ae8abf 101 QLIST_ENTRY(RTCState) link;
1d914fa0 102} RTCState;
dff38e7b
FB
103
104static void rtc_set_time(RTCState *s);
56038ef6 105static void rtc_update_time(RTCState *s);
e2826cf4 106static void rtc_set_cmos(RTCState *s, const struct tm *tm);
56038ef6 107static inline int rtc_from_bcd(RTCState *s, int a);
00cf5774 108static uint64_t get_next_alarm(RTCState *s);
56038ef6 109
41a9b8b2
YZ
110static inline bool rtc_running(RTCState *s)
111{
112 return (!(s->cmos_data[RTC_REG_B] & REG_B_SET) &&
113 (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20);
114}
115
56038ef6
YZ
116static uint64_t get_guest_rtc_ns(RTCState *s)
117{
884f17c2 118 uint64_t guest_clock = qemu_clock_get_ns(rtc_clock);
56038ef6 119
9be38598 120 return s->base_rtc * NANOSECONDS_PER_SECOND +
73bcb24d 121 guest_clock - s->last_update + s->offset;
56038ef6 122}
dff38e7b 123
93b66569
AL
124static void rtc_coalesced_timer_update(RTCState *s)
125{
126 if (s->irq_coalesced == 0) {
bc72ad67 127 timer_del(s->coalesced_timer);
93b66569
AL
128 } else {
129 /* divide each RTC interval to 2 - 8 smaller intervals */
7cd9681b 130 int c = MIN(s->irq_coalesced, 7) + 1;
884f17c2 131 int64_t next_clock = qemu_clock_get_ns(rtc_clock) +
bd618eab 132 periodic_clock_to_ns(s->period / c);
bc72ad67 133 timer_mod(s->coalesced_timer, next_clock);
93b66569
AL
134 }
135}
136
e0c8b950
XG
137static QLIST_HEAD(, RTCState) rtc_devices =
138 QLIST_HEAD_INITIALIZER(rtc_devices);
139
388ad5d2 140#ifdef TARGET_I386
e0c8b950
XG
141void qmp_rtc_reset_reinjection(Error **errp)
142{
143 RTCState *s;
144
145 QLIST_FOREACH(s, &rtc_devices, link) {
146 s->irq_coalesced = 0;
147 }
148}
149
150static bool rtc_policy_slew_deliver_irq(RTCState *s)
151{
152 apic_reset_irq_delivered();
153 qemu_irq_raise(s->irq);
154 return apic_get_irq_delivered();
155}
156
93b66569
AL
157static void rtc_coalesced_timer(void *opaque)
158{
159 RTCState *s = opaque;
160
161 if (s->irq_coalesced != 0) {
93b66569 162 s->cmos_data[RTC_REG_C] |= 0xc0;
aa6f63ff 163 DPRINTF_C("cmos: injecting from timer\n");
e0c8b950 164 if (rtc_policy_slew_deliver_irq(s)) {
93b66569 165 s->irq_coalesced--;
aa6f63ff
BS
166 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
167 s->irq_coalesced);
93b66569
AL
168 }
169 }
170
171 rtc_coalesced_timer_update(s);
172}
e0c8b950
XG
173#else
174static bool rtc_policy_slew_deliver_irq(RTCState *s)
175{
176 assert(0);
177 return false;
178}
93b66569
AL
179#endif
180
369b4135 181static uint32_t rtc_periodic_clock_ticks(RTCState *s)
dff38e7b 182{
369b4135
TY
183 int period_code;
184
185 if (!(s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
186 return 0;
187 }
dff38e7b
FB
188
189 period_code = s->cmos_data[RTC_REG_A] & 0x0f;
369b4135 190
bd618eab 191 return periodic_period_to_clock(period_code);
369b4135
TY
192}
193
194/*
195 * handle periodic timer. @old_period indicates the periodic timer update
196 * is just due to period adjustment.
197 */
198static void
199periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
200{
201 uint32_t period;
202 int64_t cur_clock, next_irq_clock, lost_clock = 0;
203
204 period = rtc_periodic_clock_ticks(s);
205
206 if (period) {
dff38e7b 207 /* compute 32 khz clock */
73bcb24d
RS
208 cur_clock =
209 muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
210
369b4135
TY
211 /*
212 * if the periodic timer's update is due to period re-configuration,
213 * we should count the clock since last interrupt.
214 */
215 if (old_period) {
216 int64_t last_periodic_clock, next_periodic_clock;
217
218 next_periodic_clock = muldiv64(s->next_periodic_time,
219 RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
220 last_periodic_clock = next_periodic_clock - old_period;
221 lost_clock = cur_clock - last_periodic_clock;
222 assert(lost_clock >= 0);
223 }
224
369b4135
TY
225 /*
226 * s->irq_coalesced can change for two reasons:
227 *
228 * a) if one or more periodic timer interrupts have been lost,
229 * lost_clock will be more that a period.
230 *
231 * b) when the period may be reconfigured, we expect the OS to
232 * treat delayed tick as the new period. So, when switching
233 * from a shorter to a longer period, scale down the missing,
234 * because the OS will treat past delayed ticks as longer
235 * (leftovers are put back into lost_clock). When switching
236 * to a shorter period, scale up the missing ticks since the
237 * OS handler will treat past delayed ticks as shorter.
238 */
239 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
240 uint32_t old_irq_coalesced = s->irq_coalesced;
241
242 s->period = period;
243 lost_clock += old_irq_coalesced * old_period;
244 s->irq_coalesced = lost_clock / s->period;
245 lost_clock %= s->period;
246 if (old_irq_coalesced != s->irq_coalesced ||
247 old_period != s->period) {
248 DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
249 "period scaled from %d to %d\n", old_irq_coalesced,
250 s->irq_coalesced, old_period, s->period);
251 rtc_coalesced_timer_update(s);
252 }
388ad5d2 253 } else {
369b4135
TY
254 /*
255 * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW
256 * is not used, we should make the time progress anyway.
257 */
258 lost_clock = MIN(lost_clock, period);
259 }
260
261 assert(lost_clock >= 0 && lost_clock <= period);
262
263 next_irq_clock = cur_clock + period - lost_clock;
bd618eab 264 s->next_periodic_time = periodic_clock_to_ns(next_irq_clock) + 1;
bc72ad67 265 timer_mod(s->periodic_timer, s->next_periodic_time);
dff38e7b 266 } else {
73822ec8 267 s->irq_coalesced = 0;
bc72ad67 268 timer_del(s->periodic_timer);
dff38e7b
FB
269 }
270}
271
272static void rtc_periodic_timer(void *opaque)
273{
274 RTCState *s = opaque;
275
369b4135 276 periodic_timer_update(s, s->next_periodic_time, 0);
663447d4 277 s->cmos_data[RTC_REG_C] |= REG_C_PF;
100d9891 278 if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
663447d4 279 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
104059da 280 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
ba32edab 281 if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT)
e0c8b950
XG
282 s->irq_reinject_on_ack_count = 0;
283 if (!rtc_policy_slew_deliver_irq(s)) {
93b66569
AL
284 s->irq_coalesced++;
285 rtc_coalesced_timer_update(s);
aa6f63ff
BS
286 DPRINTF_C("cmos: coalesced irqs increased to %d\n",
287 s->irq_coalesced);
93b66569
AL
288 }
289 } else
e0c8b950 290 qemu_irq_raise(s->irq);
100d9891 291 }
dff38e7b 292}
80cabfad 293
56038ef6
YZ
294/* handle update-ended timer */
295static void check_update_timer(RTCState *s)
296{
297 uint64_t next_update_time;
298 uint64_t guest_nsec;
00cf5774 299 int next_alarm_sec;
56038ef6 300
41a9b8b2
YZ
301 /* From the data sheet: "Holding the dividers in reset prevents
302 * interrupts from operating, while setting the SET bit allows"
6a51d83a 303 * them to occur.
56038ef6 304 */
41a9b8b2 305 if ((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) {
33f21e4f 306 assert((s->cmos_data[RTC_REG_A] & REG_A_UIP) == 0);
bc72ad67 307 timer_del(s->update_timer);
41a9b8b2
YZ
308 return;
309 }
56038ef6 310
13566fe3 311 guest_nsec = get_guest_rtc_ns(s) % NANOSECONDS_PER_SECOND;
884f17c2 312 next_update_time = qemu_clock_get_ns(rtc_clock)
13566fe3 313 + NANOSECONDS_PER_SECOND - guest_nsec;
00cf5774
PB
314
315 /* Compute time of next alarm. One second is already accounted
316 * for in next_update_time.
317 */
318 next_alarm_sec = get_next_alarm(s);
13566fe3
SH
319 s->next_alarm_time = next_update_time +
320 (next_alarm_sec - 1) * NANOSECONDS_PER_SECOND;
00cf5774 321
33f21e4f
PB
322 /* If update_in_progress latched the UIP bit, we must keep the timer
323 * programmed to the next second, so that UIP is cleared. Otherwise,
324 * if UF is already set, we might be able to optimize.
325 */
326 if (!(s->cmos_data[RTC_REG_A] & REG_A_UIP) &&
327 (s->cmos_data[RTC_REG_C] & REG_C_UF)) {
6a51d83a
PB
328 /* If AF cannot change (i.e. either it is set already, or
329 * SET=1 and then the time is not updated), nothing to do.
330 */
331 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) ||
332 (s->cmos_data[RTC_REG_C] & REG_C_AF)) {
333 timer_del(s->update_timer);
334 return;
335 }
336
00cf5774
PB
337 /* UF is set, but AF is clear. Program the timer to target
338 * the alarm time. */
339 next_update_time = s->next_alarm_time;
340 }
e93379b0 341 if (next_update_time != timer_expire_time_ns(s->update_timer)) {
bc72ad67 342 timer_mod(s->update_timer, next_update_time);
56038ef6
YZ
343 }
344}
345
346static inline uint8_t convert_hour(RTCState *s, uint8_t hour)
347{
348 if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
349 hour %= 12;
350 if (s->cmos_data[RTC_HOURS] & 0x80) {
351 hour += 12;
352 }
353 }
354 return hour;
355}
356
00cf5774 357static uint64_t get_next_alarm(RTCState *s)
56038ef6 358{
00cf5774
PB
359 int32_t alarm_sec, alarm_min, alarm_hour, cur_hour, cur_min, cur_sec;
360 int32_t hour, min, sec;
361
362 rtc_update_time(s);
56038ef6
YZ
363
364 alarm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]);
365 alarm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]);
366 alarm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]);
00cf5774 367 alarm_hour = alarm_hour == -1 ? -1 : convert_hour(s, alarm_hour);
56038ef6
YZ
368
369 cur_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
370 cur_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
371 cur_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS]);
372 cur_hour = convert_hour(s, cur_hour);
373
00cf5774
PB
374 if (alarm_hour == -1) {
375 alarm_hour = cur_hour;
376 if (alarm_min == -1) {
377 alarm_min = cur_min;
378 if (alarm_sec == -1) {
379 alarm_sec = cur_sec + 1;
380 } else if (cur_sec > alarm_sec) {
381 alarm_min++;
382 }
383 } else if (cur_min == alarm_min) {
384 if (alarm_sec == -1) {
385 alarm_sec = cur_sec + 1;
386 } else {
387 if (cur_sec > alarm_sec) {
388 alarm_hour++;
389 }
390 }
391 if (alarm_sec == SEC_PER_MIN) {
392 /* wrap to next hour, minutes is not in don't care mode */
393 alarm_sec = 0;
394 alarm_hour++;
395 }
396 } else if (cur_min > alarm_min) {
397 alarm_hour++;
398 }
399 } else if (cur_hour == alarm_hour) {
400 if (alarm_min == -1) {
401 alarm_min = cur_min;
402 if (alarm_sec == -1) {
403 alarm_sec = cur_sec + 1;
404 } else if (cur_sec > alarm_sec) {
405 alarm_min++;
406 }
407
408 if (alarm_sec == SEC_PER_MIN) {
409 alarm_sec = 0;
410 alarm_min++;
411 }
412 /* wrap to next day, hour is not in don't care mode */
413 alarm_min %= MIN_PER_HOUR;
414 } else if (cur_min == alarm_min) {
415 if (alarm_sec == -1) {
416 alarm_sec = cur_sec + 1;
417 }
418 /* wrap to next day, hours+minutes not in don't care mode */
419 alarm_sec %= SEC_PER_MIN;
420 }
56038ef6 421 }
56038ef6 422
00cf5774
PB
423 /* values that are still don't care fire at the next min/sec */
424 if (alarm_min == -1) {
425 alarm_min = 0;
426 }
427 if (alarm_sec == -1) {
428 alarm_sec = 0;
429 }
430
431 /* keep values in range */
432 if (alarm_sec == SEC_PER_MIN) {
433 alarm_sec = 0;
434 alarm_min++;
435 }
436 if (alarm_min == MIN_PER_HOUR) {
437 alarm_min = 0;
438 alarm_hour++;
439 }
440 alarm_hour %= HOUR_PER_DAY;
441
442 hour = alarm_hour - cur_hour;
443 min = hour * MIN_PER_HOUR + alarm_min - cur_min;
444 sec = min * SEC_PER_MIN + alarm_sec - cur_sec;
445 return sec <= 0 ? sec + SEC_PER_DAY : sec;
56038ef6
YZ
446}
447
448static void rtc_update_timer(void *opaque)
449{
450 RTCState *s = opaque;
451 int32_t irqs = REG_C_UF;
452 int32_t new_irqs;
453
41a9b8b2
YZ
454 assert((s->cmos_data[RTC_REG_A] & 0x60) != 0x60);
455
56038ef6
YZ
456 /* UIP might have been latched, update time and clear it. */
457 rtc_update_time(s);
458 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
459
884f17c2 460 if (qemu_clock_get_ns(rtc_clock) >= s->next_alarm_time) {
56038ef6
YZ
461 irqs |= REG_C_AF;
462 if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
fb064112 463 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC, NULL);
56038ef6
YZ
464 }
465 }
00cf5774 466
56038ef6
YZ
467 new_irqs = irqs & ~s->cmos_data[RTC_REG_C];
468 s->cmos_data[RTC_REG_C] |= irqs;
469 if ((new_irqs & s->cmos_data[RTC_REG_B]) != 0) {
470 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
471 qemu_irq_raise(s->irq);
472 }
473 check_update_timer(s);
474}
475
0da8c842
AG
476static void cmos_ioport_write(void *opaque, hwaddr addr,
477 uint64_t data, unsigned size)
80cabfad 478{
b41a2cd1 479 RTCState *s = opaque;
369b4135 480 uint32_t old_period;
9a6e2dcf 481 bool update_periodic_timer;
80cabfad
FB
482
483 if ((addr & 1) == 0) {
484 s->cmos_index = data & 0x7f;
485 } else {
c5539cb4 486 CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02" PRIx64 "\n",
ec51e364 487 s->cmos_index, data);
dff38e7b 488 switch(s->cmos_index) {
80cabfad
FB
489 case RTC_SECONDS_ALARM:
490 case RTC_MINUTES_ALARM:
491 case RTC_HOURS_ALARM:
80cabfad 492 s->cmos_data[s->cmos_index] = data;
56038ef6 493 check_update_timer(s);
80cabfad 494 break;
7cd9681b 495 case RTC_IBM_PS2_CENTURY_BYTE:
e67edb94
PB
496 s->cmos_index = RTC_CENTURY;
497 /* fall through */
498 case RTC_CENTURY:
80cabfad
FB
499 case RTC_SECONDS:
500 case RTC_MINUTES:
501 case RTC_HOURS:
502 case RTC_DAY_OF_WEEK:
503 case RTC_DAY_OF_MONTH:
504 case RTC_MONTH:
505 case RTC_YEAR:
506 s->cmos_data[s->cmos_index] = data;
dff38e7b 507 /* if in set mode, do not update the time */
41a9b8b2 508 if (rtc_running(s)) {
dff38e7b 509 rtc_set_time(s);
56038ef6 510 check_update_timer(s);
dff38e7b 511 }
80cabfad
FB
512 break;
513 case RTC_REG_A:
9a6e2dcf 514 update_periodic_timer = (s->cmos_data[RTC_REG_A] ^ data) & 0x0f;
369b4135 515 old_period = rtc_periodic_clock_ticks(s);
9a6e2dcf 516
41a9b8b2
YZ
517 if ((data & 0x60) == 0x60) {
518 if (rtc_running(s)) {
519 rtc_update_time(s);
520 }
521 /* What happens to UIP when divider reset is enabled is
522 * unclear from the datasheet. Shouldn't matter much
523 * though.
524 */
525 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
526 } else if (((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) &&
527 (data & 0x70) <= 0x20) {
528 /* when the divider reset is removed, the first update cycle
529 * begins one-half second later*/
530 if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
531 s->offset = 500000000;
532 rtc_set_time(s);
533 }
534 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
535 }
dff38e7b
FB
536 /* UIP bit is read only */
537 s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
538 (s->cmos_data[RTC_REG_A] & REG_A_UIP);
9a6e2dcf
XG
539
540 if (update_periodic_timer) {
369b4135
TY
541 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock),
542 old_period);
9a6e2dcf
XG
543 }
544
56038ef6 545 check_update_timer(s);
dff38e7b 546 break;
80cabfad 547 case RTC_REG_B:
9a6e2dcf
XG
548 update_periodic_timer = (s->cmos_data[RTC_REG_B] ^ data)
549 & REG_B_PIE;
369b4135 550 old_period = rtc_periodic_clock_ticks(s);
9a6e2dcf 551
dff38e7b 552 if (data & REG_B_SET) {
56038ef6 553 /* update cmos to when the rtc was stopping */
41a9b8b2 554 if (rtc_running(s)) {
56038ef6
YZ
555 rtc_update_time(s);
556 }
dff38e7b
FB
557 /* set mode: reset UIP mode */
558 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
559 data &= ~REG_B_UIE;
560 } else {
561 /* if disabling set mode, update the time */
41a9b8b2
YZ
562 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) &&
563 (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20) {
13566fe3 564 s->offset = get_guest_rtc_ns(s) % NANOSECONDS_PER_SECOND;
dff38e7b
FB
565 rtc_set_time(s);
566 }
567 }
9324cc50
YZ
568 /* if an interrupt flag is already set when the interrupt
569 * becomes enabled, raise an interrupt immediately. */
570 if (data & s->cmos_data[RTC_REG_C] & REG_C_MASK) {
571 s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
572 qemu_irq_raise(s->irq);
573 } else {
574 s->cmos_data[RTC_REG_C] &= ~REG_C_IRQF;
575 qemu_irq_lower(s->irq);
576 }
bedc572e 577 s->cmos_data[RTC_REG_B] = data;
9a6e2dcf
XG
578
579 if (update_periodic_timer) {
369b4135
TY
580 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock),
581 old_period);
9a6e2dcf
XG
582 }
583
56038ef6 584 check_update_timer(s);
80cabfad
FB
585 break;
586 case RTC_REG_C:
587 case RTC_REG_D:
588 /* cannot write to them */
589 break;
590 default:
591 s->cmos_data[s->cmos_index] = data;
592 break;
593 }
594 }
595}
596
abd0c6bd 597static inline int rtc_to_bcd(RTCState *s, int a)
80cabfad 598{
6f1bf24d 599 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
dff38e7b
FB
600 return a;
601 } else {
602 return ((a / 10) << 4) | (a % 10);
603 }
80cabfad
FB
604}
605
abd0c6bd 606static inline int rtc_from_bcd(RTCState *s, int a)
80cabfad 607{
00cf5774
PB
608 if ((a & 0xc0) == 0xc0) {
609 return -1;
610 }
6f1bf24d 611 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
dff38e7b
FB
612 return a;
613 } else {
614 return ((a >> 4) * 10) + (a & 0x0f);
615 }
616}
617
e2826cf4 618static void rtc_get_time(RTCState *s, struct tm *tm)
dff38e7b 619{
abd0c6bd
PB
620 tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
621 tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
622 tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
3b89eb43
PB
623 if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
624 tm->tm_hour %= 12;
625 if (s->cmos_data[RTC_HOURS] & 0x80) {
626 tm->tm_hour += 12;
627 }
43f493af 628 }
abd0c6bd
PB
629 tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
630 tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
631 tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
b8994faf
PB
632 tm->tm_year =
633 rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year +
634 rtc_from_bcd(s, s->cmos_data[RTC_CENTURY]) * 100 - 1900;
e2826cf4
PB
635}
636
637static void rtc_set_time(RTCState *s)
638{
639 struct tm tm;
80cd3478 640
e2826cf4 641 rtc_get_time(s, &tm);
e2826cf4 642 s->base_rtc = mktimegm(&tm);
884f17c2 643 s->last_update = qemu_clock_get_ns(rtc_clock);
56038ef6 644
3ab72385 645 qapi_event_send_rtc_change(qemu_timedate_diff(&tm));
43f493af
FB
646}
647
e2826cf4 648static void rtc_set_cmos(RTCState *s, const struct tm *tm)
43f493af 649{
42fc73a1 650 int year;
dff38e7b 651
abd0c6bd
PB
652 s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec);
653 s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min);
c29cd656 654 if (s->cmos_data[RTC_REG_B] & REG_B_24H) {
43f493af 655 /* 24 hour format */
abd0c6bd 656 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
43f493af
FB
657 } else {
658 /* 12 hour format */
3b89eb43
PB
659 int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12;
660 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h);
43f493af
FB
661 if (tm->tm_hour >= 12)
662 s->cmos_data[RTC_HOURS] |= 0x80;
663 }
abd0c6bd
PB
664 s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
665 s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday);
666 s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
b8994faf
PB
667 year = tm->tm_year + 1900 - s->base_year;
668 s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year % 100);
669 s->cmos_data[RTC_CENTURY] = rtc_to_bcd(s, year / 100);
43f493af
FB
670}
671
56038ef6 672static void rtc_update_time(RTCState *s)
43f493af 673{
56038ef6
YZ
674 struct tm ret;
675 time_t guest_sec;
676 int64_t guest_nsec;
677
678 guest_nsec = get_guest_rtc_ns(s);
13566fe3 679 guest_sec = guest_nsec / NANOSECONDS_PER_SECOND;
56038ef6 680 gmtime_r(&guest_sec, &ret);
02c6ccc6
AH
681
682 /* Is SET flag of Register B disabled? */
683 if ((s->cmos_data[RTC_REG_B] & REG_B_SET) == 0) {
684 rtc_set_cmos(s, &ret);
685 }
43f493af
FB
686}
687
56038ef6 688static int update_in_progress(RTCState *s)
43f493af 689{
56038ef6 690 int64_t guest_nsec;
3b46e624 691
41a9b8b2 692 if (!rtc_running(s)) {
56038ef6 693 return 0;
dff38e7b 694 }
e93379b0
AB
695 if (timer_pending(s->update_timer)) {
696 int64_t next_update_time = timer_expire_time_ns(s->update_timer);
56038ef6 697 /* Latch UIP until the timer expires. */
884f17c2
AB
698 if (qemu_clock_get_ns(rtc_clock) >=
699 (next_update_time - UIP_HOLD_LENGTH)) {
56038ef6
YZ
700 s->cmos_data[RTC_REG_A] |= REG_A_UIP;
701 return 1;
dff38e7b
FB
702 }
703 }
704
56038ef6
YZ
705 guest_nsec = get_guest_rtc_ns(s);
706 /* UIP bit will be set at last 244us of every second. */
13566fe3
SH
707 if ((guest_nsec % NANOSECONDS_PER_SECOND) >=
708 (NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH)) {
56038ef6 709 return 1;
dff38e7b 710 }
56038ef6 711 return 0;
80cabfad
FB
712}
713
0da8c842
AG
714static uint64_t cmos_ioport_read(void *opaque, hwaddr addr,
715 unsigned size)
80cabfad 716{
b41a2cd1 717 RTCState *s = opaque;
80cabfad
FB
718 int ret;
719 if ((addr & 1) == 0) {
720 return 0xff;
721 } else {
722 switch(s->cmos_index) {
7cd9681b 723 case RTC_IBM_PS2_CENTURY_BYTE:
e67edb94
PB
724 s->cmos_index = RTC_CENTURY;
725 /* fall through */
726 case RTC_CENTURY:
80cabfad
FB
727 case RTC_SECONDS:
728 case RTC_MINUTES:
729 case RTC_HOURS:
730 case RTC_DAY_OF_WEEK:
731 case RTC_DAY_OF_MONTH:
732 case RTC_MONTH:
733 case RTC_YEAR:
56038ef6
YZ
734 /* if not in set mode, calibrate cmos before
735 * reading*/
41a9b8b2 736 if (rtc_running(s)) {
56038ef6
YZ
737 rtc_update_time(s);
738 }
80cabfad
FB
739 ret = s->cmos_data[s->cmos_index];
740 break;
741 case RTC_REG_A:
33f21e4f 742 ret = s->cmos_data[s->cmos_index];
56038ef6 743 if (update_in_progress(s)) {
33f21e4f 744 ret |= REG_A_UIP;
56038ef6 745 }
80cabfad
FB
746 break;
747 case RTC_REG_C:
748 ret = s->cmos_data[s->cmos_index];
d537cf6c 749 qemu_irq_lower(s->irq);
fbc15e27 750 s->cmos_data[RTC_REG_C] = 0x00;
56038ef6
YZ
751 if (ret & (REG_C_UF | REG_C_AF)) {
752 check_update_timer(s);
753 }
e0c8b950 754
ba32edab 755 if(s->irq_coalesced &&
fbc15e27 756 (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
ba32edab
GN
757 s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
758 s->irq_reinject_on_ack_count++;
fbc15e27 759 s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF;
aa6f63ff 760 DPRINTF_C("cmos: injecting on ack\n");
e0c8b950 761 if (rtc_policy_slew_deliver_irq(s)) {
ba32edab 762 s->irq_coalesced--;
aa6f63ff
BS
763 DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
764 s->irq_coalesced);
765 }
ba32edab 766 }
80cabfad
FB
767 break;
768 default:
769 ret = s->cmos_data[s->cmos_index];
770 break;
771 }
ec51e364
IY
772 CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
773 s->cmos_index, ret);
80cabfad
FB
774 return ret;
775 }
776}
777
1d914fa0 778void rtc_set_memory(ISADevice *dev, int addr, int val)
dff38e7b 779{
0e41271e 780 RTCState *s = MC146818_RTC(dev);
dff38e7b
FB
781 if (addr >= 0 && addr <= 127)
782 s->cmos_data[addr] = val;
783}
784
b8b7456d
IM
785int rtc_get_memory(ISADevice *dev, int addr)
786{
787 RTCState *s = MC146818_RTC(dev);
788 assert(addr >= 0 && addr <= 127);
789 return s->cmos_data[addr];
790}
791
1d914fa0 792static void rtc_set_date_from_host(ISADevice *dev)
ea55ffb3 793{
0e41271e 794 RTCState *s = MC146818_RTC(dev);
f6503059 795 struct tm tm;
ea55ffb3 796
f6503059 797 qemu_get_timedate(&tm, 0);
56038ef6
YZ
798
799 s->base_rtc = mktimegm(&tm);
884f17c2 800 s->last_update = qemu_clock_get_ns(rtc_clock);
56038ef6
YZ
801 s->offset = 0;
802
803 /* set the CMOS date */
e2826cf4 804 rtc_set_cmos(s, &tm);
ea55ffb3
TS
805}
806
44b1ff31 807static int rtc_pre_save(void *opaque)
3cf294ee
JB
808{
809 RTCState *s = opaque;
810
811 rtc_update_time(s);
44b1ff31
DDAG
812
813 return 0;
3cf294ee
JB
814}
815
6b075b8a 816static int rtc_post_load(void *opaque, int version_id)
80cabfad 817{
dff38e7b
FB
818 RTCState *s = opaque;
819
3cf294ee 820 if (version_id <= 2 || rtc_clock == QEMU_CLOCK_REALTIME) {
56038ef6
YZ
821 rtc_set_time(s);
822 s->offset = 0;
823 check_update_timer(s);
824 }
825
1dfb1b2d
PD
826 /* The periodic timer is deterministic in record/replay mode,
827 * so there is no need to update it after loading the vmstate.
828 * Reading RTC here would misalign record and replay.
829 */
830 if (replay_mode == REPLAY_MODE_NONE) {
831 uint64_t now = qemu_clock_get_ns(rtc_clock);
832 if (now < s->next_periodic_time ||
833 now > (s->next_periodic_time + get_max_clock_jump())) {
369b4135 834 periodic_timer_update(s, qemu_clock_get_ns(rtc_clock), 0);
1dfb1b2d 835 }
ae46e239
PD
836 }
837
048c74c4 838 if (version_id >= 2) {
104059da 839 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
048c74c4
JQ
840 rtc_coalesced_timer_update(s);
841 }
048c74c4 842 }
73822ec8
AL
843 return 0;
844}
73822ec8 845
5cd8cada
JQ
846static bool rtc_irq_reinject_on_ack_count_needed(void *opaque)
847{
848 RTCState *s = (RTCState *)opaque;
849 return s->irq_reinject_on_ack_count != 0;
850}
851
0b102153 852static const VMStateDescription vmstate_rtc_irq_reinject_on_ack_count = {
bb426311 853 .name = "mc146818rtc/irq_reinject_on_ack_count",
0b102153
PD
854 .version_id = 1,
855 .minimum_version_id = 1,
5cd8cada 856 .needed = rtc_irq_reinject_on_ack_count_needed,
0b102153
PD
857 .fields = (VMStateField[]) {
858 VMSTATE_UINT16(irq_reinject_on_ack_count, RTCState),
859 VMSTATE_END_OF_LIST()
860 }
861};
862
6b075b8a
JQ
863static const VMStateDescription vmstate_rtc = {
864 .name = "mc146818rtc",
56038ef6 865 .version_id = 3,
6b075b8a 866 .minimum_version_id = 1,
3cf294ee 867 .pre_save = rtc_pre_save,
6b075b8a 868 .post_load = rtc_post_load,
d49805ae 869 .fields = (VMStateField[]) {
6b075b8a
JQ
870 VMSTATE_BUFFER(cmos_data, RTCState),
871 VMSTATE_UINT8(cmos_index, RTCState),
89166459 872 VMSTATE_UNUSED(7*4),
e720677e 873 VMSTATE_TIMER_PTR(periodic_timer, RTCState),
6b075b8a 874 VMSTATE_INT64(next_periodic_time, RTCState),
56038ef6 875 VMSTATE_UNUSED(3*8),
6b075b8a
JQ
876 VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
877 VMSTATE_UINT32_V(period, RTCState, 2),
56038ef6
YZ
878 VMSTATE_UINT64_V(base_rtc, RTCState, 3),
879 VMSTATE_UINT64_V(last_update, RTCState, 3),
880 VMSTATE_INT64_V(offset, RTCState, 3),
e720677e 881 VMSTATE_TIMER_PTR_V(update_timer, RTCState, 3),
00cf5774 882 VMSTATE_UINT64_V(next_alarm_time, RTCState, 3),
6b075b8a 883 VMSTATE_END_OF_LIST()
0b102153 884 },
5cd8cada
JQ
885 .subsections = (const VMStateDescription*[]) {
886 &vmstate_rtc_irq_reinject_on_ack_count,
887 NULL
6b075b8a
JQ
888 }
889};
890
17604dac
JK
891static void rtc_notify_clock_reset(Notifier *notifier, void *data)
892{
893 RTCState *s = container_of(notifier, RTCState, clock_reset_notifier);
894 int64_t now = *(int64_t *)data;
895
0e41271e 896 rtc_set_date_from_host(ISA_DEVICE(s));
369b4135 897 periodic_timer_update(s, now, 0);
56038ef6 898 check_update_timer(s);
388ad5d2 899
104059da 900 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
17604dac
JK
901 rtc_coalesced_timer_update(s);
902 }
17604dac
JK
903}
904
da98c8eb
GH
905/* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
906 BIOS will read it and start S3 resume at POST Entry */
907static void rtc_notify_suspend(Notifier *notifier, void *data)
908{
909 RTCState *s = container_of(notifier, RTCState, suspend_notifier);
0e41271e 910 rtc_set_memory(ISA_DEVICE(s), 0xF, 0xFE);
da98c8eb
GH
911}
912
eeb7c03c
GN
913static void rtc_reset(void *opaque)
914{
915 RTCState *s = opaque;
916
72716184
AL
917 s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
918 s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
56038ef6 919 check_update_timer(s);
eeb7c03c 920
72716184 921 qemu_irq_lower(s->irq);
eeb7c03c 922
104059da 923 if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
433acf0d 924 s->irq_coalesced = 0;
7cd9681b 925 s->irq_reinject_on_ack_count = 0;
433acf0d 926 }
eeb7c03c
GN
927}
928
b2c5009b 929static const MemoryRegionOps cmos_ops = {
0da8c842
AG
930 .read = cmos_ioport_read,
931 .write = cmos_ioport_write,
932 .impl = {
933 .min_access_size = 1,
934 .max_access_size = 1,
935 },
936 .endianness = DEVICE_LITTLE_ENDIAN,
b2c5009b
RH
937};
938
8e099d14 939static void rtc_get_date(Object *obj, struct tm *current_tm, Error **errp)
18297050 940{
0e41271e 941 RTCState *s = MC146818_RTC(obj);
18297050 942
56038ef6 943 rtc_update_time(s);
8e099d14 944 rtc_get_time(s, current_tm);
18297050
AL
945}
946
db895a1e 947static void rtc_realizefn(DeviceState *dev, Error **errp)
dff38e7b 948{
db895a1e 949 ISADevice *isadev = ISA_DEVICE(dev);
0e41271e 950 RTCState *s = MC146818_RTC(dev);
32e0c826 951 int base = 0x70;
80cabfad 952
80cabfad
FB
953 s->cmos_data[RTC_REG_A] = 0x26;
954 s->cmos_data[RTC_REG_B] = 0x02;
955 s->cmos_data[RTC_REG_C] = 0x00;
956 s->cmos_data[RTC_REG_D] = 0x80;
957
b8994faf
PB
958 /* This is for historical reasons. The default base year qdev property
959 * was set to 2000 for most machine types before the century byte was
960 * implemented.
961 *
962 * This if statement means that the century byte will be always 0
963 * (at least until 2079...) for base_year = 1980, but will be set
964 * correctly for base_year = 2000.
965 */
966 if (s->base_year == 2000) {
967 s->base_year = 0;
968 }
969
db895a1e 970 rtc_set_date_from_host(isadev);
ea55ffb3 971
433acf0d 972 switch (s->lost_tick_policy) {
4aa70a0e 973#ifdef TARGET_I386
104059da 974 case LOST_TICK_POLICY_SLEW:
6875204c 975 s->coalesced_timer =
884f17c2 976 timer_new_ns(rtc_clock, rtc_coalesced_timer, s);
433acf0d 977 break;
4aa70a0e 978#endif
104059da 979 case LOST_TICK_POLICY_DISCARD:
433acf0d
JK
980 break;
981 default:
db895a1e
AF
982 error_setg(errp, "Invalid lost tick policy.");
983 return;
433acf0d 984 }
433acf0d 985
884f17c2
AB
986 s->periodic_timer = timer_new_ns(rtc_clock, rtc_periodic_timer, s);
987 s->update_timer = timer_new_ns(rtc_clock, rtc_update_timer, s);
56038ef6 988 check_update_timer(s);
dff38e7b 989
17604dac 990 s->clock_reset_notifier.notify = rtc_notify_clock_reset;
13c0cbae 991 qemu_clock_register_reset_notifier(rtc_clock,
884f17c2 992 &s->clock_reset_notifier);
17604dac 993
da98c8eb
GH
994 s->suspend_notifier.notify = rtc_notify_suspend;
995 qemu_register_suspend_notifier(&s->suspend_notifier);
996
853dca12 997 memory_region_init_io(&s->io, OBJECT(s), &cmos_ops, s, "rtc", 2);
db895a1e 998 isa_register_ioport(isadev, &s->io, base);
dff38e7b 999
f98167ea
PH
1000 /* register rtc 0x70 port for coalesced_pio */
1001 memory_region_set_flush_coalesced(&s->io);
1002 memory_region_init_io(&s->coalesced_io, OBJECT(s), &cmos_ops,
1003 s, "rtc-index", 1);
1004 memory_region_add_subregion(&s->io, 0, &s->coalesced_io);
1005 memory_region_add_coalescing(&s->coalesced_io, 0, 1);
1006
db895a1e 1007 qdev_set_legacy_instance_id(dev, base, 3);
a08d4367 1008 qemu_register_reset(rtc_reset, s);
18297050 1009
8e099d14 1010 object_property_add_tm(OBJECT(s), "date", rtc_get_date, NULL);
654a36d8 1011
3638439d 1012 qdev_init_gpio_out(dev, &s->irq, 1);
32e0c826
GH
1013}
1014
6c646a11 1015ISADevice *mc146818_rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq)
32e0c826 1016{
0e41271e
AF
1017 DeviceState *dev;
1018 ISADevice *isadev;
7d932dfd 1019 RTCState *s;
eeb7c03c 1020
0e41271e
AF
1021 isadev = isa_create(bus, TYPE_MC146818_RTC);
1022 dev = DEVICE(isadev);
1023 s = MC146818_RTC(isadev);
1024 qdev_prop_set_int32(dev, "base_year", base_year);
1025 qdev_init_nofail(dev);
7d932dfd 1026 if (intercept_irq) {
3638439d 1027 qdev_connect_gpio_out(dev, 0, intercept_irq);
7d932dfd 1028 } else {
3638439d 1029 isa_connect_gpio_out(isadev, 0, RTC_ISA_IRQ);
7d932dfd 1030 }
f2ae8abf
MT
1031 QLIST_INSERT_HEAD(&rtc_devices, s, link);
1032
29551fdc
TH
1033 object_property_add_alias(qdev_get_machine(), "rtc-time", OBJECT(s),
1034 "date", NULL);
1035
0e41271e 1036 return isadev;
80cabfad
FB
1037}
1038
39bffca2
AL
1039static Property mc146818rtc_properties[] = {
1040 DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
1041 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState,
104059da 1042 lost_tick_policy, LOST_TICK_POLICY_DISCARD),
39bffca2
AL
1043 DEFINE_PROP_END_OF_LIST(),
1044};
1045
bf7bb91e 1046static void rtc_resetdev(DeviceState *d)
1047{
1048 RTCState *s = MC146818_RTC(d);
1049
1050 /* Reason: VM do suspend self will set 0xfe
1051 * Reset any values other than 0xfe(Guest suspend case) */
1052 if (s->cmos_data[0x0f] != 0xfe) {
1053 s->cmos_data[0x0f] = 0x00;
1054 }
1055}
1056
8f04ee08
AL
1057static void rtc_class_initfn(ObjectClass *klass, void *data)
1058{
39bffca2 1059 DeviceClass *dc = DEVICE_CLASS(klass);
db895a1e
AF
1060
1061 dc->realize = rtc_realizefn;
bf7bb91e 1062 dc->reset = rtc_resetdev;
39bffca2
AL
1063 dc->vmsd = &vmstate_rtc;
1064 dc->props = mc146818rtc_properties;
f3b17640 1065 /* Reason: needs to be wired up by rtc_init() */
e90f2a8c 1066 dc->user_creatable = false;
8f04ee08
AL
1067}
1068
8c43a6f0 1069static const TypeInfo mc146818rtc_info = {
0e41271e 1070 .name = TYPE_MC146818_RTC,
39bffca2
AL
1071 .parent = TYPE_ISA_DEVICE,
1072 .instance_size = sizeof(RTCState),
1073 .class_init = rtc_class_initfn,
32e0c826
GH
1074};
1075
83f7d43a 1076static void mc146818rtc_register_types(void)
100d9891 1077{
39bffca2 1078 type_register_static(&mc146818rtc_info);
100d9891 1079}
83f7d43a
AF
1080
1081type_init(mc146818rtc_register_types)