2 * ARM CMSDK APB timer emulation
4 * Copyright (c) 2017 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.
12 /* This is a model of the "APB timer" which is part of the Cortex-M
13 * System Design Kit (CMSDK) and documented in the Cortex-M System
14 * Design Kit Technical Reference Manual (ARM DDI0479C):
15 * https://developer.arm.com/products/system-design/system-design-kits/cortex-m-system-design-kit
17 * The hardware has an EXTIN input wire, which can be configured
18 * by the guest to act either as a 'timer enable' (timer does not run
19 * when EXTIN is low), or as a 'timer clock' (timer runs at frequency
20 * of EXTIN clock, not PCLK frequency). We don't model this.
22 * The documentation is not very clear about the exact behaviour;
23 * we choose to implement that the interrupt is triggered when
24 * the counter goes from 1 to 0, that the counter then holds at 0
25 * for one clock cycle before reloading from the RELOAD register,
26 * and that if the RELOAD register is 0 this does not cause an
27 * interrupt (as there is no further 1->0 transition).
30 #include "qemu/osdep.h"
32 #include "qemu/main-loop.h"
33 #include "qemu/module.h"
34 #include "qapi/error.h"
36 #include "hw/sysbus.h"
38 #include "hw/registerfields.h"
39 #include "hw/timer/cmsdk-apb-timer.h"
40 #include "migration/vmstate.h"
44 FIELD(CTRL
, SELEXTEN
, 1, 1)
45 FIELD(CTRL
, SELEXTCLK
, 2, 1)
46 FIELD(CTRL
, IRQEN
, 3, 1)
50 FIELD(INTSTATUS
, IRQ
, 0, 1)
65 static const int timer_id
[] = {
66 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
67 0x22, 0xb8, 0x1b, 0x00, /* PID0..PID3 */
68 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
71 static void cmsdk_apb_timer_update(CMSDKAPBTIMER
*s
)
73 qemu_set_irq(s
->timerint
, !!(s
->intstatus
& R_INTSTATUS_IRQ_MASK
));
76 static uint64_t cmsdk_apb_timer_read(void *opaque
, hwaddr offset
, unsigned size
)
78 CMSDKAPBTIMER
*s
= CMSDK_APB_TIMER(opaque
);
86 r
= ptimer_get_count(s
->timer
);
89 r
= ptimer_get_limit(s
->timer
);
94 case A_PID4
... A_CID3
:
95 r
= timer_id
[(offset
- A_PID4
) / 4];
98 qemu_log_mask(LOG_GUEST_ERROR
,
99 "CMSDK APB timer read: bad offset %x\n", (int) offset
);
103 trace_cmsdk_apb_timer_read(offset
, r
, size
);
107 static void cmsdk_apb_timer_write(void *opaque
, hwaddr offset
, uint64_t value
,
110 CMSDKAPBTIMER
*s
= CMSDK_APB_TIMER(opaque
);
112 trace_cmsdk_apb_timer_write(offset
, value
, size
);
117 /* Bits [1] and [2] enable using EXTIN as either clock or
118 * an enable line. We don't model this.
120 qemu_log_mask(LOG_UNIMP
,
121 "CMSDK APB timer: EXTIN input not supported\n");
123 s
->ctrl
= value
& 0xf;
124 if (s
->ctrl
& R_CTRL_EN_MASK
) {
125 ptimer_run(s
->timer
, ptimer_get_limit(s
->timer
) == 0);
127 ptimer_stop(s
->timer
);
131 /* Writing to reload also sets the current timer value */
133 ptimer_stop(s
->timer
);
135 ptimer_set_limit(s
->timer
, value
, 1);
136 if (value
&& (s
->ctrl
& R_CTRL_EN_MASK
)) {
138 * Make sure timer is running (it might have stopped if this
139 * was an expired one-shot timer)
141 ptimer_run(s
->timer
, 0);
145 if (!value
&& !ptimer_get_limit(s
->timer
)) {
146 ptimer_stop(s
->timer
);
148 ptimer_set_count(s
->timer
, value
);
149 if (value
&& (s
->ctrl
& R_CTRL_EN_MASK
)) {
150 ptimer_run(s
->timer
, ptimer_get_limit(s
->timer
) == 0);
154 /* Just one bit, which is W1C. */
156 s
->intstatus
&= ~value
;
157 cmsdk_apb_timer_update(s
);
159 case A_PID4
... A_CID3
:
160 qemu_log_mask(LOG_GUEST_ERROR
,
161 "CMSDK APB timer write: write to RO offset 0x%x\n",
165 qemu_log_mask(LOG_GUEST_ERROR
,
166 "CMSDK APB timer write: bad offset 0x%x\n", (int) offset
);
171 static const MemoryRegionOps cmsdk_apb_timer_ops
= {
172 .read
= cmsdk_apb_timer_read
,
173 .write
= cmsdk_apb_timer_write
,
174 .endianness
= DEVICE_LITTLE_ENDIAN
,
177 static void cmsdk_apb_timer_tick(void *opaque
)
179 CMSDKAPBTIMER
*s
= CMSDK_APB_TIMER(opaque
);
181 if (s
->ctrl
& R_CTRL_IRQEN_MASK
) {
182 s
->intstatus
|= R_INTSTATUS_IRQ_MASK
;
183 cmsdk_apb_timer_update(s
);
187 static void cmsdk_apb_timer_reset(DeviceState
*dev
)
189 CMSDKAPBTIMER
*s
= CMSDK_APB_TIMER(dev
);
191 trace_cmsdk_apb_timer_reset();
194 ptimer_stop(s
->timer
);
195 /* Set the limit and the count */
196 ptimer_set_limit(s
->timer
, 0, 1);
199 static void cmsdk_apb_timer_init(Object
*obj
)
201 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
202 CMSDKAPBTIMER
*s
= CMSDK_APB_TIMER(obj
);
204 memory_region_init_io(&s
->iomem
, obj
, &cmsdk_apb_timer_ops
,
205 s
, "cmsdk-apb-timer", 0x1000);
206 sysbus_init_mmio(sbd
, &s
->iomem
);
207 sysbus_init_irq(sbd
, &s
->timerint
);
210 static void cmsdk_apb_timer_realize(DeviceState
*dev
, Error
**errp
)
212 CMSDKAPBTIMER
*s
= CMSDK_APB_TIMER(dev
);
215 if (s
->pclk_frq
== 0) {
216 error_setg(errp
, "CMSDK APB timer: pclk-frq property must be set");
220 bh
= qemu_bh_new(cmsdk_apb_timer_tick
, s
);
221 s
->timer
= ptimer_init(bh
,
222 PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
|
223 PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
|
224 PTIMER_POLICY_NO_IMMEDIATE_RELOAD
|
225 PTIMER_POLICY_NO_COUNTER_ROUND_DOWN
);
227 ptimer_set_freq(s
->timer
, s
->pclk_frq
);
230 static const VMStateDescription cmsdk_apb_timer_vmstate
= {
231 .name
= "cmsdk-apb-timer",
233 .minimum_version_id
= 1,
234 .fields
= (VMStateField
[]) {
235 VMSTATE_PTIMER(timer
, CMSDKAPBTIMER
),
236 VMSTATE_UINT32(ctrl
, CMSDKAPBTIMER
),
237 VMSTATE_UINT32(value
, CMSDKAPBTIMER
),
238 VMSTATE_UINT32(reload
, CMSDKAPBTIMER
),
239 VMSTATE_UINT32(intstatus
, CMSDKAPBTIMER
),
240 VMSTATE_END_OF_LIST()
244 static Property cmsdk_apb_timer_properties
[] = {
245 DEFINE_PROP_UINT32("pclk-frq", CMSDKAPBTIMER
, pclk_frq
, 0),
246 DEFINE_PROP_END_OF_LIST(),
249 static void cmsdk_apb_timer_class_init(ObjectClass
*klass
, void *data
)
251 DeviceClass
*dc
= DEVICE_CLASS(klass
);
253 dc
->realize
= cmsdk_apb_timer_realize
;
254 dc
->vmsd
= &cmsdk_apb_timer_vmstate
;
255 dc
->reset
= cmsdk_apb_timer_reset
;
256 dc
->props
= cmsdk_apb_timer_properties
;
259 static const TypeInfo cmsdk_apb_timer_info
= {
260 .name
= TYPE_CMSDK_APB_TIMER
,
261 .parent
= TYPE_SYS_BUS_DEVICE
,
262 .instance_size
= sizeof(CMSDKAPBTIMER
),
263 .instance_init
= cmsdk_apb_timer_init
,
264 .class_init
= cmsdk_apb_timer_class_init
,
267 static void cmsdk_apb_timer_register_types(void)
269 type_register_static(&cmsdk_apb_timer_info
);
272 type_init(cmsdk_apb_timer_register_types
);