2 * QEMU model of the Xilinx timer block.
4 * Copyright (c) 2009 Edgar E. Iglesias.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "hw/sysbus.h"
26 #include "hw/ptimer.h"
28 #include "qemu/main-loop.h"
37 #define TCSR_MDT (1<<0)
38 #define TCSR_UDT (1<<1)
39 #define TCSR_GENT (1<<2)
40 #define TCSR_CAPT (1<<3)
41 #define TCSR_ARHT (1<<4)
42 #define TCSR_LOAD (1<<5)
43 #define TCSR_ENIT (1<<6)
44 #define TCSR_ENT (1<<7)
45 #define TCSR_TINT (1<<8)
46 #define TCSR_PWMA (1<<9)
47 #define TCSR_ENALL (1<<10)
54 int nr; /* for debug. */
56 unsigned long timer_div;
61 #define TYPE_XILINX_TIMER "xlnx.xps-timer"
62 #define XILINX_TIMER(obj) \
63 OBJECT_CHECK(struct timerblock, (obj), TYPE_XILINX_TIMER)
67 SysBusDevice parent_obj;
71 uint8_t one_timer_only;
73 struct xlx_timer *timers;
76 static inline unsigned int num_timers(struct timerblock *t)
78 return 2 - t->one_timer_only;
81 static inline unsigned int timer_from_addr(hwaddr addr)
83 /* Timers get a 4x32bit control reg area each. */
87 static void timer_update_irq(struct timerblock *t)
89 unsigned int i, irq = 0;
92 for (i = 0; i < num_timers(t); i++) {
93 csr = t->timers[i].regs[R_TCSR];
94 irq |= (csr & TCSR_TINT) && (csr & TCSR_ENIT);
97 /* All timers within the same slave share a single IRQ line. */
98 qemu_set_irq(t->irq, !!irq);
102 timer_read(void *opaque, hwaddr addr, unsigned int size)
104 struct timerblock *t = opaque;
105 struct xlx_timer *xt;
110 timer = timer_from_addr(addr);
111 xt = &t->timers[timer];
112 /* Further decoding to address a specific timers reg. */
117 r = ptimer_get_count(xt->ptimer);
118 if (!(xt->regs[R_TCSR] & TCSR_UDT))
120 D(qemu_log("xlx_timer t=%d read counter=%x udt=%d\n",
121 timer, r, xt->regs[R_TCSR] & TCSR_UDT));
124 if (addr < ARRAY_SIZE(xt->regs))
129 D(fprintf(stderr, "%s timer=%d %x=%x\n", __func__, timer, addr * 4, r));
133 static void timer_enable(struct xlx_timer *xt)
137 D(fprintf(stderr, "%s timer=%d down=%d\n", __func__,
138 xt->nr, xt->regs[R_TCSR] & TCSR_UDT));
140 ptimer_stop(xt->ptimer);
142 if (xt->regs[R_TCSR] & TCSR_UDT)
143 count = xt->regs[R_TLR];
145 count = ~0 - xt->regs[R_TLR];
146 ptimer_set_limit(xt->ptimer, count, 1);
147 ptimer_run(xt->ptimer, 1);
151 timer_write(void *opaque, hwaddr addr,
152 uint64_t val64, unsigned int size)
154 struct timerblock *t = opaque;
155 struct xlx_timer *xt;
157 uint32_t value = val64;
160 timer = timer_from_addr(addr);
161 xt = &t->timers[timer];
162 D(fprintf(stderr, "%s addr=%x val=%x (timer=%d off=%d)\n",
163 __func__, addr * 4, value, timer, addr & 3));
164 /* Further decoding to address a specific timers reg. */
169 if (value & TCSR_TINT)
172 xt->regs[addr] = value & 0x7ff;
173 if (value & TCSR_ENT)
178 if (addr < ARRAY_SIZE(xt->regs))
179 xt->regs[addr] = value;
185 static const MemoryRegionOps timer_ops = {
187 .write = timer_write,
188 .endianness = DEVICE_NATIVE_ENDIAN,
190 .min_access_size = 4,
195 static void timer_hit(void *opaque)
197 struct xlx_timer *xt = opaque;
198 struct timerblock *t = xt->parent;
199 D(fprintf(stderr, "%s %d\n", __func__, xt->nr));
200 xt->regs[R_TCSR] |= TCSR_TINT;
202 if (xt->regs[R_TCSR] & TCSR_ARHT)
207 static void xilinx_timer_realize(DeviceState *dev, Error **errp)
209 struct timerblock *t = XILINX_TIMER(dev);
212 /* Init all the ptimers. */
213 t->timers = g_malloc0(sizeof t->timers[0] * num_timers(t));
214 for (i = 0; i < num_timers(t); i++) {
215 struct xlx_timer *xt = &t->timers[i];
219 xt->bh = qemu_bh_new(timer_hit, xt);
220 xt->ptimer = ptimer_init(xt->bh);
221 ptimer_set_freq(xt->ptimer, t->freq_hz);
224 memory_region_init_io(&t->mmio, OBJECT(t), &timer_ops, t, "xlnx.xps-timer",
225 R_MAX * 4 * num_timers(t));
226 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &t->mmio);
229 static void xilinx_timer_init(Object *obj)
231 struct timerblock *t = XILINX_TIMER(obj);
233 /* All timers share a single irq line. */
234 sysbus_init_irq(SYS_BUS_DEVICE(obj), &t->irq);
237 static Property xilinx_timer_properties[] = {
238 DEFINE_PROP_UINT32("clock-frequency", struct timerblock, freq_hz,
240 DEFINE_PROP_UINT8("one-timer-only", struct timerblock, one_timer_only, 0),
241 DEFINE_PROP_END_OF_LIST(),
244 static void xilinx_timer_class_init(ObjectClass *klass, void *data)
246 DeviceClass *dc = DEVICE_CLASS(klass);
248 dc->realize = xilinx_timer_realize;
249 dc->props = xilinx_timer_properties;
252 static const TypeInfo xilinx_timer_info = {
253 .name = TYPE_XILINX_TIMER,
254 .parent = TYPE_SYS_BUS_DEVICE,
255 .instance_size = sizeof(struct timerblock),
256 .instance_init = xilinx_timer_init,
257 .class_init = xilinx_timer_class_init,
260 static void xilinx_timer_register_types(void)
262 type_register_static(&xilinx_timer_info);
265 type_init(xilinx_timer_register_types)