i8254.c 13.3 KB
Newer Older
B
bellard 已提交
1 2
/*
 * QEMU 8253/8254 interval timer 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 "pc.h"
#include "isa.h"
#include "qemu-timer.h"
B
bellard 已提交
28

B
bellard 已提交
29 30
//#define DEBUG_PIT

B
bellard 已提交
31 32 33 34
#define RW_STATE_LSB 1
#define RW_STATE_MSB 2
#define RW_STATE_WORD0 3
#define RW_STATE_WORD1 4
B
bellard 已提交
35

B
bellard 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
typedef struct PITChannelState {
    int count; /* can be 65536 */
    uint16_t latched_count;
    uint8_t count_latched;
    uint8_t status_latched;
    uint8_t status;
    uint8_t read_state;
    uint8_t write_state;
    uint8_t write_latch;
    uint8_t rw_mode;
    uint8_t mode;
    uint8_t bcd; /* not supported */
    uint8_t gate; /* timer start */
    int64_t count_load_time;
    /* irq handling */
    int64_t next_transition_time;
    QEMUTimer *irq_timer;
P
pbrook 已提交
53
    qemu_irq irq;
B
bellard 已提交
54 55 56 57 58 59 60
} PITChannelState;

struct PITState {
    PITChannelState channels[3];
};

static PITState pit_state;
B
bellard 已提交
61

B
bellard 已提交
62 63
static void pit_irq_timer_update(PITChannelState *s, int64_t current_time);

B
bellard 已提交
64 65 66 67 68
static int pit_get_count(PITChannelState *s)
{
    uint64_t d;
    int counter;

B
bellard 已提交
69
    d = muldiv64(qemu_get_clock(vm_clock) - s->count_load_time, PIT_FREQ, ticks_per_sec);
B
bellard 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    switch(s->mode) {
    case 0:
    case 1:
    case 4:
    case 5:
        counter = (s->count - d) & 0xffff;
        break;
    case 3:
        /* XXX: may be incorrect for odd counts */
        counter = s->count - ((2 * d) % s->count);
        break;
    default:
        counter = s->count - (d % s->count);
        break;
    }
    return counter;
}

/* get pit output bit */
B
bellard 已提交
89
static int pit_get_out1(PITChannelState *s, int64_t current_time)
B
bellard 已提交
90 91 92 93
{
    uint64_t d;
    int out;

B
bellard 已提交
94
    d = muldiv64(current_time - s->count_load_time, PIT_FREQ, ticks_per_sec);
B
bellard 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    switch(s->mode) {
    default:
    case 0:
        out = (d >= s->count);
        break;
    case 1:
        out = (d < s->count);
        break;
    case 2:
        if ((d % s->count) == 0 && d != 0)
            out = 1;
        else
            out = 0;
        break;
    case 3:
        out = (d % s->count) < ((s->count + 1) >> 1);
        break;
    case 4:
    case 5:
        out = (d == s->count);
        break;
    }
    return out;
}

B
bellard 已提交
120 121 122 123 124 125
int pit_get_out(PITState *pit, int channel, int64_t current_time)
{
    PITChannelState *s = &pit->channels[channel];
    return pit_get_out1(s, current_time);
}

B
bellard 已提交
126
/* return -1 if no transition will occur.  */
127
static int64_t pit_get_next_transition_time(PITChannelState *s,
B
bellard 已提交
128
                                            int64_t current_time)
B
bellard 已提交
129
{
B
bellard 已提交
130 131
    uint64_t d, next_time, base;
    int period2;
B
bellard 已提交
132

B
bellard 已提交
133
    d = muldiv64(current_time - s->count_load_time, PIT_FREQ, ticks_per_sec);
B
bellard 已提交
134 135 136 137
    switch(s->mode) {
    default:
    case 0:
    case 1:
B
bellard 已提交
138 139 140 141
        if (d < s->count)
            next_time = s->count;
        else
            return -1;
B
bellard 已提交
142 143
        break;
    case 2:
B
bellard 已提交
144 145 146 147 148
        base = (d / s->count) * s->count;
        if ((d - base) == 0 && d != 0)
            next_time = base + s->count;
        else
            next_time = base + s->count + 1;
B
bellard 已提交
149 150
        break;
    case 3:
B
bellard 已提交
151 152
        base = (d / s->count) * s->count;
        period2 = ((s->count + 1) >> 1);
153
        if ((d - base) < period2)
B
bellard 已提交
154 155 156
            next_time = base + period2;
        else
            next_time = base + s->count;
B
bellard 已提交
157 158 159
        break;
    case 4:
    case 5:
B
bellard 已提交
160 161 162 163
        if (d < s->count)
            next_time = s->count;
        else if (d == s->count)
            next_time = s->count + 1;
B
bellard 已提交
164
        else
B
bellard 已提交
165
            return -1;
B
bellard 已提交
166 167
        break;
    }
B
bellard 已提交
168 169
    /* convert to timer units */
    next_time = s->count_load_time + muldiv64(next_time, ticks_per_sec, PIT_FREQ);
B
bellard 已提交
170 171 172 173
    /* fix potential rounding problems */
    /* XXX: better solution: use a clock at PIT_FREQ Hz */
    if (next_time <= current_time)
        next_time = current_time + 1;
B
bellard 已提交
174
    return next_time;
B
bellard 已提交
175 176 177
}

