i8254.c 10.5 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

38 39 40 41 42 43 44 45 46
#define PIT_CLASS(class) OBJECT_CLASS_CHECK(PITClass, (class), TYPE_I8254)
#define PIT_GET_CLASS(obj) OBJECT_GET_CLASS(PITClass, (obj), TYPE_I8254)

typedef struct PITClass {
    PITCommonClass parent_class;

    DeviceRealize parent_realize;
} PITClass;

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

B
bellard 已提交
49 50 51 52 53
static int pit_get_count(PITChannelState *s)
{
    uint64_t d;
    int counter;

54
    d = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - s->count_load_time, PIT_FREQ,
55
                 get_ticks_per_sec());
B
bellard 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    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 */
75 76
static void pit_set_channel_gate(PITCommonState *s, PITChannelState *sc,
                                 int val)
B
bellard 已提交
77
{
78
    switch (sc->mode) {
B
bellard 已提交
79 80 81 82 83 84 85
    default:
    case 0:
    case 4:
        /* XXX: just disable/enable counting */
        break;
    case 1:
    case 5:
86
        if (sc->gate < val) {
B
bellard 已提交
87
            /* restart counting on rising edge */
88
            sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
89
            pit_irq_timer_update(sc, sc->count_load_time);
B
bellard 已提交
90 91 92 93
        }
        break;
    case 2:
    case 3:
94
        if (sc->gate < val) {
B
bellard 已提交
95
            /* restart counting on rising edge */
96
            sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
97
            pit_irq_timer_update(sc, sc->count_load_time);
B
bellard 已提交
98 99 100 101
        }
        /* XXX: disable/enable counting */
        break;
    }
102
    sc->gate = val;
B
bellard 已提交
103 104
}

B
bellard 已提交
105 106 107 108
static inline void pit_load_count(PITChannelState *s, int val)
{
    if (val == 0)
        val = 0x10000;
109
    s->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
B
bellard 已提交
110
    s->count = val;
B
bellard 已提交
111
    pit_irq_timer_update(s, s->count_load_time);
B
bellard 已提交
112 113
}

