Statistics
| Branch: | Revision:

root / hw / mc146818rtc.c @ ec51e364

History | View | Annotate | Download (18.8 kB)

1 80cabfad bellard
/*
2 80cabfad bellard
 * QEMU MC146818 RTC emulation
3 5fafdf24 ths
 *
4 80cabfad bellard
 * Copyright (c) 2003-2004 Fabrice Bellard
5 5fafdf24 ths
 *
6 80cabfad bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 80cabfad bellard
 * of this software and associated documentation files (the "Software"), to deal
8 80cabfad bellard
 * in the Software without restriction, including without limitation the rights
9 80cabfad bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 80cabfad bellard
 * copies of the Software, and to permit persons to whom the Software is
11 80cabfad bellard
 * furnished to do so, subject to the following conditions:
12 80cabfad bellard
 *
13 80cabfad bellard
 * The above copyright notice and this permission notice shall be included in
14 80cabfad bellard
 * all copies or substantial portions of the Software.
15 80cabfad bellard
 *
16 80cabfad bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 80cabfad bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 80cabfad bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 80cabfad bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 80cabfad bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 80cabfad bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 80cabfad bellard
 * THE SOFTWARE.
23 80cabfad bellard
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "qemu-timer.h"
26 87ecb68b pbrook
#include "sysemu.h"
27 87ecb68b pbrook
#include "pc.h"
28 aa28b9bf Blue Swirl
#include "apic.h"
29 87ecb68b pbrook
#include "isa.h"
30 16b29ae1 aliguori
#include "hpet_emul.h"
31 1d914fa0 Isaku Yamahata
#include "mc146818rtc.h"
32 80cabfad bellard
33 80cabfad bellard
//#define DEBUG_CMOS
34 80cabfad bellard
35 ec51e364 Isaku Yamahata
#ifdef DEBUG_CMOS
36 ec51e364 Isaku Yamahata
# define CMOS_DPRINTF(format, ...)      printf(format, ## __VA_ARGS__)
37 ec51e364 Isaku Yamahata
#else
38 ec51e364 Isaku Yamahata
# define CMOS_DPRINTF(format, ...)      do { } while (0)
39 ec51e364 Isaku Yamahata
#endif
40 ec51e364 Isaku Yamahata
41 dd17765b Gleb Natapov
#define RTC_REINJECT_ON_ACK_COUNT 20
42 ba32edab Gleb Natapov
43 80cabfad bellard
#define RTC_SECONDS             0
44 80cabfad bellard
#define RTC_SECONDS_ALARM       1
45 80cabfad bellard
#define RTC_MINUTES             2
46 80cabfad bellard
#define RTC_MINUTES_ALARM       3
47 80cabfad bellard
#define RTC_HOURS               4
48 80cabfad bellard
#define RTC_HOURS_ALARM         5
49 80cabfad bellard
#define RTC_ALARM_DONT_CARE    0xC0
50 80cabfad bellard
51 80cabfad bellard
#define RTC_DAY_OF_WEEK         6
52 80cabfad bellard
#define RTC_DAY_OF_MONTH        7
53 80cabfad bellard
#define RTC_MONTH               8
54 80cabfad bellard
#define RTC_YEAR                9
55 80cabfad bellard
56 80cabfad bellard
#define RTC_REG_A               10
57 80cabfad bellard
#define RTC_REG_B               11
58 80cabfad bellard
#define RTC_REG_C               12
59 80cabfad bellard
#define RTC_REG_D               13
60 80cabfad bellard
61 dff38e7b bellard
#define REG_A_UIP 0x80
62 80cabfad bellard
63 100d9891 aurel32
#define REG_B_SET  0x80
64 100d9891 aurel32
#define REG_B_PIE  0x40
65 100d9891 aurel32
#define REG_B_AIE  0x20
66 100d9891 aurel32
#define REG_B_UIE  0x10
67 100d9891 aurel32
#define REG_B_SQWE 0x08
68 100d9891 aurel32
#define REG_B_DM   0x04
69 dff38e7b bellard
70 72716184 Anthony Liguori
#define REG_C_UF   0x10
71 72716184 Anthony Liguori
#define REG_C_IRQF 0x80
72 72716184 Anthony Liguori
#define REG_C_PF   0x40
73 72716184 Anthony Liguori
#define REG_C_AF   0x20
74 72716184 Anthony Liguori
75 1d914fa0 Isaku Yamahata
typedef struct RTCState {
76 32e0c826 Gerd Hoffmann
    ISADevice dev;
77 dff38e7b bellard
    uint8_t cmos_data[128];
78 dff38e7b bellard
    uint8_t cmos_index;
79 43f493af bellard
    struct tm current_tm;
80 32e0c826 Gerd Hoffmann
    int32_t base_year;
81 d537cf6c pbrook
    qemu_irq irq;
82 100d9891 aurel32
    qemu_irq sqw_irq;
83 18c6e2ff ths
    int it_shift;
84 dff38e7b bellard
    /* periodic timer */
85 dff38e7b bellard
    QEMUTimer *periodic_timer;
86 dff38e7b bellard
    int64_t next_periodic_time;
