qdev: Replace device names containing whitespace
[qemu.git] / hw / mc146818rtc.c
blobe4d55c7c79f67567134dae627b87cd42a5b6454d
1 /*
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
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "qemu-timer.h"
26 #include "sysemu.h"
27 #include "pc.h"
28 #include "isa.h"
29 #include "hpet_emul.h"
31 //#define DEBUG_CMOS
33 #define RTC_REINJECT_ON_ACK_COUNT 1000
35 #define RTC_SECONDS 0
36 #define RTC_SECONDS_ALARM 1
37 #define RTC_MINUTES 2
38 #define RTC_MINUTES_ALARM 3
39 #define RTC_HOURS 4
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
45 #define RTC_MONTH 8
46 #define RTC_YEAR 9
48 #define RTC_REG_A 10
49 #define RTC_REG_B 11
50 #define RTC_REG_C 12
51 #define RTC_REG_D 13
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
60 #define REG_B_DM 0x04
62 #define REG_C_UF 0x10
63 #define REG_C_IRQF 0x80
64 #define REG_C_PF 0x40
65 #define REG_C_AF 0x20
67 struct RTCState {
68 ISADevice dev;
69 uint8_t cmos_data[128];
70 uint8_t cmos_index;
71 struct tm current_tm;
72 int32_t base_year;
73 qemu_irq irq;
74 qemu_irq sqw_irq;
75 int it_shift;
76 /* periodic timer */
77 QEMUTimer *periodic_timer;
78 int64_t next_periodic_time;
79 /* second update */
80 int64_t next_second_time;
81 uint16_t irq_reinject_on_ack_count;
82 uint32_t irq_coalesced;
83 uint32_t period;
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())
98 #endif
99 qemu_irq_raise(irq);
102 static void rtc_set_time(RTCState *s);
103 static void rtc_copy_date(RTCState *s);
105 #ifdef TARGET_I386
106 static void rtc_coalesced_timer_update(RTCState *s)
108 if (s->irq_coalesced == 0) {
109 qemu_del_timer(s->coalesced_timer);
110 } else {
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()) {
128 s->irq_coalesced--;
132 rtc_coalesced_timer_update(s);
134 #endif
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;
140 int enable_pie;
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
145 * disabled anyway.
147 enable_pie = !hpet_in_legacy_mode();
148 #else
149 enable_pie = 1;
150 #endif
151 if (period_code != 0
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)
155 period_code += 7;
156 /* period in 32 Khz cycles */
157 period = 1 << (period_code - 1);
158 #ifdef TARGET_I386
159 if(period != s->period)
160 s->irq_coalesced = (s->irq_coalesced * s->period) / period;
161 s->period = period;
162 #endif
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);
169 } else {
170 #ifdef TARGET_I386
171 s->irq_coalesced = 0;
172 #endif
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;
184 #ifdef TARGET_I386
185 if(rtc_td_hack) {
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()) {
191 s->irq_coalesced++;
192 rtc_coalesced_timer_update(s);
194 } else
195 #endif
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;
211 } else {
212 #ifdef DEBUG_CMOS
213 printf("cmos: write index=0x%02x val=0x%02x\n",
214 s->cmos_index, data);
215 #endif
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;
222 break;
223 case RTC_SECONDS:
224 case RTC_MINUTES:
225 case RTC_HOURS:
226 case RTC_DAY_OF_WEEK:
227 case RTC_DAY_OF_MONTH:
228 case RTC_MONTH:
229 case RTC_YEAR:
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)) {
233 rtc_set_time(s);
235 break;
236 case RTC_REG_A:
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));
241 break;
242 case RTC_REG_B:
243 if (data & REG_B_SET) {
244 /* set mode: reset UIP mode */
245 s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
246 data &= ~REG_B_UIE;
247 } else {
248 /* if disabling set mode, update the time */
249 if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
250 rtc_set_time(s);
253 s->cmos_data[RTC_REG_B] = data;
254 rtc_timer_update(s, qemu_get_clock(rtc_clock));
255 break;
256 case RTC_REG_C:
257 case RTC_REG_D:
258 /* cannot write to them */
259 break;
260 default:
261 s->cmos_data[s->cmos_index] = data;
262 break;
267 static inline int rtc_to_bcd(RTCState *s, int a)
269 if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
270 return a;
271 } else {
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) {
279 return a;
280 } else {
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)) {
294 tm->tm_hour += 12;
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;
305 int year;
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) {
310 /* 24 hour format */
311 s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
312 } else {
313 /* 12 hour format */
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;
322 if (year < 0)
323 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
333 int d;
334 if ((unsigned )month >= 12)
335 return 31;
336 d = days_tab[month];
337 if (month == 1) {
338 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0))
339 d++;
341 return d;
344 /* update 'tm' to the next second */
345 static void rtc_next_second(struct tm *tm)
347 int days_in_month;
349 tm->tm_sec++;
350 if ((unsigned)tm->tm_sec >= 60) {
351 tm->tm_sec = 0;
352 tm->tm_min++;
353 if ((unsigned)tm->tm_min >= 60) {
354 tm->tm_min = 0;
355 tm->tm_hour++;
356 if ((unsigned)tm->tm_hour >= 24) {
357 tm->tm_hour = 0;
358 /* next day */
359 tm->tm_wday++;
360 if ((unsigned)tm->tm_wday >= 7)
361 tm->tm_wday = 0;
362 days_in_month = get_days_in_month(tm->tm_mon,
363 tm->tm_year + 1900);
364 tm->tm_mday++;
365 if (tm->tm_mday < 1) {
366 tm->tm_mday = 1;
367 } else if (tm->tm_mday > days_in_month) {
368 tm->tm_mday = 1;
369 tm->tm_mon++;
370 if (tm->tm_mon >= 12) {
371 tm->tm_mon = 0;
372 tm->tm_year++;
381 static void rtc_update_second(void *opaque)
383 RTCState *s = opaque;
384 int64_t delay;
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);
390 } else {
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;
400 if (delay < 1)
401 delay = 1;
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)) {
412 rtc_copy_date(s);
415 /* check alarm */
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;
446 int ret;
447 if ((addr & 1) == 0) {
448 return 0xff;
449 } else {
450 switch(s->cmos_index) {
451 case RTC_SECONDS:
452 case RTC_MINUTES:
453 case RTC_HOURS:
454 case RTC_DAY_OF_WEEK:
455 case RTC_DAY_OF_MONTH:
456 case RTC_MONTH:
457 case RTC_YEAR:
458 ret = s->cmos_data[s->cmos_index];
459 break;
460 case RTC_REG_A:
461 ret = s->cmos_data[s->cmos_index];
462 break;
463 case RTC_REG_C:
464 ret = s->cmos_data[s->cmos_index];
465 qemu_irq_lower(s->irq);
466 #ifdef TARGET_I386
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())
473 s->irq_coalesced--;
474 break;
476 #endif
478 s->cmos_data[RTC_REG_C] = 0x00;
479 break;
480 default:
481 ret = s->cmos_data[s->cmos_index];
482 break;
484 #ifdef DEBUG_CMOS
485 printf("cmos: read index=0x%02x val=0x%02x\n",
486 s->cmos_index, ret);
487 #endif
488 return ret;
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)
500 s->current_tm = *tm;
501 rtc_copy_date(s);
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)
510 struct tm tm;
511 int val;
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)
524 #ifdef TARGET_I386
525 RTCState *s = opaque;
527 if (version_id >= 2) {
528 if (rtc_td_hack) {
529 rtc_coalesced_timer_update(s);
532 #endif
533 return 0;
536 static const VMStateDescription vmstate_rtc = {
537 .name = "mc146818rtc",
538 .version_id = 2,
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);
572 #ifdef TARGET_I386
573 if (rtc_td_hack)
574 s->irq_coalesced = 0;
575 #endif
578 static int rtc_initfn(ISADevice *dev)
580 RTCState *s = DO_UPCAST(RTCState, dev, dev);
581 int base = 0x70;
582 int isairq = 8;
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);
594 #ifdef TARGET_I386
595 if (rtc_td_hack)
596 s->coalesced_timer =
597 qemu_new_timer(rtc_clock, rtc_coalesced_timer, s);
598 #endif
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);
611 return 0;
614 RTCState *rtc_init(int base_year)
616 ISADevice *dev;
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),
627 .qdev.no_user = 1,
628 .init = rtc_initfn,
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)