Statistics
| Branch: | Revision:

root / hw / mc146818rtc.c @ 8af7a3ab

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