87 dff38e7b bellard
    /* second update */
88 dff38e7b bellard
    int64_t next_second_time;
89 ba32edab Gleb Natapov
    uint16_t irq_reinject_on_ack_count;
90 73822ec8 aliguori
    uint32_t irq_coalesced;
91 73822ec8 aliguori
    uint32_t period;
92 93b66569 aliguori
    QEMUTimer *coalesced_timer;
93 dff38e7b bellard
    QEMUTimer *second_timer;
94 dff38e7b bellard
    QEMUTimer *second_timer2;
95 1d914fa0 Isaku Yamahata
} RTCState;
96 dff38e7b bellard
97 e0ca7b94 Juan Quintela
static void rtc_irq_raise(qemu_irq irq)
98 e0ca7b94 Juan Quintela
{
99 c50c2d68 aurel32
    /* When HPET is operating in legacy mode, RTC interrupts are disabled
100 16b29ae1 aliguori
     * We block qemu_irq_raise, but not qemu_irq_lower, in case legacy
101 c50c2d68 aurel32
     * mode is established while interrupt is raised. We want it to
102 16b29ae1 aliguori
     * be lowered in any case
103 c50c2d68 aurel32
     */
104 ce88f890 Juan Quintela
#if defined TARGET_I386
105 c50c2d68 aurel32
    if (!hpet_in_legacy_mode())
106 16b29ae1 aliguori
#endif
107 16b29ae1 aliguori
        qemu_irq_raise(irq);
108 16b29ae1 aliguori
}
109 16b29ae1 aliguori
110 dff38e7b bellard
static void rtc_set_time(RTCState *s);
111 dff38e7b bellard
static void rtc_copy_date(RTCState *s);
112 dff38e7b bellard
113 93b66569 aliguori
#ifdef TARGET_I386
114 93b66569 aliguori
static void rtc_coalesced_timer_update(RTCState *s)
115 93b66569 aliguori
{
116 93b66569 aliguori
    if (s->irq_coalesced == 0) {
117 93b66569 aliguori
        qemu_del_timer(s->coalesced_timer);
118 93b66569 aliguori
    } else {
119 93b66569 aliguori
        /* divide each RTC interval to 2 - 8 smaller intervals */
120 93b66569 aliguori
        int c = MIN(s->irq_coalesced, 7) + 1; 
121 6875204c Jan Kiszka
        int64_t next_clock = qemu_get_clock(rtc_clock) +
122 6875204c Jan Kiszka
            muldiv64(s->period / c, get_ticks_per_sec(), 32768);
123 93b66569 aliguori
        qemu_mod_timer(s->coalesced_timer, next_clock);
124 93b66569 aliguori
    }
125 93b66569 aliguori
}
126 93b66569 aliguori
127 93b66569 aliguori
static void rtc_coalesced_timer(void *opaque)
128 93b66569 aliguori
{
129 93b66569 aliguori
    RTCState *s = opaque;
130 93b66569 aliguori
131 93b66569 aliguori
    if (s->irq_coalesced != 0) {
132 93b66569 aliguori
        apic_reset_irq_delivered();
133 93b66569 aliguori
        s->cmos_data[RTC_REG_C] |= 0xc0;
134 93b66569 aliguori
        rtc_irq_raise(s->irq);
135 93b66569 aliguori
        if (apic_get_irq_delivered()) {
136 93b66569 aliguori
            s->irq_coalesced--;
137 93b66569 aliguori
        }
138 93b66569 aliguori
    }
139 93b66569 aliguori
140 93b66569 aliguori
    rtc_coalesced_timer_update(s);
141 93b66569 aliguori
}
142 93b66569 aliguori
#endif
143 93b66569 aliguori
144 dff38e7b bellard
static void rtc_timer_update(RTCState *s, int64_t current_time)
145 dff38e7b bellard
{
146 dff38e7b bellard
    int period_code, period;
147 dff38e7b bellard
    int64_t cur_clock, next_irq_clock;
148 100d9891 aurel32
    int enable_pie;
149 dff38e7b bellard
150 dff38e7b bellard
    period_code = s->cmos_data[RTC_REG_A] & 0x0f;
151 ce88f890 Juan Quintela
#if defined TARGET_I386
152 c50c2d68 aurel32
    /* disable periodic timer if hpet is in legacy mode, since interrupts are
153 16b29ae1 aliguori
     * disabled anyway.
154 16b29ae1 aliguori
     */
155 a8b01dd8 pbrook
    enable_pie = !hpet_in_legacy_mode();
156 16b29ae1 aliguori
#else
157 100d9891 aurel32
    enable_pie = 1;
158 16b29ae1 aliguori
#endif
159 100d9891 aurel32
    if (period_code != 0
160 100d9891 aurel32
        && (((s->cmos_data[RTC_REG_B] & REG_B_PIE) && enable_pie)
161 100d9891 aurel32
            || ((s->cmos_data[RTC_REG_B] & REG_B_SQWE) && s->sqw_irq))) {
162 dff38e7b bellard
        if (period_code <= 2)
163 dff38e7b bellard
            period_code += 7;
164 dff38e7b bellard
        /* period in 32 Khz cycles */
165 dff38e7b bellard
        period = 1 << (period_code - 1);
166 73822ec8 aliguori
#ifdef TARGET_I386
167 73822ec8 aliguori
        if(period != s->period)
168 73822ec8 aliguori
            s->irq_coalesced = (s->irq_coalesced * s->period) / period;
169 73822ec8 aliguori
        s->period = period;
170 73822ec8 aliguori
#endif
171 dff38e7b bellard
        /* compute 32 khz clock */
172 6ee093c9 Juan Quintela
        cur_clock = muldiv64(current_time, 32768, get_ticks_per_sec());
173 dff38e7b bellard
        next_irq_clock = (cur_clock & ~(period - 1)) + period;
174 6875204c Jan Kiszka
        s->next_periodic_time =
175 6875204c Jan Kiszka
            muldiv64(next_irq_clock, get_ticks_per_sec(), 32768) + 1;
176 dff38e7b bellard
        qemu_mod_timer(s->periodic_timer, s->next_periodic_time);
177 dff38e7b bellard
    } else {
178 73822ec8 aliguori
#ifdef TARGET_I386
179 73822ec8 aliguori
        s->irq_coalesced = 0;
180 73822ec8 aliguori
#endif
181 dff38e7b bellard
        qemu_del_timer(s->periodic_timer);
182 dff38e7b bellard
    }
183 dff38e7b bellard
}
184 dff38e7b bellard
185 dff38e7b bellard
static void rtc_periodic_timer(void *opaque)
186 dff38e7b bellard
{
187 dff38e7b bellard
    RTCState *s = opaque;
188 dff38e7b bellard
189 dff38e7b bellard
    rtc_timer_update(s, s->next_periodic_time);
190 100d9891 aurel32
    if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
191 100d9891 aurel32
        s->cmos_data[RTC_REG_C] |= 0xc0;
192 93b66569 aliguori
#ifdef TARGET_I386
193 93b66569 aliguori
        if(rtc_td_hack) {
194 ba32edab Gleb Natapov
            if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT)
195 ba32edab Gleb Natapov
                s->irq_reinject_on_ack_count = 0;                
196 93b66569 aliguori
            apic_reset_irq_delivered();
197 93b66569 aliguori
            rtc_irq_raise(s->irq);
198 93b66569 aliguori
            if (!apic_get_irq_delivered()) {
199 93b66569 aliguori
                s->irq_coalesced++;
200 93b66569 aliguori
                rtc_coalesced_timer_update(s);
201 93b66569 aliguori
            }
202 93b66569 aliguori
        } else
