mc146818rtc.c 21.7 KB
Newer Older
B
bellard 已提交
1 2
/*
 * QEMU MC146818 RTC emulation
3
 *
B
bellard 已提交
4
 * Copyright (c) 2003-2004 Fabrice Bellard
5
 *
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
P
pbrook 已提交
24 25 26 27
#include "hw.h"
#include "qemu-timer.h"
#include "sysemu.h"
#include "pc.h"
28
#include "apic.h"
P
pbrook 已提交
29
#include "isa.h"
30
#include "mc146818rtc.h"
B
bellard 已提交
31 32

//#define DEBUG_CMOS
B
Blue Swirl 已提交
33
//#define DEBUG_COALESCED
B
bellard 已提交
34

35 36 37 38 39 40
#ifdef DEBUG_CMOS
# define CMOS_DPRINTF(format, ...)      printf(format, ## __VA_ARGS__)
#else
# define CMOS_DPRINTF(format, ...)      do { } while (0)
#endif

B
Blue Swirl 已提交
41 42 43 44 45 46
#ifdef DEBUG_COALESCED
# define DPRINTF_C(format, ...)      printf(format, ## __VA_ARGS__)
#else
# define DPRINTF_C(format, ...)      do { } while (0)
#endif

G
Gleb Natapov 已提交
47
#define RTC_REINJECT_ON_ACK_COUNT 20
48

B
bellard 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#define RTC_SECONDS             0
#define RTC_SECONDS_ALARM       1
#define RTC_MINUTES             2
#define RTC_MINUTES_ALARM       3
#define RTC_HOURS               4
#define RTC_HOURS_ALARM         5
#define RTC_ALARM_DONT_CARE    0xC0

#define RTC_DAY_OF_WEEK         6
#define RTC_DAY_OF_MONTH        7
#define RTC_MONTH               8
#define RTC_YEAR                9

#define RTC_REG_A               10
#define RTC_REG_B               11
#define RTC_REG_C               12
#define RTC_REG_D               13

67
#define REG_A_UIP 0x80
B
bellard 已提交
68

69 70 71 72 73 74
#define REG_B_SET  0x80
#define REG_B_PIE  0x40
#define REG_B_AIE  0x20
#define REG_B_UIE  0x10
#define REG_B_SQWE 0x08
#define REG_B_DM   0x04
A
Aurelien Jarno 已提交
75
#define REG_B_24H  0x02
76

77 78 79 80 81
#define REG_C_UF   0x10
#define REG_C_IRQF 0x80
#define REG_C_PF   0x40
#define REG_C_AF   0x20

82
typedef struct RTCState {
83
    ISADevice dev;
84
    MemoryRegion io;
85 86
    uint8_t cmos_data[128];
    uint8_t cmos_index;
87
    struct tm current_tm;
88
    int32_t base_year;
P
pbrook 已提交
89
    qemu_irq irq;
90
    qemu_irq sqw_irq;
91
    int it_shift;
92 93 94 95 96
    /* periodic timer */
    QEMUTimer *periodic_timer;
    int64_t next_periodic_time;
    /* second update */
    int64_t next_second_time;
97
    uint16_t irq_reinject_on_ack_count;
98 99
    uint32_t irq_coalesced;
    uint32_t period;
100
    QEMUTimer *coalesced_timer;
101 102
    QEMUTimer *second_timer;
    QEMUTimer *second_timer2;
103
    Notifier clock_reset_notifier;
104
} RTCState;
105 106 107 108

static void rtc_set_time(RTCState *s);
static void rtc_copy_date(RTCState *s);

109 110 111 112 113 114 115 116
#ifdef TARGET_I386
static void rtc_coalesced_timer_update(RTCState *s)
{
    if (s->irq_coalesced == 0) {
        qemu_del_timer(s->coalesced_timer);
    } else {
        /* divide each RTC interval to 2 - 8 smaller intervals */
        int c = MIN(s->irq_coalesced, 7) + 1; 
117
        int64_t next_clock = qemu_get_clock_ns(rtc_clock) +
J
Jan Kiszka 已提交
118
            muldiv64(s->period / c, get_ticks_per_sec(), 32768);
119 120 121 122 123 124 125 126 127 128 129
        qemu_mod_timer(s->coalesced_timer, next_clock);
    }
}

