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/main-loop.h"
24 #include "hw/sysbus.h"
25 #include "hw/registerfields.h"
26 #include "hw/timer/cmsdk-apb-dualtimer.h"
28 REG32(TIMER1LOAD
, 0x0)
29 REG32(TIMER1VALUE
, 0x4)
30 REG32(TIMER1CONTROL
, 0x8)
31 FIELD(CONTROL
, ONESHOT
, 0, 1)
32 FIELD(CONTROL
, SIZE
, 1, 1)
33 FIELD(CONTROL
, PRESCALE
, 2, 2)
34 FIELD(CONTROL
, INTEN
, 5, 1)
35 FIELD(CONTROL
, MODE
, 6, 1)
36 FIELD(CONTROL
, ENABLE
, 7, 1)
37 #define R_CONTROL_VALID_MASK (R_CONTROL_ONESHOT_MASK | R_CONTROL_SIZE_MASK | \
38 R_CONTROL_PRESCALE_MASK | R_CONTROL_INTEN_MASK | \
39 R_CONTROL_MODE_MASK | R_CONTROL_ENABLE_MASK)
40 REG32(TIMER1INTCLR
, 0xc)
41 REG32(TIMER1RIS
, 0x10)
42 REG32(TIMER1MIS
, 0x14)
43 REG32(TIMER1BGLOAD
, 0x18)
44 REG32(TIMER2LOAD
, 0x20)
45 REG32(TIMER2VALUE
, 0x24)
46 REG32(TIMER2CONTROL
, 0x28)
47 REG32(TIMER2INTCLR
, 0x2c)
48 REG32(TIMER2RIS
, 0x30)
49 REG32(TIMER2MIS
, 0x34)
50 REG32(TIMER2BGLOAD
, 0x38)
51 REG32(TIMERITCR
, 0xf00)
52 FIELD(TIMERITCR
, ENABLE
, 0, 1)
53 #define R_TIMERITCR_VALID_MASK R_TIMERITCR_ENABLE_MASK
54 REG32(TIMERITOP
, 0xf04)
55 FIELD(TIMERITOP
, TIMINT1
, 0, 1)
56 FIELD(TIMERITOP
, TIMINT2
, 1, 1)
57 #define R_TIMERITOP_VALID_MASK (R_TIMERITOP_TIMINT1_MASK | \
58 R_TIMERITOP_TIMINT2_MASK)
73 static const int timer_id
[] = {
74 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
75 0x23, 0xb8, 0x1b, 0x00, /* PID0..PID3 */
76 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
79 static bool cmsdk_dualtimermod_intstatus(CMSDKAPBDualTimerModule
*m
)
81 /* Return masked interrupt status for the timer module */
82 return m
->intstatus
&& (m
->control
& R_CONTROL_INTEN_MASK
);
85 static void cmsdk_apb_dualtimer_update(CMSDKAPBDualTimer
*s
)
87 bool timint1
, timint2
, timintc
;
90 /* Integration test mode: outputs driven directly from TIMERITOP bits */
91 timint1
= s
->timeritop
& R_TIMERITOP_TIMINT1_MASK
;
92 timint2
= s
->timeritop
& R_TIMERITOP_TIMINT2_MASK
;
94 timint1
= cmsdk_dualtimermod_intstatus(&s
->timermod
[0]);
95 timint2
= cmsdk_dualtimermod_intstatus(&s
->timermod
[1]);
98 timintc
= timint1
|| timint2
;
100 qemu_set_irq(s
->timermod
[0].timerint
, timint1
);
101 qemu_set_irq(s
->timermod
[1].timerint
, timint2
);
102 qemu_set_irq(s
->timerintc
, timintc
);
105 static void cmsdk_dualtimermod_write_control(CMSDKAPBDualTimerModule
*m
,
108 /* Handle a write to the CONTROL register */
111 newctrl
&= R_CONTROL_VALID_MASK
;
113 changed
= m
->control
^ newctrl
;
115 if (changed
& ~newctrl
& R_CONTROL_ENABLE_MASK
) {
116 /* ENABLE cleared, stop timer before any further changes */
117 ptimer_stop(m
->timer
);
120 if (changed
& R_CONTROL_PRESCALE_MASK
) {
123 switch (FIELD_EX32(newctrl
, CONTROL
, PRESCALE
)) {
134 /* UNDEFINED; complain, and arbitrarily treat like 2 */
135 qemu_log_mask(LOG_GUEST_ERROR
,
136 "CMSDK APB dual-timer: CONTROL.PRESCALE==0b11"
137 " is undefined behaviour\n");
141 g_assert_not_reached();
143 ptimer_set_freq(m
->timer
, m
->parent
->pclk_frq
/ divisor
);
146 if (changed
& R_CONTROL_MODE_MASK
) {
148 if (newctrl
& R_CONTROL_MODE_MASK
) {
149 /* Periodic: the limit is the LOAD register value */
152 /* Free-running: counter wraps around */
153 load
= ptimer_get_limit(m
->timer
);
154 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
155 load
= deposit32(m
->load
, 0, 16, load
);
160 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
163 ptimer_set_limit(m
->timer
, load
, 0);
166 if (changed
& R_CONTROL_SIZE_MASK
) {
167 /* Timer switched between 16 and 32 bit count */
168 uint32_t value
, load
;
170 value
= ptimer_get_count(m
->timer
);
171 load
= ptimer_get_limit(m
->timer
);
172 if (newctrl
& R_CONTROL_SIZE_MASK
) {
173 /* 16 -> 32, top half of VALUE is in struct field */
174 value
= deposit32(m
->value
, 0, 16, value
);
176 /* 32 -> 16: save top half to struct field and truncate */
181 if (newctrl
& R_CONTROL_MODE_MASK
) {
182 /* Periodic, timer limit has LOAD value */
183 if (newctrl
& R_CONTROL_SIZE_MASK
) {
184 load
= deposit32(m
->load
, 0, 16, load
);
190 /* Free-running, timer limit is set to give wraparound */
191 if (newctrl
& R_CONTROL_SIZE_MASK
) {
197 ptimer_set_count(m
->timer
, value
);
198 ptimer_set_limit(m
->timer
, load
, 0);
201 if (newctrl
& R_CONTROL_ENABLE_MASK
) {
203 * ENABLE is set; start the timer after all other changes.
204 * We start it even if the ENABLE bit didn't actually change,
205 * in case the timer was an expired one-shot timer that has
206 * now been changed into a free-running or periodic timer.
208 ptimer_run(m
->timer
, !!(newctrl
& R_CONTROL_ONESHOT_MASK
));
211 m
->control
= newctrl
;
214 static uint64_t cmsdk_apb_dualtimer_read(void *opaque
, hwaddr offset
,
217 CMSDKAPBDualTimer
*s
= CMSDK_APB_DUALTIMER(opaque
);
220 if (offset
>= A_TIMERITCR
) {
225 case A_PID4
... A_CID3
:
226 r
= timer_id
[(offset
- A_PID4
) / 4];
230 qemu_log_mask(LOG_GUEST_ERROR
,
231 "CMSDK APB dual-timer read: bad offset %x\n",
237 int timer
= offset
>> 5;
238 CMSDKAPBDualTimerModule
*m
;
240 if (timer
>= ARRAY_SIZE(s
->timermod
)) {
244 m
= &s
->timermod
[timer
];
246 switch (offset
& 0x1F) {
249 if (m
->control
& R_CONTROL_MODE_MASK
) {
251 * Periodic: the ptimer limit is the LOAD register value, (or
252 * just the low 16 bits of it if the timer is in 16-bit mode)
254 r
= ptimer_get_limit(m
->timer
);
255 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
256 r
= deposit32(m
->load
, 0, 16, r
);
259 /* Free-running: LOAD register value is just in m->load */
264 r
= ptimer_get_count(m
->timer
);
265 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
266 r
= deposit32(m
->value
, 0, 16, r
);
269 case A_TIMER1CONTROL
:
276 r
= cmsdk_dualtimermod_intstatus(m
);
283 trace_cmsdk_apb_dualtimer_read(offset
, r
, size
);
287 static void cmsdk_apb_dualtimer_write(void *opaque
, hwaddr offset
,
288 uint64_t value
, unsigned size
)
290 CMSDKAPBDualTimer
*s
= CMSDK_APB_DUALTIMER(opaque
);
292 trace_cmsdk_apb_dualtimer_write(offset
, value
, size
);
294 if (offset
>= A_TIMERITCR
) {
297 s
->timeritcr
= value
& R_TIMERITCR_VALID_MASK
;
298 cmsdk_apb_dualtimer_update(s
);
301 s
->timeritop
= value
& R_TIMERITOP_VALID_MASK
;
302 cmsdk_apb_dualtimer_update(s
);
306 qemu_log_mask(LOG_GUEST_ERROR
,
307 "CMSDK APB dual-timer write: bad offset %x\n",
312 int timer
= offset
>> 5;
313 CMSDKAPBDualTimerModule
*m
;
315 if (timer
>= ARRAY_SIZE(s
->timermod
)) {
319 m
= &s
->timermod
[timer
];
321 switch (offset
& 0x1F) {
323 /* Set the limit, and immediately reload the count from it */
326 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
329 if (!(m
->control
& R_CONTROL_MODE_MASK
)) {
331 * In free-running mode this won't set the limit but will
332 * still change the current count value.
334 ptimer_set_count(m
->timer
, value
);
337 ptimer_stop(m
->timer
);
339 ptimer_set_limit(m
->timer
, value
, 1);
340 if (value
&& (m
->control
& R_CONTROL_ENABLE_MASK
)) {
341 /* Force possibly-expired oneshot timer to restart */
342 ptimer_run(m
->timer
, 1);
347 /* Set the limit, but not the current count */
349 if (!(m
->control
& R_CONTROL_MODE_MASK
)) {
350 /* In free-running mode there is no limit */
353 if (!(m
->control
& R_CONTROL_SIZE_MASK
)) {
356 ptimer_set_limit(m
->timer
, value
, 0);
358 case A_TIMER1CONTROL
:
359 cmsdk_dualtimermod_write_control(m
, value
);
360 cmsdk_apb_dualtimer_update(s
);
364 cmsdk_apb_dualtimer_update(s
);
372 static const MemoryRegionOps cmsdk_apb_dualtimer_ops
= {
373 .read
= cmsdk_apb_dualtimer_read
,
374 .write
= cmsdk_apb_dualtimer_write
,
375 .endianness
= DEVICE_LITTLE_ENDIAN
,
376 /* byte/halfword accesses are just zero-padded on reads and writes */
377 .impl
.min_access_size
= 4,
378 .impl
.max_access_size
= 4,
379 .valid
.min_access_size
= 1,
380 .valid
.max_access_size
= 4,
383 static void cmsdk_dualtimermod_tick(void *opaque
)
385 CMSDKAPBDualTimerModule
*m
= opaque
;
388 cmsdk_apb_dualtimer_update(m
->parent
);
391 static void cmsdk_dualtimermod_reset(CMSDKAPBDualTimerModule
*m
)
393 m
->control
= R_CONTROL_INTEN_MASK
;
396 m
->value
= 0xffffffff;
397 ptimer_stop(m
->timer
);
399 * We start in free-running mode, with VALUE at 0xffffffff, and
400 * in 16-bit counter mode. This means that the ptimer count and
401 * limit must both be set to 0xffff, so we wrap at 16 bits.
403 ptimer_set_limit(m
->timer
, 0xffff, 1);
404 ptimer_set_freq(m
->timer
, m
->parent
->pclk_frq
);
407 static void cmsdk_apb_dualtimer_reset(DeviceState
*dev
)
409 CMSDKAPBDualTimer
*s
= CMSDK_APB_DUALTIMER(dev
);
412 trace_cmsdk_apb_dualtimer_reset();
414 for (i
= 0; i
< ARRAY_SIZE(s
->timermod
); i
++) {
415 cmsdk_dualtimermod_reset(&s
->timermod
[i
]);
421 static void cmsdk_apb_dualtimer_init(Object
*obj
)
423 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
424 CMSDKAPBDualTimer
*s
= CMSDK_APB_DUALTIMER(obj
);
427 memory_region_init_io(&s
->iomem
, obj
, &cmsdk_apb_dualtimer_ops
,
428 s
, "cmsdk-apb-dualtimer", 0x1000);
429 sysbus_init_mmio(sbd
, &s
->iomem
);
430 sysbus_init_irq(sbd
, &s
->timerintc
);
432 for (i
= 0; i
< ARRAY_SIZE(s
->timermod
); i
++) {
433 sysbus_init_irq(sbd
, &s
->timermod
[i
].timerint
);
437 static void cmsdk_apb_dualtimer_realize(DeviceState
*dev
, Error
**errp
)
439 CMSDKAPBDualTimer
*s
= CMSDK_APB_DUALTIMER(dev
);
442 if (s
->pclk_frq
== 0) {
443 error_setg(errp
, "CMSDK APB timer: pclk-frq property must be set");
447 for (i
= 0; i
< ARRAY_SIZE(s
->timermod
); i
++) {
448 CMSDKAPBDualTimerModule
*m
= &s
->timermod
[i
];
449 QEMUBH
*bh
= qemu_bh_new(cmsdk_dualtimermod_tick
, m
);
452 m
->timer
= ptimer_init(bh
,
453 PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
|
454 PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
|
455 PTIMER_POLICY_NO_IMMEDIATE_RELOAD
|
456 PTIMER_POLICY_NO_COUNTER_ROUND_DOWN
);
460 static const VMStateDescription cmsdk_dualtimermod_vmstate
= {
461 .name
= "cmsdk-apb-dualtimer-module",
463 .minimum_version_id
= 1,
464 .fields
= (VMStateField
[]) {
465 VMSTATE_PTIMER(timer
, CMSDKAPBDualTimerModule
),
466 VMSTATE_UINT32(load
, CMSDKAPBDualTimerModule
),
467 VMSTATE_UINT32(value
, CMSDKAPBDualTimerModule
),
468 VMSTATE_UINT32(control
, CMSDKAPBDualTimerModule
),
469 VMSTATE_UINT32(intstatus
, CMSDKAPBDualTimerModule
),
470 VMSTATE_END_OF_LIST()
474 static const VMStateDescription cmsdk_apb_dualtimer_vmstate
= {
475 .name
= "cmsdk-apb-dualtimer",
477 .minimum_version_id
= 1,
478 .fields
= (VMStateField
[]) {
479 VMSTATE_STRUCT_ARRAY(timermod
, CMSDKAPBDualTimer
,
480 CMSDK_APB_DUALTIMER_NUM_MODULES
,
481 1, cmsdk_dualtimermod_vmstate
,
482 CMSDKAPBDualTimerModule
),
483 VMSTATE_UINT32(timeritcr
, CMSDKAPBDualTimer
),
484 VMSTATE_UINT32(timeritop
, CMSDKAPBDualTimer
),
485 VMSTATE_END_OF_LIST()
489 static Property cmsdk_apb_dualtimer_properties
[] = {
490 DEFINE_PROP_UINT32("pclk-frq", CMSDKAPBDualTimer
, pclk_frq
, 0),
491 DEFINE_PROP_END_OF_LIST(),
494 static void cmsdk_apb_dualtimer_class_init(ObjectClass
*klass
, void *data
)
496 DeviceClass
*dc
= DEVICE_CLASS(klass
);
498 dc
->realize
= cmsdk_apb_dualtimer_realize
;
499 dc
->vmsd
= &cmsdk_apb_dualtimer_vmstate
;
500 dc
->reset
= cmsdk_apb_dualtimer_reset
;
501 dc
->props
= cmsdk_apb_dualtimer_properties
;
504 static const TypeInfo cmsdk_apb_dualtimer_info
= {
505 .name
= TYPE_CMSDK_APB_DUALTIMER
,
506 .parent
= TYPE_SYS_BUS_DEVICE
,
507 .instance_size
= sizeof(CMSDKAPBDualTimer
),
508 .instance_init
= cmsdk_apb_dualtimer_init
,
509 .class_init
= cmsdk_apb_dualtimer_class_init
,
512 static void cmsdk_apb_dualtimer_register_types(void)
514 type_register_static(&cmsdk_apb_dualtimer_info
);
517 type_init(cmsdk_apb_dualtimer_register_types
);