203 93b66569 aliguori
#endif
204 100d9891 aurel32
        rtc_irq_raise(s->irq);
205 100d9891 aurel32
    }
206 100d9891 aurel32
    if (s->cmos_data[RTC_REG_B] & REG_B_SQWE) {
207 100d9891 aurel32
        /* Not square wave at all but we don't want 2048Hz interrupts!
208 100d9891 aurel32
           Must be seen as a pulse.  */
209 100d9891 aurel32
        qemu_irq_raise(s->sqw_irq);
210 100d9891 aurel32
    }
211 dff38e7b bellard
}
212 80cabfad bellard
213 b41a2cd1 bellard
static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data)
214 80cabfad bellard
{
215 b41a2cd1 bellard
    RTCState *s = opaque;
216 80cabfad bellard
217 80cabfad bellard
    if ((addr & 1) == 0) {
218 80cabfad bellard
        s->cmos_index = data & 0x7f;
219 80cabfad bellard
    } else {
220 ec51e364 Isaku Yamahata
        CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n",
221 ec51e364 Isaku Yamahata
                     s->cmos_index, data);
222 dff38e7b bellard
        switch(s->cmos_index) {
223 80cabfad bellard
        case RTC_SECONDS_ALARM:
224 80cabfad bellard
        case RTC_MINUTES_ALARM:
225 80cabfad bellard
        case RTC_HOURS_ALARM:
226 80cabfad bellard
            /* XXX: not supported */
227 80cabfad bellard
            s->cmos_data[s->cmos_index] = data;
228 80cabfad bellard
            break;
229 80cabfad bellard
        case RTC_SECONDS:
230 80cabfad bellard
        case RTC_MINUTES:
231 80cabfad bellard
        case RTC_HOURS:
232 80cabfad bellard
        case RTC_DAY_OF_WEEK:
233 80cabfad bellard
        case RTC_DAY_OF_MONTH:
234 80cabfad bellard
        case RTC_MONTH:
235 80cabfad bellard
        case RTC_YEAR:
236 80cabfad bellard
            s->cmos_data[s->cmos_index] = data;
237 dff38e7b bellard
            /* if in set mode, do not update the time */
238 dff38e7b bellard
            if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
239 dff38e7b bellard
                rtc_set_time(s);
240 dff38e7b bellard
            }
241 80cabfad bellard
            break;
242 80cabfad bellard
        case RTC_REG_A:
243 dff38e7b bellard
            /* UIP bit is read only */
244 dff38e7b bellard
            s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
245 dff38e7b bellard
                (s->cmos_data[RTC_REG_A] & REG_A_UIP);
246 6875204c Jan Kiszka
            rtc_timer_update(s, qemu_get_clock(rtc_clock));
247 dff38e7b bellard
            break;
248 80cabfad bellard
        case RTC_REG_B:
249 dff38e7b bellard
            if (data & REG_B_SET) {
250 dff38e7b bellard
                /* set mode: reset UIP mode */
251 dff38e7b bellard
                s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
252 dff38e7b bellard
                data &= ~REG_B_UIE;
253 dff38e7b bellard
            } else {
254 dff38e7b bellard
                /* if disabling set mode, update the time */
255 dff38e7b bellard
                if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
256 dff38e7b bellard
                    rtc_set_time(s);
257 dff38e7b bellard
                }
258 dff38e7b bellard
            }
