Statistics
| Branch: | Revision:

root / hw / mc146818rtc.c @ a43f9c90

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