static void rtc_coalesced_timer(void *opaque)
{
    RTCState *s = opaque;

    if (s->irq_coalesced != 0) {
        apic_reset_irq_delivered();
        s->cmos_data[RTC_REG_C] |= 0xc0;
B
Blue Swirl 已提交
130
        DPRINTF_C("cmos: injecting from timer\n");
131
        qemu_irq_raise(s->irq);
132 133
        if (apic_get_irq_delivered()) {
            s->irq_coalesced--;
B
Blue Swirl 已提交
134 135
            DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
                      s->irq_coalesced);
136 137 138 139 140 141 142
        }
    }

    rtc_coalesced_timer_update(s);
}
#endif

143 144 145 146 147 148
static void rtc_timer_update(RTCState *s, int64_t current_time)
{
    int period_code, period;
    int64_t cur_clock, next_irq_clock;

    period_code = s->cmos_data[RTC_REG_A] & 0x0f;
149
    if (period_code != 0
150
        && ((s->cmos_data[RTC_REG_B] & REG_B_PIE)
151
            || ((s->cmos_data[RTC_REG_B] & REG_B_SQWE) && s->sqw_irq))) {
152 153 154 155
        if (period_code <= 2)
            period_code += 7;
        /* period in 32 Khz cycles */
        period = 1 << (period_code - 1);
156
#ifdef TARGET_I386
B
Blue Swirl 已提交
157
        if (period != s->period) {
158
            s->irq_coalesced = (s->irq_coalesced * s->period) / period;
B
Blue Swirl 已提交
159 160
            DPRINTF_C("cmos: coalesced irqs scaled to %d\n", s->irq_coalesced);
        }
161 162
        s->period = period;
#endif
163
        /* compute 32 khz clock */
164
        cur_clock = muldiv64(current_time, 32768, get_ticks_per_sec());
165
        next_irq_clock = (cur_clock & ~(period - 1)) + period;
J
Jan Kiszka 已提交
166 167
        s->next_periodic_time =
            muldiv64(next_irq_clock, get_ticks_per_sec(), 32768) + 1;
168 169
        qemu_mod_timer(s->periodic_timer, s->next_periodic_time);
    } else {
170 171 172
#ifdef TARGET_I386
        s->irq_coalesced = 0;
#endif
173 174 175 176 177 178 179 180 181
        qemu_del_timer(s->periodic_timer);
    }
}

static void rtc_periodic_timer(void *opaque)
{
    RTCState *s = opaque;

    rtc_timer_update(s, s->next_periodic_time);
182
    s->cmos_data[RTC_REG_C] |= REG_C_PF;
183
    if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
184
        s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
185 186
#ifdef TARGET_I386
        if(rtc_td_hack) {
187 188
            if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT)
                s->irq_reinject_on_ack_count = 0;		
189
            apic_reset_irq_delivered();
190
            qemu_irq_raise(s->irq);
191 192 193
            if (!apic_get_irq_delivered()) {
                s->irq_coalesced++;
                rtc_coalesced_timer_update(s);
B
Blue Swirl 已提交
194 195
                DPRINTF_C("cmos: coalesced irqs increased to %d\n",
                          s->irq_coalesced);
196 197 198
            }
        } else
#endif
199
        qemu_irq_raise(s->irq);
200 201 202 203 204 205
    }
    if (s->cmos_data[RTC_REG_B] & REG_B_SQWE) {
        /* Not square wave at all but we don't want 2048Hz interrupts!
           Must be seen as a pulse.  */
        qemu_irq_raise(s->sqw_irq);
    }
206
}
B
bellard 已提交
207

