2004-03-14 13:20:30 +01:00
|
|
|
/*
|
|
|
|
* QEMU 8253/8254 interval timer emulation
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-03-14 13:20:30 +01:00
|
|
|
* Copyright (c) 2003-2004 Fabrice Bellard
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-03-14 13:20:30 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2013-02-04 15:40:22 +01:00
|
|
|
#include "hw/hw.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/i386/pc.h"
|
|
|
|
#include "hw/isa/isa.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/timer.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/timer/i8254.h"
|
|
|
|
#include "hw/timer/i8254_internal.h"
|
2004-03-14 13:20:30 +01:00
|
|
|
|
2004-03-31 20:58:38 +02:00
|
|
|
//#define DEBUG_PIT
|
|
|
|
|
2004-05-04 01:18:25 +02:00
|
|
|
#define RW_STATE_LSB 1
|
|
|
|
#define RW_STATE_MSB 2
|
|
|
|
#define RW_STATE_WORD0 3
|
|
|
|
#define RW_STATE_WORD1 4
|
2004-03-14 13:20:30 +01:00
|
|
|
|
2012-11-25 18:47:58 +01:00
|
|
|
#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;
|
|
|
|
|
2004-03-31 20:58:38 +02:00
|
|
|
static void pit_irq_timer_update(PITChannelState *s, int64_t current_time);
|
|
|
|
|
2004-03-14 13:20:30 +01:00
|
|
|
static int pit_get_count(PITChannelState *s)
|
|
|
|
{
|
|
|
|
uint64_t d;
|
|
|
|
int counter;
|
|
|
|
|
2013-08-21 17:03:08 +02:00
|
|
|
d = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - s->count_load_time, PIT_FREQ,
|
2009-09-10 03:04:26 +02:00
|
|
|
get_ticks_per_sec());
|
2004-03-14 13:20:30 +01:00
|
|
|
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 */
|
2012-03-02 20:28:46 +01:00
|
|
|
static void pit_set_channel_gate(PITCommonState *s, PITChannelState *sc,
|
|
|
|
int val)
|
2004-03-14 13:20:30 +01:00
|
|
|
{
|
2012-03-02 20:28:46 +01:00
|
|
|
switch (sc->mode) {
|
2004-03-14 13:20:30 +01:00
|
|
|
default:
|
|
|
|
case 0:
|
|
|
|
case 4:
|
|
|
|
/* XXX: just disable/enable counting */
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 5:
|
2012-03-02 20:28:46 +01:00
|
|
|
if (sc->gate < val) {
|
2004-03-14 13:20:30 +01:00
|
|
|
/* restart counting on rising edge */
|
2013-08-21 17:03:08 +02:00
|
|
|
sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2012-03-02 20:28:46 +01:00
|
|
|
pit_irq_timer_update(sc, sc->count_load_time);
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2012-03-02 20:28:46 +01:00
|
|
|
if (sc->gate < val) {
|
2004-03-14 13:20:30 +01:00
|
|
|
/* restart counting on rising edge */
|
2013-08-21 17:03:08 +02:00
|
|
|
sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2012-03-02 20:28:46 +01:00
|
|
|
pit_irq_timer_update(sc, sc->count_load_time);
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
|
|
|
/* XXX: disable/enable counting */
|
|
|
|
break;
|
|
|
|
}
|
2012-03-02 20:28:46 +01:00
|
|
|
sc->gate = val;
|
2006-04-24 23:58:30 +02:00
|
|
|
}
|
|
|
|
|
2004-03-14 13:20:30 +01:00
|
|
|
static inline void pit_load_count(PITChannelState *s, int val)
|
|
|
|
{
|
|
|
|
if (val == 0)
|
|
|
|
val = 0x10000;
|
2013-08-21 17:03:08 +02:00
|
|
|
s->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2004-03-14 13:20:30 +01:00
|
|
|
s->count = val;
|
2004-03-31 20:58:38 +02:00
|
|
|
pit_irq_timer_update(s, s->count_load_time);
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
|
|
|
|
2004-05-04 01:18:25 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-08 13:12:31 +02:00
|
|
|
static void pit_ioport_write(void *opaque, hwaddr addr,
|
|
|
|
uint64_t val, unsigned size)
|
2004-03-14 13:20:30 +01:00
|
|
|
{
|
2012-03-02 20:28:46 +01:00
|
|
|
PITCommonState *pit = opaque;
|
2004-03-14 13:20:30 +01:00
|
|
|
int channel, access;
|
|
|
|
PITChannelState *s;
|
|
|
|
|
|
|
|
addr &= 3;
|
|
|
|
if (addr == 3) {
|
|
|
|
channel = val >> 6;
|
2004-05-04 01:18:25 +02:00
|
|
|
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 */
|
2012-02-01 20:31:43 +01:00
|
|
|
s->status =
|
|
|
|
(pit_get_out(s,
|
2013-08-21 17:03:08 +02:00
|
|
|
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)) << 7) |
|
2004-05-04 01:18:25 +02:00
|
|
|
(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 ? */
|
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
2004-05-04 01:18:25 +02:00
|
|
|
s = &pit->channels[addr];
|
|
|
|
switch(s->write_state) {
|
|
|
|
default:
|
2004-03-14 13:20:30 +01:00
|
|
|
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:
|
2004-05-04 01:18:25 +02:00
|
|
|
s->write_latch = val;
|
|
|
|
s->write_state = RW_STATE_WORD1;
|
|
|
|
break;
|
2004-03-14 13:20:30 +01:00
|
|
|
case RW_STATE_WORD1:
|
2004-05-04 01:18:25 +02:00
|
|
|
pit_load_count(s, s->write_latch | (val << 8));
|
|
|
|
s->write_state = RW_STATE_WORD0;
|
2004-03-14 13:20:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-08 13:12:31 +02:00
|
|
|
static uint64_t pit_ioport_read(void *opaque, hwaddr addr,
|
|
|
|
unsigned size)
|
2004-03-14 13:20:30 +01:00
|
|
|
{
|
2012-03-02 20:28:46 +01:00
|
|
|
PITCommonState *pit = opaque;
|
2004-03-14 13:20:30 +01:00
|
|
|
int ret, count;
|
|
|
|
PITChannelState *s;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-03-14 13:20:30 +01:00
|
|
|
addr &= 3;
|
2015-06-17 12:46:11 +02:00
|
|
|
|
|
|
|
if (addr == 3) {
|
|
|
|
/* Mode/Command register is write only, read is ignored */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-04 01:18:25 +02:00
|
|
|
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:
|
2004-03-14 13:20:30 +01:00
|
|
|
ret = s->latched_count >> 8;
|
2004-05-04 01:18:25 +02:00
|
|
|
s->count_latched = 0;
|
|
|
|
break;
|
|
|
|
case RW_STATE_WORD0:
|
2004-03-14 13:20:30 +01:00
|
|
|
ret = s->latched_count & 0xff;
|
2004-05-04 01:18:25 +02:00
|
|
|
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;
|
|
|
|
}
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-03-31 20:58:38 +02:00
|
|
|
static void pit_irq_timer_update(PITChannelState *s, int64_t current_time)
|
|
|
|
{
|
|
|
|
int64_t expire_time;
|
|
|
|
int irq_level;
|
|
|
|
|
2012-02-01 20:31:41 +01:00
|
|
|
if (!s->irq_timer || s->irq_disabled) {
|
2004-03-31 20:58:38 +02:00
|
|
|
return;
|
2012-02-01 20:31:41 +01:00
|
|
|
}
|
2004-03-31 20:58:38 +02:00
|
|
|
expire_time = pit_get_next_transition_time(s, current_time);
|
2012-02-01 20:31:43 +01:00
|
|
|
irq_level = pit_get_out(s, current_time);
|
2007-04-07 20:14:41 +02:00
|
|
|
qemu_set_irq(s->irq, irq_level);
|
2004-03-31 20:58:38 +02:00
|
|
|
#ifdef DEBUG_PIT
|
|
|
|
printf("irq_level=%d next_delay=%f\n",
|
2007-09-16 23:08:06 +02:00
|
|
|
irq_level,
|
2009-09-10 03:04:26 +02:00
|
|
|
(double)(expire_time - current_time) / get_ticks_per_sec());
|
2004-03-31 20:58:38 +02:00
|
|
|
#endif
|
|
|
|
s->next_transition_time = expire_time;
|
|
|
|
if (expire_time != -1)
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_mod(s->irq_timer, expire_time);
|
2004-03-31 20:58:38 +02:00
|
|
|
else
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_del(s->irq_timer);
|
2004-03-31 20:58:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pit_irq_timer(void *opaque)
|
|
|
|
{
|
|
|
|
PITChannelState *s = opaque;
|
|
|
|
|
|
|
|
pit_irq_timer_update(s, s->next_transition_time);
|
|
|
|
}
|
|
|
|
|
2012-03-02 20:28:46 +01:00
|
|
|
static void pit_reset(DeviceState *dev)
|
2004-03-31 20:58:38 +02:00
|
|
|
{
|
2012-11-25 18:05:53 +01:00
|
|
|
PITCommonState *pit = PIT_COMMON(dev);
|
2004-03-31 20:58:38 +02:00
|
|
|
PITChannelState *s;
|
|
|
|
|
2012-03-02 20:28:46 +01:00
|
|
|
pit_reset_common(pit);
|
2009-08-20 19:42:31 +02:00
|
|
|
|
2012-03-02 20:28:46 +01:00
|
|
|
s = &pit->channels[0];
|
|
|
|
if (!s->irq_disabled) {
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_mod(s->irq_timer, s->next_transition_time);
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
2004-06-20 14:58:36 +02:00
|
|
|
}
|
|
|
|
|
2012-02-01 20:31:41 +01:00
|
|
|
/* 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)
|
2008-12-18 00:28:44 +01:00
|
|
|
{
|
2012-03-02 20:28:46 +01:00
|
|
|
PITCommonState *pit = opaque;
|
2012-02-01 20:31:41 +01:00
|
|
|
PITChannelState *s = &pit->channels[0];
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
s->irq_disabled = 0;
|
2013-08-21 17:03:08 +02:00
|
|
|
pit_irq_timer_update(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
|
2012-02-01 20:31:41 +01:00
|
|
|
} else {
|
|
|
|
s->irq_disabled = 1;
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_del(s->irq_timer);
|
2012-02-01 20:31:41 +01:00
|
|
|
}
|
2008-12-18 00:28:44 +01:00
|
|
|
}
|
|
|
|
|
2011-08-11 00:28:15 +02:00
|
|
|
static const MemoryRegionOps pit_ioport_ops = {
|
2012-10-08 13:12:31 +02:00
|
|
|
.read = pit_ioport_read,
|
|
|
|
.write = pit_ioport_write,
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 1,
|
|
|
|
.max_access_size = 1,
|
|
|
|
},
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
2011-08-11 00:28:15 +02:00
|
|
|
};
|
|
|
|
|
2012-03-02 20:28:47 +01:00
|
|
|
static void pit_post_load(PITCommonState *s)
|
|
|
|
{
|
|
|
|
PITChannelState *sc = &s->channels[0];
|
|
|
|
|
|
|
|
if (sc->next_transition_time != -1) {
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_mod(sc->irq_timer, sc->next_transition_time);
|
2012-03-02 20:28:47 +01:00
|
|
|
} else {
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_del(sc->irq_timer);
|
2012-03-02 20:28:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-25 12:44:21 +02:00
|
|
|
static void pit_realizefn(DeviceState *dev, Error **errp)
|
2004-06-20 14:58:36 +02:00
|
|
|
{
|
2012-11-25 18:47:58 +01:00
|
|
|
PITCommonState *pit = PIT_COMMON(dev);
|
|
|
|
PITClass *pc = PIT_GET_CLASS(dev);
|
2004-06-20 14:58:36 +02:00
|
|
|
PITChannelState *s;
|
|
|
|
|
|
|
|
s = &pit->channels[0];
|
|
|
|
/* the timer 0 is connected to an IRQ */
|
2013-08-21 17:03:08 +02:00
|
|
|
s->irq_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, pit_irq_timer, s);
|
2012-11-25 18:47:58 +01:00
|
|
|
qdev_init_gpio_out(dev, &s->irq, 1);
|
2004-03-14 13:20:30 +01:00
|
|
|
|
2013-06-07 03:25:08 +02:00
|
|
|
memory_region_init_io(&pit->ioports, OBJECT(pit), &pit_ioport_ops,
|
|
|
|
pit, "pit", 4);
|
2012-02-01 20:31:41 +01:00
|
|
|
|
2012-11-25 18:47:58 +01:00
|
|
|
qdev_init_gpio_in(dev, pit_irq_control, 1);
|
2011-03-06 16:09:49 +01:00
|
|
|
|
2014-04-25 12:44:21 +02:00
|
|
|
pc->parent_realize(dev, errp);
|
2011-02-13 20:54:40 +01:00
|
|
|
}
|
|
|
|
|
2011-12-08 04:34:16 +01:00
|
|
|
static Property pit_properties[] = {
|
2014-02-08 11:01:53 +01:00
|
|
|
DEFINE_PROP_UINT32("iobase", PITCommonState, iobase, -1),
|
2011-12-08 04:34:16 +01:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2011-12-04 18:52:49 +01:00
|
|
|
static void pit_class_initfn(ObjectClass *klass, void *data)
|
|
|
|
{
|
2012-11-25 18:47:58 +01:00
|
|
|
PITClass *pc = PIT_CLASS(klass);
|
2012-03-02 20:28:46 +01:00
|
|
|
PITCommonClass *k = PIT_COMMON_CLASS(klass);
|
2011-12-08 04:34:16 +01:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-03-02 20:28:46 +01:00
|
|
|
|
2012-11-25 18:47:58 +01:00
|
|
|
pc->parent_realize = dc->realize;
|
|
|
|
dc->realize = pit_realizefn;
|
2012-03-02 20:28:46 +01:00
|
|
|
k->set_channel_gate = pit_set_channel_gate;
|
|
|
|
k->get_channel_info = pit_get_channel_info_common;
|
2012-03-02 20:28:47 +01:00
|
|
|
k->post_load = pit_post_load;
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->reset = pit_reset;
|
|
|
|
dc->props = pit_properties;
|
2011-12-04 18:52:49 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo pit_info = {
|
2012-11-25 18:05:53 +01:00
|
|
|
.name = TYPE_I8254,
|
2012-03-02 20:28:46 +01:00
|
|
|
.parent = TYPE_PIT_COMMON,
|
|
|
|
.instance_size = sizeof(PITCommonState),
|
2011-12-08 04:34:16 +01:00
|
|
|
.class_init = pit_class_initfn,
|
2012-11-25 18:47:58 +01:00
|
|
|
.class_size = sizeof(PITClass),
|
2011-02-13 20:54:40 +01:00
|
|
|
};
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
static void pit_register_types(void)
|
2011-02-13 20:54:40 +01:00
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
type_register_static(&pit_info);
|
2004-03-14 13:20:30 +01:00
|
|
|
}
|
2012-02-09 15:20:55 +01:00
|
|
|
|
|
|
|
type_init(pit_register_types)
|