iotests: Basic tests for internal snapshots
[qemu/kevin.git] / hw / watchdog / allwinner-wdt.c
blob6205765efec7c172016fd87fbd1079e704da31a9
1 /*
2 * Allwinner Watchdog emulation
4 * Copyright (C) 2023 Strahinja Jankovic <strahinja.p.jankovic@gmail.com>
6 * This file is derived from Allwinner RTC,
7 * by Niek Linnenbank.
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "qemu/osdep.h"
24 #include "qemu/log.h"
25 #include "qemu/units.h"
26 #include "qemu/module.h"
27 #include "trace.h"
28 #include "hw/sysbus.h"
29 #include "hw/registerfields.h"
30 #include "hw/watchdog/allwinner-wdt.h"
31 #include "sysemu/watchdog.h"
32 #include "migration/vmstate.h"
34 /* WDT registers */
35 enum {
36 REG_IRQ_EN = 0, /* Watchdog interrupt enable */
37 REG_IRQ_STA, /* Watchdog interrupt status */
38 REG_CTRL, /* Watchdog control register */
39 REG_CFG, /* Watchdog configuration register */
40 REG_MODE, /* Watchdog mode register */
43 /* Universal WDT register flags */
44 #define WDT_RESTART_MASK (1 << 0)
45 #define WDT_EN_MASK (1 << 0)
47 /* sun4i specific WDT register flags */
48 #define RST_EN_SUN4I_MASK (1 << 1)
49 #define INTV_VALUE_SUN4I_SHIFT (3)
50 #define INTV_VALUE_SUN4I_MASK (0xfu << INTV_VALUE_SUN4I_SHIFT)
52 /* sun6i specific WDT register flags */
53 #define RST_EN_SUN6I_MASK (1 << 0)
54 #define KEY_FIELD_SUN6I_SHIFT (1)
55 #define KEY_FIELD_SUN6I_MASK (0xfffu << KEY_FIELD_SUN6I_SHIFT)
56 #define KEY_FIELD_SUN6I (0xA57u)
57 #define INTV_VALUE_SUN6I_SHIFT (4)
58 #define INTV_VALUE_SUN6I_MASK (0xfu << INTV_VALUE_SUN6I_SHIFT)
60 /* Map of INTV_VALUE to 0.5s units. */
61 static const uint8_t allwinner_wdt_count_map[] = {
67 10,
68 12,
69 16,
70 20,
71 24,
72 28,
76 /* WDT sun4i register map (offset to name) */
77 const uint8_t allwinner_wdt_sun4i_regmap[] = {
78 [0x0000] = REG_CTRL,
79 [0x0004] = REG_MODE,
82 /* WDT sun6i register map (offset to name) */
83 const uint8_t allwinner_wdt_sun6i_regmap[] = {
84 [0x0000] = REG_IRQ_EN,
85 [0x0004] = REG_IRQ_STA,
86 [0x0010] = REG_CTRL,
87 [0x0014] = REG_CFG,
88 [0x0018] = REG_MODE,
91 static bool allwinner_wdt_sun4i_read(AwWdtState *s, uint32_t offset)
93 /* no sun4i specific registers currently implemented */
94 return false;
97 static bool allwinner_wdt_sun4i_write(AwWdtState *s, uint32_t offset,
98 uint32_t data)
100 /* no sun4i specific registers currently implemented */
101 return false;
104 static bool allwinner_wdt_sun4i_can_reset_system(AwWdtState *s)
106 if (s->regs[REG_MODE] & RST_EN_SUN4I_MASK) {
107 return true;
108 } else {
109 return false;
113 static bool allwinner_wdt_sun4i_is_key_valid(AwWdtState *s, uint32_t val)
115 /* sun4i has no key */
116 return true;
119 static uint8_t allwinner_wdt_sun4i_get_intv_value(AwWdtState *s)
121 return ((s->regs[REG_MODE] & INTV_VALUE_SUN4I_MASK) >>
122 INTV_VALUE_SUN4I_SHIFT);
125 static bool allwinner_wdt_sun6i_read(AwWdtState *s, uint32_t offset)
127 const AwWdtClass *c = AW_WDT_GET_CLASS(s);
129 switch (c->regmap[offset]) {
130 case REG_IRQ_EN:
131 case REG_IRQ_STA:
132 case REG_CFG:
133 return true;
134 default:
135 break;
137 return false;
140 static bool allwinner_wdt_sun6i_write(AwWdtState *s, uint32_t offset,
141 uint32_t data)
143 const AwWdtClass *c = AW_WDT_GET_CLASS(s);
145 switch (c->regmap[offset]) {
146 case REG_IRQ_EN:
147 case REG_IRQ_STA:
148 case REG_CFG:
149 return true;
150 default:
151 break;
153 return false;
156 static bool allwinner_wdt_sun6i_can_reset_system(AwWdtState *s)
158 if (s->regs[REG_CFG] & RST_EN_SUN6I_MASK) {
159 return true;
160 } else {
161 return false;
165 static bool allwinner_wdt_sun6i_is_key_valid(AwWdtState *s, uint32_t val)
167 uint16_t key = (val & KEY_FIELD_SUN6I_MASK) >> KEY_FIELD_SUN6I_SHIFT;
168 return (key == KEY_FIELD_SUN6I);
171 static uint8_t allwinner_wdt_sun6i_get_intv_value(AwWdtState *s)
173 return ((s->regs[REG_MODE] & INTV_VALUE_SUN6I_MASK) >>
174 INTV_VALUE_SUN6I_SHIFT);
177 static void allwinner_wdt_update_timer(AwWdtState *s)
179 const AwWdtClass *c = AW_WDT_GET_CLASS(s);
180 uint8_t count = c->get_intv_value(s);
182 ptimer_transaction_begin(s->timer);
183 ptimer_stop(s->timer);
185 /* Use map to convert. */
186 if (count < sizeof(allwinner_wdt_count_map)) {
187 ptimer_set_count(s->timer, allwinner_wdt_count_map[count]);
188 } else {
189 qemu_log_mask(LOG_GUEST_ERROR, "%s: incorrect INTV_VALUE 0x%02x\n",
190 __func__, count);
193 ptimer_run(s->timer, 1);
194 ptimer_transaction_commit(s->timer);
196 trace_allwinner_wdt_update_timer(count);
199 static uint64_t allwinner_wdt_read(void *opaque, hwaddr offset,
200 unsigned size)
202 AwWdtState *s = AW_WDT(opaque);
203 const AwWdtClass *c = AW_WDT_GET_CLASS(s);
204 uint64_t r;
206 if (offset >= c->regmap_size) {
207 qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
208 __func__, (uint32_t)offset);
209 return 0;
212 switch (c->regmap[offset]) {
213 case REG_CTRL:
214 case REG_MODE:
215 r = s->regs[c->regmap[offset]];
216 break;
217 default:
218 if (!c->read(s, offset)) {
219 qemu_log_mask(LOG_UNIMP, "%s: unimplemented register 0x%04x\n",
220 __func__, (uint32_t)offset);
221 return 0;
223 r = s->regs[c->regmap[offset]];
224 break;
227 trace_allwinner_wdt_read(offset, r, size);
229 return r;
232 static void allwinner_wdt_write(void *opaque, hwaddr offset,
233 uint64_t val, unsigned size)
235 AwWdtState *s = AW_WDT(opaque);
236 const AwWdtClass *c = AW_WDT_GET_CLASS(s);
237 uint32_t old_val;
239 if (offset >= c->regmap_size) {
240 qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
241 __func__, (uint32_t)offset);
242 return;
245 trace_allwinner_wdt_write(offset, val, size);
247 switch (c->regmap[offset]) {
248 case REG_CTRL:
249 if (c->is_key_valid(s, val)) {
250 if (val & WDT_RESTART_MASK) {
251 /* Kick timer */
252 allwinner_wdt_update_timer(s);
255 break;
256 case REG_MODE:
257 old_val = s->regs[REG_MODE];
258 s->regs[REG_MODE] = (uint32_t)val;
260 /* Check for rising edge on WDOG_MODE_EN */
261 if ((s->regs[REG_MODE] & ~old_val) & WDT_EN_MASK) {
262 allwinner_wdt_update_timer(s);
264 break;
265 default:
266 if (!c->write(s, offset, val)) {
267 qemu_log_mask(LOG_UNIMP, "%s: unimplemented register 0x%04x\n",
268 __func__, (uint32_t)offset);
270 s->regs[c->regmap[offset]] = (uint32_t)val;
271 break;
275 static const MemoryRegionOps allwinner_wdt_ops = {
276 .read = allwinner_wdt_read,
277 .write = allwinner_wdt_write,
278 .endianness = DEVICE_NATIVE_ENDIAN,
279 .valid = {
280 .min_access_size = 4,
281 .max_access_size = 4,
283 .impl.min_access_size = 4,
286 static void allwinner_wdt_expired(void *opaque)
288 AwWdtState *s = AW_WDT(opaque);
289 const AwWdtClass *c = AW_WDT_GET_CLASS(s);
291 bool enabled = s->regs[REG_MODE] & WDT_EN_MASK;
292 bool reset_enabled = c->can_reset_system(s);
294 trace_allwinner_wdt_expired(enabled, reset_enabled);
296 /* Perform watchdog action if watchdog is enabled and can trigger reset */
297 if (enabled && reset_enabled) {
298 watchdog_perform_action();
302 static void allwinner_wdt_reset_enter(Object *obj, ResetType type)
304 AwWdtState *s = AW_WDT(obj);
306 trace_allwinner_wdt_reset_enter();
308 /* Clear registers */
309 memset(s->regs, 0, sizeof(s->regs));
312 static const VMStateDescription allwinner_wdt_vmstate = {
313 .name = "allwinner-wdt",
314 .version_id = 1,
315 .minimum_version_id = 1,
316 .fields = (VMStateField[]) {
317 VMSTATE_PTIMER(timer, AwWdtState),
318 VMSTATE_UINT32_ARRAY(regs, AwWdtState, AW_WDT_REGS_NUM),
319 VMSTATE_END_OF_LIST()
323 static void allwinner_wdt_init(Object *obj)
325 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
326 AwWdtState *s = AW_WDT(obj);
327 const AwWdtClass *c = AW_WDT_GET_CLASS(s);
329 /* Memory mapping */
330 memory_region_init_io(&s->iomem, OBJECT(s), &allwinner_wdt_ops, s,
331 TYPE_AW_WDT, c->regmap_size * 4);
332 sysbus_init_mmio(sbd, &s->iomem);
335 static void allwinner_wdt_realize(DeviceState *dev, Error **errp)
337 AwWdtState *s = AW_WDT(dev);
339 s->timer = ptimer_init(allwinner_wdt_expired, s,
340 PTIMER_POLICY_NO_IMMEDIATE_TRIGGER |
341 PTIMER_POLICY_NO_IMMEDIATE_RELOAD |
342 PTIMER_POLICY_NO_COUNTER_ROUND_DOWN);
344 ptimer_transaction_begin(s->timer);
345 /* Set to 2Hz (0.5s period); other periods are multiples of 0.5s. */
346 ptimer_set_freq(s->timer, 2);
347 ptimer_set_limit(s->timer, 0xff, 1);
348 ptimer_transaction_commit(s->timer);
351 static void allwinner_wdt_class_init(ObjectClass *klass, void *data)
353 DeviceClass *dc = DEVICE_CLASS(klass);
354 ResettableClass *rc = RESETTABLE_CLASS(klass);
356 rc->phases.enter = allwinner_wdt_reset_enter;
357 dc->realize = allwinner_wdt_realize;
358 dc->vmsd = &allwinner_wdt_vmstate;
361 static void allwinner_wdt_sun4i_class_init(ObjectClass *klass, void *data)
363 AwWdtClass *awc = AW_WDT_CLASS(klass);
365 awc->regmap = allwinner_wdt_sun4i_regmap;
366 awc->regmap_size = sizeof(allwinner_wdt_sun4i_regmap);
367 awc->read = allwinner_wdt_sun4i_read;
368 awc->write = allwinner_wdt_sun4i_write;
369 awc->can_reset_system = allwinner_wdt_sun4i_can_reset_system;
370 awc->is_key_valid = allwinner_wdt_sun4i_is_key_valid;
371 awc->get_intv_value = allwinner_wdt_sun4i_get_intv_value;
374 static void allwinner_wdt_sun6i_class_init(ObjectClass *klass, void *data)
376 AwWdtClass *awc = AW_WDT_CLASS(klass);
378 awc->regmap = allwinner_wdt_sun6i_regmap;
379 awc->regmap_size = sizeof(allwinner_wdt_sun6i_regmap);
380 awc->read = allwinner_wdt_sun6i_read;
381 awc->write = allwinner_wdt_sun6i_write;
382 awc->can_reset_system = allwinner_wdt_sun6i_can_reset_system;
383 awc->is_key_valid = allwinner_wdt_sun6i_is_key_valid;
384 awc->get_intv_value = allwinner_wdt_sun6i_get_intv_value;
387 static const TypeInfo allwinner_wdt_info = {
388 .name = TYPE_AW_WDT,
389 .parent = TYPE_SYS_BUS_DEVICE,
390 .instance_init = allwinner_wdt_init,
391 .instance_size = sizeof(AwWdtState),
392 .class_init = allwinner_wdt_class_init,
393 .class_size = sizeof(AwWdtClass),
394 .abstract = true,
397 static const TypeInfo allwinner_wdt_sun4i_info = {
398 .name = TYPE_AW_WDT_SUN4I,
399 .parent = TYPE_AW_WDT,
400 .class_init = allwinner_wdt_sun4i_class_init,
403 static const TypeInfo allwinner_wdt_sun6i_info = {
404 .name = TYPE_AW_WDT_SUN6I,
405 .parent = TYPE_AW_WDT,
406 .class_init = allwinner_wdt_sun6i_class_init,
409 static void allwinner_wdt_register(void)
411 type_register_static(&allwinner_wdt_info);
412 type_register_static(&allwinner_wdt_sun4i_info);
413 type_register_static(&allwinner_wdt_sun6i_info);
416 type_init(allwinner_wdt_register)