6 * Copyright (C) 2016 IBM Corp.
8 * This code is licensed under the GPL version 2 or later. See
9 * the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "hw/ptimer.h"
14 #include "hw/sysbus.h"
15 #include "hw/timer/aspeed_timer.h"
16 #include "qemu-common.h"
17 #include "qemu/bitops.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/timer.h"
23 #define TIMER_NR_REGS 4
25 #define TIMER_CTRL_BITS 4
26 #define TIMER_CTRL_MASK ((1 << TIMER_CTRL_BITS) - 1)
28 #define TIMER_CLOCK_USE_EXT true
29 #define TIMER_CLOCK_EXT_HZ 1000000
30 #define TIMER_CLOCK_USE_APB false
31 #define TIMER_CLOCK_APB_HZ 24000000
33 #define TIMER_REG_STATUS 0
34 #define TIMER_REG_RELOAD 1
35 #define TIMER_REG_MATCH_FIRST 2
36 #define TIMER_REG_MATCH_SECOND 3
38 #define TIMER_FIRST_CAP_PULSE 4
43 op_overflow_interrupt,
48 * Avoid mutual references between AspeedTimerCtrlState and AspeedTimer
49 * structs, as it's a waste of memory. The ptimer BH callback needs to know
50 * whether a specific AspeedTimer is enabled, but this information is held in
51 * AspeedTimerCtrlState. So, provide a helper to hoist ourselves from an
52 * arbitrary AspeedTimer to AspeedTimerCtrlState.
54 static inline AspeedTimerCtrlState *timer_to_ctrl(AspeedTimer *t)
56 const AspeedTimer (*timers)[] = (void *)t - (t->id * sizeof(*t));
57 return container_of(timers, AspeedTimerCtrlState, timers);
60 static inline bool timer_ctrl_status(AspeedTimer *t, enum timer_ctrl_op op)
62 return !!(timer_to_ctrl(t)->ctrl & BIT(t->id * TIMER_CTRL_BITS + op));
65 static inline bool timer_enabled(AspeedTimer *t)
67 return timer_ctrl_status(t, op_enable);
70 static inline bool timer_overflow_interrupt(AspeedTimer *t)
72 return timer_ctrl_status(t, op_overflow_interrupt);
75 static inline bool timer_can_pulse(AspeedTimer *t)
77 return t->id >= TIMER_FIRST_CAP_PULSE;
80 static void aspeed_timer_expire(void *opaque)
82 AspeedTimer *t = opaque;
84 /* Only support interrupts on match values of zero for the moment - this is
85 * sufficient to boot an aspeed_defconfig Linux kernel.
87 * TODO: matching on arbitrary values (see e.g. hw/timer/a9gtimer.c)
89 bool match = !(t->match[0] && t->match[1]);
90 bool interrupt = timer_overflow_interrupt(t) || match;
91 if (timer_enabled(t) && interrupt) {
93 qemu_set_irq(t->irq, t->level);
97 static uint64_t aspeed_timer_get_value(AspeedTimer *t, int reg)
102 case TIMER_REG_STATUS:
103 value = ptimer_get_count(t->timer);
105 case TIMER_REG_RELOAD:
108 case TIMER_REG_MATCH_FIRST:
109 case TIMER_REG_MATCH_SECOND:
110 value = t->match[reg - 2];
113 qemu_log_mask(LOG_UNIMP, "%s: Programming error: unexpected reg: %d\n",
121 static uint64_t aspeed_timer_read(void *opaque, hwaddr offset, unsigned size)
123 AspeedTimerCtrlState *s = opaque;
124 const int reg = (offset & 0xf) / 4;
128 case 0x30: /* Control Register */
131 case 0x34: /* Control Register 2 */
134 case 0x00 ... 0x2c: /* Timers 1 - 4 */
135 value = aspeed_timer_get_value(&s->timers[(offset >> 4)], reg);
137 case 0x40 ... 0x8c: /* Timers 5 - 8 */
138 value = aspeed_timer_get_value(&s->timers[(offset >> 4) - 1], reg);
144 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
149 trace_aspeed_timer_read(offset, size, value);
153 static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg,
158 trace_aspeed_timer_set_value(timer, reg, value);
159 t = &s->timers[timer];
161 case TIMER_REG_STATUS:
162 if (timer_enabled(t)) {
163 ptimer_set_count(t->timer, value);
166 case TIMER_REG_RELOAD:
168 ptimer_set_limit(t->timer, value, 1);
170 case TIMER_REG_MATCH_FIRST:
171 case TIMER_REG_MATCH_SECOND:
173 /* Non-zero match values are unsupported. As such an interrupt will
174 * always be triggered when the timer reaches zero even if the
175 * overflow interrupt control bit is clear.
177 qemu_log_mask(LOG_UNIMP, "%s: Match value unsupported by device: "
178 "0x%" PRIx32 "\n", __func__, value);
180 t->match[reg - 2] = value;
184 qemu_log_mask(LOG_UNIMP, "%s: Programming error: unexpected reg: %d\n",
190 /* Control register operations are broken out into helpers that can be
191 * explicitly called on aspeed_timer_reset(), but also from
192 * aspeed_timer_ctrl_op().
195 static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable)
197 trace_aspeed_timer_ctrl_enable(t->id, enable);
199 ptimer_run(t->timer, 0);
201 ptimer_stop(t->timer);
202 ptimer_set_limit(t->timer, t->reload, 1);
206 static void aspeed_timer_ctrl_external_clock(AspeedTimer *t, bool enable)
208 trace_aspeed_timer_ctrl_external_clock(t->id, enable);
210 ptimer_set_freq(t->timer, TIMER_CLOCK_EXT_HZ);
212 ptimer_set_freq(t->timer, TIMER_CLOCK_APB_HZ);
216 static void aspeed_timer_ctrl_overflow_interrupt(AspeedTimer *t, bool enable)
218 trace_aspeed_timer_ctrl_overflow_interrupt(t->id, enable);
221 static void aspeed_timer_ctrl_pulse_enable(AspeedTimer *t, bool enable)
223 if (timer_can_pulse(t)) {
224 trace_aspeed_timer_ctrl_pulse_enable(t->id, enable);
226 qemu_log_mask(LOG_GUEST_ERROR,
227 "%s: Timer does not support pulse mode\n", __func__);
232 * Given the actions are fixed in number and completely described in helper
233 * functions, dispatch with a lookup table rather than manage control flow with
234 * a switch statement.
236 static void (*const ctrl_ops[])(AspeedTimer *, bool) = {
237 [op_enable] = aspeed_timer_ctrl_enable,
238 [op_external_clock] = aspeed_timer_ctrl_external_clock,
239 [op_overflow_interrupt] = aspeed_timer_ctrl_overflow_interrupt,
240 [op_pulse_enable] = aspeed_timer_ctrl_pulse_enable,
244 * Conditionally affect changes chosen by a timer's control bit.
246 * The aspeed_timer_ctrl_op() interface is convenient for the
247 * aspeed_timer_set_ctrl() function as the "no change" early exit can be
248 * calculated for all operations, which cleans up the caller code. However the
249 * interface isn't convenient for the reset function where we want to enter a
250 * specific state without artificially constructing old and new values that
251 * will fall through the change guard (and motivates extracting the actions
252 * out to helper functions).
254 * @t: The timer to manipulate
255 * @op: The type of operation to be performed
256 * @old: The old state of the timer's control bits
257 * @new: The incoming state for the timer's control bits
259 static void aspeed_timer_ctrl_op(AspeedTimer *t, enum timer_ctrl_op op,
260 uint8_t old, uint8_t new)
262 const uint8_t mask = BIT(op);
263 const bool enable = !!(new & mask);
264 const bool changed = ((old ^ new) & mask);
268 ctrl_ops[op](t, enable);
271 static void aspeed_timer_set_ctrl(AspeedTimerCtrlState *s, uint32_t reg)
275 uint8_t t_old, t_new;
277 const uint8_t enable_mask = BIT(op_enable);
279 /* Handle a dependency between the 'enable' and remaining three
280 * configuration bits - i.e. if more than one bit in the control set has
281 * changed, including the 'enable' bit, then we want either disable the
282 * timer and perform configuration, or perform configuration and then
285 for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
287 shift = (i * TIMER_CTRL_BITS);
288 t_old = (s->ctrl >> shift) & TIMER_CTRL_MASK;
289 t_new = (reg >> shift) & TIMER_CTRL_MASK;
291 /* If we are disabling, do so first */
292 if ((t_old & enable_mask) && !(t_new & enable_mask)) {
293 aspeed_timer_ctrl_enable(t, false);
295 aspeed_timer_ctrl_op(t, op_external_clock, t_old, t_new);
296 aspeed_timer_ctrl_op(t, op_overflow_interrupt, t_old, t_new);
297 aspeed_timer_ctrl_op(t, op_pulse_enable, t_old, t_new);
298 /* If we are enabling, do so last */
299 if (!(t_old & enable_mask) && (t_new & enable_mask)) {
300 aspeed_timer_ctrl_enable(t, true);
306 static void aspeed_timer_set_ctrl2(AspeedTimerCtrlState *s, uint32_t value)
308 trace_aspeed_timer_set_ctrl2(value);
311 static void aspeed_timer_write(void *opaque, hwaddr offset, uint64_t value,
314 const uint32_t tv = (uint32_t)(value & 0xFFFFFFFF);
315 const int reg = (offset & 0xf) / 4;
316 AspeedTimerCtrlState *s = opaque;
319 /* Control Registers */
321 aspeed_timer_set_ctrl(s, tv);
324 aspeed_timer_set_ctrl2(s, tv);
326 /* Timer Registers */
328 aspeed_timer_set_value(s, (offset >> TIMER_NR_REGS), reg, tv);
331 aspeed_timer_set_value(s, (offset >> TIMER_NR_REGS) - 1, reg, tv);
337 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
343 static const MemoryRegionOps aspeed_timer_ops = {
344 .read = aspeed_timer_read,
345 .write = aspeed_timer_write,
346 .endianness = DEVICE_LITTLE_ENDIAN,
347 .valid.min_access_size = 4,
348 .valid.max_access_size = 4,
349 .valid.unaligned = false,
352 static void aspeed_init_one_timer(AspeedTimerCtrlState *s, uint8_t id)
355 AspeedTimer *t = &s->timers[id];
358 bh = qemu_bh_new(aspeed_timer_expire, t);
359 t->timer = ptimer_init(bh);
362 static void aspeed_timer_realize(DeviceState *dev, Error **errp)
365 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
366 AspeedTimerCtrlState *s = ASPEED_TIMER(dev);
368 for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
369 aspeed_init_one_timer(s, i);
370 sysbus_init_irq(sbd, &s->timers[i].irq);
372 memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_timer_ops, s,
373 TYPE_ASPEED_TIMER, 0x1000);
374 sysbus_init_mmio(sbd, &s->iomem);
377 static void aspeed_timer_reset(DeviceState *dev)
380 AspeedTimerCtrlState *s = ASPEED_TIMER(dev);
382 for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
383 AspeedTimer *t = &s->timers[i];
384 /* Explicitly call helpers to avoid any conditional behaviour through
385 * aspeed_timer_set_ctrl().
387 aspeed_timer_ctrl_enable(t, false);
388 aspeed_timer_ctrl_external_clock(t, TIMER_CLOCK_USE_APB);
389 aspeed_timer_ctrl_overflow_interrupt(t, false);
390 aspeed_timer_ctrl_pulse_enable(t, false);
400 static const VMStateDescription vmstate_aspeed_timer = {
401 .name = "aspeed.timer",
403 .minimum_version_id = 1,
404 .fields = (VMStateField[]) {
405 VMSTATE_UINT8(id, AspeedTimer),
406 VMSTATE_INT32(level, AspeedTimer),
407 VMSTATE_PTIMER(timer, AspeedTimer),
408 VMSTATE_UINT32(reload, AspeedTimer),
409 VMSTATE_UINT32_ARRAY(match, AspeedTimer, 2),
410 VMSTATE_END_OF_LIST()
414 static const VMStateDescription vmstate_aspeed_timer_state = {
415 .name = "aspeed.timerctrl",
417 .minimum_version_id = 1,
418 .fields = (VMStateField[]) {
419 VMSTATE_UINT32(ctrl, AspeedTimerCtrlState),
420 VMSTATE_UINT32(ctrl2, AspeedTimerCtrlState),
421 VMSTATE_STRUCT_ARRAY(timers, AspeedTimerCtrlState,
422 ASPEED_TIMER_NR_TIMERS, 1, vmstate_aspeed_timer,
424 VMSTATE_END_OF_LIST()
428 static void timer_class_init(ObjectClass *klass, void *data)
430 DeviceClass *dc = DEVICE_CLASS(klass);
432 dc->realize = aspeed_timer_realize;
433 dc->reset = aspeed_timer_reset;
434 dc->desc = "ASPEED Timer";
435 dc->vmsd = &vmstate_aspeed_timer_state;
438 static const TypeInfo aspeed_timer_info = {
439 .name = TYPE_ASPEED_TIMER,
440 .parent = TYPE_SYS_BUS_DEVICE,
441 .instance_size = sizeof(AspeedTimerCtrlState),
442 .class_init = timer_class_init,
445 static void aspeed_timer_register_types(void)
447 type_register_static(&aspeed_timer_info);
450 type_init(aspeed_timer_register_types)