/* val must be 0 or 1 */
B
bellard 已提交
178
void pit_set_gate(PITState *pit, int channel, int val)
B
bellard 已提交
179
{
B
bellard 已提交
180 181
    PITChannelState *s = &pit->channels[channel];

B
bellard 已提交
182 183 184 185 186 187 188 189 190 191
    switch(s->mode) {
    default:
    case 0:
    case 4:
        /* XXX: just disable/enable counting */
        break;
    case 1:
    case 5:
        if (s->gate < val) {
            /* restart counting on rising edge */
B
bellard 已提交
192 193
            s->count_load_time = qemu_get_clock(vm_clock);
            pit_irq_timer_update(s, s->count_load_time);
B
bellard 已提交
194 195 196 197 198 199
        }
        break;
    case 2:
    case 3:
        if (s->gate < val) {
            /* restart counting on rising edge */
B
bellard 已提交
200 201
            s->count_load_time = qemu_get_clock(vm_clock);
            pit_irq_timer_update(s, s->count_load_time);
B
bellard 已提交
202 203 204 205 206 207 208
        }
        /* XXX: disable/enable counting */
        break;
    }
    s->gate = val;
}

B
bellard 已提交
209 210 211 212 213 214
int pit_get_gate(PITState *pit, int channel)
{
    PITChannelState *s = &pit->channels[channel];
    return s->gate;
}

B
bellard 已提交
215 216 217 218 219 220 221 222 223 224 225 226
int pit_get_initial_count(PITState *pit, int channel)
{
    PITChannelState *s = &pit->channels[channel];
    return s->count;
}

int pit_get_mode(PITState *pit, int channel)
{
    PITChannelState *s = &pit->channels[channel];
    return s->mode;
}

B
bellard 已提交
227 228 229 230
static inline void pit_load_count(PITChannelState *s, int val)
{
    if (val == 0)
        val = 0x10000;
B
bellard 已提交
231
    s->count_load_time = qemu_get_clock(vm_clock);
B
bellard 已提交
232
    s->count = val;
B
bellard 已提交
233
    pit_irq_timer_update(s, s->count_load_time);
B
bellard 已提交
234 235
}

B
bellard 已提交
236 237 238 239 240 241 242 243 244
/* if already latched, do not latch again */
static void pit_latch_count(PITChannelState *s)
{
    if (!s->count_latched) {
        s->latched_count = pit_get_count(s);
        s->count_latched = s->rw_mode;
    }
}

B
bellard 已提交
245
static void pit_ioport_write(void *opaque, uint32_t addr, uint32_t val)
B
bellard 已提交
246
{
B
bellard 已提交
247
    PITState *pit = opaque;
B
bellard 已提交
248 249 250 251 252 253
    int channel, access;
    PITChannelState *s;

    addr &= 3;
    if (addr == 3) {
        channel = val >> 6;
B
bellard 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
        if (channel == 3) {
            /* read back command */
            for(channel = 0; channel < 3; channel++) {
                s = &pit->channels[channel];
                if (val & (2 << channel)) {
                    if (!(val & 0x20)) {
                        pit_latch_count(s);
                    }
                    if (!(val & 0x10) && !s->status_latched) {
                        /* status latch */
                        /* XXX: add BCD and null count */
                        s->status =  (pit_get_out1(s, qemu_get_clock(vm_clock)) << 7) |
                            (s->rw_mode << 4) |
                            (s->mode << 1) |
                            s->bcd;
                        s->status_latched = 1;
                    }
                }
            }
        } else {
            s = &pit->channels[channel];
            access = (val >> 4) & 3;
            if (access == 0) {
                pit_latch_count(s);
            } else {
                s->rw_mode = access;
                s->read_state = access;
                s->write_state = access;

                s->mode = (val >> 1) & 7;
                s->bcd = val & 1;
                /* XXX: update irq timer ? */
            }
B
bellard 已提交
287 288
        }
    } else {
B
bellard 已提交
289 290 291
        s = &pit->channels[addr];
        switch(s->write_state) {
        default:
B
bellard 已提交
292 293 294 295 296 297 298
        case RW_STATE_LSB:
            pit_load_count(s, val);
            break;
        case RW_STATE_MSB:
            pit_load_count(s, val << 8);
            break;
        case RW_STATE_WORD0:
B
bellard 已提交
299 300 301
            s->write_latch = val;
            s->write_state = RW_STATE_WORD1;
            break;
B
bellard 已提交
302
        case RW_STATE_WORD1:
B
bellard 已提交
303 304
            pit_load_count(s, s->write_latch | (val << 8));
            s->write_state = RW_STATE_WORD0;
B
bellard 已提交
305 306 307 308 309
            break;
        }
    }
}

