2011-03-08 01:32:38 +03:00
|
|
|
/*
|
|
|
|
* QEMU model of the Milkymist System Controller.
|
|
|
|
*
|
2012-03-31 21:54:09 +04:00
|
|
|
* Copyright (c) 2010-2012 Michael Walle <michael@walle.cc>
|
2011-03-08 01:32:38 +03: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:
|
2016-06-20 19:08:41 +03:00
|
|
|
* http://milkymist.walle.cc/socdoc/sysctl.pdf
|
2011-03-08 01:32:38 +03:00
|
|
|
*/
|
|
|
|
|
2016-01-26 21:16:57 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/sysbus.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2011-03-08 01:32:38 +03:00
|
|
|
#include "trace.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/timer.h"
|
2019-08-12 08:23:59 +03:00
|
|
|
#include "sysemu/runstate.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/ptimer.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/error-report.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2011-03-08 01:32:38 +03:00
|
|
|
|
|
|
|
enum {
|
|
|
|
CTRL_ENABLE = (1<<0),
|
|
|
|
CTRL_AUTORESTART = (1<<1),
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ICAP_READY = (1<<0),
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2012-03-31 21:54:09 +04:00
|
|
|
R_GPIO_IN = 0,
|
2011-03-08 01:32:38 +03:00
|
|
|
R_GPIO_OUT,
|
|
|
|
R_GPIO_INTEN,
|
2012-03-31 21:54:09 +04:00
|
|
|
R_TIMER0_CONTROL = 4,
|
2011-03-08 01:32:38 +03:00
|
|
|
R_TIMER0_COMPARE,
|
|
|
|
R_TIMER0_COUNTER,
|
2012-03-31 21:54:09 +04:00
|
|
|
R_TIMER1_CONTROL = 8,
|
2011-03-08 01:32:38 +03:00
|
|
|
R_TIMER1_COMPARE,
|
|
|
|
R_TIMER1_COUNTER,
|
2012-03-31 21:54:09 +04:00
|
|
|
R_ICAP = 16,
|
|
|
|
R_DBG_SCRATCHPAD = 20,
|
|
|
|
R_DBG_WRITE_LOCK,
|
|
|
|
R_CLK_FREQUENCY = 29,
|
2011-03-08 01:32:38 +03:00
|
|
|
R_CAPABILITIES,
|
|
|
|
R_SYSTEM_ID,
|
|
|
|
R_MAX
|
|
|
|
};
|
|
|
|
|
2013-07-27 17:06:42 +04:00
|
|
|
#define TYPE_MILKYMIST_SYSCTL "milkymist-sysctl"
|
|
|
|
#define MILKYMIST_SYSCTL(obj) \
|
|
|
|
OBJECT_CHECK(MilkymistSysctlState, (obj), TYPE_MILKYMIST_SYSCTL)
|
|
|
|
|
2011-03-08 01:32:38 +03:00
|
|
|
struct MilkymistSysctlState {
|
2013-07-27 17:06:42 +04:00
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2011-08-31 18:48:43 +04:00
|
|
|
MemoryRegion regs_region;
|
2011-03-08 01:32:38 +03:00
|
|
|
|
|
|
|
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:
|
2017-05-16 00:41:13 +03:00
|
|
|
qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
|
2011-03-08 01:32:38 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t sysctl_read(void *opaque, hwaddr addr,
|
2011-08-31 18:48:43 +04:00
|
|
|
unsigned size)
|
2011-03-08 01:32:38 +03: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-03-31 21:54:09 +04:00
|
|
|
case R_DBG_SCRATCHPAD:
|
|
|
|
case R_DBG_WRITE_LOCK:
|
|
|
|
case R_CLK_FREQUENCY:
|
2011-03-08 01:32:38 +03:00
|
|
|
case R_CAPABILITIES:
|
|
|
|
case R_SYSTEM_ID:
|
|
|
|
r = s->regs[addr];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-06-22 16:03:56 +04:00
|
|
|
error_report("milkymist_sysctl: read access to unknown register 0x"
|
2011-03-08 01:32:38 +03:00
|
|
|
TARGET_FMT_plx, addr << 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_milkymist_sysctl_memory_read(addr << 2, r);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void sysctl_write(void *opaque, hwaddr addr, uint64_t value,
|
2011-08-31 18:48:43 +04:00
|
|
|
unsigned size)
|
2011-03-08 01:32:38 +03: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-03-31 21:54:09 +04:00
|
|
|
case R_DBG_SCRATCHPAD:
|
2011-04-13 02:29:35 +04:00
|
|
|
s->regs[addr] = value;
|
2011-03-08 01:32:38 +03:00
|
|
|
break;
|
|
|
|
case R_TIMER0_COMPARE:
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_begin(s->ptimer0);
|
2011-03-08 01:32:38 +03:00
|
|
|
ptimer_set_limit(s->ptimer0, value, 0);
|
|
|
|
s->regs[addr] = value;
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_commit(s->ptimer0);
|
2011-03-08 01:32:38 +03:00
|
|
|
break;
|
|
|
|
case R_TIMER1_COMPARE:
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_begin(s->ptimer1);
|
2011-03-08 01:32:38 +03:00
|
|
|
ptimer_set_limit(s->ptimer1, value, 0);
|
|
|
|
s->regs[addr] = value;
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_commit(s->ptimer1);
|
2011-03-08 01:32:38 +03:00
|
|
|
break;
|
|
|
|
case R_TIMER0_CONTROL:
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_begin(s->ptimer0);
|
2011-03-08 01:32:38 +03:00
|
|
|
s->regs[addr] = value;
|
|
|
|
if (s->regs[R_TIMER0_CONTROL] & CTRL_ENABLE) {
|
2011-04-13 02:29:35 +04:00
|
|
|
trace_milkymist_sysctl_start_timer0();
|
|
|
|
ptimer_set_count(s->ptimer0,
|
|
|
|
s->regs[R_TIMER0_COMPARE] - s->regs[R_TIMER0_COUNTER]);
|
2011-03-08 01:32:38 +03:00
|
|
|
ptimer_run(s->ptimer0, 0);
|
|
|
|
} else {
|
2011-04-13 02:29:35 +04:00
|
|
|
trace_milkymist_sysctl_stop_timer0();
|
2011-03-08 01:32:38 +03:00
|
|
|
ptimer_stop(s->ptimer0);
|
|
|
|
}
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_commit(s->ptimer0);
|
2011-03-08 01:32:38 +03:00
|
|
|
break;
|
|
|
|
case R_TIMER1_CONTROL:
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_begin(s->ptimer1);
|
2011-03-08 01:32:38 +03:00
|
|
|
s->regs[addr] = value;
|
|
|
|
if (s->regs[R_TIMER1_CONTROL] & CTRL_ENABLE) {
|
|
|
|
trace_milkymist_sysctl_start_timer1();
|
2011-04-13 02:29:35 +04:00
|
|
|
ptimer_set_count(s->ptimer1,
|
|
|
|
s->regs[R_TIMER1_COMPARE] - s->regs[R_TIMER1_COUNTER]);
|
2011-03-08 01:32:38 +03:00
|
|
|
ptimer_run(s->ptimer1, 0);
|
|
|
|
} else {
|
|
|
|
trace_milkymist_sysctl_stop_timer1();
|
|
|
|
ptimer_stop(s->ptimer1);
|
|
|
|
}
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_commit(s->ptimer1);
|
2011-03-08 01:32:38 +03:00
|
|
|
break;
|
|
|
|
case R_ICAP:
|
|
|
|
sysctl_icap_write(s, value);
|
|
|
|
break;
|
2012-03-31 21:54:09 +04:00
|
|
|
case R_DBG_WRITE_LOCK:
|
|
|
|
s->regs[addr] = 1;
|
|
|
|
break;
|
2011-03-08 01:32:38 +03:00
|
|
|
case R_SYSTEM_ID:
|
2017-05-16 00:41:13 +03:00
|
|
|
qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
|
2011-03-08 01:32:38 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case R_GPIO_IN:
|
2012-03-31 21:54:09 +04:00
|
|
|
case R_CLK_FREQUENCY:
|
2011-03-08 01:32:38 +03:00
|
|
|
case R_CAPABILITIES:
|
|
|
|
error_report("milkymist_sysctl: write to read-only register 0x"
|
|
|
|
TARGET_FMT_plx, addr << 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-06-22 16:03:56 +04:00
|
|
|
error_report("milkymist_sysctl: write access to unknown register 0x"
|
2011-03-08 01:32:38 +03:00
|
|
|
TARGET_FMT_plx, addr << 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 18:48:43 +04: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 01:32:38 +03: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)
|
|
|
|
{
|
2013-07-27 17:06:42 +04:00
|
|
|
MilkymistSysctlState *s = MILKYMIST_SYSCTL(d);
|
2011-03-08 01:32:38 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < R_MAX; i++) {
|
|
|
|
s->regs[i] = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_begin(s->ptimer0);
|
2011-03-08 01:32:38 +03:00
|
|
|
ptimer_stop(s->ptimer0);
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_commit(s->ptimer0);
|
|
|
|
ptimer_transaction_begin(s->ptimer1);
|
2011-03-08 01:32:38 +03:00
|
|
|
ptimer_stop(s->ptimer1);
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_commit(s->ptimer1);
|
2011-03-08 01:32:38 +03:00
|
|
|
|
|
|
|
/* defaults */
|
|
|
|
s->regs[R_ICAP] = ICAP_READY;
|
|
|
|
s->regs[R_SYSTEM_ID] = s->systemid;
|
2012-03-31 21:54:09 +04:00
|
|
|
s->regs[R_CLK_FREQUENCY] = s->freq_hz;
|
2011-03-08 01:32:38 +03:00
|
|
|
s->regs[R_CAPABILITIES] = s->capabilities;
|
|
|
|
s->regs[R_GPIO_IN] = s->strappings;
|
|
|
|
}
|
|
|
|
|
2016-02-22 06:15:30 +03:00
|
|
|
static void milkymist_sysctl_init(Object *obj)
|
2011-03-08 01:32:38 +03:00
|
|
|
{
|
2016-02-22 06:15:30 +03:00
|
|
|
MilkymistSysctlState *s = MILKYMIST_SYSCTL(obj);
|
|
|
|
SysBusDevice *dev = SYS_BUS_DEVICE(obj);
|
2011-03-08 01:32:38 +03:00
|
|
|
|
|
|
|
sysbus_init_irq(dev, &s->gpio_irq);
|
|
|
|
sysbus_init_irq(dev, &s->timer0_irq);
|
|
|
|
sysbus_init_irq(dev, &s->timer1_irq);
|
|
|
|
|
2016-02-22 06:15:30 +03:00
|
|
|
memory_region_init_io(&s->regs_region, obj, &sysctl_mmio_ops, s,
|
2011-08-31 18:48:43 +04:00
|
|
|
"milkymist-sysctl", R_MAX * 4);
|
2011-11-27 13:38:10 +04:00
|
|
|
sysbus_init_mmio(dev, &s->regs_region);
|
2016-02-22 06:15:30 +03:00
|
|
|
}
|
2011-03-08 01:32:38 +03:00
|
|
|
|
2016-02-22 06:15:30 +03:00
|
|
|
static void milkymist_sysctl_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
MilkymistSysctlState *s = MILKYMIST_SYSCTL(dev);
|
|
|
|
|
2019-10-21 17:10:40 +03:00
|
|
|
s->ptimer0 = ptimer_init(timer0_hit, s, PTIMER_POLICY_DEFAULT);
|
|
|
|
s->ptimer1 = ptimer_init(timer1_hit, s, PTIMER_POLICY_DEFAULT);
|
2019-10-01 16:36:27 +03:00
|
|
|
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_begin(s->ptimer0);
|
2016-02-22 06:15:30 +03:00
|
|
|
ptimer_set_freq(s->ptimer0, s->freq_hz);
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_commit(s->ptimer0);
|
|
|
|
ptimer_transaction_begin(s->ptimer1);
|
2016-02-22 06:15:30 +03:00
|
|
|
ptimer_set_freq(s->ptimer1, s->freq_hz);
|
2019-10-21 17:10:40 +03:00
|
|
|
ptimer_transaction_commit(s->ptimer1);
|
2011-03-08 01:32:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_milkymist_sysctl = {
|
|
|
|
.name = "milkymist-sysctl",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-04-16 18:01:33 +04:00
|
|
|
.fields = (VMStateField[]) {
|
2011-03-08 01:32:38 +03:00
|
|
|
VMSTATE_UINT32_ARRAY(regs, MilkymistSysctlState, R_MAX),
|
|
|
|
VMSTATE_PTIMER(ptimer0, MilkymistSysctlState),
|
|
|
|
VMSTATE_PTIMER(ptimer1, MilkymistSysctlState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-24 23:12:29 +04: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 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 23:12:29 +04:00
|
|
|
|
2016-02-22 06:15:30 +03:00
|
|
|
dc->realize = milkymist_sysctl_realize;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->reset = milkymist_sysctl_reset;
|
|
|
|
dc->vmsd = &vmstate_milkymist_sysctl;
|
|
|
|
dc->props = milkymist_sysctl_properties;
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo milkymist_sysctl_info = {
|
2013-07-27 17:06:42 +04:00
|
|
|
.name = TYPE_MILKYMIST_SYSCTL,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(MilkymistSysctlState),
|
2016-02-22 06:15:30 +03:00
|
|
|
.instance_init = milkymist_sysctl_init,
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = milkymist_sysctl_class_init,
|
2011-03-08 01:32:38 +03:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void milkymist_sysctl_register_types(void)
|
2011-03-08 01:32:38 +03:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&milkymist_sysctl_info);
|
2011-03-08 01:32:38 +03:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(milkymist_sysctl_register_types)
|