2 * ARM CMSDK APB dual-timer emulation
4 * Copyright (c) 2018 Linaro Limited
5 * Written by Peter Maydell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 or
9 * (at your option) any later version.
13 * This is a model of the "APB dual-input timer" which is part of the Cortex-M
14 * System Design Kit (CMSDK) and documented in the Cortex-M System
15 * Design Kit Technical Reference Manual (ARM DDI0479C):
16 * https://developer.arm.com/products/system-design/system-design-kits/cortex-m-system-design-kit
19 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "qemu/module.h"
24 #include "hw/sysbus.h"
26 #include "hw/qdev-properties.h"
27 #include "hw/registerfields.h"
28 #include "hw/qdev-clock.h"
29 #include "hw/timer/cmsdk-apb-dualtimer.h"
30 #include "migration/vmstate.h"
32 REG32(TIMER1LOAD
, 0x0)
33 REG32(TIMER1VALUE
, 0x4)
34 REG32(TIMER1CONTROL
, 0x8)
35 FIELD(CONTROL
, ONESHOT
, 0, 1)
36 FIELD(CONTROL
, SIZE
, 1, 1)
37 FIELD(CONTROL
, PRESCALE
, 2, 2)
38 FIELD(CONTROL
, INTEN
, 5, 1)
39 FIELD(CONTROL
, MODE
, 6, 1)
40 FIELD(CONTROL
, ENABLE
, 7, 1)
41 #define R_CONTROL_VALID_MASK (R_CONTROL_ONESHOT_MASK | R_CONTROL_SIZE_MASK | \
42 R_CONTROL_PRESCALE_MASK | R_CONTROL_INTEN_MASK | \
43 R_CONTROL_MODE_MASK | R_CONTROL_ENABLE_MASK)
44 REG32(TIMER1INTCLR
, 0xc)
45 REG32(TIMER1RIS
, 0x10)
46 REG32(TIMER1MIS
, 0x14)
47 REG32(TIMER1BGLOAD
, 0x18)
48 REG32(TIMER2LOAD
, 0x20)
49 REG32(TIMER2VALUE
, 0x24)
50 REG32(TIMER2CONTROL
, 0x28)
51 REG32(TIMER2INTCLR
, 0x2c)
52 REG32(TIMER2RIS
, 0x30)
53 REG32(TIMER2MIS
, 0x34)
54 REG32(TIMER2BGLOAD
, 0x38)
55 REG32(TIMERITCR
, 0xf00)
56 FIELD(TIMERITCR
, ENABLE
, 0, 1)
57 #define R_TIMERITCR_VALID_MASK R_TIMERITCR_ENABLE_MASK
58 REG32(TIMERITOP
, 0xf04)
59 FIELD(TIMERITOP
, TIMINT1
, 0, 1)
60 FIELD(TIMERITOP
, TIMINT2
, 1, 1)
61 #define R_TIMERITOP_VALID_MASK (R_TIMERITOP_TIMINT1_MASK | \
62 R_TIMERITOP_TIMINT2_MASK)
77 static const int timer_id
[] = {
78 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
79 0x23, 0xb8, 0x1b, 0x00, /* PID0..PID3 */
80 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
83 static bool cmsdk_dualtimermod_intstatus(CMSDKAPBDualTimerModule
*m
)
85 /* Return masked interrupt status for the timer module */
86 return m
->intstatus
&& (m
->control
& R_CONTROL_INTEN_MASK
);
89 static void cmsdk_apb_dualtimer_update(CMSDKAPBDualTimer
*s
)
91 bool timint1
, timint2
, timintc
;
94 /* Integration test mode: outputs driven directly from TIMERITOP bits */
95 timint1
= s
->timeritop
& R_TIMERITOP_TIMINT1_MASK
;
96 timint2
= s
->timeritop
& R_TIMERITOP_TIMINT2_MASK
;
98 timint1
= cmsdk_dualtimermod_intstatus(&s
->timermod
[0]);
99 timint2
= cmsdk_dualtimermod_intstatus(&s
->timermod
[1]);
102 timintc
= timint1
|| timint2
;
104 qemu_set_irq(s
->timermod
[0].timerint
, timint1
);
105 qemu_set_irq(s
->timermod
[1].timerint
, timint2
);
106 qemu_set_irq(s
->timerintc
, timintc
);
109 static int cmsdk_dualtimermod_divisor(CMSDKAPBDualTimerModule
*m
)
111 /* Return the divisor set by the current CONTROL.PRESCALE value */
112 switch (FIELD_EX32(m
->control
, CONTROL
, PRESCALE
)) {
118 case 3: /* UNDEFINED, we treat like 2 (and complained when it was set) */
121 g_assert_not_reached();
125 static void cmsdk_dualtimermod_write_control(CMSDKAPBDualTimerModule
*m
,
128 /* Handle a write to the CONTROL register */
131 ptimer_transaction_begin(m
->timer
);
133 newctrl
&= R_CONTROL_VALID_MASK
;
135 changed
= m
->control
^ newctrl
;
137 if (changed
& ~newctrl
& R_CONTROL_ENABLE_MASK
) {
138 /* ENABLE cleared, stop timer before any further changes */
139 ptimer_stop(m
->timer
);
142 if (changed
& R_CONTROL_PRESCALE_MASK
) {
145 switch (FIELD_EX32(newctrl
, CONTROL
, PRESCALE
)) {
156 /* UNDEFINED; complain, and arbitrarily treat like 2 */
157 qemu_log_mask(LOG_GUEST_ERROR
,
158 "CMSDK APB dual-timer: CONTROL.PRESCALE==0b11"
159 " is undefined behaviour\n");
163 g_assert_not_reached();
165 ptimer_set_period_from_clock(m
->timer
, m
->parent
->timclk
, divisor
);
168 if (changed
& R_CONTROL_MODE_MASK
) {
170 if (newctrl
& R_CONTROL_MODE_MASK
) {
171 /* Periodic: the limit is the LOAD register value */
174 /* Free-running: counter wraps around */
175 load
= ptimer_get_limit(m
->timer
);
176 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
177 load
= deposit32(m
->load
, 0, 16, load
);
182 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
185 ptimer_set_limit(m
->timer
, load
, 0);
188 if (changed
& R_CONTROL_SIZE_MASK
) {
189 /* Timer switched between 16 and 32 bit count */
190 uint32_t value
, load
;
192 value
= ptimer_get_count(m
->timer
);
193 load
= ptimer_get_limit(m
->timer
);
194 if (newctrl
& R_CONTROL_SIZE_MASK
) {
195 /* 16 -> 32, top half of VALUE is in struct field */
196 value
= deposit32(m
->value
, 0, 16, value
);
198 /* 32 -> 16: save top half to struct field and truncate */
203 if (newctrl
& R_CONTROL_MODE_MASK
) {
204 /* Periodic, timer limit has LOAD value */
205 if (newctrl
& R_CONTROL_SIZE_MASK
) {
206 load
= deposit32(m
->load
, 0, 16, load
);
212 /* Free-running, timer limit is set to give wraparound */
213 if (newctrl
& R_CONTROL_SIZE_MASK
) {
219 ptimer_set_count(m
->timer
, value
);
220 ptimer_set_limit(m
->timer
, load
, 0);
223 if (newctrl
& R_CONTROL_ENABLE_MASK
) {
225 * ENABLE is set; start the timer after all other changes.
226 * We start it even if the ENABLE bit didn't actually change,
227 * in case the timer was an expired one-shot timer that has
228 * now been changed into a free-running or periodic timer.
230 ptimer_run(m
->timer
, !!(newctrl
& R_CONTROL_ONESHOT_MASK
));
233 m
->control
= newctrl
;
235 ptimer_transaction_commit(m
->timer
);
238 static uint64_t cmsdk_apb_dualtimer_read(void *opaque
, hwaddr offset
,
241 CMSDKAPBDualTimer
*s
= CMSDK_APB_DUALTIMER(opaque
);
244 if (offset
>= A_TIMERITCR
) {
249 case A_PID4
... A_CID3
:
250 r
= timer_id
[(offset
- A_PID4
) / 4];
254 qemu_log_mask(LOG_GUEST_ERROR
,
255 "CMSDK APB dual-timer read: bad offset %x\n",
261 int timer
= offset
>> 5;
262 CMSDKAPBDualTimerModule
*m
;
264 if (timer
>= ARRAY_SIZE(s
->timermod
)) {
268 m
= &s
->timermod
[timer
];
270 switch (offset
& 0x1F) {
273 if (m
->control
& R_CONTROL_MODE_MASK
) {
275 * Periodic: the ptimer limit is the LOAD register value, (or
276 * just the low 16 bits of it if the timer is in 16-bit mode)
278 r
= ptimer_get_limit(m
->timer
);
279 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
280 r
= deposit32(m
->load
, 0, 16, r
);
283 /* Free-running: LOAD register value is just in m->load */
288 r
= ptimer_get_count(m
->timer
);
289 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
290 r
= deposit32(m
->value
, 0, 16, r
);
293 case A_TIMER1CONTROL
:
300 r
= cmsdk_dualtimermod_intstatus(m
);
307 trace_cmsdk_apb_dualtimer_read(offset
, r
, size
);
311 static void cmsdk_apb_dualtimer_write(void *opaque
, hwaddr offset
,
312 uint64_t value
, unsigned size
)
314 CMSDKAPBDualTimer
*s
= CMSDK_APB_DUALTIMER(opaque
);
316 trace_cmsdk_apb_dualtimer_write(offset
, value
, size
);
318 if (offset
>= A_TIMERITCR
) {
321 s
->timeritcr
= value
& R_TIMERITCR_VALID_MASK
;
322 cmsdk_apb_dualtimer_update(s
);
325 s
->timeritop
= value
& R_TIMERITOP_VALID_MASK
;
326 cmsdk_apb_dualtimer_update(s
);
330 qemu_log_mask(LOG_GUEST_ERROR
,
331 "CMSDK APB dual-timer write: bad offset %x\n",
336 int timer
= offset
>> 5;
337 CMSDKAPBDualTimerModule
*m
;
339 if (timer
>= ARRAY_SIZE(s
->timermod
)) {
343 m
= &s
->timermod
[timer
];
345 switch (offset
& 0x1F) {
347 /* Set the limit, and immediately reload the count from it */
350 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
353 ptimer_transaction_begin(m
->timer
);
354 if (!(m
->control
& R_CONTROL_MODE_MASK
)) {
356 * In free-running mode this won't set the limit but will
357 * still change the current count value.
359 ptimer_set_count(m
->timer
, value
);
362 ptimer_stop(m
->timer
);
364 ptimer_set_limit(m
->timer
, value
, 1);
365 if (value
&& (m
->control
& R_CONTROL_ENABLE_MASK
)) {
366 /* Force possibly-expired oneshot timer to restart */
367 ptimer_run(m
->timer
, 1);
370 ptimer_transaction_commit(m
->timer
);
373 /* Set the limit, but not the current count */
375 if (!(m
->control
& R_CONTROL_MODE_MASK
)) {
376 /* In free-running mode there is no limit */
379 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
382 ptimer_transaction_begin(m
->timer
);
383 ptimer_set_limit(m
->timer
, value
, 0);
384 ptimer_transaction_commit(m
->timer
);
386 case A_TIMER1CONTROL
:
387 cmsdk_dualtimermod_write_control(m
, value
);
388 cmsdk_apb_dualtimer_update(s
);
392 cmsdk_apb_dualtimer_update(s
);
400 static const MemoryRegionOps cmsdk_apb_dualtimer_ops
= {
401 .read
= cmsdk_apb_dualtimer_read
,
402 .write
= cmsdk_apb_dualtimer_write
,
403 .endianness
= DEVICE_LITTLE_ENDIAN
,
404 /* byte/halfword accesses are just zero-padded on reads and writes */
405 .impl
.min_access_size
= 4,
406 .impl
.max_access_size
= 4,
407 .valid
.min_access_size
= 1,
408 .valid
.max_access_size
= 4,
411 static void cmsdk_dualtimermod_tick(void *opaque
)
413 CMSDKAPBDualTimerModule
*m
= opaque
;
416 cmsdk_apb_dualtimer_update(m
->parent
);
419 static void cmsdk_dualtimermod_reset(CMSDKAPBDualTimerModule
*m
)
421 m
->control
= R_CONTROL_INTEN_MASK
;
424 m
->value
= 0xffffffff;
425 ptimer_transaction_begin(m
->timer
);
426 ptimer_stop(m
->timer
);
428 * We start in free-running mode, with VALUE at 0xffffffff, and
429 * in 16-bit counter mode. This means that the ptimer count and
430 * limit must both be set to 0xffff, so we wrap at 16 bits.
432 ptimer_set_limit(m
->timer
, 0xffff, 1);
433 ptimer_set_period_from_clock(m
->timer
, m
->parent
->timclk
,
434 cmsdk_dualtimermod_divisor(m
));
435 ptimer_transaction_commit(m
->timer
);
438 static void cmsdk_apb_dualtimer_reset(DeviceState
*dev
)
440 CMSDKAPBDualTimer
*s
= CMSDK_APB_DUALTIMER(dev
);
443 trace_cmsdk_apb_dualtimer_reset();
445 for (i
= 0; i
< ARRAY_SIZE(s
->timermod
); i
++) {
446 cmsdk_dualtimermod_reset(&s
->timermod
[i
]);
452 static void cmsdk_apb_dualtimer_clk_update(void *opaque
, ClockEvent event
)
454 CMSDKAPBDualTimer
*s
= CMSDK_APB_DUALTIMER(opaque
);
457 for (i
= 0; i
< ARRAY_SIZE(s
->timermod
); i
++) {
458 CMSDKAPBDualTimerModule
*m
= &s
->timermod
[i
];
459 ptimer_transaction_begin(m
->timer
);
460 ptimer_set_period_from_clock(m
->timer
, m
->parent
->timclk
,
461 cmsdk_dualtimermod_divisor(m
));
462 ptimer_transaction_commit(m
->timer
);
466 static void cmsdk_apb_dualtimer_init(Object
*obj
)
468 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
469 CMSDKAPBDualTimer
*s
= CMSDK_APB_DUALTIMER(obj
);
472 memory_region_init_io(&s
->iomem
, obj
, &cmsdk_apb_dualtimer_ops
,
473 s
, "cmsdk-apb-dualtimer", 0x1000);
474 sysbus_init_mmio(sbd
, &s
->iomem
);
475 sysbus_init_irq(sbd
, &s
->timerintc
);
477 for (i
= 0; i
< ARRAY_SIZE(s
->timermod
); i
++) {
478 sysbus_init_irq(sbd
, &s
->timermod
[i
].timerint
);
480 s
->timclk
= qdev_init_clock_in(DEVICE(s
), "TIMCLK",
481 cmsdk_apb_dualtimer_clk_update
, s
,
485 static void cmsdk_apb_dualtimer_realize(DeviceState
*dev
, Error
**errp
)
487 CMSDKAPBDualTimer
*s
= CMSDK_APB_DUALTIMER(dev
);
490 if (!clock_has_source(s
->timclk
)) {
491 error_setg(errp
, "CMSDK APB dualtimer: TIMCLK clock must be connected");
495 for (i
= 0; i
< ARRAY_SIZE(s
->timermod
); i
++) {
496 CMSDKAPBDualTimerModule
*m
= &s
->timermod
[i
];
499 m
->timer
= ptimer_init(cmsdk_dualtimermod_tick
, m
,
500 PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
|
501 PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
|
502 PTIMER_POLICY_NO_IMMEDIATE_RELOAD
|
503 PTIMER_POLICY_NO_COUNTER_ROUND_DOWN
);
507 static const VMStateDescription cmsdk_dualtimermod_vmstate
= {
508 .name
= "cmsdk-apb-dualtimer-module",
510 .minimum_version_id
= 1,
511 .fields
= (VMStateField
[]) {
512 VMSTATE_PTIMER(timer
, CMSDKAPBDualTimerModule
),
513 VMSTATE_UINT32(load
, CMSDKAPBDualTimerModule
),
514 VMSTATE_UINT32(value
, CMSDKAPBDualTimerModule
),
515 VMSTATE_UINT32(control
, CMSDKAPBDualTimerModule
),
516 VMSTATE_UINT32(intstatus
, CMSDKAPBDualTimerModule
),
517 VMSTATE_END_OF_LIST()
521 static const VMStateDescription cmsdk_apb_dualtimer_vmstate
= {
522 .name
= "cmsdk-apb-dualtimer",
524 .minimum_version_id
= 2,
525 .fields
= (VMStateField
[]) {
526 VMSTATE_CLOCK(timclk
, CMSDKAPBDualTimer
),
527 VMSTATE_STRUCT_ARRAY(timermod
, CMSDKAPBDualTimer
,
528 CMSDK_APB_DUALTIMER_NUM_MODULES
,
529 1, cmsdk_dualtimermod_vmstate
,
530 CMSDKAPBDualTimerModule
),
531 VMSTATE_UINT32(timeritcr
, CMSDKAPBDualTimer
),
532 VMSTATE_UINT32(timeritop
, CMSDKAPBDualTimer
),
533 VMSTATE_END_OF_LIST()
537 static void cmsdk_apb_dualtimer_class_init(ObjectClass
*klass
, void *data
)
539 DeviceClass
*dc
= DEVICE_CLASS(klass
);
541 dc
->realize
= cmsdk_apb_dualtimer_realize
;
542 dc
->vmsd
= &cmsdk_apb_dualtimer_vmstate
;
543 dc
->reset
= cmsdk_apb_dualtimer_reset
;
546 static const TypeInfo cmsdk_apb_dualtimer_info
= {
547 .name
= TYPE_CMSDK_APB_DUALTIMER
,
548 .parent
= TYPE_SYS_BUS_DEVICE
,
549 .instance_size
= sizeof(CMSDKAPBDualTimer
),
550 .instance_init
= cmsdk_apb_dualtimer_init
,
551 .class_init
= cmsdk_apb_dualtimer_class_init
,
554 static void cmsdk_apb_dualtimer_register_types(void)
556 type_register_static(&cmsdk_apb_dualtimer_info
);
559 type_init(cmsdk_apb_dualtimer_register_types
);