2011-02-17 23:45:10 +01:00
|
|
|
/*
|
|
|
|
* QEMU model of the LatticeMico32 timer block.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 Michael Walle <michael@walle.cc>
|
|
|
|
*
|
|
|
|
* 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.latticesemi.com/documents/mico32timer.pdf
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:16:57 +01:00
|
|
|
#include "qemu/osdep.h"
|
2019-08-12 07:23:42 +02:00
|
|
|
#include "hw/irq.h"
|
2013-02-04 15:40:22 +01:00
|
|
|
#include "hw/sysbus.h"
|
2019-08-12 07:23:45 +02:00
|
|
|
#include "migration/vmstate.h"
|
2011-02-17 23:45:10 +01:00
|
|
|
#include "trace.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/timer.h"
|
2013-02-04 15:40:22 +01:00
|
|
|
#include "hw/ptimer.h"
|
2019-08-12 07:23:51 +02:00
|
|
|
#include "hw/qdev-properties.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/error-report.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2011-02-17 23:45:10 +01:00
|
|
|
|
|
|
|
#define DEFAULT_FREQUENCY (50*1000000)
|
|
|
|
|
|
|
|
enum {
|
|
|
|
R_SR = 0,
|
|
|
|
R_CR,
|
|
|
|
R_PERIOD,
|
|
|
|
R_SNAPSHOT,
|
|
|
|
R_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
SR_TO = (1 << 0),
|
|
|
|
SR_RUN = (1 << 1),
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CR_ITO = (1 << 0),
|
|
|
|
CR_CONT = (1 << 1),
|
|
|
|
CR_START = (1 << 2),
|
|
|
|
CR_STOP = (1 << 3),
|
|
|
|
};
|
|
|
|
|
2013-07-27 14:54:54 +02:00
|
|
|
#define TYPE_LM32_TIMER "lm32-timer"
|
|
|
|
#define LM32_TIMER(obj) OBJECT_CHECK(LM32TimerState, (obj), TYPE_LM32_TIMER)
|
|
|
|
|
2011-02-17 23:45:10 +01:00
|
|
|
struct LM32TimerState {
|
2013-07-27 14:54:54 +02:00
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2011-11-20 10:44:25 +01:00
|
|
|
MemoryRegion iomem;
|
2011-02-17 23:45:10 +01:00
|
|
|
|
|
|
|
ptimer_state *ptimer;
|
|
|
|
|
|
|
|
qemu_irq irq;
|
|
|
|
uint32_t freq_hz;
|
|
|
|
|
|
|
|
uint32_t regs[R_MAX];
|
|
|
|
};
|
|
|
|
typedef struct LM32TimerState LM32TimerState;
|
|
|
|
|
|
|
|
static void timer_update_irq(LM32TimerState *s)
|
|
|
|
{
|
|
|
|
int state = (s->regs[R_SR] & SR_TO) && (s->regs[R_CR] & CR_ITO);
|
|
|
|
|
|
|
|
trace_lm32_timer_irq_state(state);
|
|
|
|
qemu_set_irq(s->irq, state);
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static uint64_t timer_read(void *opaque, hwaddr addr, unsigned size)
|
2011-02-17 23:45:10 +01:00
|
|
|
{
|
|
|
|
LM32TimerState *s = opaque;
|
|
|
|
uint32_t r = 0;
|
|
|
|
|
|
|
|
addr >>= 2;
|
|
|
|
switch (addr) {
|
|
|
|
case R_SR:
|
|
|
|
case R_CR:
|
|
|
|
case R_PERIOD:
|
|
|
|
r = s->regs[addr];
|
|
|
|
break;
|
|
|
|
case R_SNAPSHOT:
|
|
|
|
r = (uint32_t)ptimer_get_count(s->ptimer);
|
|
|
|
break;
|
|
|
|
default:
|
2011-06-22 14:03:56 +02:00
|
|
|
error_report("lm32_timer: read access to unknown register 0x"
|
2011-02-17 23:45:10 +01:00
|
|
|
TARGET_FMT_plx, addr << 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_lm32_timer_memory_read(addr << 2, r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static void timer_write(void *opaque, hwaddr addr,
|
2011-11-20 10:44:25 +01:00
|
|
|
uint64_t value, unsigned size)
|
2011-02-17 23:45:10 +01:00
|
|
|
{
|
|
|
|
LM32TimerState *s = opaque;
|
|
|
|
|
|
|
|
trace_lm32_timer_memory_write(addr, value);
|
|
|
|
|
|
|
|
addr >>= 2;
|
|
|
|
switch (addr) {
|
|
|
|
case R_SR:
|
|
|
|
s->regs[R_SR] &= ~SR_TO;
|
|
|
|
break;
|
|
|
|
case R_CR:
|
2019-10-22 17:50:36 +02:00
|
|
|
ptimer_transaction_begin(s->ptimer);
|
2011-02-17 23:45:10 +01:00
|
|
|
s->regs[R_CR] = value;
|
|
|
|
if (s->regs[R_CR] & CR_START) {
|
|
|
|
ptimer_run(s->ptimer, 1);
|
|
|
|
}
|
|
|
|
if (s->regs[R_CR] & CR_STOP) {
|
|
|
|
ptimer_stop(s->ptimer);
|
|
|
|
}
|
2019-10-22 17:50:36 +02:00
|
|
|
ptimer_transaction_commit(s->ptimer);
|
2011-02-17 23:45:10 +01:00
|
|
|
break;
|
|
|
|
case R_PERIOD:
|
|
|
|
s->regs[R_PERIOD] = value;
|
2019-10-22 17:50:36 +02:00
|
|
|
ptimer_transaction_begin(s->ptimer);
|
2011-02-17 23:45:10 +01:00
|
|
|
ptimer_set_count(s->ptimer, value);
|
2019-10-22 17:50:36 +02:00
|
|
|
ptimer_transaction_commit(s->ptimer);
|
2011-02-17 23:45:10 +01:00
|
|
|
break;
|
|
|
|
case R_SNAPSHOT:
|
|
|
|
error_report("lm32_timer: write access to read only register 0x"
|
|
|
|
TARGET_FMT_plx, addr << 2);
|
|
|
|
break;
|
|
|
|
default:
|
2011-06-22 14:03:56 +02:00
|
|
|
error_report("lm32_timer: write access to unknown register 0x"
|
2011-02-17 23:45:10 +01:00
|
|
|
TARGET_FMT_plx, addr << 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timer_update_irq(s);
|
|
|
|
}
|
|
|
|
|
2011-11-20 10:44:25 +01:00
|
|
|
static const MemoryRegionOps timer_ops = {
|
|
|
|
.read = timer_read,
|
|
|
|
.write = timer_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4,
|
|
|
|
},
|
2011-02-17 23:45:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void timer_hit(void *opaque)
|
|
|
|
{
|
|
|
|
LM32TimerState *s = opaque;
|
|
|
|
|
|
|
|
trace_lm32_timer_hit();
|
|
|
|
|
|
|
|
s->regs[R_SR] |= SR_TO;
|
|
|
|
|
|
|
|
if (s->regs[R_CR] & CR_CONT) {
|
|
|
|
ptimer_set_count(s->ptimer, s->regs[R_PERIOD]);
|
|
|
|
ptimer_run(s->ptimer, 1);
|
|
|
|
}
|
|
|
|
timer_update_irq(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void timer_reset(DeviceState *d)
|
|
|
|
{
|
2013-07-27 14:54:54 +02:00
|
|
|
LM32TimerState *s = LM32_TIMER(d);
|
2011-02-17 23:45:10 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < R_MAX; i++) {
|
|
|
|
s->regs[i] = 0;
|
|
|
|
}
|
2019-10-22 17:50:36 +02:00
|
|
|
ptimer_transaction_begin(s->ptimer);
|
2011-02-17 23:45:10 +01:00
|
|
|
ptimer_stop(s->ptimer);
|
2019-10-22 17:50:36 +02:00
|
|
|
ptimer_transaction_commit(s->ptimer);
|
2011-02-17 23:45:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 04:15:27 +01:00
|
|
|
static void lm32_timer_init(Object *obj)
|
2011-02-17 23:45:10 +01:00
|
|
|
{
|
2016-02-22 04:15:27 +01:00
|
|
|
LM32TimerState *s = LM32_TIMER(obj);
|
|
|
|
SysBusDevice *dev = SYS_BUS_DEVICE(obj);
|
2011-02-17 23:45:10 +01:00
|
|
|
|
|
|
|
sysbus_init_irq(dev, &s->irq);
|
|
|
|
|
2016-02-22 04:15:27 +01:00
|
|
|
memory_region_init_io(&s->iomem, obj, &timer_ops, s,
|
2013-06-07 03:25:08 +02:00
|
|
|
"timer", R_MAX * 4);
|
2011-11-27 10:38:10 +01:00
|
|
|
sysbus_init_mmio(dev, &s->iomem);
|
2016-02-22 04:15:27 +01:00
|
|
|
}
|
2011-02-17 23:45:10 +01:00
|
|
|
|
2016-02-22 04:15:27 +01:00
|
|
|
static void lm32_timer_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
LM32TimerState *s = LM32_TIMER(dev);
|
|
|
|
|
2019-10-22 17:50:36 +02:00
|
|
|
s->ptimer = ptimer_init(timer_hit, s, PTIMER_POLICY_DEFAULT);
|
2019-10-01 15:36:27 +02:00
|
|
|
|
2019-10-22 17:50:36 +02:00
|
|
|
ptimer_transaction_begin(s->ptimer);
|
2016-02-22 04:15:27 +01:00
|
|
|
ptimer_set_freq(s->ptimer, s->freq_hz);
|
2019-10-22 17:50:36 +02:00
|
|
|
ptimer_transaction_commit(s->ptimer);
|
2011-02-17 23:45:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_lm32_timer = {
|
|
|
|
.name = "lm32-timer",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-04-16 16:01:33 +02:00
|
|
|
.fields = (VMStateField[]) {
|
2011-02-17 23:45:10 +01:00
|
|
|
VMSTATE_PTIMER(ptimer, LM32TimerState),
|
|
|
|
VMSTATE_UINT32(freq_hz, LM32TimerState),
|
|
|
|
VMSTATE_UINT32_ARRAY(regs, LM32TimerState, R_MAX),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-24 20:12:29 +01:00
|
|
|
static Property lm32_timer_properties[] = {
|
|
|
|
DEFINE_PROP_UINT32("frequency", LM32TimerState, freq_hz, DEFAULT_FREQUENCY),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void lm32_timer_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
|
|
|
|
2016-02-22 04:15:27 +01:00
|
|
|
dc->realize = lm32_timer_realize;
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->reset = timer_reset;
|
|
|
|
dc->vmsd = &vmstate_lm32_timer;
|
2020-01-10 16:30:32 +01:00
|
|
|
device_class_set_props(dc, lm32_timer_properties);
|
2012-01-24 20:12:29 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo lm32_timer_info = {
|
2013-07-27 14:54:54 +02:00
|
|
|
.name = TYPE_LM32_TIMER,
|
2011-12-08 04:34:16 +01:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(LM32TimerState),
|
2016-02-22 04:15:27 +01:00
|
|
|
.instance_init = lm32_timer_init,
|
2011-12-08 04:34:16 +01:00
|
|
|
.class_init = lm32_timer_class_init,
|
2011-02-17 23:45:10 +01:00
|
|
|
};
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
static void lm32_timer_register_types(void)
|
2011-02-17 23:45:10 +01:00
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
type_register_static(&lm32_timer_info);
|
2011-02-17 23:45:10 +01:00
|
|
|
}
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
type_init(lm32_timer_register_types)
|