259 dff38e7b bellard
            s->cmos_data[RTC_REG_B] = data;
260 6875204c Jan Kiszka
            rtc_timer_update(s, qemu_get_clock(rtc_clock));
261 80cabfad bellard
            break;
262 80cabfad bellard
        case RTC_REG_C:
263 80cabfad bellard
        case RTC_REG_D:
264 80cabfad bellard
            /* cannot write to them */
265 80cabfad bellard
            break;
266 80cabfad bellard
        default:
267 80cabfad bellard
            s->cmos_data[s->cmos_index] = data;
268 80cabfad bellard
            break;
269 80cabfad bellard
        }
270 80cabfad bellard
    }
271 80cabfad bellard
}
272 80cabfad bellard
273 abd0c6bd Paul Brook
static inline int rtc_to_bcd(RTCState *s, int a)
274 80cabfad bellard
{
275 6f1bf24d aurel32
    if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
276 dff38e7b bellard
        return a;
277 dff38e7b bellard
    } else {
278 dff38e7b bellard
        return ((a / 10) << 4) | (a % 10);
279 dff38e7b bellard
    }
280 80cabfad bellard
}
281 80cabfad bellard
282 abd0c6bd Paul Brook
static inline int rtc_from_bcd(RTCState *s, int a)
283 80cabfad bellard
{
284 6f1bf24d aurel32
    if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
285 dff38e7b bellard
        return a;
286 dff38e7b bellard
    } else {
287 dff38e7b bellard
        return ((a >> 4) * 10) + (a & 0x0f);
288 dff38e7b bellard
    }
289 dff38e7b bellard
}
290 dff38e7b bellard
291 dff38e7b bellard
static void rtc_set_time(RTCState *s)
292 dff38e7b bellard
{
293 43f493af bellard
    struct tm *tm = &s->current_tm;
294 dff38e7b bellard
295 abd0c6bd Paul Brook
    tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
296 abd0c6bd Paul Brook
    tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
297 abd0c6bd Paul Brook
    tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
298 43f493af bellard
    if (!(s->cmos_data[RTC_REG_B] & 0x02) &&
299 43f493af bellard
        (s->cmos_data[RTC_HOURS] & 0x80)) {
300 43f493af bellard
        tm->tm_hour += 12;
301 43f493af bellard
    }
302 abd0c6bd Paul Brook
    tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
303 abd0c6bd Paul Brook
    tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
304 abd0c6bd Paul Brook
    tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
305 abd0c6bd Paul Brook
    tm->tm_year = rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year - 1900;
306 80cd3478 Luiz Capitulino
307 80cd3478 Luiz Capitulino
    rtc_change_mon_event(tm);
308 43f493af bellard
}
309 43f493af bellard
310 43f493af bellard
static void rtc_copy_date(RTCState *s)
311 43f493af bellard
{
312 43f493af bellard
    const struct tm *tm = &s->current_tm;
313 42fc73a1 aurel32
    int year;
314 dff38e7b bellard
315 abd0c6bd Paul Brook
    s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec);
316 abd0c6bd Paul Brook
    s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min);
317 43f493af bellard
    if (s->cmos_data[RTC_REG_B] & 0x02) {
318 43f493af bellard
        /* 24 hour format */
319 abd0c6bd Paul Brook
        s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
320 43f493af bellard
    } else {
321 43f493af bellard
        /* 12 hour format */
322 abd0c6bd Paul Brook
        s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour % 12);
323 43f493af bellard
        if (tm->tm_hour >= 12)
324 43f493af bellard
            s->cmos_data[RTC_HOURS] |= 0x80;
325 43f493af bellard
    }
326 abd0c6bd Paul Brook
    s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
327 abd0c6bd Paul Brook
    s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday);
328 abd0c6bd Paul Brook
    s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
329 42fc73a1 aurel32
    year = (tm->tm_year - s->base_year) % 100;
330 42fc73a1 aurel32
    if (year < 0)
331 42fc73a1 aurel32
        year += 100;
332 abd0c6bd Paul Brook
    s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year);
