2011-03-08 06:32:38 +08:00
|
|
|
/*
|
|
|
|
* QEMU model of the Milkymist System Controller.
|
|
|
|
*
|
2012-04-01 01:54:09 +08:00
|
|
|
* Copyright (c) 2010-2012 Michael Walle <michael@walle.cc>
|
2011-03-08 06:32:38 +08:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Specification available at:
|
|
|
|
* http://www.milkymist.org/socdoc/sysctl.pdf
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hw.h"
|
|
|
|
#include "sysbus.h"
|
|
|
|
#include "sysemu.h"
|
|
|
|
#include "trace.h"
|
|
|
|
#include "qemu-timer.h"
|
2012-01-14 00:07:19 +08:00
|
|
|
#include "ptimer.h"
|
2011-03-08 06:32:38 +08:00
|
|
|
#include "qemu-error.h"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CTRL_ENABLE = (1<<0),
|
|
|
|
CTRL_AUTORESTART = (1<<1),
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ICAP_READY = (1<<0),
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2012-04-01 01:54:09 +08:00
|
|
|
R_GPIO_IN = 0,
|
2011-03-08 06:32:38 +08:00
|
|
|
R_GPIO_OUT,
|
|
|
|
R_GPIO_INTEN,
|
2012-04-01 01:54:09 +08:00
|
|
|
R_TIMER0_CONTROL = 4,
|
2011-03-08 06:32:38 +08:00
|
|
|
R_TIMER0_COMPARE,
|
|
|
|
R_TIMER0_COUNTER,
|
2012-04-01 01:54:09 +08:00
|
|
|
R_TIMER1_CONTROL = 8,
|
2011-03-08 06:32:38 +08:00
|
|
|
R_TIMER1_COMPARE,
|
|
|
|
R_TIMER1_COUNTER,
|
2012-04-01 01:54:09 +08:00
|
|
|
R_ICAP = 16,
|
|
|
|
R_DBG_SCRATCHPAD = 20,
|
|
|
|
R_DBG_WRITE_LOCK,
|
|
|
|
R_CLK_FREQUENCY = 29,
|
2011-03-08 06:32:38 +08:00
|
|
|
R_CAPABILITIES,
|
|
|
|
R_SYSTEM_ID,
|
|
|
|
R_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MilkymistSysctlState {
|
|
|
|
SysBusDevice busdev;
|
2011-08-31 22:48:43 +08:00
|
|
|
MemoryRegion regs_region;
|
2011-03-08 06:32:38 +08:00
|
|
|
|
|
|
|
QEMUBH *bh0;
|
|
|
|
QEMUBH *bh1;
|
|
|
|
ptimer_state *ptimer0;
|
|
|
|
ptimer_state *ptimer1;
|
|
|
|
|
|
|
|
uint32_t freq_hz;
|
|
|
|
uint32_t capabilities;
|
|
|
|
uint32_t systemid;
|
|
|
|
uint32_t strappings;
|
|
|
|
|
|
|
|
uint32_t regs[R_MAX];
|
|
|
|
|
|
|
|
qemu_irq gpio_irq;
|
|
|
|
qemu_irq timer0_irq;
|
|
|
|
qemu_irq timer1_irq;
|
|
|
|
};
|
|
|
|
typedef struct MilkymistSysctlState MilkymistSysctlState;
|
|
|
|
|
|
|
|
static void sysctl_icap_write(MilkymistSysctlState *s, uint32_t value)
|
|
|
|
{
|
|
|
|
trace_milkymist_sysctl_icap_write(value);
|
|
|
|
switch (value & 0xffff) {
|
|
|
|
case 0x000e:
|
|
|
|
qemu_system_shutdown_request();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 22:48:43 +08:00
|
|
|
static uint64_t sysctl_read(void *opaque, target_phys_addr_t addr,
|
|
|
|
unsigned size)
|
2011-03-08 06:32:38 +08:00
|
|
|
{
|
|
|
|
MilkymistSysctlState *s = opaque;
|
|
|
|
uint32_t r = 0;
|
|
|
|
|
|
|
|
addr >>= 2;
|
|
|
|
switch (addr) {
|
|
|
|
case R_TIMER0_COUNTER:
|
|
|
|
r = (uint32_t)ptimer_get_count(s->ptimer0);
|
|
|
|
/* milkymist timer counts up */
|
|
|
|
r = s->regs[R_TIMER0_COMPARE] - r;
|
|
|
|
break;
|
|
|
|
case R_TIMER1_COUNTER:
|
|
|
|
r = (uint32_t)ptimer_get_count(s->ptimer1);
|
|
|
|
/* milkymist timer counts up */
|
|
|
|
r = s->regs[R_TIMER1_COMPARE] - r;
|
|
|
|
break;
|
|
|
|
case R_GPIO_IN:
|
|
|
|
case R_GPIO_OUT:
|
|
|
|
case R_GPIO_INTEN:
|
|
|
|
case R_TIMER0_CONTROL:
|
|
|
|
case R_TIMER0_COMPARE:
|
|
|
|
case R_TIMER1_CONTROL:
|
|
|
|
case R_TIMER1_COMPARE:
|
|
|
|
case R_ICAP:
|
2012-04-01 01:54:09 +08:00
|
|
|
case R_DBG_SCRATCHPAD:
|
|
|
|
case R_DBG_WRITE_LOCK:
|
|
|
|
case R_CLK_FREQUENCY:
|
2011-03-08 06:32:38 +08:00
|
|
|
case R_CAPABILITIES:
|
|
|
|
case R_SYSTEM_ID:
|
|
|
|
r = s->regs[addr];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-06-22 20:03:56 +08:00
|
|
|
error_report("milkymist_sysctl: read access to unknown register 0x"
|
2011-03-08 06:32:38 +08:00
|
|
|
TARGET_FMT_plx, addr << 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_milkymist_sysctl_memory_read(addr << 2, r);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-08-31 22:48:43 +08:00
|
|
|
static void sysctl_write(void *opaque, target_phys_addr_t addr, uint64_t value,
|
|
|
|
unsigned size)
|
2011-03-08 06:32:38 +08:00
|
|
|
{
|
|
|
|
MilkymistSysctlState *s = opaque;
|
|
|
|
|
|
|
|
trace_milkymist_sysctl_memory_write(addr, value);
|
|
|
|
|
|
|
|
addr >>= 2;
|
|
|
|
switch (addr) {
|
|
|
|
case R_GPIO_OUT:
|
|
|
|
case R_GPIO_INTEN:
|
|
|
|
case R_TIMER0_COUNTER:
|
|
|
|
case R_TIMER1_COUNTER:
|
2012-04-01 01:54:09 +08:00
|
|
|
case R_DBG_SCRATCHPAD:
|
2011-04-13 06:29:35 +08:00
|
|
|
s->regs[addr] = value;
|
2011-03-08 06:32:38 +08:00
|
|
|
break;
|
|
|
|
case R_TIMER0_COMPARE:
|
|
|
|
ptimer_set_limit(s->ptimer0, value, 0);
|
|
|
|
s->regs[addr] = value;
|
|
|
|
break;
|
|
|
|
case R_TIMER1_COMPARE:
|
|
|
|
ptimer_set_limit(s->ptimer1, value, 0);
|
|
|
|
s->regs[addr] = value;
|
|
|
|
break;
|
|
|
|
case R_TIMER0_CONTROL:
|
|
|
|
s->regs[addr] = value;
|
|
|
|
if (s->regs[R_TIMER0_CONTROL] & CTRL_ENABLE) {
|
2011-04-13 06:29:35 +08:00
|
|
|
trace_milkymist_sysctl_start_timer0();
|
|
|
|
ptimer_set_count(s->ptimer0,
|
|
|
|
s->regs[R_TIMER0_COMPARE] - s->regs[R_TIMER0_COUNTER]);
|
2011-03-08 06:32:38 +08:00
|
|
|
ptimer_run(s->ptimer0, 0);
|
|
|
|
} else {
|
2011-04-13 06:29:35 +08:00
|
|
|
trace_milkymist_sysctl_stop_timer0();
|
2011-03-08 06:32:38 +08:00
|
|
|
ptimer_stop(s->ptimer0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case R_TIMER1_CONTROL:
|
|
|
|
s->regs[addr] = value;
|
|
|
|
if (s->regs[R_TIMER1_CONTROL] & CTRL_ENABLE) {
|
|
|
|
trace_milkymist_sysctl_start_timer1();
|
2011-04-13 06:29:35 +08:00
|
|
|
ptimer_set_count(s->ptimer1,
|
|
|
|
s->regs[R_TIMER1_COMPARE] - s->regs[R_TIMER1_COUNTER]);
|
2011-03-08 06:32:38 +08:00
|
|
|
ptimer_run(s->ptimer1, 0);
|
|
|
|
} else {
|
|
|
|
trace_milkymist_sysctl_stop_timer1();
|
|
|
|
ptimer_stop(s->ptimer1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case R_ICAP:
|
|
|
|
sysctl_icap_write(s, value);
|
|
|
|
break;
|
2012-04-01 01:54:09 +08:00
|
|
|
case R_DBG_WRITE_LOCK:
|
|
|
|
s->regs[addr] = 1;
|
|
|
|
break;
|
2011-03-08 06:32:38 +08:00
|
|
|
case R_SYSTEM_ID:
|
|
|
|
qemu_system_reset_request();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case R_GPIO_IN:
|
2012-04-01 01:54:09 +08:00
|
|
|
case R_CLK_FREQUENCY:
|
2011-03-08 06:32:38 +08:00
|
|
|
case R_CAPABILITIES:
|
|
|
|
error_report("milkymist_sysctl: write to read-only register 0x"
|
|
|
|
TARGET_FMT_plx, addr << 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-06-22 20:03:56 +08:00
|
|
|
error_report("milkymist_sysctl: write access to unknown register 0x"
|
2011-03-08 06:32:38 +08:00
|
|
|
TARGET_FMT_plx, addr << 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 22:48:43 +08:00
|
|
|
static const MemoryRegionOps sysctl_mmio_ops = {
|
|
|
|
.read = sysctl_read,
|
|
|
|
.write = sysctl_write,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4,
|
|
|
|
},
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2011-03-08 06:32:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void timer0_hit(void *opaque)
|
|
|
|
{
|
|
|
|
MilkymistSysctlState *s = opaque;
|
|
|
|
|
|
|
|
if (!(s->regs[R_TIMER0_CONTROL] & CTRL_AUTORESTART)) {
|
|
|
|
s->regs[R_TIMER0_CONTROL] &= ~CTRL_ENABLE;
|
|
|
|
trace_milkymist_sysctl_stop_timer0();
|
|
|
|
ptimer_stop(s->ptimer0);
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_milkymist_sysctl_pulse_irq_timer0();
|
|
|
|
qemu_irq_pulse(s->timer0_irq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void timer1_hit(void *opaque)
|
|
|
|
{
|
|
|
|
MilkymistSysctlState *s = opaque;
|
|
|
|
|
|
|
|
if (!(s->regs[R_TIMER1_CONTROL] & CTRL_AUTORESTART)) {
|
|
|
|
s->regs[R_TIMER1_CONTROL] &= ~CTRL_ENABLE;
|
|
|
|
trace_milkymist_sysctl_stop_timer1();
|
|
|
|
ptimer_stop(s->ptimer1);
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_milkymist_sysctl_pulse_irq_timer1();
|
|
|
|
qemu_irq_pulse(s->timer1_irq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void milkymist_sysctl_reset(DeviceState *d)
|
|
|
|
{
|
|
|
|
MilkymistSysctlState *s =
|
|
|
|
container_of(d, MilkymistSysctlState, busdev.qdev);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < R_MAX; i++) {
|
|
|
|
s->regs[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptimer_stop(s->ptimer0);
|
|
|
|
ptimer_stop(s->ptimer1);
|
|
|
|
|
|
|
|
/* defaults */
|
|
|
|
s->regs[R_ICAP] = ICAP_READY;
|
|
|
|
s->regs[R_SYSTEM_ID] = s->systemid;
|
2012-04-01 01:54:09 +08:00
|
|
|
s->regs[R_CLK_FREQUENCY] = s->freq_hz;
|
2011-03-08 06:32:38 +08:00
|
|
|
s->regs[R_CAPABILITIES] = s->capabilities;
|
|
|
|
s->regs[R_GPIO_IN] = s->strappings;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int milkymist_sysctl_init(SysBusDevice *dev)
|
|
|
|
{
|
|
|
|
MilkymistSysctlState *s = FROM_SYSBUS(typeof(*s), dev);
|
|
|
|
|
|
|
|
sysbus_init_irq(dev, &s->gpio_irq);
|
|
|
|
sysbus_init_irq(dev, &s->timer0_irq);
|
|
|
|
sysbus_init_irq(dev, &s->timer1_irq);
|
|
|
|
|
|
|
|
s->bh0 = qemu_bh_new(timer0_hit, s);
|
|
|
|
s->bh1 = qemu_bh_new(timer1_hit, s);
|
|
|
|
s->ptimer0 = ptimer_init(s->bh0);
|
|
|
|
s->ptimer1 = ptimer_init(s->bh1);
|
|
|
|
ptimer_set_freq(s->ptimer0, s->freq_hz);
|
|
|
|
ptimer_set_freq(s->ptimer1, s->freq_hz);
|
|
|
|
|
2011-08-31 22:48:43 +08:00
|
|
|
memory_region_init_io(&s->regs_region, &sysctl_mmio_ops, s,
|
|
|
|
"milkymist-sysctl", R_MAX * 4);
|
2011-11-27 17:38:10 +08:00
|
|
|
sysbus_init_mmio(dev, &s->regs_region);
|
2011-03-08 06:32:38 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_milkymist_sysctl = {
|
|
|
|
.name = "milkymist-sysctl",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.minimum_version_id_old = 1,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_UINT32_ARRAY(regs, MilkymistSysctlState, R_MAX),
|
|
|
|
VMSTATE_PTIMER(ptimer0, MilkymistSysctlState),
|
|
|
|
VMSTATE_PTIMER(ptimer1, MilkymistSysctlState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-25 03:12:29 +08:00
|
|
|
static Property milkymist_sysctl_properties[] = {
|
|
|
|
DEFINE_PROP_UINT32("frequency", MilkymistSysctlState,
|
|
|
|
freq_hz, 80000000),
|
|
|
|
DEFINE_PROP_UINT32("capabilities", MilkymistSysctlState,
|
|
|
|
capabilities, 0x00000000),
|
|
|
|
DEFINE_PROP_UINT32("systemid", MilkymistSysctlState,
|
|
|
|
systemid, 0x10014d31),
|
|
|
|
DEFINE_PROP_UINT32("gpio_strappings", MilkymistSysctlState,
|
|
|
|
strappings, 0x00000001),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void milkymist_sysctl_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 11:34:16 +08:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-25 03:12:29 +08:00
|
|
|
SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
k->init = milkymist_sysctl_init;
|
2011-12-08 11:34:16 +08:00
|
|
|
dc->reset = milkymist_sysctl_reset;
|
|
|
|
dc->vmsd = &vmstate_milkymist_sysctl;
|
|
|
|
dc->props = milkymist_sysctl_properties;
|
2012-01-25 03:12:29 +08:00
|
|
|
}
|
|
|
|
|
2011-12-08 11:34:16 +08:00
|
|
|
static TypeInfo milkymist_sysctl_info = {
|
|
|
|
.name = "milkymist-sysctl",
|
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(MilkymistSysctlState),
|
|
|
|
.class_init = milkymist_sysctl_class_init,
|
2011-03-08 06:32:38 +08:00
|
|
|
};
|
|
|
|
|
2012-02-09 22:20:55 +08:00
|
|
|
static void milkymist_sysctl_register_types(void)
|
2011-03-08 06:32:38 +08:00
|
|
|
{
|
2011-12-08 11:34:16 +08:00
|
|
|
type_register_static(&milkymist_sysctl_info);
|
2011-03-08 06:32:38 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 22:20:55 +08:00
|
|
|
type_init(milkymist_sysctl_register_types)
|