B
bellard 已提交
208
static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data)
B
bellard 已提交
209
{
B
bellard 已提交
210
    RTCState *s = opaque;
B
bellard 已提交
211 212 213 214

    if ((addr & 1) == 0) {
        s->cmos_index = data & 0x7f;
    } else {
215 216
        CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02x\n",
                     s->cmos_index, data);
217
        switch(s->cmos_index) {
B
bellard 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230
        case RTC_SECONDS_ALARM:
        case RTC_MINUTES_ALARM:
        case RTC_HOURS_ALARM:
            s->cmos_data[s->cmos_index] = data;
            break;
        case RTC_SECONDS:
        case RTC_MINUTES:
        case RTC_HOURS:
        case RTC_DAY_OF_WEEK:
        case RTC_DAY_OF_MONTH:
        case RTC_MONTH:
        case RTC_YEAR:
            s->cmos_data[s->cmos_index] = data;
231 232 233 234
            /* if in set mode, do not update the time */
            if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
                rtc_set_time(s);
            }
B
bellard 已提交
235 236
            break;
        case RTC_REG_A:
237 238 239
            /* UIP bit is read only */
            s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
                (s->cmos_data[RTC_REG_A] & REG_A_UIP);
240
            rtc_timer_update(s, qemu_get_clock_ns(rtc_clock));
241
            break;
B
bellard 已提交
242
        case RTC_REG_B:
243 244 245 246 247 248 249 250 251 252
            if (data & REG_B_SET) {
                /* set mode: reset UIP mode */
                s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
                data &= ~REG_B_UIE;
            } else {
                /* if disabling set mode, update the time */
                if (s->cmos_data[RTC_REG_B] & REG_B_SET) {
                    rtc_set_time(s);
                }
            }
253 254 255 256 257 258 259 260 261
            if (((s->cmos_data[RTC_REG_B] ^ data) & (REG_B_DM | REG_B_24H)) &&
                !(data & REG_B_SET)) {
                /* If the time format has changed and not in set mode,
                   update the registers immediately. */
                s->cmos_data[RTC_REG_B] = data;
                rtc_copy_date(s);
            } else {
                s->cmos_data[RTC_REG_B] = data;
            }
262
            rtc_timer_update(s, qemu_get_clock_ns(rtc_clock));
B
bellard 已提交
263 264 265 266 267 268 269 270 271 272 273 274
            break;
        case RTC_REG_C:
        case RTC_REG_D:
            /* cannot write to them */
            break;
        default:
            s->cmos_data[s->cmos_index] = data;
            break;
        }
    }
}

P
Paul Brook 已提交
275
static inline int rtc_to_bcd(RTCState *s, int a)
B
bellard 已提交
276
{
A
aurel32 已提交
277
    if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
278 279 280 281
        return a;
    } else {
        return ((a / 10) << 4) | (a % 10);
    }
B
bellard 已提交
282 283
}

P
Paul Brook 已提交
284
static inline int rtc_from_bcd(RTCState *s, int a)
B
bellard 已提交
285
{
A
aurel32 已提交
286
    if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
287 288 289 290 291 292 293 294
        return a;
    } else {
        return ((a >> 4) * 10) + (a & 0x0f);
    }
}

static void rtc_set_time(RTCState *s)
{
295
    struct tm *tm = &s->current_tm;
296

P
Paul Brook 已提交
297 298 299
    tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
    tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
    tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
P
Paolo Bonzini 已提交
300 301 302 303 304
    if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
        tm->tm_hour %= 12;
        if (s->cmos_data[RTC_HOURS] & 0x80) {
            tm->tm_hour += 12;
        }
305
    }
P
Paul Brook 已提交
306 307 308 309
    tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
    tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
    tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
    tm->tm_year = rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year - 1900;
310 311

    rtc_change_mon_event(tm);
312 313 314 315 316
}

static void rtc_copy_date(RTCState *s)
{
    const struct tm *tm = &s->current_tm;
317
    int year;
318

P
Paul Brook 已提交
319 320
    s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec);
    s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min);