333 43f493af bellard
}
334 43f493af bellard
335 43f493af bellard
/* month is between 0 and 11. */
336 43f493af bellard
static int get_days_in_month(int month, int year)
337 43f493af bellard
{
338 5fafdf24 ths
    static const int days_tab[12] = {
339 5fafdf24 ths
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
340 43f493af bellard
    };
341 43f493af bellard
    int d;
342 43f493af bellard
    if ((unsigned )month >= 12)
343 43f493af bellard
        return 31;
344 43f493af bellard
    d = days_tab[month];
345 43f493af bellard
    if (month == 1) {
346 43f493af bellard
        if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0))
347 43f493af bellard
            d++;
348 43f493af bellard
    }
349 43f493af bellard
    return d;
350 43f493af bellard
}
351 43f493af bellard
352 43f493af bellard
/* update 'tm' to the next second */
353 43f493af bellard
static void rtc_next_second(struct tm *tm)
354 43f493af bellard
{
355 43f493af bellard
    int days_in_month;
356 43f493af bellard
357 43f493af bellard
    tm->tm_sec++;
358 43f493af bellard
    if ((unsigned)tm->tm_sec >= 60) {
359 43f493af bellard
        tm->tm_sec = 0;
360 43f493af bellard
        tm->tm_min++;
361 43f493af bellard
        if ((unsigned)tm->tm_min >= 60) {
362 43f493af bellard
            tm->tm_min = 0;
363 43f493af bellard
            tm->tm_hour++;
364 43f493af bellard
            if ((unsigned)tm->tm_hour >= 24) {
365 43f493af bellard
                tm->tm_hour = 0;
366 43f493af bellard
                /* next day */
367 43f493af bellard
                tm->tm_wday++;
368 43f493af bellard
                if ((unsigned)tm->tm_wday >= 7)
369 43f493af bellard
                    tm->tm_wday = 0;
370 5fafdf24 ths
                days_in_month = get_days_in_month(tm->tm_mon,
371 43f493af bellard
                                                  tm->tm_year + 1900);
372 43f493af bellard
                tm->tm_mday++;
373 43f493af bellard
                if (tm->tm_mday < 1) {
374 43f493af bellard
                    tm->tm_mday = 1;
375 43f493af bellard
                } else if (tm->tm_mday > days_in_month) {
376 43f493af bellard
                    tm->tm_mday = 1;
377 43f493af bellard
                    tm->tm_mon++;
378 43f493af bellard
                    if (tm->tm_mon >= 12) {
379 43f493af bellard
                        tm->tm_mon = 0;
380 43f493af bellard
                        tm->tm_year++;
381 43f493af bellard
                    }
382 43f493af bellard
                }
383 43f493af bellard
            }
384 43f493af bellard
        }
385 43f493af bellard
    }
386 dff38e7b bellard
}
387 dff38e7b bellard
388 43f493af bellard
389 dff38e7b bellard
static void rtc_update_second(void *opaque)
390 dff38e7b bellard
{
391 dff38e7b bellard
    RTCState *s = opaque;
392 4721c457 bellard
    int64_t delay;
393 dff38e7b bellard
394 dff38e7b bellard
    /* if the oscillator is not in normal operation, we do not update */
395 dff38e7b bellard
    if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) {
396 6ee093c9 Juan Quintela
        s->next_second_time += get_ticks_per_sec();
397 dff38e7b bellard
        qemu_mod_timer(s->second_timer, s->next_second_time);
398 dff38e7b bellard
    } else {
399 43f493af bellard
        rtc_next_second(&s->current_tm);
400 3b46e624 ths
401 dff38e7b bellard
        if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
402 dff38e7b bellard
            /* update in progress bit */
403 dff38e7b bellard
            s->cmos_data[RTC_REG_A] |= REG_A_UIP;
404 dff38e7b bellard
        }
405 4721c457 bellard
        /* should be 244 us = 8 / 32768 seconds, but currently the
406 4721c457 bellard
           timers do not have the necessary resolution. */
407 6ee093c9 Juan Quintela
        delay = (get_ticks_per_sec() * 1) / 100;
408 4721c457 bellard
        if (delay < 1)
409 4721c457 bellard
            delay = 1;
410 5fafdf24 ths
        qemu_mod_timer(s->second_timer2,
411 4721c457 bellard
                       s->next_second_time + delay);
412 dff38e7b bellard
    }
413 dff38e7b bellard
}
414 dff38e7b bellard
415 dff38e7b bellard
static void rtc_update_second2(void *opaque)
416 dff38e7b bellard
{
417 dff38e7b bellard
    RTCState *s = opaque;
418 dff38e7b bellard
419 dff38e7b bellard
    if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
420 dff38e7b bellard
        rtc_copy_date(s);
421 dff38e7b bellard
    }
422 dff38e7b bellard
423 dff38e7b bellard
    /* check alarm */
424 dff38e7b bellard
    if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
425 dff38e7b bellard
        if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 ||
426 43f493af bellard
             s->cmos_data[RTC_SECONDS_ALARM] == s->current_tm.tm_sec) &&
427 dff38e7b bellard
            ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 ||
428 43f493af bellard
             s->cmos_data[RTC_MINUTES_ALARM] == s->current_tm.tm_mon) &&
429 dff38e7b bellard
            ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 ||
