i8254.c 10.0 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.
 */
24
#include "hw/hw.h"
P
Paolo Bonzini 已提交
25 26
#include "hw/i386/pc.h"
#include "hw/isa/isa.h"
27
#include "qemu/timer.h"
P
Paolo Bonzini 已提交
28 29
#include "hw/timer/i8254.h"
#include "hw/timer/i8254_internal.h"
B
bellard 已提交
30

B
bellard 已提交
31 32
//#define DEBUG_PIT

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

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

B
bellard 已提交
40 41 42 43 44
static int pit_get_count(PITChannelState *s)
{
    uint64_t d;
    int counter;

45
    d = muldiv64(qemu_get_clock_ns(vm_clock) - s->count_load_time, PIT_FREQ,
46
                 get_ticks_per_sec());
B
bellard 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    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;
}

/* val must be 0 or 1 */
66 67
static void pit_set_channel_gate(PITCommonState *s, PITChannelState *sc,
                                 int val)
B
bellard 已提交
68
{
69
    switch (sc->mode) {
B
bellard 已提交
70 71 72 73 74 75 76
    default:
    case 0:
    case 4:
        /* XXX: just disable/enable counting */
        break;
    case 1:
    case 5:
77
        if (sc->gate < val) {
B
bellard 已提交
78
            /* restart counting on rising edge */
79 80
            sc->count_load_time = qemu_get_clock_ns(vm_clock);
            pit_irq_timer_update(sc, sc->count_load_time);
B
bellard 已提交
81 82 83 84
        }
        break;
    case 2:
    case 3:
85
        if (sc->gate < val) {
B
bellard 已提交
86
            /* restart counting on rising edge */
87 88
            sc->count_load_time = qemu_get_clock_ns(vm_clock);
            pit_irq_timer_update(sc, sc->count_load_time);
B
bellard 已提交
89 90 91 92
        }
        /* XXX: disable/enable counting */
        break;
    }
93
    sc->gate = val;
B
bellard 已提交
94 95
}

B
bellard 已提交
96 97 98 99
static inline void pit_load_count(PITChannelState *s, int val)
{
    if (val == 0)
        val = 0x10000;
100
    s->count_load_time = qemu_get_clock_ns(vm_clock);
B
bellard 已提交
101
    s->count = val;
B
bellard 已提交
102
    pit_irq_timer_update(s, s->count_load_time);
B
bellard 已提交
103 104
}

B
bellard 已提交
105 106 107 108 109 110 111 112 113
/* 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;
    }
}

114 115
static void pit_ioport_write(void *opaque, hwaddr addr,
                             uint64_t val, unsigned size)
B
bellard 已提交
116
{
117
    PITCommonState *pit = opaque;
B
bellard 已提交
118 119 120 121 122 123
    int channel, access;
    PITChannelState *s;

    addr &= 3;
    if (addr == 3) {
        channel = val >> 6;
B
bellard 已提交
124 125 126 127 128 129 130 131 132 133 134
        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 */
135 136 137
                        s->status =
                            (pit_get_out(s,
                                         qemu_get_clock_ns(vm_clock)) << 7) |
B
bellard 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
                            (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 已提交
159 160
        }
    } else {
B
bellard 已提交
161 162 163
        s = &pit->channels[addr];
        switch(s->write_state) {
        default:
B
bellard 已提交
164 165 166 167 168 169 170
        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 已提交
171 172 173
            s->write_latch = val;
            s->write_state = RW_STATE_WORD1;
            break;
B
bellard 已提交
174
        case RW_STATE_WORD1:
B
bellard 已提交
175 176
            pit_load_count(s, s->write_latch | (val << 8));
            s->write_state = RW_STATE_WORD0;
B
bellard 已提交
177 178 179 180 181
            break;
        }
    }
}

182 183
static uint64_t pit_ioport_read(void *opaque, hwaddr addr,
                                unsigned size)
B
bellard 已提交
184
{
185
    PITCommonState *pit = opaque;
B
bellard 已提交
186 187
    int ret, count;
    PITChannelState *s;
188

B
bellard 已提交
189
    addr &= 3;
B
bellard 已提交
190 191 192 193 194 195 196 197 198 199 200 201
    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 已提交
202
            ret = s->latched_count >> 8;
B
bellard 已提交
203 204 205
            s->count_latched = 0;
            break;
        case RW_STATE_WORD0:
B
bellard 已提交
206
            ret = s->latched_count & 0xff;
B
bellard 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            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 已提交
232 233 234 235
    }
    return ret;
}

