2011-12-05 16:47:49 +01:00
|
|
|
/*
|
|
|
|
* Private peripheral timer/watchdog blocks for ARM 11MPCore and A9MP
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2007 CodeSourcery.
|
|
|
|
* Copyright (c) 2011 Linaro Limited
|
|
|
|
* Written by Paul Brook, Peter Maydell
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sysbus.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/timer.h"
|
2011-12-05 16:47:49 +01:00
|
|
|
|
|
|
|
/* This device implements the per-cpu private timer and watchdog block
|
|
|
|
* which is used in both the ARM11MPCore and Cortex-A9MP.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_CPUS 4
|
|
|
|
|
|
|
|
/* State of a single timer or watchdog block */
|
|
|
|
typedef struct {
|
|
|
|
uint32_t count;
|
|
|
|
uint32_t load;
|
|
|
|
uint32_t control;
|
|
|
|
uint32_t status;
|
|
|
|
int64_t tick;
|
|
|
|
QEMUTimer *timer;
|
|
|
|
qemu_irq irq;
|
|
|
|
MemoryRegion iomem;
|
2013-02-28 19:23:13 +01:00
|
|
|
} TimerBlock;
|
2011-12-05 16:47:49 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
SysBusDevice busdev;
|
|
|
|
uint32_t num_cpu;
|
2013-02-28 19:23:13 +01:00
|
|
|
TimerBlock timerblock[MAX_CPUS];
|
|
|
|
MemoryRegion iomem;
|
2013-02-28 19:23:13 +01:00
|
|
|
} ARMMPTimerState;
|
2011-12-05 16:47:49 +01:00
|
|
|
|
2013-02-28 19:23:13 +01:00
|
|
|
static inline int get_current_cpu(ARMMPTimerState *s)
|
2011-12-05 16:47:49 +01:00
|
|
|
{
|
2012-12-17 06:18:02 +01:00
|
|
|
CPUState *cpu_single_cpu = ENV_GET_CPU(cpu_single_env);
|
|
|
|
|
|
|
|
if (cpu_single_cpu->cpu_index >= s->num_cpu) {
|
2011-12-05 16:47:49 +01:00
|
|
|
hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n",
|
2012-12-17 06:18:02 +01:00
|
|
|
s->num_cpu, cpu_single_cpu->cpu_index);
|
2011-12-05 16:47:49 +01:00
|
|
|
}
|
2012-12-17 06:18:02 +01:00
|
|
|
return cpu_single_cpu->cpu_index;
|
2011-12-05 16:47:49 +01:00
|
|
|
}
|
|
|
|
|
2013-02-28 19:23:13 +01:00
|
|
|
static inline void timerblock_update_irq(TimerBlock *tb)
|
2011-12-05 16:47:49 +01:00
|
|
|
{
|
|
|
|
qemu_set_irq(tb->irq, tb->status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
|
2013-02-28 19:23:13 +01:00
|
|
|
static inline uint32_t timerblock_scale(TimerBlock *tb)
|
2011-12-05 16:47:49 +01:00
|
|
|
{
|
|
|
|
return (((tb->control >> 8) & 0xff) + 1) * 10;
|
|
|
|
}
|
|
|
|
|
2013-02-28 19:23:13 +01:00
|
|
|
static void timerblock_reload(TimerBlock *tb, int restart)
|
2011-12-05 16:47:49 +01:00
|
|
|
{
|
|
|
|
if (tb->count == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (restart) {
|
|
|
|
tb->tick = qemu_get_clock_ns(vm_clock);
|
|
|
|
}
|
|
|
|
tb->tick += (int64_t)tb->count * timerblock_scale(tb);
|
|
|
|
qemu_mod_timer(tb->timer, tb->tick);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void timerblock_tick(void *opaque)
|
|
|
|
{
|
2013-02-28 19:23:13 +01:00
|
|
|
TimerBlock *tb = (TimerBlock *)opaque;
|
2011-12-05 16:47:49 +01:00
|
|
|
tb->status = 1;
|
|
|
|
if (tb->control & 2) {
|
|
|
|
tb->count = tb->load;
|
|
|
|
timerblock_reload(tb, 0);
|
|
|
|
} else {
|
|
|
|
tb->count = 0;
|
|
|
|
}
|
|
|
|
timerblock_update_irq(tb);
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static uint64_t timerblock_read(void *opaque, hwaddr addr,
|
2011-12-05 16:47:49 +01:00
|
|
|
unsigned size)
|
|
|
|
{
|
2013-02-28 19:23:13 +01:00
|
|
|
TimerBlock *tb = (TimerBlock *)opaque;
|
2011-12-05 16:47:49 +01:00
|
|
|
int64_t val;
|
|
|
|
switch (addr) {
|
|
|
|
case 0: /* Load */
|
|
|
|
return tb->load;
|
|
|
|
case 4: /* Counter. */
|
|
|
|
if (((tb->control & 1) == 0) || (tb->count == 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Slow and ugly, but hopefully won't happen too often. */
|
|
|
|
val = tb->tick - qemu_get_clock_ns(vm_clock);
|
|
|
|
val /= timerblock_scale(tb);
|
|
|
|
if (val < 0) {
|
|
|
|
val = 0;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
case 8: /* Control. */
|
|
|
|
return tb->control;
|
|
|
|
case 12: /* Interrupt status. */
|
|
|
|
return tb->status;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static void timerblock_write(void *opaque, hwaddr addr,
|
2011-12-05 16:47:49 +01:00
|
|
|
uint64_t value, unsigned size)
|
|
|
|
{
|
2013-02-28 19:23:13 +01:00
|
|
|
TimerBlock *tb = (TimerBlock *)opaque;
|
2011-12-05 16:47:49 +01:00
|
|
|
int64_t old;
|
|
|
|
switch (addr) {
|
|
|
|
case 0: /* Load */
|
|
|
|
tb->load = value;
|
|
|
|
/* Fall through. */
|
|
|
|
case 4: /* Counter. */
|
|
|
|
if ((tb->control & 1) && tb->count) {
|
|
|
|
/* Cancel the previous timer. */
|
|
|
|
qemu_del_timer(tb->timer);
|
|
|
|
}
|
|
|
|
tb->count = value;
|
|
|
|
if (tb->control & 1) {
|
|
|
|
timerblock_reload(tb, 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8: /* Control. */
|
|
|
|
old = tb->control;
|
|
|
|
tb->control = value;
|
|
|
|
if (((old & 1) == 0) && (value & 1)) {
|
|
|
|
if (tb->count == 0 && (tb->control & 2)) {
|
|
|
|
tb->count = tb->load;
|
|
|
|
}
|
|
|
|
timerblock_reload(tb, 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 12: /* Interrupt status. */
|
|
|
|
tb->status &= ~value;
|
|
|
|
timerblock_update_irq(tb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrapper functions to implement the "read timer/watchdog for
|
|
|
|
* the current CPU" memory regions.
|
|
|
|
*/
|
2012-10-23 12:30:10 +02:00
|
|
|
static uint64_t arm_thistimer_read(void *opaque, hwaddr addr,
|
2011-12-05 16:47:49 +01:00
|
|
|
unsigned size)
|
|
|
|
{
|
2013-02-28 19:23:13 +01:00
|
|
|
ARMMPTimerState *s = (ARMMPTimerState *)opaque;
|
2011-12-05 16:47:49 +01:00
|
|
|
int id = get_current_cpu(s);
|
2013-02-28 19:23:13 +01:00
|
|
|
return timerblock_read(&s->timerblock[id], addr, size);
|
2011-12-05 16:47:49 +01:00
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static void arm_thistimer_write(void *opaque, hwaddr addr,
|
2011-12-05 16:47:49 +01:00
|
|
|
uint64_t value, unsigned size)
|
|
|
|
{
|
2013-02-28 19:23:13 +01:00
|
|
|
ARMMPTimerState *s = (ARMMPTimerState *)opaque;
|
2011-12-05 16:47:49 +01:00
|
|
|
int id = get_current_cpu(s);
|
2013-02-28 19:23:13 +01:00
|
|
|
timerblock_write(&s->timerblock[id], addr, value, size);
|
2011-12-05 16:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps arm_thistimer_ops = {
|
|
|
|
.read = arm_thistimer_read,
|
|
|
|
.write = arm_thistimer_write,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4,
|
|
|
|
},
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const MemoryRegionOps timerblock_ops = {
|
|
|
|
.read = timerblock_read,
|
|
|
|
.write = timerblock_write,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4,
|
|
|
|
},
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2013-02-28 19:23:13 +01:00
|
|
|
static void timerblock_reset(TimerBlock *tb)
|
2011-12-05 16:47:49 +01:00
|
|
|
{
|
|
|
|
tb->count = 0;
|
|
|
|
tb->load = 0;
|
|
|
|
tb->control = 0;
|
|
|
|
tb->status = 0;
|
|
|
|
tb->tick = 0;
|
2012-04-20 17:38:52 +02:00
|
|
|
if (tb->timer) {
|
|
|
|
qemu_del_timer(tb->timer);
|
|
|
|
}
|
2011-12-05 16:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void arm_mptimer_reset(DeviceState *dev)
|
|
|
|
{
|
2013-02-28 19:23:13 +01:00
|
|
|
ARMMPTimerState *s =
|
|
|
|
FROM_SYSBUS(ARMMPTimerState, SYS_BUS_DEVICE(dev));
|
2011-12-05 16:47:49 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(s->timerblock); i++) {
|
|
|
|
timerblock_reset(&s->timerblock[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int arm_mptimer_init(SysBusDevice *dev)
|
|
|
|
{
|
2013-02-28 19:23:13 +01:00
|
|
|
ARMMPTimerState *s = FROM_SYSBUS(ARMMPTimerState, dev);
|
2011-12-05 16:47:49 +01:00
|
|
|
int i;
|
|
|
|
if (s->num_cpu < 1 || s->num_cpu > MAX_CPUS) {
|
|
|
|
hw_error("%s: num-cpu must be between 1 and %d\n", __func__, MAX_CPUS);
|
|
|
|
}
|
2013-02-28 19:23:13 +01:00
|
|
|
/* We implement one timer block per CPU, and expose multiple MMIO regions:
|
2011-12-05 16:47:49 +01:00
|
|
|
* * region 0 is "timer for this core"
|
2013-02-28 19:23:13 +01:00
|
|
|
* * region 1 is "timer for core 0"
|
|
|
|
* * region 2 is "timer for core 1"
|
2011-12-05 16:47:49 +01:00
|
|
|
* and so on.
|
|
|
|
* The outgoing interrupt lines are
|
|
|
|
* * timer for core 0
|
|
|
|
* * timer for core 1
|
|
|
|
* and so on.
|
|
|
|
*/
|
2013-02-28 19:23:13 +01:00
|
|
|
memory_region_init_io(&s->iomem, &arm_thistimer_ops, s,
|
2011-12-05 16:47:49 +01:00
|
|
|
"arm_mptimer_timer", 0x20);
|
2013-02-28 19:23:13 +01:00
|
|
|
sysbus_init_mmio(dev, &s->iomem);
|
|
|
|
for (i = 0; i < s->num_cpu; i++) {
|
2013-02-28 19:23:13 +01:00
|
|
|
TimerBlock *tb = &s->timerblock[i];
|
2011-12-05 16:47:49 +01:00
|
|
|
tb->timer = qemu_new_timer_ns(vm_clock, timerblock_tick, tb);
|
|
|
|
sysbus_init_irq(dev, &tb->irq);
|
|
|
|
memory_region_init_io(&tb->iomem, &timerblock_ops, tb,
|
|
|
|
"arm_mptimer_timerblock", 0x20);
|
|
|
|
sysbus_init_mmio(dev, &tb->iomem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_timerblock = {
|
|
|
|
.name = "arm_mptimer_timerblock",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.fields = (VMStateField[]) {
|
2013-02-28 19:23:13 +01:00
|
|
|
VMSTATE_UINT32(count, TimerBlock),
|
|
|
|
VMSTATE_UINT32(load, TimerBlock),
|
|
|
|
VMSTATE_UINT32(control, TimerBlock),
|
|
|
|
VMSTATE_UINT32(status, TimerBlock),
|
|
|
|
VMSTATE_INT64(tick, TimerBlock),
|
2011-12-05 16:47:49 +01:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_arm_mptimer = {
|
|
|
|
.name = "arm_mptimer",
|
2013-02-28 19:23:13 +01:00
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 2,
|
2011-12-05 16:47:49 +01:00
|
|
|
.fields = (VMStateField[]) {
|
2013-02-28 19:23:13 +01:00
|
|
|
VMSTATE_STRUCT_VARRAY_UINT32(timerblock, ARMMPTimerState, num_cpu,
|
|
|
|
2, vmstate_timerblock, TimerBlock),
|
2011-12-05 16:47:49 +01:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-08 04:34:16 +01:00
|
|
|
static Property arm_mptimer_properties[] = {
|
2013-02-28 19:23:13 +01:00
|
|
|
DEFINE_PROP_UINT32("num-cpu", ARMMPTimerState, num_cpu, 0),
|
2011-12-08 04:34:16 +01:00
|
|
|
DEFINE_PROP_END_OF_LIST()
|
|
|
|
};
|
|
|
|
|
2012-01-24 20:12:29 +01:00
|
|
|
static void arm_mptimer_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 20:12:29 +01:00
|
|
|
SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
sbc->init = arm_mptimer_init;
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->vmsd = &vmstate_arm_mptimer;
|
|
|
|
dc->reset = arm_mptimer_reset;
|
|
|
|
dc->no_user = 1;
|
|
|
|
dc->props = arm_mptimer_properties;
|
2012-01-24 20:12:29 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo arm_mptimer_info = {
|
2011-12-08 04:34:16 +01:00
|
|
|
.name = "arm_mptimer",
|
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
2013-02-28 19:23:13 +01:00
|
|
|
.instance_size = sizeof(ARMMPTimerState),
|
2011-12-08 04:34:16 +01:00
|
|
|
.class_init = arm_mptimer_class_init,
|
2011-12-05 16:47:49 +01:00
|
|
|
};
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
static void arm_mptimer_register_types(void)
|
2011-12-05 16:47:49 +01:00
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
type_register_static(&arm_mptimer_info);
|
2011-12-05 16:47:49 +01:00
|
|
|
}
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
type_init(arm_mptimer_register_types)
|