2 * ARM CMSDK APB watchdog 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 watchdog" 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
18 * We also support the variant of this device found in the TI
19 * Stellaris/Luminary boards and documented in:
20 * http://www.ti.com/lit/ds/symlink/lm3s6965.pdf
23 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/module.h"
28 #include "sysemu/watchdog.h"
29 #include "hw/sysbus.h"
31 #include "hw/qdev-properties.h"
32 #include "hw/registerfields.h"
33 #include "hw/watchdog/cmsdk-apb-watchdog.h"
34 #include "migration/vmstate.h"
38 REG32(WDOGCONTROL
, 0x8)
39 FIELD(WDOGCONTROL
, INTEN
, 0, 1)
40 FIELD(WDOGCONTROL
, RESEN
, 1, 1)
41 #define R_WDOGCONTROL_VALID_MASK (R_WDOGCONTROL_INTEN_MASK | \
42 R_WDOGCONTROL_RESEN_MASK)
43 REG32(WDOGINTCLR
, 0xc)
45 FIELD(WDOGRIS
, INT
, 0, 1)
47 REG32(WDOGTEST
, 0x418) /* only in Stellaris/Luminary version of the device */
48 REG32(WDOGLOCK
, 0xc00)
49 #define WDOG_UNLOCK_VALUE 0x1ACCE551
50 REG32(WDOGITCR
, 0xf00)
51 FIELD(WDOGITCR
, ENABLE
, 0, 1)
52 #define R_WDOGITCR_VALID_MASK R_WDOGITCR_ENABLE_MASK
53 REG32(WDOGITOP
, 0xf04)
54 FIELD(WDOGITOP
, WDOGRES
, 0, 1)
55 FIELD(WDOGITOP
, WDOGINT
, 1, 1)
56 #define R_WDOGITOP_VALID_MASK (R_WDOGITOP_WDOGRES_MASK | \
57 R_WDOGITOP_WDOGINT_MASK)
72 static const uint32_t cmsdk_apb_watchdog_id
[] = {
73 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
74 0x24, 0xb8, 0x1b, 0x00, /* PID0..PID3 */
75 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
78 static const uint32_t luminary_watchdog_id
[] = {
79 0x00, 0x00, 0x00, 0x00, /* PID4..PID7 */
80 0x05, 0x18, 0x18, 0x01, /* PID0..PID3 */
81 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
84 static bool cmsdk_apb_watchdog_intstatus(CMSDKAPBWatchdog
*s
)
86 /* Return masked interrupt status */
87 return s
->intstatus
&& (s
->control
& R_WDOGCONTROL_INTEN_MASK
);
90 static bool cmsdk_apb_watchdog_resetstatus(CMSDKAPBWatchdog
*s
)
92 /* Return masked reset status */
93 return s
->resetstatus
&& (s
->control
& R_WDOGCONTROL_RESEN_MASK
);
96 static void cmsdk_apb_watchdog_update(CMSDKAPBWatchdog
*s
)
103 * Not checking that !s->is_luminary since s->itcr can't be written
104 * when s->is_luminary in the first place.
106 wdogint
= s
->itop
& R_WDOGITOP_WDOGINT_MASK
;
107 wdogres
= s
->itop
& R_WDOGITOP_WDOGRES_MASK
;
109 wdogint
= cmsdk_apb_watchdog_intstatus(s
);
110 wdogres
= cmsdk_apb_watchdog_resetstatus(s
);
113 qemu_set_irq(s
->wdogint
, wdogint
);
115 watchdog_perform_action();
119 static uint64_t cmsdk_apb_watchdog_read(void *opaque
, hwaddr offset
,
122 CMSDKAPBWatchdog
*s
= CMSDK_APB_WATCHDOG(opaque
);
127 r
= ptimer_get_limit(s
->timer
);
130 r
= ptimer_get_count(s
->timer
);
139 r
= cmsdk_apb_watchdog_intstatus(s
);
145 if (s
->is_luminary
) {
150 case A_PID4
... A_CID3
:
151 r
= s
->id
[(offset
- A_PID4
) / 4];
155 if (s
->is_luminary
) {
158 qemu_log_mask(LOG_GUEST_ERROR
,
159 "CMSDK APB watchdog read: read of WO offset %x\n",
164 if (!s
->is_luminary
) {
167 qemu_log_mask(LOG_UNIMP
,
168 "Luminary watchdog read: stall not implemented\n");
173 qemu_log_mask(LOG_GUEST_ERROR
,
174 "CMSDK APB watchdog read: bad offset %x\n", (int)offset
);
178 trace_cmsdk_apb_watchdog_read(offset
, r
, size
);
182 static void cmsdk_apb_watchdog_write(void *opaque
, hwaddr offset
,
183 uint64_t value
, unsigned size
)
185 CMSDKAPBWatchdog
*s
= CMSDK_APB_WATCHDOG(opaque
);
187 trace_cmsdk_apb_watchdog_write(offset
, value
, size
);
189 if (s
->lock
&& offset
!= A_WDOGLOCK
) {
190 /* Write access is disabled via WDOGLOCK */
191 qemu_log_mask(LOG_GUEST_ERROR
,
192 "CMSDK APB watchdog write: write to locked watchdog\n");
199 * Reset the load value and the current count, and make sure
202 ptimer_transaction_begin(s
->timer
);
203 ptimer_set_limit(s
->timer
, value
, 1);
204 ptimer_run(s
->timer
, 0);
205 ptimer_transaction_commit(s
->timer
);
208 if (s
->is_luminary
&& 0 != (R_WDOGCONTROL_INTEN_MASK
& s
->control
)) {
210 * The Luminary version of this device ignores writes to
211 * this register after the guest has enabled interrupts
212 * (so they can only be disabled again via reset).
216 s
->control
= value
& R_WDOGCONTROL_VALID_MASK
;
217 cmsdk_apb_watchdog_update(s
);
221 ptimer_transaction_begin(s
->timer
);
222 ptimer_set_count(s
->timer
, ptimer_get_limit(s
->timer
));
223 ptimer_transaction_commit(s
->timer
);
224 cmsdk_apb_watchdog_update(s
);
227 s
->lock
= (value
!= WDOG_UNLOCK_VALUE
);
228 trace_cmsdk_apb_watchdog_lock(s
->lock
);
231 if (s
->is_luminary
) {
234 s
->itcr
= value
& R_WDOGITCR_VALID_MASK
;
235 cmsdk_apb_watchdog_update(s
);
238 if (s
->is_luminary
) {
241 s
->itop
= value
& R_WDOGITOP_VALID_MASK
;
242 cmsdk_apb_watchdog_update(s
);
247 case A_PID4
... A_CID3
:
248 qemu_log_mask(LOG_GUEST_ERROR
,
249 "CMSDK APB watchdog write: write to RO offset 0x%x\n",
253 if (!s
->is_luminary
) {
256 qemu_log_mask(LOG_UNIMP
,
257 "Luminary watchdog write: stall not implemented\n");
261 qemu_log_mask(LOG_GUEST_ERROR
,
262 "CMSDK APB watchdog write: bad offset 0x%x\n",
268 static const MemoryRegionOps cmsdk_apb_watchdog_ops
= {
269 .read
= cmsdk_apb_watchdog_read
,
270 .write
= cmsdk_apb_watchdog_write
,
271 .endianness
= DEVICE_LITTLE_ENDIAN
,
272 /* byte/halfword accesses are just zero-padded on reads and writes */
273 .impl
.min_access_size
= 4,
274 .impl
.max_access_size
= 4,
275 .valid
.min_access_size
= 1,
276 .valid
.max_access_size
= 4,
279 static void cmsdk_apb_watchdog_tick(void *opaque
)
281 CMSDKAPBWatchdog
*s
= CMSDK_APB_WATCHDOG(opaque
);
284 /* Count expired for the first time: raise interrupt */
285 s
->intstatus
= R_WDOGRIS_INT_MASK
;
287 /* Count expired for the second time: raise reset and stop clock */
289 ptimer_stop(s
->timer
);
291 cmsdk_apb_watchdog_update(s
);
294 static void cmsdk_apb_watchdog_reset(DeviceState
*dev
)
296 CMSDKAPBWatchdog
*s
= CMSDK_APB_WATCHDOG(dev
);
298 trace_cmsdk_apb_watchdog_reset();
305 /* Set the limit and the count */
306 ptimer_transaction_begin(s
->timer
);
307 ptimer_set_limit(s
->timer
, 0xffffffff, 1);
308 ptimer_run(s
->timer
, 0);
309 ptimer_transaction_commit(s
->timer
);
312 static void cmsdk_apb_watchdog_init(Object
*obj
)
314 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
315 CMSDKAPBWatchdog
*s
= CMSDK_APB_WATCHDOG(obj
);
317 memory_region_init_io(&s
->iomem
, obj
, &cmsdk_apb_watchdog_ops
,
318 s
, "cmsdk-apb-watchdog", 0x1000);
319 sysbus_init_mmio(sbd
, &s
->iomem
);
320 sysbus_init_irq(sbd
, &s
->wdogint
);
322 s
->is_luminary
= false;
323 s
->id
= cmsdk_apb_watchdog_id
;
326 static void cmsdk_apb_watchdog_realize(DeviceState
*dev
, Error
**errp
)
328 CMSDKAPBWatchdog
*s
= CMSDK_APB_WATCHDOG(dev
);
330 if (s
->wdogclk_frq
== 0) {
332 "CMSDK APB watchdog: wdogclk-frq property must be set");
336 s
->timer
= ptimer_init(cmsdk_apb_watchdog_tick
, s
,
337 PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
|
338 PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
|
339 PTIMER_POLICY_NO_IMMEDIATE_RELOAD
|
340 PTIMER_POLICY_NO_COUNTER_ROUND_DOWN
);
342 ptimer_transaction_begin(s
->timer
);
343 ptimer_set_freq(s
->timer
, s
->wdogclk_frq
);
344 ptimer_transaction_commit(s
->timer
);
347 static const VMStateDescription cmsdk_apb_watchdog_vmstate
= {
348 .name
= "cmsdk-apb-watchdog",
350 .minimum_version_id
= 1,
351 .fields
= (VMStateField
[]) {
352 VMSTATE_PTIMER(timer
, CMSDKAPBWatchdog
),
353 VMSTATE_UINT32(control
, CMSDKAPBWatchdog
),
354 VMSTATE_UINT32(intstatus
, CMSDKAPBWatchdog
),
355 VMSTATE_UINT32(lock
, CMSDKAPBWatchdog
),
356 VMSTATE_UINT32(itcr
, CMSDKAPBWatchdog
),
357 VMSTATE_UINT32(itop
, CMSDKAPBWatchdog
),
358 VMSTATE_UINT32(resetstatus
, CMSDKAPBWatchdog
),
359 VMSTATE_END_OF_LIST()
363 static Property cmsdk_apb_watchdog_properties
[] = {
364 DEFINE_PROP_UINT32("wdogclk-frq", CMSDKAPBWatchdog
, wdogclk_frq
, 0),
365 DEFINE_PROP_END_OF_LIST(),
368 static void cmsdk_apb_watchdog_class_init(ObjectClass
*klass
, void *data
)
370 DeviceClass
*dc
= DEVICE_CLASS(klass
);
372 dc
->realize
= cmsdk_apb_watchdog_realize
;
373 dc
->vmsd
= &cmsdk_apb_watchdog_vmstate
;
374 dc
->reset
= cmsdk_apb_watchdog_reset
;
375 device_class_set_props(dc
, cmsdk_apb_watchdog_properties
);
378 static const TypeInfo cmsdk_apb_watchdog_info
= {
379 .name
= TYPE_CMSDK_APB_WATCHDOG
,
380 .parent
= TYPE_SYS_BUS_DEVICE
,
381 .instance_size
= sizeof(CMSDKAPBWatchdog
),
382 .instance_init
= cmsdk_apb_watchdog_init
,
383 .class_init
= cmsdk_apb_watchdog_class_init
,
386 static void luminary_watchdog_init(Object
*obj
)
388 CMSDKAPBWatchdog
*s
= CMSDK_APB_WATCHDOG(obj
);
390 s
->is_luminary
= true;
391 s
->id
= luminary_watchdog_id
;
394 static const TypeInfo luminary_watchdog_info
= {
395 .name
= TYPE_LUMINARY_WATCHDOG
,
396 .parent
= TYPE_CMSDK_APB_WATCHDOG
,
397 .instance_init
= luminary_watchdog_init
400 static void cmsdk_apb_watchdog_register_types(void)
402 type_register_static(&cmsdk_apb_watchdog_info
);
403 type_register_static(&luminary_watchdog_info
);
406 type_init(cmsdk_apb_watchdog_register_types
);