B
bellard 已提交
236 237 238 239 240
static void pit_irq_timer_update(PITChannelState *s, int64_t current_time)
{
    int64_t expire_time;
    int irq_level;

241
    if (!s->irq_timer || s->irq_disabled) {
B
bellard 已提交
242
        return;
243
    }
B
bellard 已提交
244
    expire_time = pit_get_next_transition_time(s, current_time);
245
    irq_level = pit_get_out(s, current_time);
P
pbrook 已提交
246
    qemu_set_irq(s->irq, irq_level);
B
bellard 已提交
247 248
#ifdef DEBUG_PIT
    printf("irq_level=%d next_delay=%f\n",
249
           irq_level,
250
           (double)(expire_time - current_time) / get_ticks_per_sec());
B
bellard 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
#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);
}

266
static void pit_reset(DeviceState *dev)
B
bellard 已提交
267
{
A
Andreas Färber 已提交
268
    PITCommonState *pit = PIT_COMMON(dev);
B
bellard 已提交
269 270
    PITChannelState *s;

271
    pit_reset_common(pit);
J
Juan Quintela 已提交
272

273 274 275
    s = &pit->channels[0];
    if (!s->irq_disabled) {
        qemu_mod_timer(s->irq_timer, s->next_transition_time);
B
bellard 已提交
276
    }
B
bellard 已提交
277 278
}

279 280 281
/* When HPET is operating in legacy mode, suppress the ignored timer IRQ,
 * reenable it when legacy mode is left again. */
static void pit_irq_control(void *opaque, int n, int enable)
A
aliguori 已提交
282
{
283
    PITCommonState *pit = opaque;
284 285 286 287 288 289 290 291 292
    PITChannelState *s = &pit->channels[0];

    if (enable) {
        s->irq_disabled = 0;
        pit_irq_timer_update(s, qemu_get_clock_ns(vm_clock));
    } else {
        s->irq_disabled = 1;
        qemu_del_timer(s->irq_timer);
    }
A
aliguori 已提交
293 294
}

295
static const MemoryRegionOps pit_ioport_ops = {
296 297 298 299 300 301 302
    .read = pit_ioport_read,
    .write = pit_ioport_write,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 1,
    },
    .endianness = DEVICE_LITTLE_ENDIAN,
303 304
};

J
Jan Kiszka 已提交
305 306 307 308 309 310 311 312 313 314 315
static void pit_post_load(PITCommonState *s)
{
    PITChannelState *sc = &s->channels[0];

    if (sc->next_transition_time != -1) {
        qemu_mod_timer(sc->irq_timer, sc->next_transition_time);
    } else {
        qemu_del_timer(sc->irq_timer);
    }
}

316
static int pit_initfn(PITCommonState *pit)
B
bellard 已提交
317 318 319 320 321
{
    PITChannelState *s;

    s = &pit->channels[0];
    /* the timer 0 is connected to an IRQ */
322
    s->irq_timer = qemu_new_timer_ns(vm_clock, pit_irq_timer, s);
323
    qdev_init_gpio_out(&pit->dev.qdev, &s->irq, 1);
B
bellard 已提交
324

325
    memory_region_init_io(&pit->ioports, &pit_ioport_ops, pit, "pit", 4);
326

327
    qdev_init_gpio_in(&pit->dev.qdev, pit_irq_control, 1);
328

B
Blue Swirl 已提交
329 330 331
    return 0;
}

332
static Property pit_properties[] = {
333
    DEFINE_PROP_HEX32("iobase", PITCommonState, iobase,  -1),
334 335 336
    DEFINE_PROP_END_OF_LIST(),
};

337 338
static void pit_class_initfn(ObjectClass *klass, void *data)
{
339
    PITCommonClass *k = PIT_COMMON_CLASS(klass);
340
    DeviceClass *dc = DEVICE_CLASS(klass);
341 342 343 344

    k->init = pit_initfn;
    k->set_channel_gate = pit_set_channel_gate;
    k->get_channel_info = pit_get_channel_info_common;
J
Jan Kiszka 已提交
345
    k->post_load = pit_post_load;
346 347
    dc->reset = pit_reset;
    dc->props = pit_properties;
348 349
}

350
static const TypeInfo pit_info = {
A
Andreas Färber 已提交
351
    .name          = TYPE_I8254,
352 353
    .parent        = TYPE_PIT_COMMON,
    .instance_size = sizeof(PITCommonState),
354
    .class_init    = pit_class_initfn,
B
Blue Swirl 已提交
355 356
};

A
Andreas Färber 已提交
357
static void pit_register_types(void)
B
Blue Swirl 已提交
358
{
359
    type_register_static(&pit_info);
B
bellard 已提交
360
}
A
Andreas Färber 已提交
361 362

type_init(pit_register_types)