A
Aurelien Jarno 已提交
321
    if (s->cmos_data[RTC_REG_B] & REG_B_24H) {
322
        /* 24 hour format */
P
Paul Brook 已提交
323
        s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
324 325
    } else {
        /* 12 hour format */
P
Paolo Bonzini 已提交
326 327
        int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12;
        s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h);
328 329 330
        if (tm->tm_hour >= 12)
            s->cmos_data[RTC_HOURS] |= 0x80;
    }
P
Paul Brook 已提交
331 332 333
    s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
    s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday);
    s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
334 335 336
    year = (tm->tm_year - s->base_year) % 100;
    if (year < 0)
        year += 100;
P
Paul Brook 已提交
337
    s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year);
338 339 340 341 342
}

/* month is between 0 and 11. */
static int get_days_in_month(int month, int year)
{
343 344
    static const int days_tab[12] = {
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    };
    int d;
    if ((unsigned )month >= 12)
        return 31;
    d = days_tab[month];
    if (month == 1) {
        if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0))
            d++;
    }
    return d;
}

/* update 'tm' to the next second */
static void rtc_next_second(struct tm *tm)
{
    int days_in_month;

    tm->tm_sec++;
    if ((unsigned)tm->tm_sec >= 60) {
        tm->tm_sec = 0;
        tm->tm_min++;
        if ((unsigned)tm->tm_min >= 60) {
            tm->tm_min = 0;
            tm->tm_hour++;
            if ((unsigned)tm->tm_hour >= 24) {
                tm->tm_hour = 0;
                /* next day */
                tm->tm_wday++;
                if ((unsigned)tm->tm_wday >= 7)
                    tm->tm_wday = 0;
375
                days_in_month = get_days_in_month(tm->tm_mon,
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
                                                  tm->tm_year + 1900);
                tm->tm_mday++;
                if (tm->tm_mday < 1) {
                    tm->tm_mday = 1;
                } else if (tm->tm_mday > days_in_month) {
                    tm->tm_mday = 1;
                    tm->tm_mon++;
                    if (tm->tm_mon >= 12) {
                        tm->tm_mon = 0;
                        tm->tm_year++;
                    }
                }
            }
        }
    }
391 392
}

393

394 395 396
static void rtc_update_second(void *opaque)
{
    RTCState *s = opaque;
B
bellard 已提交
397
    int64_t delay;
398 399 400

    /* if the oscillator is not in normal operation, we do not update */
    if ((s->cmos_data[RTC_REG_A] & 0x70) != 0x20) {
401
        s->next_second_time += get_ticks_per_sec();
402 403
        qemu_mod_timer(s->second_timer, s->next_second_time);
    } else {
404
        rtc_next_second(&s->current_tm);
405

406 407 408 409
        if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
            /* update in progress bit */
            s->cmos_data[RTC_REG_A] |= REG_A_UIP;
        }
B
bellard 已提交
410 411
        /* should be 244 us = 8 / 32768 seconds, but currently the
           timers do not have the necessary resolution. */
412
        delay = (get_ticks_per_sec() * 1) / 100;
B
bellard 已提交
413 414
        if (delay < 1)
            delay = 1;
415
        qemu_mod_timer(s->second_timer2,
B
bellard 已提交
416
                       s->next_second_time + delay);
417 418 419 420 421 422 423 424 425 426 427 428
    }
}

static void rtc_update_second2(void *opaque)
{
    RTCState *s = opaque;

    if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
        rtc_copy_date(s);
    }

    /* check alarm */
429 430 431 432 433 434 435 436 437
    if (((s->cmos_data[RTC_SECONDS_ALARM] & 0xc0) == 0xc0 ||
         rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]) == s->current_tm.tm_sec) &&
        ((s->cmos_data[RTC_MINUTES_ALARM] & 0xc0) == 0xc0 ||
         rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]) == s->current_tm.tm_min) &&
        ((s->cmos_data[RTC_HOURS_ALARM] & 0xc0) == 0xc0 ||
         rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]) == s->current_tm.tm_hour)) {

        s->cmos_data[RTC_REG_C] |= REG_C_AF;
        if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
438
            qemu_irq_raise(s->irq);
439
            s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
440 441 442 443
        }
    }

    /* update ended interrupt */
