2009-05-20 22:11:44 +04:00
|
|
|
/*
|
|
|
|
* QEMU model of the Xilinx timer block.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 Edgar E. Iglesias.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:18 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/sysbus.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.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/log.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2009-05-20 22:11:44 +04:00
|
|
|
|
|
|
|
#define D(x)
|
|
|
|
|
|
|
|
#define R_TCSR 0
|
|
|
|
#define R_TLR 1
|
|
|
|
#define R_TCR 2
|
|
|
|
#define R_MAX 4
|
|
|
|
|
|
|
|
#define TCSR_MDT (1<<0)
|
|
|
|
#define TCSR_UDT (1<<1)
|
|
|
|
#define TCSR_GENT (1<<2)
|
|
|
|
#define TCSR_CAPT (1<<3)
|
|
|
|
#define TCSR_ARHT (1<<4)
|
|
|
|
#define TCSR_LOAD (1<<5)
|
|
|
|
#define TCSR_ENIT (1<<6)
|
|
|
|
#define TCSR_ENT (1<<7)
|
|
|
|
#define TCSR_TINT (1<<8)
|
|
|
|
#define TCSR_PWMA (1<<9)
|
|
|
|
#define TCSR_ENALL (1<<10)
|
|
|
|
|
|
|
|
struct xlx_timer
|
|
|
|
{
|
|
|
|
ptimer_state *ptimer;
|
|
|
|
void *parent;
|
|
|
|
int nr; /* for debug. */
|
|
|
|
|
|
|
|
unsigned long timer_div;
|
|
|
|
|
|
|
|
uint32_t regs[R_MAX];
|
|
|
|
};
|
|
|
|
|
2013-07-27 17:32:47 +04:00
|
|
|
#define TYPE_XILINX_TIMER "xlnx.xps-timer"
|
2023-01-09 17:03:06 +03:00
|
|
|
typedef struct XpsTimerState XpsTimerState;
|
|
|
|
DECLARE_INSTANCE_CHECKER(XpsTimerState, XILINX_TIMER, TYPE_XILINX_TIMER)
|
2013-07-27 17:32:47 +04:00
|
|
|
|
2023-01-09 17:03:06 +03:00
|
|
|
struct XpsTimerState
|
2009-05-20 22:11:44 +04:00
|
|
|
{
|
2013-07-27 17:32:47 +04:00
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2011-08-26 02:13:47 +04:00
|
|
|
MemoryRegion mmio;
|
2009-05-20 22:11:44 +04:00
|
|
|
qemu_irq irq;
|
2012-06-13 08:46:43 +04:00
|
|
|
uint8_t one_timer_only;
|
2009-07-15 15:43:31 +04:00
|
|
|
uint32_t freq_hz;
|
2009-05-20 22:11:44 +04:00
|
|
|
struct xlx_timer *timers;
|
|
|
|
};
|
|
|
|
|
2023-01-09 17:03:06 +03:00
|
|
|
static inline unsigned int num_timers(XpsTimerState *t)
|
2012-06-13 08:46:43 +04:00
|
|
|
{
|
|
|
|
return 2 - t->one_timer_only;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static inline unsigned int timer_from_addr(hwaddr addr)
|
2009-05-20 22:11:44 +04:00
|
|
|
{
|
|
|
|
/* Timers get a 4x32bit control reg area each. */
|
|
|
|
return addr >> 2;
|
|
|
|
}
|
|
|
|
|
2023-01-09 17:03:06 +03:00
|
|
|
static void timer_update_irq(XpsTimerState *t)
|
2009-05-20 22:11:44 +04:00
|
|
|
{
|
|
|
|
unsigned int i, irq = 0;
|
|
|
|
uint32_t csr;
|
|
|
|
|
2012-06-13 08:46:43 +04:00
|
|
|
for (i = 0; i < num_timers(t); i++) {
|
2009-05-20 22:11:44 +04:00
|
|
|
csr = t->timers[i].regs[R_TCSR];
|
|
|
|
irq |= (csr & TCSR_TINT) && (csr & TCSR_ENIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All timers within the same slave share a single IRQ line. */
|
|
|
|
qemu_set_irq(t->irq, !!irq);
|
|
|
|
}
|
|
|
|
|
2011-08-26 02:13:47 +04:00
|
|
|
static uint64_t
|
2012-10-23 14:30:10 +04:00
|
|
|
timer_read(void *opaque, hwaddr addr, unsigned int size)
|
2009-05-20 22:11:44 +04:00
|
|
|
{
|
2023-01-09 17:03:06 +03:00
|
|
|
XpsTimerState *t = opaque;
|
2009-05-20 22:11:44 +04:00
|
|
|
struct xlx_timer *xt;
|
|
|
|
uint32_t r = 0;
|
|
|
|
unsigned int timer;
|
|
|
|
|
|
|
|
addr >>= 2;
|
|
|
|
timer = timer_from_addr(addr);
|
|
|
|
xt = &t->timers[timer];
|
|
|
|
/* Further decoding to address a specific timers reg. */
|
|
|
|
addr &= 0x3;
|
|
|
|
switch (addr)
|
|
|
|
{
|
|
|
|
case R_TCR:
|
|
|
|
r = ptimer_get_count(xt->ptimer);
|
|
|
|
if (!(xt->regs[R_TCSR] & TCSR_UDT))
|
|
|
|
r = ~r;
|
|
|
|
D(qemu_log("xlx_timer t=%d read counter=%x udt=%d\n",
|
|
|
|
timer, r, xt->regs[R_TCSR] & TCSR_UDT));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (addr < ARRAY_SIZE(xt->regs))
|
|
|
|
r = xt->regs[addr];
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2012-06-28 10:28:03 +04:00
|
|
|
D(fprintf(stderr, "%s timer=%d %x=%x\n", __func__, timer, addr * 4, r));
|
2009-05-20 22:11:44 +04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-10-17 16:21:21 +03:00
|
|
|
/* Must be called inside ptimer transaction block */
|
2009-05-20 22:11:44 +04:00
|
|
|
static void timer_enable(struct xlx_timer *xt)
|
|
|
|
{
|
|
|
|
uint64_t count;
|
|
|
|
|
2012-06-28 10:28:03 +04:00
|
|
|
D(fprintf(stderr, "%s timer=%d down=%d\n", __func__,
|
2009-05-20 22:11:44 +04:00
|
|
|
xt->nr, xt->regs[R_TCSR] & TCSR_UDT));
|
|
|
|
|
|
|
|
ptimer_stop(xt->ptimer);
|
|
|
|
|
|
|
|
if (xt->regs[R_TCSR] & TCSR_UDT)
|
|
|
|
count = xt->regs[R_TLR];
|
|
|
|
else
|
|
|
|
count = ~0 - xt->regs[R_TLR];
|
2012-06-16 09:20:59 +04:00
|
|
|
ptimer_set_limit(xt->ptimer, count, 1);
|
2009-05-20 22:11:44 +04:00
|
|
|
ptimer_run(xt->ptimer, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-23 14:30:10 +04:00
|
|
|
timer_write(void *opaque, hwaddr addr,
|
2011-08-26 02:13:47 +04:00
|
|
|
uint64_t val64, unsigned int size)
|
2009-05-20 22:11:44 +04:00
|
|
|
{
|
2023-01-09 17:03:06 +03:00
|
|
|
XpsTimerState *t = opaque;
|
2009-05-20 22:11:44 +04:00
|
|
|
struct xlx_timer *xt;
|
|
|
|
unsigned int timer;
|
2011-08-26 02:13:47 +04:00
|
|
|
uint32_t value = val64;
|
2009-05-20 22:11:44 +04:00
|
|
|
|
|
|
|
addr >>= 2;
|
|
|
|
timer = timer_from_addr(addr);
|
|
|
|
xt = &t->timers[timer];
|
2012-06-28 10:28:03 +04:00
|
|
|
D(fprintf(stderr, "%s addr=%x val=%x (timer=%d off=%d)\n",
|
2009-05-20 22:11:44 +04:00
|
|
|
__func__, addr * 4, value, timer, addr & 3));
|
|
|
|
/* Further decoding to address a specific timers reg. */
|
|
|
|
addr &= 3;
|
|
|
|
switch (addr)
|
|
|
|
{
|
|
|
|
case R_TCSR:
|
|
|
|
if (value & TCSR_TINT)
|
|
|
|
value &= ~TCSR_TINT;
|
|
|
|
|
2014-04-25 19:39:48 +04:00
|
|
|
xt->regs[addr] = value & 0x7ff;
|
2019-10-17 16:21:21 +03:00
|
|
|
if (value & TCSR_ENT) {
|
|
|
|
ptimer_transaction_begin(xt->ptimer);
|
2009-05-20 22:11:44 +04:00
|
|
|
timer_enable(xt);
|
2019-10-17 16:21:21 +03:00
|
|
|
ptimer_transaction_commit(xt->ptimer);
|
|
|
|
}
|
2009-05-20 22:11:44 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (addr < ARRAY_SIZE(xt->regs))
|
|
|
|
xt->regs[addr] = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timer_update_irq(t);
|
|
|
|
}
|
|
|
|
|
2011-08-26 02:13:47 +04: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
|
|
|
|
}
|
2009-05-20 22:11:44 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void timer_hit(void *opaque)
|
|
|
|
{
|
|
|
|
struct xlx_timer *xt = opaque;
|
2023-01-09 17:03:06 +03:00
|
|
|
XpsTimerState *t = xt->parent;
|
2012-09-10 04:20:07 +04:00
|
|
|
D(fprintf(stderr, "%s %d\n", __func__, xt->nr));
|
2009-05-20 22:11:44 +04:00
|
|
|
xt->regs[R_TCSR] |= TCSR_TINT;
|
|
|
|
|
|
|
|
if (xt->regs[R_TCSR] & TCSR_ARHT)
|
|
|
|
timer_enable(xt);
|
|
|
|
timer_update_irq(t);
|
|
|
|
}
|
|
|
|
|
2014-05-29 13:23:20 +04:00
|
|
|
static void xilinx_timer_realize(DeviceState *dev, Error **errp)
|
2009-05-20 22:11:44 +04:00
|
|
|
{
|
2023-01-09 17:03:06 +03:00
|
|
|
XpsTimerState *t = XILINX_TIMER(dev);
|
2009-05-20 22:11:44 +04:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* Init all the ptimers. */
|
2012-06-13 08:46:43 +04:00
|
|
|
t->timers = g_malloc0(sizeof t->timers[0] * num_timers(t));
|
|
|
|
for (i = 0; i < num_timers(t); i++) {
|
2009-05-20 22:11:44 +04:00
|
|
|
struct xlx_timer *xt = &t->timers[i];
|
|
|
|
|
|
|
|
xt->parent = t;
|
|
|
|
xt->nr = i;
|
2022-05-16 13:30:58 +03:00
|
|
|
xt->ptimer = ptimer_init(timer_hit, xt, PTIMER_POLICY_LEGACY);
|
2019-10-17 16:21:21 +03:00
|
|
|
ptimer_transaction_begin(xt->ptimer);
|
2009-07-15 15:43:31 +04:00
|
|
|
ptimer_set_freq(xt->ptimer, t->freq_hz);
|
2019-10-17 16:21:21 +03:00
|
|
|
ptimer_transaction_commit(xt->ptimer);
|
2009-05-20 22:11:44 +04:00
|
|
|
}
|
|
|
|
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&t->mmio, OBJECT(t), &timer_ops, t, "xlnx.xps-timer",
|
2012-06-13 08:46:43 +04:00
|
|
|
R_MAX * 4 * num_timers(t));
|
2014-05-29 13:23:20 +04:00
|
|
|
sysbus_init_mmio(SYS_BUS_DEVICE(dev), &t->mmio);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xilinx_timer_init(Object *obj)
|
|
|
|
{
|
2023-01-09 17:03:06 +03:00
|
|
|
XpsTimerState *t = XILINX_TIMER(obj);
|
2014-05-29 13:23:20 +04:00
|
|
|
|
|
|
|
/* All timers share a single irq line. */
|
|
|
|
sysbus_init_irq(SYS_BUS_DEVICE(obj), &t->irq);
|
2009-05-20 22:11:44 +04:00
|
|
|
}
|
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static Property xilinx_timer_properties[] = {
|
2023-01-09 17:03:06 +03:00
|
|
|
DEFINE_PROP_UINT32("clock-frequency", XpsTimerState, freq_hz, 62 * 1000000),
|
|
|
|
DEFINE_PROP_UINT8("one-timer-only", XpsTimerState, one_timer_only, 0),
|
2012-01-24 23:12:29 +04:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void xilinx_timer_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
|
|
|
|
2014-05-29 13:23:20 +04:00
|
|
|
dc->realize = xilinx_timer_realize;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, xilinx_timer_properties);
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo xilinx_timer_info = {
|
2013-07-27 17:32:47 +04:00
|
|
|
.name = TYPE_XILINX_TIMER,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
2023-01-09 17:03:06 +03:00
|
|
|
.instance_size = sizeof(XpsTimerState),
|
2014-05-29 13:23:20 +04:00
|
|
|
.instance_init = xilinx_timer_init,
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = xilinx_timer_class_init,
|
2009-07-15 15:43:31 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void xilinx_timer_register_types(void)
|
2009-05-20 22:11:44 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&xilinx_timer_info);
|
2009-05-20 22:11:44 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(xilinx_timer_register_types)
|