430 43f493af bellard
             s->cmos_data[RTC_HOURS_ALARM] == s->current_tm.tm_hour)) {
431 dff38e7b bellard
432 5fafdf24 ths
            s->cmos_data[RTC_REG_C] |= 0xa0;
433 16b29ae1 aliguori
            rtc_irq_raise(s->irq);
434 dff38e7b bellard
        }
435 dff38e7b bellard
    }
436 dff38e7b bellard
437 dff38e7b bellard
    /* update ended interrupt */
438 98815437 Bernhard Kauer
    s->cmos_data[RTC_REG_C] |= REG_C_UF;
439 dff38e7b bellard
    if (s->cmos_data[RTC_REG_B] & REG_B_UIE) {
440 98815437 Bernhard Kauer
      s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
441 98815437 Bernhard Kauer
      rtc_irq_raise(s->irq);
442 dff38e7b bellard
    }
443 dff38e7b bellard
444 dff38e7b bellard
    /* clear update in progress bit */
445 dff38e7b bellard
    s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
446 dff38e7b bellard
447 6ee093c9 Juan Quintela
    s->next_second_time += get_ticks_per_sec();
448 dff38e7b bellard
    qemu_mod_timer(s->second_timer, s->next_second_time);
449 80cabfad bellard
}
450 80cabfad bellard
451 b41a2cd1 bellard
static uint32_t cmos_ioport_read(void *opaque, uint32_t addr)
452 80cabfad bellard
{
453 b41a2cd1 bellard
    RTCState *s = opaque;
454 80cabfad bellard
    int ret;
455 80cabfad bellard
    if ((addr & 1) == 0) {
456 80cabfad bellard
        return 0xff;
457 80cabfad bellard
    } else {
458 80cabfad bellard
        switch(s->cmos_index) {
459 80cabfad bellard
        case RTC_SECONDS:
460 80cabfad bellard
        case RTC_MINUTES:
461 80cabfad bellard
        case RTC_HOURS:
462 80cabfad bellard
        case RTC_DAY_OF_WEEK:
463 80cabfad bellard
        case RTC_DAY_OF_MONTH:
464 80cabfad bellard
        case RTC_MONTH:
465 80cabfad bellard
        case RTC_YEAR:
466 80cabfad bellard
            ret = s->cmos_data[s->cmos_index];
467 80cabfad bellard
            break;
468 80cabfad bellard
        case RTC_REG_A:
469 80cabfad bellard
            ret = s->cmos_data[s->cmos_index];
470 80cabfad bellard
            break;
471 80cabfad bellard
        case RTC_REG_C:
472 80cabfad bellard
            ret = s->cmos_data[s->cmos_index];
473 d537cf6c pbrook
            qemu_irq_lower(s->irq);
474 ba32edab Gleb Natapov
#ifdef TARGET_I386
475 ba32edab Gleb Natapov
            if(s->irq_coalesced &&
476 ba32edab Gleb Natapov
                    s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
477 ba32edab Gleb Natapov
                s->irq_reinject_on_ack_count++;
478 ba32edab Gleb Natapov
                apic_reset_irq_delivered();
479 ba32edab Gleb Natapov
                qemu_irq_raise(s->irq);
480 ba32edab Gleb Natapov
                if (apic_get_irq_delivered())
481 ba32edab Gleb Natapov
                    s->irq_coalesced--;
482 ba32edab Gleb Natapov
                break;
483 ba32edab Gleb Natapov
            }
484 ba32edab Gleb Natapov
#endif
485 ba32edab Gleb Natapov
486 5fafdf24 ths
            s->cmos_data[RTC_REG_C] = 0x00;
487 80cabfad bellard
            break;
488 80cabfad bellard
        default:
489 80cabfad bellard
            ret = s->cmos_data[s->cmos_index];
490 80cabfad bellard
            break;
491 80cabfad bellard
        }
492 ec51e364 Isaku Yamahata
        CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
493 ec51e364 Isaku Yamahata
                     s->cmos_index, ret);
494 80cabfad bellard
        return ret;
495 80cabfad bellard
    }
