monitor/qmp: Update comment for commit 4eaca8de268
[qemu/armbru.git] / hw / timer / grlib_gptimer.c
blob32dbf870d4b61ef9554d9cf9787575d67e13f9c5
1 /*
2 * QEMU GRLIB GPTimer Emulator
4 * Copyright (c) 2010-2019 AdaCore
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "hw/sparc/grlib.h"
27 #include "hw/sysbus.h"
28 #include "qemu/timer.h"
29 #include "hw/irq.h"
30 #include "hw/ptimer.h"
31 #include "hw/qdev-properties.h"
32 #include "qemu/main-loop.h"
33 #include "qemu/module.h"
35 #include "trace.h"
37 #define UNIT_REG_SIZE 16 /* Size of memory mapped regs for the unit */
38 #define GPTIMER_REG_SIZE 16 /* Size of memory mapped regs for a GPTimer */
40 #define GPTIMER_MAX_TIMERS 8
42 /* GPTimer Config register fields */
43 #define GPTIMER_ENABLE (1 << 0)
44 #define GPTIMER_RESTART (1 << 1)
45 #define GPTIMER_LOAD (1 << 2)
46 #define GPTIMER_INT_ENABLE (1 << 3)
47 #define GPTIMER_INT_PENDING (1 << 4)
48 #define GPTIMER_CHAIN (1 << 5) /* Not supported */
49 #define GPTIMER_DEBUG_HALT (1 << 6) /* Not supported */
51 /* Memory mapped register offsets */
52 #define SCALER_OFFSET 0x00
53 #define SCALER_RELOAD_OFFSET 0x04
54 #define CONFIG_OFFSET 0x08
55 #define COUNTER_OFFSET 0x00
56 #define COUNTER_RELOAD_OFFSET 0x04
57 #define TIMER_BASE 0x10
59 #define GRLIB_GPTIMER(obj) \
60 OBJECT_CHECK(GPTimerUnit, (obj), TYPE_GRLIB_GPTIMER)
62 typedef struct GPTimer GPTimer;
63 typedef struct GPTimerUnit GPTimerUnit;
65 struct GPTimer {
66 QEMUBH *bh;
67 struct ptimer_state *ptimer;
69 qemu_irq irq;
70 int id;
71 GPTimerUnit *unit;
73 /* registers */
74 uint32_t counter;
75 uint32_t reload;
76 uint32_t config;
79 struct GPTimerUnit {
80 SysBusDevice parent_obj;
82 MemoryRegion iomem;
84 uint32_t nr_timers; /* Number of timers available */
85 uint32_t freq_hz; /* System frequency */
86 uint32_t irq_line; /* Base irq line */
88 GPTimer *timers;
90 /* registers */
91 uint32_t scaler;
92 uint32_t reload;
93 uint32_t config;
96 static void grlib_gptimer_enable(GPTimer *timer)
98 assert(timer != NULL);
101 ptimer_stop(timer->ptimer);
103 if (!(timer->config & GPTIMER_ENABLE)) {
104 /* Timer disabled */
105 trace_grlib_gptimer_disabled(timer->id, timer->config);
106 return;
109 /* ptimer is triggered when the counter reach 0 but GPTimer is triggered at
110 underflow. Set count + 1 to simulate the GPTimer behavior. */
112 trace_grlib_gptimer_enable(timer->id, timer->counter);
114 ptimer_set_count(timer->ptimer, (uint64_t)timer->counter + 1);
115 ptimer_run(timer->ptimer, 1);
118 static void grlib_gptimer_restart(GPTimer *timer)
120 assert(timer != NULL);
122 trace_grlib_gptimer_restart(timer->id, timer->reload);
124 timer->counter = timer->reload;
125 grlib_gptimer_enable(timer);
128 static void grlib_gptimer_set_scaler(GPTimerUnit *unit, uint32_t scaler)
130 int i = 0;
131 uint32_t value = 0;
133 assert(unit != NULL);
135 if (scaler > 0) {
136 value = unit->freq_hz / (scaler + 1);
137 } else {
138 value = unit->freq_hz;
141 trace_grlib_gptimer_set_scaler(scaler, value);
143 for (i = 0; i < unit->nr_timers; i++) {
144 ptimer_set_freq(unit->timers[i].ptimer, value);
148 static void grlib_gptimer_hit(void *opaque)
150 GPTimer *timer = opaque;
151 assert(timer != NULL);
153 trace_grlib_gptimer_hit(timer->id);
155 /* Timer expired */
157 if (timer->config & GPTIMER_INT_ENABLE) {
158 /* Set the pending bit (only unset by write in the config register) */
159 timer->config |= GPTIMER_INT_PENDING;
160 qemu_irq_pulse(timer->irq);
163 if (timer->config & GPTIMER_RESTART) {
164 grlib_gptimer_restart(timer);
168 static uint64_t grlib_gptimer_read(void *opaque, hwaddr addr,
169 unsigned size)
171 GPTimerUnit *unit = opaque;
172 hwaddr timer_addr;
173 int id;
174 uint32_t value = 0;
176 addr &= 0xff;
178 /* Unit registers */
179 switch (addr) {
180 case SCALER_OFFSET:
181 trace_grlib_gptimer_readl(-1, addr, unit->scaler);
182 return unit->scaler;
184 case SCALER_RELOAD_OFFSET:
185 trace_grlib_gptimer_readl(-1, addr, unit->reload);
186 return unit->reload;
188 case CONFIG_OFFSET:
189 trace_grlib_gptimer_readl(-1, addr, unit->config);
190 return unit->config;
192 default:
193 break;
196 timer_addr = (addr % TIMER_BASE);
197 id = (addr - TIMER_BASE) / TIMER_BASE;
199 if (id >= 0 && id < unit->nr_timers) {
201 /* GPTimer registers */
202 switch (timer_addr) {
203 case COUNTER_OFFSET:
204 value = ptimer_get_count(unit->timers[id].ptimer);
205 trace_grlib_gptimer_readl(id, addr, value);
206 return value;
208 case COUNTER_RELOAD_OFFSET:
209 value = unit->timers[id].reload;
210 trace_grlib_gptimer_readl(id, addr, value);
211 return value;
213 case CONFIG_OFFSET:
214 trace_grlib_gptimer_readl(id, addr, unit->timers[id].config);
215 return unit->timers[id].config;
217 default:
218 break;
223 trace_grlib_gptimer_readl(-1, addr, 0);
224 return 0;
227 static void grlib_gptimer_write(void *opaque, hwaddr addr,
228 uint64_t value, unsigned size)
230 GPTimerUnit *unit = opaque;
231 hwaddr timer_addr;
232 int id;
234 addr &= 0xff;
236 /* Unit registers */
237 switch (addr) {
238 case SCALER_OFFSET:
239 value &= 0xFFFF; /* clean up the value */
240 unit->scaler = value;
241 trace_grlib_gptimer_writel(-1, addr, unit->scaler);
242 return;
244 case SCALER_RELOAD_OFFSET:
245 value &= 0xFFFF; /* clean up the value */
246 unit->reload = value;
247 trace_grlib_gptimer_writel(-1, addr, unit->reload);
248 grlib_gptimer_set_scaler(unit, value);
249 return;
251 case CONFIG_OFFSET:
252 /* Read Only (disable timer freeze not supported) */
253 trace_grlib_gptimer_writel(-1, addr, 0);
254 return;
256 default:
257 break;
260 timer_addr = (addr % TIMER_BASE);
261 id = (addr - TIMER_BASE) / TIMER_BASE;
263 if (id >= 0 && id < unit->nr_timers) {
265 /* GPTimer registers */
266 switch (timer_addr) {
267 case COUNTER_OFFSET:
268 trace_grlib_gptimer_writel(id, addr, value);
269 unit->timers[id].counter = value;
270 grlib_gptimer_enable(&unit->timers[id]);
271 return;
273 case COUNTER_RELOAD_OFFSET:
274 trace_grlib_gptimer_writel(id, addr, value);
275 unit->timers[id].reload = value;
276 return;
278 case CONFIG_OFFSET:
279 trace_grlib_gptimer_writel(id, addr, value);
281 if (value & GPTIMER_INT_PENDING) {
282 /* clear pending bit */
283 value &= ~GPTIMER_INT_PENDING;
284 } else {
285 /* keep pending bit */
286 value |= unit->timers[id].config & GPTIMER_INT_PENDING;
289 unit->timers[id].config = value;
291 /* gptimer_restart calls gptimer_enable, so if "enable" and "load"
292 bits are present, we just have to call restart. */
294 if (value & GPTIMER_LOAD) {
295 grlib_gptimer_restart(&unit->timers[id]);
296 } else if (value & GPTIMER_ENABLE) {
297 grlib_gptimer_enable(&unit->timers[id]);
300 /* These fields must always be read as 0 */
301 value &= ~(GPTIMER_LOAD & GPTIMER_DEBUG_HALT);
303 unit->timers[id].config = value;
304 return;
306 default:
307 break;
312 trace_grlib_gptimer_writel(-1, addr, value);
315 static const MemoryRegionOps grlib_gptimer_ops = {
316 .read = grlib_gptimer_read,
317 .write = grlib_gptimer_write,
318 .endianness = DEVICE_NATIVE_ENDIAN,
319 .valid = {
320 .min_access_size = 4,
321 .max_access_size = 4,
325 static void grlib_gptimer_reset(DeviceState *d)
327 GPTimerUnit *unit = GRLIB_GPTIMER(d);
328 int i = 0;
330 assert(unit != NULL);
332 unit->scaler = 0;
333 unit->reload = 0;
335 unit->config = unit->nr_timers;
336 unit->config |= unit->irq_line << 3;
337 unit->config |= 1 << 8; /* separate interrupt */
338 unit->config |= 1 << 9; /* Disable timer freeze */
341 for (i = 0; i < unit->nr_timers; i++) {
342 GPTimer *timer = &unit->timers[i];
344 timer->counter = 0;
345 timer->reload = 0;
346 timer->config = 0;
347 ptimer_stop(timer->ptimer);
348 ptimer_set_count(timer->ptimer, 0);
349 ptimer_set_freq(timer->ptimer, unit->freq_hz);
353 static void grlib_gptimer_realize(DeviceState *dev, Error **errp)
355 GPTimerUnit *unit = GRLIB_GPTIMER(dev);
356 unsigned int i;
357 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
359 assert(unit->nr_timers > 0);
360 assert(unit->nr_timers <= GPTIMER_MAX_TIMERS);
362 unit->timers = g_malloc0(sizeof unit->timers[0] * unit->nr_timers);
364 for (i = 0; i < unit->nr_timers; i++) {
365 GPTimer *timer = &unit->timers[i];
367 timer->unit = unit;
368 timer->bh = qemu_bh_new(grlib_gptimer_hit, timer);
369 timer->ptimer = ptimer_init(timer->bh, PTIMER_POLICY_DEFAULT);
370 timer->id = i;
372 /* One IRQ line for each timer */
373 sysbus_init_irq(sbd, &timer->irq);
375 ptimer_set_freq(timer->ptimer, unit->freq_hz);
378 memory_region_init_io(&unit->iomem, OBJECT(unit), &grlib_gptimer_ops,
379 unit, "gptimer",
380 UNIT_REG_SIZE + GPTIMER_REG_SIZE * unit->nr_timers);
382 sysbus_init_mmio(sbd, &unit->iomem);
385 static Property grlib_gptimer_properties[] = {
386 DEFINE_PROP_UINT32("frequency", GPTimerUnit, freq_hz, 40000000),
387 DEFINE_PROP_UINT32("irq-line", GPTimerUnit, irq_line, 8),
388 DEFINE_PROP_UINT32("nr-timers", GPTimerUnit, nr_timers, 2),
389 DEFINE_PROP_END_OF_LIST(),
392 static void grlib_gptimer_class_init(ObjectClass *klass, void *data)
394 DeviceClass *dc = DEVICE_CLASS(klass);
396 dc->realize = grlib_gptimer_realize;
397 dc->reset = grlib_gptimer_reset;
398 dc->props = grlib_gptimer_properties;
401 static const TypeInfo grlib_gptimer_info = {
402 .name = TYPE_GRLIB_GPTIMER,
403 .parent = TYPE_SYS_BUS_DEVICE,
404 .instance_size = sizeof(GPTimerUnit),
405 .class_init = grlib_gptimer_class_init,
408 static void grlib_gptimer_register_types(void)
410 type_register_static(&grlib_gptimer_info);
413 type_init(grlib_gptimer_register_types)