1 /*
2 * ASPEED AST2400 Timer
3 *
4 * Andrew Jeffery <andrew@aj.id.au>
5 *
6 * Copyright (C) 2016 IBM Corp.
7 *
8 * This code is licensed under the GPL version 2 or later. See
9 * the COPYING file in the top-level directory.
10 */
11
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "hw/irq.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"
20 #include "qemu/log.h"
21 #include "qemu/module.h"
22 #include "hw/qdev-properties.h"
23 #include "trace.h"
24
25 #define TIMER_NR_REGS 4
26
27 #define TIMER_CTRL_BITS 4
28 #define TIMER_CTRL_MASK ((1 << TIMER_CTRL_BITS) - 1)
29
30 #define TIMER_CLOCK_USE_EXT true
31 #define TIMER_CLOCK_EXT_HZ 1000000
32 #define TIMER_CLOCK_USE_APB false
33
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
38
39 #define TIMER_FIRST_CAP_PULSE 4
40
41 enum timer_ctrl_op {
42 op_enable = 0,
43 op_external_clock,
44 op_overflow_interrupt,
45 op_pulse_enable
46 };
47
48 /*
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".
52 */
53 #define TIMER_MIN_NS (20 * SCALE_US)
54
55 /**
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.
61 */
timer_to_ctrl(AspeedTimer * t)62 static inline AspeedTimerCtrlState *timer_to_ctrl(AspeedTimer *t)
63 {
64 const AspeedTimer (*timers)[] = (void *)t - (t->id * sizeof(*t));
65 return container_of(timers, AspeedTimerCtrlState, timers);
66 }
67
timer_ctrl_status(AspeedTimer * t,enum timer_ctrl_op op)68 static inline bool timer_ctrl_status(AspeedTimer *t, enum timer_ctrl_op op)
69 {
70 return !!(timer_to_ctrl(t)->ctrl & BIT(t->id * TIMER_CTRL_BITS + op));
71 }
72
timer_enabled(AspeedTimer * t)73 static inline bool timer_enabled(AspeedTimer *t)
74 {
75 return timer_ctrl_status(t, op_enable);
76 }
77
timer_overflow_interrupt(AspeedTimer * t)78 static inline bool timer_overflow_interrupt(AspeedTimer *t)
79 {
80 return timer_ctrl_status(t, op_overflow_interrupt);
81 }
82
timer_can_pulse(AspeedTimer * t)83 static inline bool timer_can_pulse(AspeedTimer *t)
84 {
85 return t->id >= TIMER_FIRST_CAP_PULSE;
86 }
87
timer_external_clock(AspeedTimer * t)88 static inline bool timer_external_clock(AspeedTimer *t)
89 {
90 return timer_ctrl_status(t, op_external_clock);
91 }
92
calculate_rate(struct AspeedTimer * t)93 static inline uint32_t calculate_rate(struct AspeedTimer *t)
94 {
95 AspeedTimerCtrlState *s = timer_to_ctrl(t);
96
97 return timer_external_clock(t) ? TIMER_CLOCK_EXT_HZ :
98 aspeed_scu_get_apb_freq(s->scu);
99 }
100
calculate_ticks(struct AspeedTimer * t,uint64_t now_ns)101 static inline uint32_t calculate_ticks(struct AspeedTimer *t, uint64_t now_ns)
102 {
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);
106
107 return t->reload - MIN(t->reload, ticks);
108 }
109
calculate_min_ticks(AspeedTimer * t,uint32_t value)110 static uint32_t calculate_min_ticks(AspeedTimer *t, uint32_t value)
111 {
112 uint32_t rate = calculate_rate(t);
113 uint32_t min_ticks = muldiv64(TIMER_MIN_NS, rate, NANOSECONDS_PER_SECOND);
114
115 return value < min_ticks ? min_ticks : value;
116 }
117
calculate_time(struct AspeedTimer * t,uint32_t ticks)118 static inline uint64_t calculate_time(struct AspeedTimer *t, uint32_t ticks)
119 {
120 uint64_t delta_ns;
121 uint64_t delta_ticks;
122
123 delta_ticks = t->reload - MIN(t->reload, ticks);
124 delta_ns = muldiv64(delta_ticks, NANOSECONDS_PER_SECOND, calculate_rate(t));
125
126 return t->start + delta_ns;
127 }
128
calculate_match(struct AspeedTimer * t,int i)129 static inline uint32_t calculate_match(struct AspeedTimer *t, int i)
130 {
131 return t->match[i] < t->reload ? t->match[i] : 0;
132 }
133
calculate_next(struct AspeedTimer * t)134 static uint64_t calculate_next(struct AspeedTimer *t)
135 {
136 uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
137 uint64_t next;
138
139 /*
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.
143 */
144
145 next = calculate_time(t, MAX(calculate_match(t, 0), calculate_match(t, 1)));
146 if (now < next) {
147 return next;
148 }
149
150 next = calculate_time(t, MIN(calculate_match(t, 0), calculate_match(t, 1)));
151 if (now < next) {
152 return next;
153 }
154
155 next = calculate_time(t, 0);
156 if (now < next) {
157 return next;
158 }
159
160 /* We've missed all deadlines, fire interrupt and try again */
161 timer_del(&t->timer);
162
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);
168 }
169
170 next = MAX(calculate_match(t, 0), calculate_match(t, 1));
171 t->start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
172
173 return calculate_time(t, next);
174 }
175
aspeed_timer_mod(AspeedTimer * t)176 static void aspeed_timer_mod(AspeedTimer *t)
177 {
178 uint64_t next = calculate_next(t);
179 if (next) {
180 timer_mod(&t->timer, next);
181 }
182 }
183
aspeed_timer_expire(void * opaque)184 static void aspeed_timer_expire(void *opaque)
185 {
186 AspeedTimer *t = opaque;
187 bool interrupt = false;
188 uint32_t ticks;
189
190 if (!timer_enabled(t)) {
191 return;
192 }
193
194 ticks = calculate_ticks(t, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
195
196 if (!ticks) {
197 interrupt = timer_overflow_interrupt(t) || !t->match[0] || !t->match[1];
198 } else if (ticks <= MIN(t->match[0], t->match[1])) {
199 interrupt = true;
200 } else if (ticks <= MAX(t->match[0], t->match[1])) {
201 interrupt = true;
202 }
203
204 if (interrupt) {
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);
209 }
210
211 aspeed_timer_mod(t);
212 }
213
aspeed_timer_get_value(AspeedTimer * t,int reg)214 static uint64_t aspeed_timer_get_value(AspeedTimer *t, int reg)
215 {
216 uint64_t value;
217
218 switch (reg) {
219 case TIMER_REG_STATUS:
220 if (timer_enabled(t)) {
221 value = calculate_ticks(t, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
222 } else {
223 value = t->reload;
224 }
225 break;
226 case TIMER_REG_RELOAD:
227 value = t->reload;
228 break;
229 case TIMER_REG_MATCH_FIRST:
230 case TIMER_REG_MATCH_SECOND:
231 value = t->match[reg - 2];
232 break;
233 default:
234 qemu_log_mask(LOG_UNIMP, "%s: Programming error: unexpected reg: %d\n",
235 __func__, reg);
236 value = 0;
237 break;
238 }
239 return value;
240 }
241
aspeed_timer_read(void * opaque,hwaddr offset,unsigned size)242 static uint64_t aspeed_timer_read(void *opaque, hwaddr offset, unsigned size)
243 {
244 AspeedTimerCtrlState *s = opaque;
245 const int reg = (offset & 0xf) / 4;
246 uint64_t value;
247
248 switch (offset) {
249 case 0x30: /* Control Register */
250 value = s->ctrl;
251 break;
252 case 0x00 ... 0x2c: /* Timers 1 - 4 */
253 value = aspeed_timer_get_value(&s->timers[(offset >> 4)], reg);
254 break;
255 case 0x40 ... 0x8c: /* Timers 5 - 8 */
256 value = aspeed_timer_get_value(&s->timers[(offset >> 4) - 1], reg);
257 break;
258 default:
259 value = ASPEED_TIMER_GET_CLASS(s)->read(s, offset);
260 break;
261 }
262 trace_aspeed_timer_read(offset, size, value);
263 return value;
264 }
265
aspeed_timer_set_value(AspeedTimerCtrlState * s,int timer,int reg,uint32_t value)266 static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg,
267 uint32_t value)
268 {
269 AspeedTimer *t;
270 uint32_t old_reload;
271
272 trace_aspeed_timer_set_value(timer, reg, value);
273 t = &s->timers[timer];
274 switch (reg) {
275 case TIMER_REG_RELOAD:
276 old_reload = t->reload;
277 t->reload = calculate_min_ticks(t, value);
278
279 /*
280 * If the reload value was not previously set, or zero, and
281 * the current value is valid, try to start the timer if it is
282 * enabled.
283 */
284 if (old_reload || !t->reload) {
285 break;
286 }
287 /* fall through to re-enable */
288 case TIMER_REG_STATUS:
289 if (timer_enabled(t)) {
290 uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
291 int64_t delta = (int64_t) value - (int64_t) calculate_ticks(t, now);
292 uint32_t rate = calculate_rate(t);
293
294 if (delta >= 0) {
295 t->start += muldiv64(delta, NANOSECONDS_PER_SECOND, rate);
296 } else {
297 t->start -= muldiv64(-delta, NANOSECONDS_PER_SECOND, rate);
298 }
299 aspeed_timer_mod(t);
300 }
301 break;
302 case TIMER_REG_MATCH_FIRST:
303 case TIMER_REG_MATCH_SECOND:
304 t->match[reg - 2] = value;
305 if (timer_enabled(t)) {
306 aspeed_timer_mod(t);
307 }
308 break;
309 default:
310 qemu_log_mask(LOG_UNIMP, "%s: Programming error: unexpected reg: %d\n",
311 __func__, reg);
312 break;
313 }
314 }
315
316 /*
317 * Control register operations are broken out into helpers that can be
318 * explicitly called on aspeed_timer_reset(), but also from
319 * aspeed_timer_ctrl_op().
320 */
321
aspeed_timer_ctrl_enable(AspeedTimer * t,bool enable)322 static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable)
323 {
324 trace_aspeed_timer_ctrl_enable(t->id, enable);
325 if (enable) {
326 t->start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
327 aspeed_timer_mod(t);
328 } else {
329 timer_del(&t->timer);
330 }
331 }
332
aspeed_timer_ctrl_external_clock(AspeedTimer * t,bool enable)333 static void aspeed_timer_ctrl_external_clock(AspeedTimer *t, bool enable)
334 {
335 trace_aspeed_timer_ctrl_external_clock(t->id, enable);
336 }
337
aspeed_timer_ctrl_overflow_interrupt(AspeedTimer * t,bool enable)338 static void aspeed_timer_ctrl_overflow_interrupt(AspeedTimer *t, bool enable)
339 {
340 trace_aspeed_timer_ctrl_overflow_interrupt(t->id, enable);
341 }
342
aspeed_timer_ctrl_pulse_enable(AspeedTimer * t,bool enable)343 static void aspeed_timer_ctrl_pulse_enable(AspeedTimer *t, bool enable)
344 {
345 if (timer_can_pulse(t)) {
346 trace_aspeed_timer_ctrl_pulse_enable(t->id, enable);
347 } else {
348 qemu_log_mask(LOG_GUEST_ERROR,
349 "%s: Timer does not support pulse mode\n", __func__);
350 }
351 }
352
353 /**
354 * Given the actions are fixed in number and completely described in helper
355 * functions, dispatch with a lookup table rather than manage control flow with
356 * a switch statement.
357 */
358 static void (*const ctrl_ops[])(AspeedTimer *, bool) = {
359 [op_enable] = aspeed_timer_ctrl_enable,
360 [op_external_clock] = aspeed_timer_ctrl_external_clock,
361 [op_overflow_interrupt] = aspeed_timer_ctrl_overflow_interrupt,
362 [op_pulse_enable] = aspeed_timer_ctrl_pulse_enable,
363 };
364
365 /**
366 * Conditionally affect changes chosen by a timer's control bit.
367 *
368 * The aspeed_timer_ctrl_op() interface is convenient for the
369 * aspeed_timer_set_ctrl() function as the "no change" early exit can be
370 * calculated for all operations, which cleans up the caller code. However the
371 * interface isn't convenient for the reset function where we want to enter a
372 * specific state without artificially constructing old and new values that
373 * will fall through the change guard (and motivates extracting the actions
374 * out to helper functions).
375 *
376 * @t: The timer to manipulate
377 * @op: The type of operation to be performed
378 * @old: The old state of the timer's control bits
379 * @new: The incoming state for the timer's control bits
380 */
aspeed_timer_ctrl_op(AspeedTimer * t,enum timer_ctrl_op op,uint8_t old,uint8_t new)381 static void aspeed_timer_ctrl_op(AspeedTimer *t, enum timer_ctrl_op op,
382 uint8_t old, uint8_t new)
383 {
384 const uint8_t mask = BIT(op);
385 const bool enable = !!(new & mask);
386 const bool changed = ((old ^ new) & mask);
387 if (!changed) {
388 return;
389 }
390 ctrl_ops[op](t, enable);
391 }
392
aspeed_timer_set_ctrl(AspeedTimerCtrlState * s,uint32_t reg)393 static void aspeed_timer_set_ctrl(AspeedTimerCtrlState *s, uint32_t reg)
394 {
395 int i;
396 int shift;
397 uint8_t t_old, t_new;
398 AspeedTimer *t;
399 const uint8_t enable_mask = BIT(op_enable);
400
401 /*
402 * Handle a dependency between the 'enable' and remaining three
403 * configuration bits - i.e. if more than one bit in the control set has
404 * changed, including the 'enable' bit, then we want either disable the
405 * timer and perform configuration, or perform configuration and then
406 * enable the timer
407 */
408 for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
409 t = &s->timers[i];
410 shift = (i * TIMER_CTRL_BITS);
411 t_old = (s->ctrl >> shift) & TIMER_CTRL_MASK;
412 t_new = (reg >> shift) & TIMER_CTRL_MASK;
413
414 /* If we are disabling, do so first */
415 if ((t_old & enable_mask) && !(t_new & enable_mask)) {
416 aspeed_timer_ctrl_enable(t, false);
417 }
418 aspeed_timer_ctrl_op(t, op_external_clock, t_old, t_new);
419 aspeed_timer_ctrl_op(t, op_overflow_interrupt, t_old, t_new);
420 aspeed_timer_ctrl_op(t, op_pulse_enable, t_old, t_new);
421 /* If we are enabling, do so last */
422 if (!(t_old & enable_mask) && (t_new & enable_mask)) {
423 aspeed_timer_ctrl_enable(t, true);
424 }
425 }
426 s->ctrl = reg;
427 }
428
aspeed_timer_set_ctrl2(AspeedTimerCtrlState * s,uint32_t value)429 static void aspeed_timer_set_ctrl2(AspeedTimerCtrlState *s, uint32_t value)
430 {
431 trace_aspeed_timer_set_ctrl2(value);
432 }
433
aspeed_timer_write(void * opaque,hwaddr offset,uint64_t value,unsigned size)434 static void aspeed_timer_write(void *opaque, hwaddr offset, uint64_t value,
435 unsigned size)
436 {
437 const uint32_t tv = (uint32_t)(value & 0xFFFFFFFF);
438 const int reg = (offset & 0xf) / 4;
439 AspeedTimerCtrlState *s = opaque;
440
441 switch (offset) {
442 /* Control Registers */
443 case 0x30:
444 aspeed_timer_set_ctrl(s, tv);
445 break;
446 /* Timer Registers */
447 case 0x00 ... 0x2c:
448 aspeed_timer_set_value(s, (offset >> TIMER_NR_REGS), reg, tv);
449 break;
450 case 0x40 ... 0x8c:
451 aspeed_timer_set_value(s, (offset >> TIMER_NR_REGS) - 1, reg, tv);
452 break;
453 default:
454 ASPEED_TIMER_GET_CLASS(s)->write(s, offset, value);
455 break;
456 }
457 }
458
459 static const MemoryRegionOps aspeed_timer_ops = {
460 .read = aspeed_timer_read,
461 .write = aspeed_timer_write,
462 .endianness = DEVICE_LITTLE_ENDIAN,
463 .valid.min_access_size = 1,
464 .valid.max_access_size = 4,
465 .valid.unaligned = false,
466 };
467
aspeed_2400_timer_read(AspeedTimerCtrlState * s,hwaddr offset)468 static uint64_t aspeed_2400_timer_read(AspeedTimerCtrlState *s, hwaddr offset)
469 {
470 uint64_t value;
471
472 switch (offset) {
473 case 0x34:
474 value = s->ctrl2;
475 break;
476 case 0x38:
477 case 0x3C:
478 default:
479 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
480 __func__, offset);
481 value = 0;
482 break;
483 }
484 return value;
485 }
486
aspeed_2400_timer_write(AspeedTimerCtrlState * s,hwaddr offset,uint64_t value)487 static void aspeed_2400_timer_write(AspeedTimerCtrlState *s, hwaddr offset,
488 uint64_t value)
489 {
490 const uint32_t tv = (uint32_t)(value & 0xFFFFFFFF);
491
492 switch (offset) {
493 case 0x34:
494 aspeed_timer_set_ctrl2(s, tv);
495 break;
496 case 0x38:
497 case 0x3C:
498 default:
499 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
500 __func__, offset);
501 break;
502 }
503 }
504
aspeed_2500_timer_read(AspeedTimerCtrlState * s,hwaddr offset)505 static uint64_t aspeed_2500_timer_read(AspeedTimerCtrlState *s, hwaddr offset)
506 {
507 uint64_t value;
508
509 switch (offset) {
510 case 0x34:
511 value = s->ctrl2;
512 break;
513 case 0x38:
514 value = s->ctrl3 & BIT(0);
515 break;
516 case 0x3C:
517 default:
518 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
519 __func__, offset);
520 value = 0;
521 break;
522 }
523 return value;
524 }
525
aspeed_2500_timer_write(AspeedTimerCtrlState * s,hwaddr offset,uint64_t value)526 static void aspeed_2500_timer_write(AspeedTimerCtrlState *s, hwaddr offset,
527 uint64_t value)
528 {
529 const uint32_t tv = (uint32_t)(value & 0xFFFFFFFF);
530 uint8_t command;
531
532 switch (offset) {
533 case 0x34:
534 aspeed_timer_set_ctrl2(s, tv);
535 break;
536 case 0x38:
537 command = (value >> 1) & 0xFF;
538 if (command == 0xAE) {
539 s->ctrl3 = 0x1;
540 } else if (command == 0xEA) {
541 s->ctrl3 = 0x0;
542 }
543 break;
544 case 0x3C:
545 if (s->ctrl3 & BIT(0)) {
546 aspeed_timer_set_ctrl(s, s->ctrl & ~tv);
547 }
548 break;
549
550 default:
551 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
552 __func__, offset);
553 break;
554 }
555 }
556
aspeed_2600_timer_read(AspeedTimerCtrlState * s,hwaddr offset)557 static uint64_t aspeed_2600_timer_read(AspeedTimerCtrlState *s, hwaddr offset)
558 {
559 uint64_t value;
560
561 switch (offset) {
562 case 0x34:
563 value = s->irq_sts;
564 break;
565 case 0x38:
566 case 0x3C:
567 default:
568 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
569 __func__, offset);
570 value = 0;
571 break;
572 }
573 return value;
574 }
575
aspeed_2600_timer_write(AspeedTimerCtrlState * s,hwaddr offset,uint64_t value)576 static void aspeed_2600_timer_write(AspeedTimerCtrlState *s, hwaddr offset,
577 uint64_t value)
578 {
579 const uint32_t tv = (uint32_t)(value & 0xFFFFFFFF);
580
581 switch (offset) {
582 case 0x34:
583 s->irq_sts &= ~tv;
584 break;
585 case 0x3C:
586 aspeed_timer_set_ctrl(s, s->ctrl & ~tv);
587 break;
588 case 0x38:
589 default:
590 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
591 __func__, offset);
592 break;
593 }
594 }
595
aspeed_init_one_timer(AspeedTimerCtrlState * s,uint8_t id)596 static void aspeed_init_one_timer(AspeedTimerCtrlState *s, uint8_t id)
597 {
598 AspeedTimer *t = &s->timers[id];
599
600 t->id = id;
601 timer_init_ns(&t->timer, QEMU_CLOCK_VIRTUAL, aspeed_timer_expire, t);
602 }
603
aspeed_timer_realize(DeviceState * dev,Error ** errp)604 static void aspeed_timer_realize(DeviceState *dev, Error **errp)
605 {
606 int i;
607 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
608 AspeedTimerCtrlState *s = ASPEED_TIMER(dev);
609
610 assert(s->scu);
611
612 for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
613 aspeed_init_one_timer(s, i);
614 sysbus_init_irq(sbd, &s->timers[i].irq);
615 }
616 memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_timer_ops, s,
617 TYPE_ASPEED_TIMER, 0x1000);
618 sysbus_init_mmio(sbd, &s->iomem);
619 }
620
aspeed_timer_reset(DeviceState * dev)621 static void aspeed_timer_reset(DeviceState *dev)
622 {
623 int i;
624 AspeedTimerCtrlState *s = ASPEED_TIMER(dev);
625
626 for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) {
627 AspeedTimer *t = &s->timers[i];
628 /*
629 * Explicitly call helpers to avoid any conditional behaviour through
630 * aspeed_timer_set_ctrl().
631 */
632 aspeed_timer_ctrl_enable(t, false);
633 aspeed_timer_ctrl_external_clock(t, TIMER_CLOCK_USE_APB);
634 aspeed_timer_ctrl_overflow_interrupt(t, false);
635 aspeed_timer_ctrl_pulse_enable(t, false);
636 t->level = 0;
637 t->reload = 0;
638 t->match[0] = 0;
639 t->match[1] = 0;
640 }
641 s->ctrl = 0;
642 s->ctrl2 = 0;
643 s->ctrl3 = 0;
644 s->irq_sts = 0;
645 }
646
647 static const VMStateDescription vmstate_aspeed_timer = {
648 .name = "aspeed.timer",
649 .version_id = 2,
650 .minimum_version_id = 2,
651 .fields = (const VMStateField[]) {
652 VMSTATE_UINT8(id, AspeedTimer),
653 VMSTATE_INT32(level, AspeedTimer),
654 VMSTATE_TIMER(timer, AspeedTimer),
655 VMSTATE_UINT32(reload, AspeedTimer),
656 VMSTATE_UINT32_ARRAY(match, AspeedTimer, 2),
657 VMSTATE_END_OF_LIST()
658 }
659 };
660
661 static const VMStateDescription vmstate_aspeed_timer_state = {
662 .name = "aspeed.timerctrl",
663 .version_id = 2,
664 .minimum_version_id = 2,
665 .fields = (const VMStateField[]) {
666 VMSTATE_UINT32(ctrl, AspeedTimerCtrlState),
667 VMSTATE_UINT32(ctrl2, AspeedTimerCtrlState),
668 VMSTATE_UINT32(ctrl3, AspeedTimerCtrlState),
669 VMSTATE_UINT32(irq_sts, AspeedTimerCtrlState),
670 VMSTATE_STRUCT_ARRAY(timers, AspeedTimerCtrlState,
671 ASPEED_TIMER_NR_TIMERS, 1, vmstate_aspeed_timer,
672 AspeedTimer),
673 VMSTATE_END_OF_LIST()
674 }
675 };
676
677 static Property aspeed_timer_properties[] = {
678 DEFINE_PROP_LINK("scu", AspeedTimerCtrlState, scu, TYPE_ASPEED_SCU,
679 AspeedSCUState *),
680 DEFINE_PROP_END_OF_LIST(),
681 };
682
timer_class_init(ObjectClass * klass,void * data)683 static void timer_class_init(ObjectClass *klass, void *data)
684 {
685 DeviceClass *dc = DEVICE_CLASS(klass);
686
687 dc->realize = aspeed_timer_realize;
688 device_class_set_legacy_reset(dc, aspeed_timer_reset);
689 dc->desc = "ASPEED Timer";
690 dc->vmsd = &vmstate_aspeed_timer_state;
691 device_class_set_props(dc, aspeed_timer_properties);
692 }
693
694 static const TypeInfo aspeed_timer_info = {
695 .name = TYPE_ASPEED_TIMER,
696 .parent = TYPE_SYS_BUS_DEVICE,
697 .instance_size = sizeof(AspeedTimerCtrlState),
698 .class_init = timer_class_init,
699 .class_size = sizeof(AspeedTimerClass),
700 .abstract = true,
701 };
702
aspeed_2400_timer_class_init(ObjectClass * klass,void * data)703 static void aspeed_2400_timer_class_init(ObjectClass *klass, void *data)
704 {
705 DeviceClass *dc = DEVICE_CLASS(klass);
706 AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
707
708 dc->desc = "ASPEED 2400 Timer";
709 awc->read = aspeed_2400_timer_read;
710 awc->write = aspeed_2400_timer_write;
711 }
712
713 static const TypeInfo aspeed_2400_timer_info = {
714 .name = TYPE_ASPEED_2400_TIMER,
715 .parent = TYPE_ASPEED_TIMER,
716 .class_init = aspeed_2400_timer_class_init,
717 };
718
aspeed_2500_timer_class_init(ObjectClass * klass,void * data)719 static void aspeed_2500_timer_class_init(ObjectClass *klass, void *data)
720 {
721 DeviceClass *dc = DEVICE_CLASS(klass);
722 AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
723
724 dc->desc = "ASPEED 2500 Timer";
725 awc->read = aspeed_2500_timer_read;
726 awc->write = aspeed_2500_timer_write;
727 }
728
729 static const TypeInfo aspeed_2500_timer_info = {
730 .name = TYPE_ASPEED_2500_TIMER,
731 .parent = TYPE_ASPEED_TIMER,
732 .class_init = aspeed_2500_timer_class_init,
733 };
734
aspeed_2600_timer_class_init(ObjectClass * klass,void * data)735 static void aspeed_2600_timer_class_init(ObjectClass *klass, void *data)
736 {
737 DeviceClass *dc = DEVICE_CLASS(klass);
738 AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
739
740 dc->desc = "ASPEED 2600 Timer";
741 awc->read = aspeed_2600_timer_read;
742 awc->write = aspeed_2600_timer_write;
743 }
744
745 static const TypeInfo aspeed_2600_timer_info = {
746 .name = TYPE_ASPEED_2600_TIMER,
747 .parent = TYPE_ASPEED_TIMER,
748 .class_init = aspeed_2600_timer_class_init,
749 };
750
aspeed_1030_timer_class_init(ObjectClass * klass,void * data)751 static void aspeed_1030_timer_class_init(ObjectClass *klass, void *data)
752 {
753 DeviceClass *dc = DEVICE_CLASS(klass);
754 AspeedTimerClass *awc = ASPEED_TIMER_CLASS(klass);
755
756 dc->desc = "ASPEED 1030 Timer";
757 awc->read = aspeed_2600_timer_read;
758 awc->write = aspeed_2600_timer_write;
759 }
760
761 static const TypeInfo aspeed_1030_timer_info = {
762 .name = TYPE_ASPEED_1030_TIMER,
763 .parent = TYPE_ASPEED_TIMER,
764 .class_init = aspeed_1030_timer_class_init,
765 };
766
aspeed_timer_register_types(void)767 static void aspeed_timer_register_types(void)
768 {
769 type_register_static(&aspeed_timer_info);
770 type_register_static(&aspeed_2400_timer_info);
771 type_register_static(&aspeed_2500_timer_info);
772 type_register_static(&aspeed_2600_timer_info);
773 type_register_static(&aspeed_1030_timer_info);
774 }
775
776 type_init(aspeed_timer_register_types)
777