B
bellard 已提交
310
static uint32_t pit_ioport_read(void *opaque, uint32_t addr)
B
bellard 已提交
311
{
B
bellard 已提交
312
    PITState *pit = opaque;
B
bellard 已提交
313 314
    int ret, count;
    PITChannelState *s;
315

B
bellard 已提交
316
    addr &= 3;
B
bellard 已提交
317 318 319 320 321 322 323 324 325 326 327 328
    s = &pit->channels[addr];
    if (s->status_latched) {
        s->status_latched = 0;
        ret = s->status;
    } else if (s->count_latched) {
        switch(s->count_latched) {
        default:
        case RW_STATE_LSB:
            ret = s->latched_count & 0xff;
            s->count_latched = 0;
            break;
        case RW_STATE_MSB:
B
bellard 已提交
329
            ret = s->latched_count >> 8;
B
bellard 已提交
330 331 332
            s->count_latched = 0;
            break;
        case RW_STATE_WORD0:
B
bellard 已提交
333
            ret = s->latched_count & 0xff;
B
bellard 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
            s->count_latched = RW_STATE_MSB;
            break;
        }
    } else {
        switch(s->read_state) {
        default:
        case RW_STATE_LSB:
            count = pit_get_count(s);
            ret = count & 0xff;
            break;
        case RW_STATE_MSB:
            count = pit_get_count(s);
            ret = (count >> 8) & 0xff;
            break;
        case RW_STATE_WORD0:
            count = pit_get_count(s);
            ret = count & 0xff;
            s->read_state = RW_STATE_WORD1;
            break;
        case RW_STATE_WORD1:
            count = pit_get_count(s);
            ret = (count >> 8) & 0xff;
            s->read_state = RW_STATE_WORD0;
            break;
        }
B
bellard 已提交
359 360 361 362
    }
    return ret;
}

B
bellard 已提交
363 364 365 366 367 368 369 370
static void pit_irq_timer_update(PITChannelState *s, int64_t current_time)
{
    int64_t expire_time;
    int irq_level;

    if (!s->irq_timer)
        return;
    expire_time = pit_get_next_transition_time(s, current_time);
B
bellard 已提交
371
    irq_level = pit_get_out1(s, current_time);
P
pbrook 已提交
372
    qemu_set_irq(s->irq, irq_level);
B
bellard 已提交
373 374
#ifdef DEBUG_PIT
    printf("irq_level=%d next_delay=%f\n",
375
           irq_level,
B
bellard 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
           (double)(expire_time - current_time) / ticks_per_sec);
#endif
    s->next_transition_time = expire_time;
    if (expire_time != -1)
        qemu_mod_timer(s->irq_timer, expire_time);
    else
        qemu_del_timer(s->irq_timer);
}

static void pit_irq_timer(void *opaque)
{
    PITChannelState *s = opaque;

    pit_irq_timer_update(s, s->next_transition_time);
}