B
Bernhard Kauer 已提交
444
    s->cmos_data[RTC_REG_C] |= REG_C_UF;
445
    if (s->cmos_data[RTC_REG_B] & REG_B_UIE) {
446 447
        s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
        qemu_irq_raise(s->irq);
448 449 450 451 452
    }

    /* clear update in progress bit */
    s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;

453
    s->next_second_time += get_ticks_per_sec();
454
    qemu_mod_timer(s->second_timer, s->next_second_time);
B
bellard 已提交
455 456
}

B
bellard 已提交
457
static uint32_t cmos_ioport_read(void *opaque, uint32_t addr)
B
bellard 已提交
458
{
B
bellard 已提交
459
    RTCState *s = opaque;
B
bellard 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    int ret;
    if ((addr & 1) == 0) {
        return 0xff;
    } else {
        switch(s->cmos_index) {
        case RTC_SECONDS:
        case RTC_MINUTES:
        case RTC_HOURS:
        case RTC_DAY_OF_WEEK:
        case RTC_DAY_OF_MONTH:
        case RTC_MONTH:
        case RTC_YEAR:
            ret = s->cmos_data[s->cmos_index];
            break;
        case RTC_REG_A:
            ret = s->cmos_data[s->cmos_index];
            break;
        case RTC_REG_C:
            ret = s->cmos_data[s->cmos_index];
P
pbrook 已提交
479
            qemu_irq_lower(s->irq);
480
            s->cmos_data[RTC_REG_C] = 0x00;
481 482
#ifdef TARGET_I386
            if(s->irq_coalesced &&
483
                    (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
484 485
                    s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
                s->irq_reinject_on_ack_count++;
486
                s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF;
487
                apic_reset_irq_delivered();
B
Blue Swirl 已提交
488
                DPRINTF_C("cmos: injecting on ack\n");
489
                qemu_irq_raise(s->irq);
B
Blue Swirl 已提交
490
                if (apic_get_irq_delivered()) {
491
                    s->irq_coalesced--;
B
Blue Swirl 已提交
492 493 494
                    DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
                              s->irq_coalesced);
                }
495 496
            }
#endif
B
bellard 已提交
497 498 499 500 501
            break;
        default:
            ret = s->cmos_data[s->cmos_index];
            break;
        }
502 503
        CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
                     s->cmos_index, ret);
B
bellard 已提交
504 505 506 507
        return ret;
    }
}

508
void rtc_set_memory(ISADevice *dev, int addr, int val)
509
{
510
    RTCState *s = DO_UPCAST(RTCState, dev, dev);
511 512 513 514
    if (addr >= 0 && addr <= 127)
        s->cmos_data[addr] = val;
}

515
void rtc_set_date(ISADevice *dev, const struct tm *tm)
516
{
517
    RTCState *s = DO_UPCAST(RTCState, dev, dev);
518
    s->current_tm = *tm;
519 520 521
    rtc_copy_date(s);
}

522 523 524 525
/* PC cmos mappings */
#define REG_IBM_CENTURY_BYTE        0x32
#define REG_IBM_PS2_CENTURY_BYTE    0x37

526
static void rtc_set_date_from_host(ISADevice *dev)
527
{
528
    RTCState *s = DO_UPCAST(RTCState, dev, dev);
529
    struct tm tm;
530 531 532
    int val;

    /* set the CMOS date */
533
    qemu_get_timedate(&tm, 0);
534
    rtc_set_date(dev, &tm);
535

P
Paul Brook 已提交
536
    val = rtc_to_bcd(s, (tm.tm_year / 100) + 19);
537 538
    rtc_set_memory(dev, REG_IBM_CENTURY_BYTE, val);
    rtc_set_memory(dev, REG_IBM_PS2_CENTURY_BYTE, val);
539 540
}

