2007-09-17 01:08:06 +04:00
|
|
|
/*
|
2006-04-09 05:32:52 +04:00
|
|
|
* ARM PrimeCell Timer modules.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2006 CodeSourcery.
|
|
|
|
* Written by Paul Brook
|
|
|
|
*
|
|
|
|
* This code is licenced under the GPL.
|
|
|
|
*/
|
|
|
|
|
2009-05-15 01:35:07 +04:00
|
|
|
#include "sysbus.h"
|
2007-11-17 20:14:51 +03:00
|
|
|
#include "qemu-timer.h"
|
2006-04-09 05:32:52 +04:00
|
|
|
|
|
|
|
/* Common timer implementation. */
|
|
|
|
|
|
|
|
#define TIMER_CTRL_ONESHOT (1 << 0)
|
|
|
|
#define TIMER_CTRL_32BIT (1 << 1)
|
|
|
|
#define TIMER_CTRL_DIV1 (0 << 2)
|
|
|
|
#define TIMER_CTRL_DIV16 (1 << 2)
|
|
|
|
#define TIMER_CTRL_DIV256 (2 << 2)
|
|
|
|
#define TIMER_CTRL_IE (1 << 5)
|
|
|
|
#define TIMER_CTRL_PERIODIC (1 << 6)
|
|
|
|
#define TIMER_CTRL_ENABLE (1 << 7)
|
|
|
|
|
|
|
|
typedef struct {
|
2007-05-23 04:06:54 +04:00
|
|
|
ptimer_state *timer;
|
2006-04-09 05:32:52 +04:00
|
|
|
uint32_t control;
|
|
|
|
uint32_t limit;
|
|
|
|
int freq;
|
|
|
|
int int_level;
|
2007-04-07 22:14:41 +04:00
|
|
|
qemu_irq irq;
|
2006-04-09 05:32:52 +04:00
|
|
|
} arm_timer_state;
|
|
|
|
|
|
|
|
/* Check all active timers, and schedule the next timer interrupt. */
|
|
|
|
|
2007-05-23 04:06:54 +04:00
|
|
|
static void arm_timer_update(arm_timer_state *s)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
|
|
|
/* Update interrupts. */
|
|
|
|
if (s->int_level && (s->control & TIMER_CTRL_IE)) {
|
2007-04-07 22:14:41 +04:00
|
|
|
qemu_irq_raise(s->irq);
|
2006-04-09 05:32:52 +04:00
|
|
|
} else {
|
2007-04-07 22:14:41 +04:00
|
|
|
qemu_irq_lower(s->irq);
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static uint32_t arm_timer_read(void *opaque, target_phys_addr_t offset)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
|
|
|
arm_timer_state *s = (arm_timer_state *)opaque;
|
|
|
|
|
|
|
|
switch (offset >> 2) {
|
|
|
|
case 0: /* TimerLoad */
|
|
|
|
case 6: /* TimerBGLoad */
|
|
|
|
return s->limit;
|
|
|
|
case 1: /* TimerValue */
|
2007-05-23 04:06:54 +04:00
|
|
|
return ptimer_get_count(s->timer);
|
2006-04-09 05:32:52 +04:00
|
|
|
case 2: /* TimerControl */
|
|
|
|
return s->control;
|
|
|
|
case 4: /* TimerRIS */
|
|
|
|
return s->int_level;
|
|
|
|
case 5: /* TimerMIS */
|
|
|
|
if ((s->control & TIMER_CTRL_IE) == 0)
|
|
|
|
return 0;
|
|
|
|
return s->int_level;
|
|
|
|
default:
|
2009-05-08 05:35:15 +04:00
|
|
|
hw_error("arm_timer_read: Bad offset %x\n", (int)offset);
|
2006-04-09 05:32:52 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-23 04:06:54 +04:00
|
|
|
/* Reset the timer limit after settings have changed. */
|
|
|
|
static void arm_timer_recalibrate(arm_timer_state *s, int reload)
|
|
|
|
{
|
|
|
|
uint32_t limit;
|
|
|
|
|
2010-05-02 13:50:52 +04:00
|
|
|
if ((s->control & (TIMER_CTRL_PERIODIC | TIMER_CTRL_ONESHOT)) == 0) {
|
2007-05-23 04:06:54 +04:00
|
|
|
/* Free running. */
|
|
|
|
if (s->control & TIMER_CTRL_32BIT)
|
|
|
|
limit = 0xffffffff;
|
|
|
|
else
|
|
|
|
limit = 0xffff;
|
|
|
|
} else {
|
|
|
|
/* Periodic. */
|
|
|
|
limit = s->limit;
|
|
|
|
}
|
|
|
|
ptimer_set_limit(s->timer, limit, reload);
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static void arm_timer_write(void *opaque, target_phys_addr_t offset,
|
2006-04-09 05:32:52 +04:00
|
|
|
uint32_t value)
|
|
|
|
{
|
|
|
|
arm_timer_state *s = (arm_timer_state *)opaque;
|
2007-05-23 04:06:54 +04:00
|
|
|
int freq;
|
2006-04-09 05:32:52 +04:00
|
|
|
|
|
|
|
switch (offset >> 2) {
|
|
|
|
case 0: /* TimerLoad */
|
|
|
|
s->limit = value;
|
2007-05-23 04:06:54 +04:00
|
|
|
arm_timer_recalibrate(s, 1);
|
2006-04-09 05:32:52 +04:00
|
|
|
break;
|
|
|
|
case 1: /* TimerValue */
|
|
|
|
/* ??? Linux seems to want to write to this readonly register.
|
|
|
|
Ignore it. */
|
|
|
|
break;
|
|
|
|
case 2: /* TimerControl */
|
|
|
|
if (s->control & TIMER_CTRL_ENABLE) {
|
|
|
|
/* Pause the timer if it is running. This may cause some
|
|
|
|
inaccuracy dure to rounding, but avoids a whole lot of other
|
|
|
|
messyness. */
|
2007-05-23 04:06:54 +04:00
|
|
|
ptimer_stop(s->timer);
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
s->control = value;
|
2007-05-23 04:06:54 +04:00
|
|
|
freq = s->freq;
|
2006-04-09 05:32:52 +04:00
|
|
|
/* ??? Need to recalculate expiry time after changing divisor. */
|
|
|
|
switch ((value >> 2) & 3) {
|
2007-05-23 04:06:54 +04:00
|
|
|
case 1: freq >>= 4; break;
|
|
|
|
case 2: freq >>= 8; break;
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
2010-05-02 13:50:51 +04:00
|
|
|
arm_timer_recalibrate(s, s->control & TIMER_CTRL_ENABLE);
|
2007-05-23 04:06:54 +04:00
|
|
|
ptimer_set_freq(s->timer, freq);
|
2006-04-09 05:32:52 +04:00
|
|
|
if (s->control & TIMER_CTRL_ENABLE) {
|
|
|
|
/* Restart the timer if still enabled. */
|
2007-05-23 04:06:54 +04:00
|
|
|
ptimer_run(s->timer, (s->control & TIMER_CTRL_ONESHOT) != 0);
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: /* TimerIntClr */
|
|
|
|
s->int_level = 0;
|
|
|
|
break;
|
|
|
|
case 6: /* TimerBGLoad */
|
|
|
|
s->limit = value;
|
2007-05-23 04:06:54 +04:00
|
|
|
arm_timer_recalibrate(s, 0);
|
2006-04-09 05:32:52 +04:00
|
|
|
break;
|
|
|
|
default:
|
2009-05-08 05:35:15 +04:00
|
|
|
hw_error("arm_timer_write: Bad offset %x\n", (int)offset);
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
2007-05-23 04:06:54 +04:00
|
|
|
arm_timer_update(s);
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void arm_timer_tick(void *opaque)
|
|
|
|
{
|
2007-05-23 04:06:54 +04:00
|
|
|
arm_timer_state *s = (arm_timer_state *)opaque;
|
|
|
|
s->int_level = 1;
|
|
|
|
arm_timer_update(s);
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
|
2008-07-02 20:48:32 +04:00
|
|
|
static void arm_timer_save(QEMUFile *f, void *opaque)
|
|
|
|
{
|
|
|
|
arm_timer_state *s = (arm_timer_state *)opaque;
|
|
|
|
qemu_put_be32(f, s->control);
|
|
|
|
qemu_put_be32(f, s->limit);
|
|
|
|
qemu_put_be32(f, s->int_level);
|
|
|
|
qemu_put_ptimer(f, s->timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int arm_timer_load(QEMUFile *f, void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
arm_timer_state *s = (arm_timer_state *)opaque;
|
|
|
|
|
|
|
|
if (version_id != 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
s->control = qemu_get_be32(f);
|
|
|
|
s->limit = qemu_get_be32(f);
|
|
|
|
s->int_level = qemu_get_be32(f);
|
|
|
|
qemu_get_ptimer(f, s->timer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-15 01:35:07 +04:00
|
|
|
static arm_timer_state *arm_timer_init(uint32_t freq)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
|
|
|
arm_timer_state *s;
|
2007-05-23 04:06:54 +04:00
|
|
|
QEMUBH *bh;
|
2006-04-09 05:32:52 +04:00
|
|
|
|
|
|
|
s = (arm_timer_state *)qemu_mallocz(sizeof(arm_timer_state));
|
2007-05-23 04:06:54 +04:00
|
|
|
s->freq = freq;
|
2006-04-09 05:32:52 +04:00
|
|
|
s->control = TIMER_CTRL_IE;
|
|
|
|
|
2007-05-23 04:06:54 +04:00
|
|
|
bh = qemu_bh_new(arm_timer_tick, s);
|
|
|
|
s->timer = ptimer_init(bh);
|
2008-07-02 20:48:32 +04:00
|
|
|
register_savevm("arm_timer", -1, 1, arm_timer_save, arm_timer_load, s);
|
2006-04-09 05:32:52 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARM PrimeCell SP804 dual timer module.
|
|
|
|
Docs for this device don't seem to be publicly available. This
|
2007-04-07 00:58:25 +04:00
|
|
|
implementation is based on guesswork, the linux kernel sources and the
|
2006-04-09 05:32:52 +04:00
|
|
|
Integrator/CP timer modules. */
|
|
|
|
|
|
|
|
typedef struct {
|
2009-05-15 01:35:07 +04:00
|
|
|
SysBusDevice busdev;
|
|
|
|
arm_timer_state *timer[2];
|
2006-04-09 05:32:52 +04:00
|
|
|
int level[2];
|
2007-04-07 22:14:41 +04:00
|
|
|
qemu_irq irq;
|
2006-04-09 05:32:52 +04:00
|
|
|
} sp804_state;
|
|
|
|
|
2007-04-07 22:14:41 +04:00
|
|
|
/* Merge the IRQs from the two component devices. */
|
2006-04-09 05:32:52 +04:00
|
|
|
static void sp804_set_irq(void *opaque, int irq, int level)
|
|
|
|
{
|
|
|
|
sp804_state *s = (sp804_state *)opaque;
|
|
|
|
|
|
|
|
s->level[irq] = level;
|
2007-04-07 22:14:41 +04:00
|
|
|
qemu_set_irq(s->irq, s->level[0] || s->level[1]);
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static uint32_t sp804_read(void *opaque, target_phys_addr_t offset)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
|
|
|
sp804_state *s = (sp804_state *)opaque;
|
|
|
|
|
|
|
|
/* ??? Don't know the PrimeCell ID for this device. */
|
|
|
|
if (offset < 0x20) {
|
|
|
|
return arm_timer_read(s->timer[0], offset);
|
|
|
|
} else {
|
|
|
|
return arm_timer_read(s->timer[1], offset - 0x20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static void sp804_write(void *opaque, target_phys_addr_t offset,
|
2006-04-09 05:32:52 +04:00
|
|
|
uint32_t value)
|
|
|
|
{
|
|
|
|
sp804_state *s = (sp804_state *)opaque;
|
|
|
|
|
|
|
|
if (offset < 0x20) {
|
|
|
|
arm_timer_write(s->timer[0], offset, value);
|
|
|
|
} else {
|
|
|
|
arm_timer_write(s->timer[1], offset - 0x20, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-25 22:29:31 +04:00
|
|
|
static CPUReadMemoryFunc * const sp804_readfn[] = {
|
2006-04-09 05:32:52 +04:00
|
|
|
sp804_read,
|
|
|
|
sp804_read,
|
|
|
|
sp804_read
|
|
|
|
};
|
|
|
|
|
2009-08-25 22:29:31 +04:00
|
|
|
static CPUWriteMemoryFunc * const sp804_writefn[] = {
|
2006-04-09 05:32:52 +04:00
|
|
|
sp804_write,
|
|
|
|
sp804_write,
|
|
|
|
sp804_write
|
|
|
|
};
|
|
|
|
|
2008-07-02 20:48:32 +04:00
|
|
|
static void sp804_save(QEMUFile *f, void *opaque)
|
|
|
|
{
|
|
|
|
sp804_state *s = (sp804_state *)opaque;
|
|
|
|
qemu_put_be32(f, s->level[0]);
|
|
|
|
qemu_put_be32(f, s->level[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sp804_load(QEMUFile *f, void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
sp804_state *s = (sp804_state *)opaque;
|
|
|
|
|
|
|
|
if (version_id != 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
s->level[0] = qemu_get_be32(f);
|
|
|
|
s->level[1] = qemu_get_be32(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-14 12:36:05 +04:00
|
|
|
static int sp804_init(SysBusDevice *dev)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
|
|
|
int iomemtype;
|
2009-05-15 01:35:07 +04:00
|
|
|
sp804_state *s = FROM_SYSBUS(sp804_state, dev);
|
2007-04-07 22:14:41 +04:00
|
|
|
qemu_irq *qi;
|
2006-04-09 05:32:52 +04:00
|
|
|
|
2007-04-07 22:14:41 +04:00
|
|
|
qi = qemu_allocate_irqs(sp804_set_irq, s, 2);
|
2009-05-15 01:35:07 +04:00
|
|
|
sysbus_init_irq(dev, &s->irq);
|
2006-04-09 05:32:52 +04:00
|
|
|
/* ??? The timers are actually configurable between 32kHz and 1MHz, but
|
|
|
|
we don't implement that. */
|
2009-05-15 01:35:07 +04:00
|
|
|
s->timer[0] = arm_timer_init(1000000);
|
|
|
|
s->timer[1] = arm_timer_init(1000000);
|
|
|
|
s->timer[0]->irq = qi[0];
|
|
|
|
s->timer[1]->irq = qi[1];
|
2009-06-14 12:38:51 +04:00
|
|
|
iomemtype = cpu_register_io_memory(sp804_readfn,
|
2006-04-09 05:32:52 +04:00
|
|
|
sp804_writefn, s);
|
2009-05-15 01:35:07 +04:00
|
|
|
sysbus_init_mmio(dev, 0x1000, iomemtype);
|
2008-07-02 20:48:32 +04:00
|
|
|
register_savevm("sp804", -1, 1, sp804_save, sp804_load, s);
|
2009-08-14 12:36:05 +04:00
|
|
|
return 0;
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Integrator/CP timer module. */
|
|
|
|
|
|
|
|
typedef struct {
|
2009-05-15 01:35:07 +04:00
|
|
|
SysBusDevice busdev;
|
|
|
|
arm_timer_state *timer[3];
|
2006-04-09 05:32:52 +04:00
|
|
|
} icp_pit_state;
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static uint32_t icp_pit_read(void *opaque, target_phys_addr_t offset)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
|
|
|
icp_pit_state *s = (icp_pit_state *)opaque;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* ??? Don't know the PrimeCell ID for this device. */
|
|
|
|
n = offset >> 8;
|
2009-05-08 05:35:15 +04:00
|
|
|
if (n > 3) {
|
|
|
|
hw_error("sp804_read: Bad timer %d\n", n);
|
|
|
|
}
|
2006-04-09 05:32:52 +04:00
|
|
|
|
|
|
|
return arm_timer_read(s->timer[n], offset & 0xff);
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
static void icp_pit_write(void *opaque, target_phys_addr_t offset,
|
2006-04-09 05:32:52 +04:00
|
|
|
uint32_t value)
|
|
|
|
{
|
|
|
|
icp_pit_state *s = (icp_pit_state *)opaque;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = offset >> 8;
|
2009-05-08 05:35:15 +04:00
|
|
|
if (n > 3) {
|
|
|
|
hw_error("sp804_write: Bad timer %d\n", n);
|
|
|
|
}
|
2006-04-09 05:32:52 +04:00
|
|
|
|
|
|
|
arm_timer_write(s->timer[n], offset & 0xff, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-25 22:29:31 +04:00
|
|
|
static CPUReadMemoryFunc * const icp_pit_readfn[] = {
|
2006-04-09 05:32:52 +04:00
|
|
|
icp_pit_read,
|
|
|
|
icp_pit_read,
|
|
|
|
icp_pit_read
|
|
|
|
};
|
|
|
|
|
2009-08-25 22:29:31 +04:00
|
|
|
static CPUWriteMemoryFunc * const icp_pit_writefn[] = {
|
2006-04-09 05:32:52 +04:00
|
|
|
icp_pit_write,
|
|
|
|
icp_pit_write,
|
|
|
|
icp_pit_write
|
|
|
|
};
|
|
|
|
|
2009-08-14 12:36:05 +04:00
|
|
|
static int icp_pit_init(SysBusDevice *dev)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
|
|
|
int iomemtype;
|
2009-05-15 01:35:07 +04:00
|
|
|
icp_pit_state *s = FROM_SYSBUS(icp_pit_state, dev);
|
2006-04-09 05:32:52 +04:00
|
|
|
|
|
|
|
/* Timer 0 runs at the system clock speed (40MHz). */
|
2009-05-15 01:35:07 +04:00
|
|
|
s->timer[0] = arm_timer_init(40000000);
|
2006-04-09 05:32:52 +04:00
|
|
|
/* The other two timers run at 1MHz. */
|
2009-05-15 01:35:07 +04:00
|
|
|
s->timer[1] = arm_timer_init(1000000);
|
|
|
|
s->timer[2] = arm_timer_init(1000000);
|
|
|
|
|
|
|
|
sysbus_init_irq(dev, &s->timer[0]->irq);
|
|
|
|
sysbus_init_irq(dev, &s->timer[1]->irq);
|
|
|
|
sysbus_init_irq(dev, &s->timer[2]->irq);
|
2006-04-09 05:32:52 +04:00
|
|
|
|
2009-06-14 12:38:51 +04:00
|
|
|
iomemtype = cpu_register_io_memory(icp_pit_readfn,
|
2006-04-09 05:32:52 +04:00
|
|
|
icp_pit_writefn, s);
|
2009-05-15 01:35:07 +04:00
|
|
|
sysbus_init_mmio(dev, 0x1000, iomemtype);
|
2008-07-02 20:48:32 +04:00
|
|
|
/* This device has no state to save/restore. The component timers will
|
|
|
|
save themselves. */
|
2009-08-14 12:36:05 +04:00
|
|
|
return 0;
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
2009-05-15 01:35:07 +04:00
|
|
|
|
|
|
|
static void arm_timer_register_devices(void)
|
|
|
|
{
|
|
|
|
sysbus_register_dev("integrator_pit", sizeof(icp_pit_state), icp_pit_init);
|
|
|
|
sysbus_register_dev("sp804", sizeof(sp804_state), sp804_init);
|
|
|
|
}
|
|
|
|
|
|
|
|
device_init(arm_timer_register_devices)
|