496 80cabfad bellard
}
497 80cabfad bellard
498 1d914fa0 Isaku Yamahata
void rtc_set_memory(ISADevice *dev, int addr, int val)
499 dff38e7b bellard
{
500 1d914fa0 Isaku Yamahata
    RTCState *s = DO_UPCAST(RTCState, dev, dev);
501 dff38e7b bellard
    if (addr >= 0 && addr <= 127)
502 dff38e7b bellard
        s->cmos_data[addr] = val;
503 dff38e7b bellard
}
504 dff38e7b bellard
505 1d914fa0 Isaku Yamahata
void rtc_set_date(ISADevice *dev, const struct tm *tm)
506 dff38e7b bellard
{
507 1d914fa0 Isaku Yamahata
    RTCState *s = DO_UPCAST(RTCState, dev, dev);
508 43f493af bellard
    s->current_tm = *tm;
509 dff38e7b bellard
    rtc_copy_date(s);
510 dff38e7b bellard
}
511 dff38e7b bellard
512 ea55ffb3 ths
/* PC cmos mappings */
513 ea55ffb3 ths
#define REG_IBM_CENTURY_BYTE        0x32
514 ea55ffb3 ths
#define REG_IBM_PS2_CENTURY_BYTE    0x37
515 ea55ffb3 ths
516 1d914fa0 Isaku Yamahata
static void rtc_set_date_from_host(ISADevice *dev)
517 ea55ffb3 ths
{
518 1d914fa0 Isaku Yamahata
    RTCState *s = DO_UPCAST(RTCState, dev, dev);
519 f6503059 balrog
    struct tm tm;
520 ea55ffb3 ths
    int val;
521 ea55ffb3 ths
522 ea55ffb3 ths
    /* set the CMOS date */
523 f6503059 balrog
    qemu_get_timedate(&tm, 0);
524 1d914fa0 Isaku Yamahata
    rtc_set_date(dev, &tm);
525 ea55ffb3 ths
526 abd0c6bd Paul Brook
    val = rtc_to_bcd(s, (tm.tm_year / 100) + 19);
527 1d914fa0 Isaku Yamahata
    rtc_set_memory(dev, REG_IBM_CENTURY_BYTE, val);
528 1d914fa0 Isaku Yamahata
    rtc_set_memory(dev, REG_IBM_PS2_CENTURY_BYTE, val);
529 ea55ffb3 ths
}
530 ea55ffb3 ths
531 6b075b8a Juan Quintela
static int rtc_post_load(void *opaque, int version_id)
532 80cabfad bellard
{
533 6b075b8a Juan Quintela
#ifdef TARGET_I386
534 dff38e7b bellard
    RTCState *s = opaque;
535 dff38e7b bellard
536 048c74c4 Juan Quintela
    if (version_id >= 2) {
537 048c74c4 Juan Quintela
        if (rtc_td_hack) {
538 048c74c4 Juan Quintela
            rtc_coalesced_timer_update(s);
539 048c74c4 Juan Quintela
        }
540 048c74c4 Juan Quintela
    }
541 6b075b8a Juan Quintela
#endif
542 73822ec8 aliguori
    return 0;
543 73822ec8 aliguori
}
544 73822ec8 aliguori
545 6b075b8a Juan Quintela
static const VMStateDescription vmstate_rtc = {
546 6b075b8a Juan Quintela
    .name = "mc146818rtc",
547 6b075b8a Juan Quintela
    .version_id = 2,
548 6b075b8a Juan Quintela
    .minimum_version_id = 1,
549 6b075b8a Juan Quintela
    .minimum_version_id_old = 1,
550 6b075b8a Juan Quintela
    .post_load = rtc_post_load,
551 6b075b8a Juan Quintela
    .fields      = (VMStateField []) {
552 6b075b8a Juan Quintela
        VMSTATE_BUFFER(cmos_data, RTCState),
553 6b075b8a Juan Quintela
        VMSTATE_UINT8(cmos_index, RTCState),
554 6b075b8a Juan Quintela
        VMSTATE_INT32(current_tm.tm_sec, RTCState),
555 6b075b8a Juan Quintela
        VMSTATE_INT32(current_tm.tm_min, RTCState),
556 6b075b8a Juan Quintela
        VMSTATE_INT32(current_tm.tm_hour, RTCState),
557 6b075b8a Juan Quintela
        VMSTATE_INT32(current_tm.tm_wday, RTCState),
558 6b075b8a Juan Quintela
        VMSTATE_INT32(current_tm.tm_mday, RTCState),
559 6b075b8a Juan Quintela
        VMSTATE_INT32(current_tm.tm_mon, RTCState),
560 6b075b8a Juan Quintela
        VMSTATE_INT32(current_tm.tm_year, RTCState),
561 6b075b8a Juan Quintela
        VMSTATE_TIMER(periodic_timer, RTCState),
562 6b075b8a Juan Quintela
        VMSTATE_INT64(next_periodic_time, RTCState),
563 6b075b8a Juan Quintela
        VMSTATE_INT64(next_second_time, RTCState),
564 6b075b8a Juan Quintela
        VMSTATE_TIMER(second_timer, RTCState),
565 6b075b8a Juan Quintela
        VMSTATE_TIMER(second_timer2, RTCState),
566 6b075b8a Juan Quintela
        VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
567 6b075b8a Juan Quintela
        VMSTATE_UINT32_V(period, RTCState, 2),
568 6b075b8a Juan Quintela
        VMSTATE_END_OF_LIST()
569 6b075b8a Juan Quintela
    }
570 6b075b8a Juan Quintela
};
571 6b075b8a Juan Quintela
572 eeb7c03c Gleb Natapov
static void rtc_reset(void *opaque)
573 eeb7c03c Gleb Natapov
{
574 eeb7c03c Gleb Natapov
    RTCState *s = opaque;
575 eeb7c03c Gleb Natapov
576 72716184 Anthony Liguori
    s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
577 72716184 Anthony Liguori
    s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
578 eeb7c03c Gleb Natapov
579 72716184 Anthony Liguori
    qemu_irq_lower(s->irq);
580 eeb7c03c Gleb Natapov
581 eeb7c03c Gleb Natapov
#ifdef TARGET_I386
582 eeb7c03c Gleb Natapov
    if (rtc_td_hack)
583 eeb7c03c Gleb Natapov
            s->irq_coalesced = 0;
584 eeb7c03c Gleb Natapov
#endif
585 eeb7c03c Gleb Natapov
}
586 eeb7c03c Gleb Natapov
587 32e0c826 Gerd Hoffmann
static int rtc_initfn(ISADevice *dev)
588 dff38e7b bellard
{
589 32e0c826 Gerd Hoffmann
    RTCState *s = DO_UPCAST(RTCState, dev, dev);
590 32e0c826 Gerd Hoffmann
    int base = 0x70;
591 32e0c826 Gerd Hoffmann
    int isairq = 8;
592 dff38e7b bellard
593 32e0c826 Gerd Hoffmann
    isa_init_irq(dev, &s->irq, isairq);
594 80cabfad bellard
595 80cabfad bellard
    s->cmos_data[RTC_REG_A] = 0x26;
596 80cabfad bellard
    s->cmos_data[RTC_REG_B] = 0x02;
597 80cabfad bellard
    s->cmos_data[RTC_REG_C] = 0x00;
598 80cabfad bellard
    s->cmos_data[RTC_REG_D] = 0x80;
599 80cabfad bellard
600 1d914fa0 Isaku Yamahata
    rtc_set_date_from_host(dev);
601 ea55ffb3 ths
602 6875204c Jan Kiszka
    s->periodic_timer = qemu_new_timer(rtc_clock, rtc_periodic_timer, s);
603 93b66569 aliguori
#ifdef TARGET_I386
604 93b66569 aliguori
    if (rtc_td_hack)
605 6875204c Jan Kiszka
        s->coalesced_timer =
606 6875204c Jan Kiszka
            qemu_new_timer(rtc_clock, rtc_coalesced_timer, s);
607 93b66569 aliguori
#endif
608 6875204c Jan Kiszka
    s->second_timer = qemu_new_timer(rtc_clock, rtc_update_second, s);
609 6875204c Jan Kiszka
    s->second_timer2 = qemu_new_timer(rtc_clock, rtc_update_second2, s);
610 dff38e7b bellard
611 6875204c Jan Kiszka
    s->next_second_time =
612 6875204c Jan Kiszka
        qemu_get_clock(rtc_clock) + (get_ticks_per_sec() * 99) / 100;
613 dff38e7b bellard
    qemu_mod_timer(s->second_timer2, s->next_second_time);
614 dff38e7b bellard
615 b41a2cd1 bellard
    register_ioport_write(base, 2, 1, cmos_ioport_write, s);
616 b41a2cd1 bellard
    register_ioport_read(base, 2, 1, cmos_ioport_read, s);
617 dff38e7b bellard
618 dc683910 Jan Kiszka
    qdev_set_legacy_instance_id(&dev->qdev, base, 2);
619 a08d4367 Jan Kiszka
    qemu_register_reset(rtc_reset, s);
620 32e0c826 Gerd Hoffmann
    return 0;
621 32e0c826 Gerd Hoffmann
}
622 32e0c826 Gerd Hoffmann
623 1d914fa0 Isaku Yamahata
ISADevice *rtc_init(int base_year)
624 32e0c826 Gerd Hoffmann
{
625 32e0c826 Gerd Hoffmann
    ISADevice *dev;
626 eeb7c03c Gleb Natapov
627 32e0c826 Gerd Hoffmann
    dev = isa_create("mc146818rtc");
628 32e0c826 Gerd Hoffmann
    qdev_prop_set_int32(&dev->qdev, "base_year", base_year);
629 e23a1b33 Markus Armbruster
    qdev_init_nofail(&dev->qdev);
630 1d914fa0 Isaku Yamahata
    return dev;
631 80cabfad bellard
}
632 80cabfad bellard
633 32e0c826 Gerd Hoffmann
static ISADeviceInfo mc146818rtc_info = {
634 32e0c826 Gerd Hoffmann
    .qdev.name     = "mc146818rtc",
635 32e0c826 Gerd Hoffmann
    .qdev.size     = sizeof(RTCState),
636 32e0c826 Gerd Hoffmann
    .qdev.no_user  = 1,
637 dc683910 Jan Kiszka
    .qdev.vmsd     = &vmstate_rtc,
638 32e0c826 Gerd Hoffmann
    .init          = rtc_initfn,
639 32e0c826 Gerd Hoffmann
    .qdev.props    = (Property[]) {
640 32e0c826 Gerd Hoffmann
        DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
641 32e0c826 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
642 32e0c826 Gerd Hoffmann
    }
643 32e0c826 Gerd Hoffmann
};
644 32e0c826 Gerd Hoffmann
645 32e0c826 Gerd Hoffmann
static void mc146818rtc_register(void)
646 100d9891 aurel32
{
647 32e0c826 Gerd Hoffmann
    isa_qdev_register(&mc146818rtc_info);
648 100d9891 aurel32
}
649 32e0c826 Gerd Hoffmann
device_init(mc146818rtc_register)