J
Juan Quintela 已提交
541
static int rtc_post_load(void *opaque, int version_id)
B
bellard 已提交
542
{
J
Juan Quintela 已提交
543
#ifdef TARGET_I386
544 545
    RTCState *s = opaque;

546 547 548 549 550
    if (version_id >= 2) {
        if (rtc_td_hack) {
            rtc_coalesced_timer_update(s);
        }
    }
J
Juan Quintela 已提交
551
#endif
552 553 554
    return 0;
}

J
Juan Quintela 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
static const VMStateDescription vmstate_rtc = {
    .name = "mc146818rtc",
    .version_id = 2,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .post_load = rtc_post_load,
    .fields      = (VMStateField []) {
        VMSTATE_BUFFER(cmos_data, RTCState),
        VMSTATE_UINT8(cmos_index, RTCState),
        VMSTATE_INT32(current_tm.tm_sec, RTCState),
        VMSTATE_INT32(current_tm.tm_min, RTCState),
        VMSTATE_INT32(current_tm.tm_hour, RTCState),
        VMSTATE_INT32(current_tm.tm_wday, RTCState),
        VMSTATE_INT32(current_tm.tm_mday, RTCState),
        VMSTATE_INT32(current_tm.tm_mon, RTCState),
        VMSTATE_INT32(current_tm.tm_year, RTCState),
        VMSTATE_TIMER(periodic_timer, RTCState),
        VMSTATE_INT64(next_periodic_time, RTCState),
        VMSTATE_INT64(next_second_time, RTCState),
        VMSTATE_TIMER(second_timer, RTCState),
        VMSTATE_TIMER(second_timer2, RTCState),
        VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
        VMSTATE_UINT32_V(period, RTCState, 2),
        VMSTATE_END_OF_LIST()
    }
};

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
static void rtc_notify_clock_reset(Notifier *notifier, void *data)
{
    RTCState *s = container_of(notifier, RTCState, clock_reset_notifier);
    int64_t now = *(int64_t *)data;

    rtc_set_date_from_host(&s->dev);
    s->next_second_time = now + (get_ticks_per_sec() * 99) / 100;
    qemu_mod_timer(s->second_timer2, s->next_second_time);
    rtc_timer_update(s, now);
#ifdef TARGET_I386
    if (rtc_td_hack) {
        rtc_coalesced_timer_update(s);
    }
#endif
}

G
Gleb Natapov 已提交
598 599 600 601
static void rtc_reset(void *opaque)
{
    RTCState *s = opaque;

602 603
    s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
    s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
G
Gleb Natapov 已提交
604

605
    qemu_irq_lower(s->irq);
G
Gleb Natapov 已提交
606 607 608 609 610 611 612

#ifdef TARGET_I386
    if (rtc_td_hack)
	    s->irq_coalesced = 0;
#endif
}

613 614 615 616 617 618 619 620 621
static const MemoryRegionPortio cmos_portio[] = {
    {0, 2, 1, .read = cmos_ioport_read, .write = cmos_ioport_write },
    PORTIO_END_OF_LIST(),
};

static const MemoryRegionOps cmos_ops = {
    .old_portio = cmos_portio
};

622 623 624 625 626 627 628 629 630 631
// FIXME add int32 visitor
static void visit_type_int32(Visitor *v, int *value, const char *name, Error **errp)
{
    int64_t val = *value;
    visit_type_int(v, &val, name, errp);
}