B
bellard 已提交
114 115 116 117 118 119 120 121 122
/* 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;
    }
}

123 124
static void pit_ioport_write(void *opaque, hwaddr addr,
                             uint64_t val, unsigned size)
B
bellard 已提交
125
{
126
    PITCommonState *pit = opaque;
B
bellard 已提交
127 128 129 130 131 132
    int channel, access;
    PITChannelState *s;

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

191 192
static uint64_t pit_ioport_read(void *opaque, hwaddr addr,
                                unsigned size)
B
bellard 已提交
193
{
194
    PITCommonState *pit = opaque;
B
bellard 已提交
195 196
    int ret, count;
    PITChannelState *s;
197

B
bellard 已提交
198
    addr &= 3;
B
bellard 已提交
199 200 201 202 203 204 205 206 207 208 209 210
    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 已提交
211
            ret = s->latched_count >> 8;
B
bellard 已提交
212 213 214
            s->count_latched = 0;
            break;
        case RW_STATE_WORD0:
B
bellard 已提交
215
            ret = s->latched_count & 0xff;
B
bellard 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            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 已提交
241 242 243 244
    }
    return ret;
}

B
bellard 已提交
245 246 247 248 249
static void pit_irq_timer_update(PITChannelState *s, int64_t current_time)
{
    int64_t expire_time;
    int irq_level;

250
    if (!s->irq_timer || s->irq_disabled) {
B
bellard 已提交
251
        return;
252
    }
B
bellard 已提交
253
    expire_time = pit_get_next_transition_time(s, current_time);
254
    irq_level = pit_get_out(s, current_time);
P
pbrook 已提交
255
    qemu_set_irq(s->irq, irq_level);
B
bellard 已提交
256 257
#ifdef DEBUG_PIT
    printf("irq_level=%d next_delay=%f\n",
258
           irq_level,
259
           (double)(expire_time - current_time) / get_ticks_per_sec());
B
bellard 已提交
260 261 262
#endif
    s->next_transition_time = expire_time;
    if (expire_time != -1)
263
        timer_mod(s->irq_timer, expire_time);
B
bellard 已提交
264
    else
265
        timer_del(s->irq_timer);
B
bellard 已提交
266 267 268 269 270 271 272 273 274
}

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

    pit_irq_timer_update(s, s->next_transition_time);
}

275
static void pit_reset(DeviceState *dev)
B
bellard 已提交
276
{
A
Andreas Färber 已提交
277
    PITCommonState *pit = PIT_COMMON(dev);
B
bellard 已提交
278 279
    PITChannelState *s;

280
    pit_reset_common(pit);
J
Juan Quintela 已提交
281

282 283
    s = &pit->channels[0];
    if (!s->irq_disabled) {
284
        timer_mod(s->irq_timer, s->next_transition_time);
B
bellard 已提交
285
    }
B
bellard 已提交
286 287
}

288 289 290
/* 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 已提交
291
{
292
    PITCommonState *pit = opaque;
293 294 295 296
    PITChannelState *s = &pit->channels[0];

    if (enable) {
        s->irq_disabled = 0;
297
        pit_irq_timer_update(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
298 299
    } else {
        s->irq_disabled = 1;
300
        timer_del(s->irq_timer);
301
    }
A
aliguori 已提交
302 303
}

304
static const MemoryRegionOps pit_ioport_ops = {
305 306 307 308 309 310 311
    .read = pit_ioport_read,
    .write = pit_ioport_write,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 1,
    },
    .endianness = DEVICE_LITTLE_ENDIAN,
312 313
};

J
Jan Kiszka 已提交
314 315 316 317 318
static void pit_post_load(PITCommonState *s)
{
    PITChannelState *sc = &s->channels[0];

    if (sc->next_transition_time != -1) {
319
        timer_mod(sc->irq_timer, sc->next_transition_time);
J
Jan Kiszka 已提交
320
    } else {
321
        timer_del(sc->irq_timer);
J
Jan Kiszka 已提交
322 323 324
    }
}

325
static void pit_realizefn(DeviceState *dev, Error **errp)
B
bellard 已提交
326
{
327 328
    PITCommonState *pit = PIT_COMMON(dev);
    PITClass *pc = PIT_GET_CLASS(dev);
B
bellard 已提交
329 330 331 332
    PITChannelState *s;

    s = &pit->channels[0];
    /* the timer 0 is connected to an IRQ */
333
    s->irq_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, pit_irq_timer, s);
334
    qdev_init_gpio_out(dev, &s->irq, 1);
B
bellard 已提交
335

336 337
    memory_region_init_io(&pit->ioports, OBJECT(pit), &pit_ioport_ops,
                          pit, "pit", 4);
338

339
    qdev_init_gpio_in(dev, pit_irq_control, 1);
340

341
    pc->parent_realize(dev, errp);
B
Blue Swirl 已提交
342 343
}

344
static Property pit_properties[] = {
345
    DEFINE_PROP_UINT32("iobase", PITCommonState, iobase,  -1),
346 347 348
    DEFINE_PROP_END_OF_LIST(),
};

349 350
static void pit_class_initfn(ObjectClass *klass, void *data)
{
351
    PITClass *pc = PIT_CLASS(klass);
352
    PITCommonClass *k = PIT_COMMON_CLASS(klass);
353
    DeviceClass *dc = DEVICE_CLASS(klass);
354

355 356
    pc->parent_realize = dc->realize;
    dc->realize = pit_realizefn;
357 358
    k->set_channel_gate = pit_set_channel_gate;
    k->get_channel_info = pit_get_channel_info_common;
J
Jan Kiszka 已提交
359
    k->post_load = pit_post_load;
360 361
    dc->reset = pit_reset;
    dc->props = pit_properties;
362 363
}

364
static const TypeInfo pit_info = {
A
Andreas Färber 已提交
365
    .name          = TYPE_I8254,
366 367
    .parent        = TYPE_PIT_COMMON,
    .instance_size = sizeof(PITCommonState),
368
    .class_init    = pit_class_initfn,
369
    .class_size    = sizeof(PITClass),
B
Blue Swirl 已提交
370 371
};

A
Andreas Färber 已提交
372
static void pit_register_types(void)
B
Blue Swirl 已提交
373
{
374
    type_register_static(&pit_info);
B
bellard 已提交
375
}
A
Andreas Färber 已提交
376 377

type_init(pit_register_types)