static void pit_save(QEMUFile *f, void *opaque)
{
B
bellard 已提交
394
    PITState *pit = opaque;
B
bellard 已提交
395 396
    PITChannelState *s;
    int i;
397

B
bellard 已提交
398
    for(i = 0; i < 3; i++) {
B
bellard 已提交
399
        s = &pit->channels[i];
400
        qemu_put_be32(f, s->count);
B
bellard 已提交
401
        qemu_put_be16s(f, &s->latched_count);
B
bellard 已提交
402 403 404 405 406 407 408
        qemu_put_8s(f, &s->count_latched);
        qemu_put_8s(f, &s->status_latched);
        qemu_put_8s(f, &s->status);
        qemu_put_8s(f, &s->read_state);
        qemu_put_8s(f, &s->write_state);
        qemu_put_8s(f, &s->write_latch);
        qemu_put_8s(f, &s->rw_mode);
B
bellard 已提交
409 410 411
        qemu_put_8s(f, &s->mode);
        qemu_put_8s(f, &s->bcd);
        qemu_put_8s(f, &s->gate);
412
        qemu_put_be64(f, s->count_load_time);
B
bellard 已提交
413
        if (s->irq_timer) {
414
            qemu_put_be64(f, s->next_transition_time);
B
bellard 已提交
415 416 417 418 419 420 421
            qemu_put_timer(f, s->irq_timer);
        }
    }
}

static int pit_load(QEMUFile *f, void *opaque, int version_id)
{
B
bellard 已提交
422
    PITState *pit = opaque;
B
bellard 已提交
423 424
    PITChannelState *s;
    int i;
425

B
bellard 已提交
426 427 428 429
    if (version_id != 1)
        return -EINVAL;

    for(i = 0; i < 3; i++) {
B
bellard 已提交
430
        s = &pit->channels[i];
431
        s->count=qemu_get_be32(f);
B
bellard 已提交
432
        qemu_get_be16s(f, &s->latched_count);
B
bellard 已提交
433 434 435 436 437 438 439
        qemu_get_8s(f, &s->count_latched);
        qemu_get_8s(f, &s->status_latched);
        qemu_get_8s(f, &s->status);
        qemu_get_8s(f, &s->read_state);
        qemu_get_8s(f, &s->write_state);
        qemu_get_8s(f, &s->write_latch);
        qemu_get_8s(f, &s->rw_mode);
B
bellard 已提交
440 441 442
        qemu_get_8s(f, &s->mode);
        qemu_get_8s(f, &s->bcd);
        qemu_get_8s(f, &s->gate);
443
        s->count_load_time=qemu_get_be64(f);
B
bellard 已提交
444
        if (s->irq_timer) {
445
            s->next_transition_time=qemu_get_be64(f);
B
bellard 已提交
446 447 448 449 450 451
            qemu_get_timer(f, s->irq_timer);
        }
    }
    return 0;
}

B
bellard 已提交
452
static void pit_reset(void *opaque)
B
bellard 已提交
453
{
B
bellard 已提交
454
    PITState *pit = opaque;
B
bellard 已提交
455 456 457 458
    PITChannelState *s;
    int i;

    for(i = 0;i < 3; i++) {
B
bellard 已提交
459
        s = &pit->channels[i];
B
bellard 已提交
460 461 462 463
        s->mode = 3;
        s->gate = (i != 2);
        pit_load_count(s, 0);
    }
B
bellard 已提交
464 465
}

A
aliguori 已提交
466 467 468 469
/* When HPET is operating in legacy mode, i8254 timer0 is disabled */
void hpet_pit_disable(void) {
    PITChannelState *s;
    s = &pit_state.channels[0];
470 471
    if (s->irq_timer)
        qemu_del_timer(s->irq_timer);
A
aliguori 已提交
472 473
}

474
/* When HPET is reset or leaving legacy mode, it must reenable i8254
A
aliguori 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487
 * timer 0
 */

void hpet_pit_enable(void)
{
    PITState *pit = &pit_state;
    PITChannelState *s;
    s = &pit->channels[0];
    s->mode = 3;
    s->gate = 1;
    pit_load_count(s, 0);
}

P
pbrook 已提交
488
PITState *pit_init(int base, qemu_irq irq)
B
bellard 已提交
489 490 491 492 493 494 495 496
{
    PITState *pit = &pit_state;
    PITChannelState *s;

    s = &pit->channels[0];
    /* the timer 0 is connected to an IRQ */
    s->irq_timer = qemu_new_timer(vm_clock, pit_irq_timer, s);
    s->irq = irq;
B
bellard 已提交
497

B
bellard 已提交
498
    register_savevm("i8254", base, 1, pit_save, pit_load, pit);
B
bellard 已提交
499

500
    qemu_register_reset(pit_reset, pit);
B
bellard 已提交
501 502
    register_ioport_write(base, 4, 1, pit_ioport_write, pit);
    register_ioport_read(base, 3, 1, pit_ioport_read, pit);
B
bellard 已提交
503 504 505

    pit_reset(pit);

B
bellard 已提交
506
    return pit;
B
bellard 已提交
507
}