static void rtc_get_date(DeviceState *dev, Visitor *v, void *opaque,
                         const char *name, Error **errp)
{
632
    ISADevice *isa = ISA_DEVICE(dev);
633 634 635 636 637 638 639 640 641 642 643 644
    RTCState *s = DO_UPCAST(RTCState, dev, isa);

    visit_start_struct(v, NULL, "struct tm", name, 0, errp);
    visit_type_int32(v, &s->current_tm.tm_year, "tm_year", errp);
    visit_type_int32(v, &s->current_tm.tm_mon, "tm_mon", errp);
    visit_type_int32(v, &s->current_tm.tm_mday, "tm_mday", errp);
    visit_type_int32(v, &s->current_tm.tm_hour, "tm_hour", errp);
    visit_type_int32(v, &s->current_tm.tm_min, "tm_min", errp);
    visit_type_int32(v, &s->current_tm.tm_sec, "tm_sec", errp);
    visit_end_struct(v, errp);
}

645
static int rtc_initfn(ISADevice *dev)
646
{
647 648
    RTCState *s = DO_UPCAST(RTCState, dev, dev);
    int base = 0x70;
B
bellard 已提交
649 650 651 652 653 654

    s->cmos_data[RTC_REG_A] = 0x26;
    s->cmos_data[RTC_REG_B] = 0x02;
    s->cmos_data[RTC_REG_C] = 0x00;
    s->cmos_data[RTC_REG_D] = 0x80;

655
    rtc_set_date_from_host(dev);
656

657
    s->periodic_timer = qemu_new_timer_ns(rtc_clock, rtc_periodic_timer, s);
658 659
#ifdef TARGET_I386
    if (rtc_td_hack)
J
Jan Kiszka 已提交
660
        s->coalesced_timer =
661
            qemu_new_timer_ns(rtc_clock, rtc_coalesced_timer, s);
662
#endif
663 664
    s->second_timer = qemu_new_timer_ns(rtc_clock, rtc_update_second, s);
    s->second_timer2 = qemu_new_timer_ns(rtc_clock, rtc_update_second2, s);
665

666 667 668
    s->clock_reset_notifier.notify = rtc_notify_clock_reset;
    qemu_register_clock_reset_notifier(rtc_clock, &s->clock_reset_notifier);

J
Jan Kiszka 已提交
669
    s->next_second_time =
670
        qemu_get_clock_ns(rtc_clock) + (get_ticks_per_sec() * 99) / 100;
671 672
    qemu_mod_timer(s->second_timer2, s->next_second_time);

673 674
    memory_region_init_io(&s->io, &cmos_ops, s, "rtc", 2);
    isa_register_ioport(dev, &s->io, base);
675

676
    qdev_set_legacy_instance_id(&dev->qdev, base, 2);
677
    qemu_register_reset(rtc_reset, s);
678 679 680 681

    qdev_property_add(&s->dev.qdev, "date", "struct tm",
                      rtc_get_date, NULL, NULL, s, NULL);

682 683 684
    return 0;
}

685
ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq)
686 687
{
    ISADevice *dev;
688
    RTCState *s;
G
Gleb Natapov 已提交
689

690
    dev = isa_create(bus, "mc146818rtc");
691
    s = DO_UPCAST(RTCState, dev, dev);
692
    qdev_prop_set_int32(&dev->qdev, "base_year", base_year);
M
Markus Armbruster 已提交
693
    qdev_init_nofail(&dev->qdev);
694 695 696 697 698
    if (intercept_irq) {
        s->irq = intercept_irq;
    } else {
        isa_init_irq(dev, &s->irq, RTC_ISA_IRQ);
    }
699
    return dev;
B
bellard 已提交
700 701
}

702 703 704 705 706 707 708 709 710 711 712 713 714
static void rtc_class_initfn(ObjectClass *klass, void *data)
{
    ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
    ic->init = rtc_initfn;
}

static DeviceInfo mc146818rtc_info = {
    .name     = "mc146818rtc",
    .size     = sizeof(RTCState),
    .no_user  = 1,
    .vmsd     = &vmstate_rtc,
    .class_init          = rtc_class_initfn,
    .props    = (Property[]) {
715 716 717 718 719 720
        DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
        DEFINE_PROP_END_OF_LIST(),
    }
};

static void mc146818rtc_register(void)
721
{
722
    isa_qdev_register(&mc146818rtc_info);
723
}
724
device_init(mc146818rtc_register)