4 * Andrew Jeffery <andrew@aj.id.au>
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 "qapi/error.h"
15 #include "hw/sysbus.h"
16 #include "hw/timer/aspeed_timer.h"
17 #include "migration/vmstate.h"
18 #include "qemu/bitops.h"
19 #include "qemu/timer.h"
21 #include "qemu/module.h"
22 #include "hw/qdev-properties.h"
25 #define TIMER_NR_REGS 4
27 #define TIMER_CTRL_BITS 4
28 #define TIMER_CTRL_MASK ((1 << TIMER_CTRL_BITS) - 1)
30 #define TIMER_CLOCK_USE_EXT true
31 #define TIMER_CLOCK_EXT_HZ 1000000
32 #define TIMER_CLOCK_USE_APB false
34 #define TIMER_REG_STATUS 0
35 #define TIMER_REG_RELOAD 1
36 #define TIMER_REG_MATCH_FIRST 2
37 #define TIMER_REG_MATCH_SECOND 3
39 #define TIMER_FIRST_CAP_PULSE 4
44 op_overflow_interrupt
,
49 * Minimum value of the reload register to filter out short period
50 * timers which have a noticeable impact in emulation. 5us should be
51 * enough, use 20us for "safety".
53 #define TIMER_MIN_NS (20 * SCALE_US)
56 * Avoid mutual references between AspeedTimerCtrlState and AspeedTimer
57 * structs, as it's a waste of memory. The ptimer BH callback needs to know
58 * whether a specific AspeedTimer is enabled, but this information is held in
59 * AspeedTimerCtrlState. So, provide a helper to hoist ourselves from an
60 * arbitrary AspeedTimer to AspeedTimerCtrlState.
62 static inline AspeedTimerCtrlState
*timer_to_ctrl(AspeedTimer
*t
)
64 const AspeedTimer (*timers
)[] = (void *)t
- (t
->id
* sizeof(*t
));
65 return container_of(timers
, AspeedTimerCtrlState
, timers
);
68 static inline bool timer_ctrl_status(AspeedTimer
*t
, enum timer_ctrl_op op
)
70 return !!(timer_to_ctrl(t
)->ctrl
& BIT(t
->id
* TIMER_CTRL_BITS
+ op
));
73 static inline bool timer_enabled(AspeedTimer
*t
)
75 return timer_ctrl_status(t
, op_enable
);
78 static inline bool timer_overflow_interrupt(AspeedTimer
*t
)
80 return timer_ctrl_status(t
, op_overflow_interrupt
);
83 static inline bool timer_can_pulse(AspeedTimer
*t
)
85 return t
->id
>= TIMER_FIRST_CAP_PULSE
;
88 static inline bool timer_external_clock(AspeedTimer
*t
)
90 return timer_ctrl_status(t
, op_external_clock
);
93 static inline uint32_t calculate_rate(struct AspeedTimer
*t
)
95 AspeedTimerCtrlState
*s
= timer_to_ctrl(t
);
97 return timer_external_clock(t
) ? TIMER_CLOCK_EXT_HZ
:
98 aspeed_scu_get_apb_freq(s
->scu
);
101 static inline uint32_t calculate_ticks(struct AspeedTimer
*t
, uint64_t now_ns
)
103 uint64_t delta_ns
= now_ns
- MIN(now_ns
, t
->start
);
104 uint32_t rate
= calculate_rate(t
);
105 uint64_t ticks
= muldiv64(delta_ns
, rate
, NANOSECONDS_PER_SECOND
);
107 return t
->reload
- MIN(t
->reload
, ticks
);
110 static uint32_t calculate_min_ticks(AspeedTimer
*t
, uint32_t value
)
112 uint32_t rate
= calculate_rate(t
);
113 uint32_t min_ticks
= muldiv64(TIMER_MIN_NS
, rate
, NANOSECONDS_PER_SECOND
);
115 return value
< min_ticks
? min_ticks
: value
;
118 static inline uint64_t calculate_time(struct AspeedTimer
*t
, uint32_t ticks
)
121 uint64_t delta_ticks
;
123 delta_ticks
= t
->reload
- MIN(t
->reload
, ticks
);
124 delta_ns
= muldiv64(delta_ticks
, NANOSECONDS_PER_SECOND
, calculate_rate(t
));
126 return t
->start
+ delta_ns
;
129 static inline uint32_t calculate_match(struct AspeedTimer
*t
, int i
)
131 return t
->match
[i
] < t
->reload
? t
->match
[i
] : 0;
134 static uint64_t calculate_next(struct AspeedTimer
*t
)
136 uint64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
140 * We don't know the relationship between the values in the match
141 * registers, so sort using MAX/MIN/zero. We sort in that order as
142 * the timer counts down to zero.
145 next
= calculate_time(t
, MAX(calculate_match(t
, 0), calculate_match(t
, 1)));
150 next
= calculate_time(t
, MIN(calculate_match(t
, 0), calculate_match(t
, 1)));
155 next
= calculate_time(t
, 0);
160 /* We've missed all deadlines, fire interrupt and try again */
161 timer_del(&t
->timer
);
163 if (timer_overflow_interrupt(t
)) {
164 AspeedTimerCtrlState
*s
= timer_to_ctrl(t
);
165 t
->level
= !t
->level
;
166 s
->irq_sts
|= BIT(t
->id
);
167 qemu_set_irq(t
->irq
, t
->level
);
170 next
= MAX(MAX(calculate_match(t
, 0), calculate_match(t
, 1)), 0);
171 t
->start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
173 return calculate_time(t
, next
);
176 static void aspeed_timer_mod(AspeedTimer
*t
)
178 uint64_t next
= calculate_next(t
);
180 timer_mod(&t
->timer
, next
);
184 static void aspeed_timer_expire(void *opaque
)
186 AspeedTimer
*t
= opaque
;
187 bool interrupt
= false;
190 if (!timer_enabled(t
)) {
194 ticks
= calculate_ticks(t
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
197 interrupt
= timer_overflow_interrupt(t
) || !t
->match
[0] || !t
->match
[1];
198 } else if (ticks
<= MIN(t
->match
[0], t
->match
[1])) {
200 } else if (ticks
<= MAX(t
->match
[0], t
->match
[1])) {
205 AspeedTimerCtrlState
*s
= timer_to_ctrl(t
);
206 t
->level
= !t
->level
;
207 s
->irq_sts
|= BIT(t
->id
);
208 qemu_set_irq(t
->irq
, t
->level
);
214 static uint64_t aspeed_timer_get_value(AspeedTimer
*t
, int reg
)
219 case TIMER_REG_STATUS
:
220 if (timer_enabled(t
)) {
221 value
= calculate_ticks(t
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
226 case TIMER_REG_RELOAD
:
229 case TIMER_REG_MATCH_FIRST
:
230 case TIMER_REG_MATCH_SECOND
:
231 value
= t
->match
[reg
- 2];
234 qemu_log_mask(LOG_UNIMP
, "%s: Programming error: unexpected reg: %d\n",
242 static uint64_t aspeed_timer_read(void *opaque
, hwaddr offset
, unsigned size
)
244 AspeedTimerCtrlState
*s
= opaque
;
245 const int reg
= (offset
& 0xf) / 4;
249 case 0x30: /* Control Register */
252 case 0x00 ... 0x2c: /* Timers 1 - 4 */
253 value
= aspeed_timer_get_value(&s
->timers
[(offset
>> 4)], reg
);
255 case 0x40 ... 0x8c: /* Timers 5 - 8 */
256 value
= aspeed_timer_get_value(&s
->timers
[(offset
>> 4) - 1], reg
);
259 value
= ASPEED_TIMER_GET_CLASS(s
)->read(s
, offset
);
262 trace_aspeed_timer_read(offset
, size
, value
);
266 static void aspeed_timer_set_value(AspeedTimerCtrlState
*s
, int timer
, int reg
,
272 trace_aspeed_timer_set_value(timer
, reg
, value
);
273 t
= &s
->timers
[timer
];
275 case TIMER_REG_RELOAD
:
276 old_reload
= t
->reload
;
277 t
->reload
= calculate_min_ticks(t
, value
);
279 /* If the reload value was not previously set, or zero, and
280 * the current value is valid, try to start the timer if it is
283 if (old_reload
|| !t
->reload
) {
286 /* fall through to re-enable */
287 case TIMER_REG_STATUS
:
288 if (timer_enabled(t
)) {
289 uint64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
290 int64_t delta
= (int64_t) value
- (int64_t) calculate_ticks(t
, now
);
291 uint32_t rate
= calculate_rate(t
);
294 t
->start
+= muldiv64(delta
, NANOSECONDS_PER_SECOND
, rate
);
296 t
->start
-= muldiv64(-delta
, NANOSECONDS_PER_SECOND
, rate
);
301 case TIMER_REG_MATCH_FIRST
:
302 case TIMER_REG_MATCH_SECOND
:
303 t
->match
[reg
- 2] = value
;
304 if (timer_enabled(t
)) {
309 qemu_log_mask(LOG_UNIMP
, "%s: Programming error: unexpected reg: %d\n",
315 /* Control register operations are broken out into helpers that can be
316 * explicitly called on aspeed_timer_reset(), but also from
317 * aspeed_timer_ctrl_op().
320 static void aspeed_timer_ctrl_enable(AspeedTimer
*t
, bool enable
)
322 trace_aspeed_timer_ctrl_enable(t
->id
, enable
);
324 t
->start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
327 timer_del(&t
->timer
);
331 static void aspeed_timer_ctrl_external_clock(AspeedTimer
*t
, bool enable
)
333 trace_aspeed_timer_ctrl_external_clock(t
->id
, enable
);
336 static void aspeed_timer_ctrl_overflow_interrupt(AspeedTimer
*t
, bool enable
)
338 trace_aspeed_timer_ctrl_overflow_interrupt(t
->id
, enable
);
341 static void aspeed_timer_ctrl_pulse_enable(AspeedTimer
*t
, bool enable
)
343 if (timer_can_pulse(t
)) {
344 trace_aspeed_timer_ctrl_pulse_enable(t
->id
, enable
);
346 qemu_log_mask(LOG_GUEST_ERROR
,
347 "%s: Timer does not support pulse mode\n", __func__
);
352 * Given the actions are fixed in number and completely described in helper
353 * functions, dispatch with a lookup table rather than manage control flow with
354 * a switch statement.
356 static void (*const ctrl_ops
[])(AspeedTimer
*, bool) = {
357 [op_enable
] = aspeed_timer_ctrl_enable
,
358 [op_external_clock
] = aspeed_timer_ctrl_external_clock
,
359 [op_overflow_interrupt
] = aspeed_timer_ctrl_overflow_interrupt
,
360 [op_pulse_enable
] = aspeed_timer_ctrl_pulse_enable
,
364 * Conditionally affect changes chosen by a timer's control bit.
366 * The aspeed_timer_ctrl_op() interface is convenient for the
367 * aspeed_timer_set_ctrl() function as the "no change" early exit can be
368 * calculated for all operations, which cleans up the caller code. However the
369 * interface isn't convenient for the reset function where we want to enter a
370 * specific state without artificially constructing old and new values that
371 * will fall through the change guard (and motivates extracting the actions
372 * out to helper functions).
374 * @t: The timer to manipulate
375 * @op: The type of operation to be performed
376 * @old: The old state of the timer's control bits
377 * @new: The incoming state for the timer's control bits
379 static void aspeed_timer_ctrl_op(AspeedTimer
*t
, enum timer_ctrl_op op
,
380 uint8_t old
, uint8_t new)
382 const uint8_t mask
= BIT(op
);
383 const bool enable
= !!(new & mask
);
384 const bool changed
= ((old
^ new) & mask
);
388 ctrl_ops
[op
](t
, enable
);
391 static void aspeed_timer_set_ctrl(AspeedTimerCtrlState
*s
, uint32_t reg
)
395 uint8_t t_old
, t_new
;
397 const uint8_t enable_mask
= BIT(op_enable
);
399 /* Handle a dependency between the 'enable' and remaining three
400 * configuration bits - i.e. if more than one bit in the control set has
401 * changed, including the 'enable' bit, then we want either disable the
402 * timer and perform configuration, or perform configuration and then
405 for (i
= 0; i
< ASPEED_TIMER_NR_TIMERS
; i
++) {
407 shift
= (i
* TIMER_CTRL_BITS
);
408 t_old
= (s
->ctrl
>> shift
) & TIMER_CTRL_MASK
;
409 t_new
= (reg
>> shift
) & TIMER_CTRL_MASK
;
411 /* If we are disabling, do so first */
412 if ((t_old
& enable_mask
) && !(t_new
& enable_mask
)) {
413 aspeed_timer_ctrl_enable(t
, false);
415 aspeed_timer_ctrl_op(t
, op_external_clock
, t_old
, t_new
);
416 aspeed_timer_ctrl_op(t
, op_overflow_interrupt
, t_old
, t_new
);
417 aspeed_timer_ctrl_op(t
, op_pulse_enable
, t_old
, t_new
);
418 /* If we are enabling, do so last */
419 if (!(t_old
& enable_mask
) && (t_new
& enable_mask
)) {
420 aspeed_timer_ctrl_enable(t
, true);
426 static void aspeed_timer_set_ctrl2(AspeedTimerCtrlState
*s
, uint32_t value
)
428 trace_aspeed_timer_set_ctrl2(value
);
431 static void aspeed_timer_write(void *opaque
, hwaddr offset
, uint64_t value
,
434 const uint32_t tv
= (uint32_t)(value
& 0xFFFFFFFF);
435 const int reg
= (offset
& 0xf) / 4;
436 AspeedTimerCtrlState
*s
= opaque
;
439 /* Control Registers */
441 aspeed_timer_set_ctrl(s
, tv
);
443 /* Timer Registers */
445 aspeed_timer_set_value(s
, (offset
>> TIMER_NR_REGS
), reg
, tv
);
448 aspeed_timer_set_value(s
, (offset
>> TIMER_NR_REGS
) - 1, reg
, tv
);
451 ASPEED_TIMER_GET_CLASS(s
)->write(s
, offset
, value
);
456 static const MemoryRegionOps aspeed_timer_ops
= {
457 .read
= aspeed_timer_read
,
458 .write
= aspeed_timer_write
,
459 .endianness
= DEVICE_LITTLE_ENDIAN
,
460 .valid
.min_access_size
= 4,
461 .valid
.max_access_size
= 4,
462 .valid
.unaligned
= false,
465 static uint64_t aspeed_2400_timer_read(AspeedTimerCtrlState
*s
, hwaddr offset
)
476 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIx
"\n",
484 static void aspeed_2400_timer_write(AspeedTimerCtrlState
*s
, hwaddr offset
,
487 const uint32_t tv
= (uint32_t)(value
& 0xFFFFFFFF);
491 aspeed_timer_set_ctrl2(s
, tv
);
496 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIx
"\n",
502 static uint64_t aspeed_2500_timer_read(AspeedTimerCtrlState
*s
, hwaddr offset
)
511 value
= s
->ctrl3
& BIT(0);
515 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIx
"\n",
523 static void aspeed_2500_timer_write(AspeedTimerCtrlState
*s
, hwaddr offset
,
526 const uint32_t tv
= (uint32_t)(value
& 0xFFFFFFFF);
531 aspeed_timer_set_ctrl2(s
, tv
);
534 command
= (value
>> 1) & 0xFF;
535 if (command
== 0xAE) {
537 } else if (command
== 0xEA) {
542 if (s
->ctrl3
& BIT(0)) {
543 aspeed_timer_set_ctrl(s
, s
->ctrl
& ~tv
);
548 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIx
"\n",
554 static uint64_t aspeed_2600_timer_read(AspeedTimerCtrlState
*s
, hwaddr offset
)
565 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIx
"\n",
573 static void aspeed_2600_timer_write(AspeedTimerCtrlState
*s
, hwaddr offset
,
576 const uint32_t tv
= (uint32_t)(value
& 0xFFFFFFFF);
583 aspeed_timer_set_ctrl(s
, s
->ctrl
& ~tv
);
588 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIx
"\n",
594 static void aspeed_init_one_timer(AspeedTimerCtrlState
*s
, uint8_t id
)
596 AspeedTimer
*t
= &s
->timers
[id
];
599 timer_init_ns(&t
->timer
, QEMU_CLOCK_VIRTUAL
, aspeed_timer_expire
, t
);
602 static void aspeed_timer_realize(DeviceState
*dev
, Error
**errp
)
605 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
606 AspeedTimerCtrlState
*s
= ASPEED_TIMER(dev
);
610 for (i
= 0; i
< ASPEED_TIMER_NR_TIMERS
; i
++) {
611 aspeed_init_one_timer(s
, i
);
612 sysbus_init_irq(sbd
, &s
->timers
[i
].irq
);
614 memory_region_init_io(&s
->iomem
, OBJECT(s
), &aspeed_timer_ops
, s
,
615 TYPE_ASPEED_TIMER
, 0x1000);
616 sysbus_init_mmio(sbd
, &s
->iomem
);
619 static void aspeed_timer_reset(DeviceState
*dev
)
622 AspeedTimerCtrlState
*s
= ASPEED_TIMER(dev
);
624 for (i
= 0; i
< ASPEED_TIMER_NR_TIMERS
; i
++) {
625 AspeedTimer
*t
= &s
->timers
[i
];
626 /* Explicitly call helpers to avoid any conditional behaviour through
627 * aspeed_timer_set_ctrl().
629 aspeed_timer_ctrl_enable(t
, false);
630 aspeed_timer_ctrl_external_clock(t
, TIMER_CLOCK_USE_APB
);
631 aspeed_timer_ctrl_overflow_interrupt(t
, false);
632 aspeed_timer_ctrl_pulse_enable(t
, false);
644 static const VMStateDescription vmstate_aspeed_timer
= {
645 .name
= "aspeed.timer",
647 .minimum_version_id
= 2,
648 .fields
= (VMStateField
[]) {
649 VMSTATE_UINT8(id
, AspeedTimer
),
650 VMSTATE_INT32(level
, AspeedTimer
),
651 VMSTATE_TIMER(timer
, AspeedTimer
),
652 VMSTATE_UINT32(reload
, AspeedTimer
),
653 VMSTATE_UINT32_ARRAY(match
, AspeedTimer
, 2),
654 VMSTATE_END_OF_LIST()
658 static const VMStateDescription vmstate_aspeed_timer_state
= {
659 .name
= "aspeed.timerctrl",
661 .minimum_version_id
= 2,
662 .fields
= (VMStateField
[]) {
663 VMSTATE_UINT32(ctrl
, AspeedTimerCtrlState
),
664 VMSTATE_UINT32(ctrl2
, AspeedTimerCtrlState
),
665 VMSTATE_UINT32(ctrl3
, AspeedTimerCtrlState
),
666 VMSTATE_UINT32(irq_sts
, AspeedTimerCtrlState
),
667 VMSTATE_STRUCT_ARRAY(timers
, AspeedTimerCtrlState
,
668 ASPEED_TIMER_NR_TIMERS
, 1, vmstate_aspeed_timer
,
670 VMSTATE_END_OF_LIST()
674 static Property aspeed_timer_properties
[] = {
675 DEFINE_PROP_LINK("scu", AspeedTimerCtrlState
, scu
, TYPE_ASPEED_SCU
,
677 DEFINE_PROP_END_OF_LIST(),
680 static void timer_class_init(ObjectClass
*klass
, void *data
)
682 DeviceClass
*dc
= DEVICE_CLASS(klass
);
684 dc
->realize
= aspeed_timer_realize
;
685 dc
->reset
= aspeed_timer_reset
;
686 dc
->desc
= "ASPEED Timer";
687 dc
->vmsd
= &vmstate_aspeed_timer_state
;
688 device_class_set_props(dc
, aspeed_timer_properties
);
691 static const TypeInfo aspeed_timer_info
= {
692 .name
= TYPE_ASPEED_TIMER
,
693 .parent
= TYPE_SYS_BUS_DEVICE
,
694 .instance_size
= sizeof(AspeedTimerCtrlState
),
695 .class_init
= timer_class_init
,
696 .class_size
= sizeof(AspeedTimerClass
),
700 static void aspeed_2400_timer_class_init(ObjectClass
*klass
, void *data
)
702 DeviceClass
*dc
= DEVICE_CLASS(klass
);
703 AspeedTimerClass
*awc
= ASPEED_TIMER_CLASS(klass
);
705 dc
->desc
= "ASPEED 2400 Timer";
706 awc
->read
= aspeed_2400_timer_read
;
707 awc
->write
= aspeed_2400_timer_write
;
710 static const TypeInfo aspeed_2400_timer_info
= {
711 .name
= TYPE_ASPEED_2400_TIMER
,
712 .parent
= TYPE_ASPEED_TIMER
,
713 .class_init
= aspeed_2400_timer_class_init
,
716 static void aspeed_2500_timer_class_init(ObjectClass
*klass
, void *data
)
718 DeviceClass
*dc
= DEVICE_CLASS(klass
);
719 AspeedTimerClass
*awc
= ASPEED_TIMER_CLASS(klass
);
721 dc
->desc
= "ASPEED 2500 Timer";
722 awc
->read
= aspeed_2500_timer_read
;
723 awc
->write
= aspeed_2500_timer_write
;
726 static const TypeInfo aspeed_2500_timer_info
= {
727 .name
= TYPE_ASPEED_2500_TIMER
,
728 .parent
= TYPE_ASPEED_TIMER
,
729 .class_init
= aspeed_2500_timer_class_init
,
732 static void aspeed_2600_timer_class_init(ObjectClass
*klass
, void *data
)
734 DeviceClass
*dc
= DEVICE_CLASS(klass
);
735 AspeedTimerClass
*awc
= ASPEED_TIMER_CLASS(klass
);
737 dc
->desc
= "ASPEED 2600 Timer";
738 awc
->read
= aspeed_2600_timer_read
;
739 awc
->write
= aspeed_2600_timer_write
;
742 static const TypeInfo aspeed_2600_timer_info
= {
743 .name
= TYPE_ASPEED_2600_TIMER
,
744 .parent
= TYPE_ASPEED_TIMER
,
745 .class_init
= aspeed_2600_timer_class_init
,
748 static void aspeed_timer_register_types(void)
750 type_register_static(&aspeed_timer_info
);
751 type_register_static(&aspeed_2400_timer_info
);
752 type_register_static(&aspeed_2500_timer_info
);
753 type_register_static(&aspeed_2600_timer_info
);
756 type_init(aspeed_timer_register_types
)