2 * ASPEED Watchdog Controller
4 * Copyright (C) 2016-2017 IBM Corp.
6 * This code is licensed under the GPL version 2 or later. See the
7 * COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
12 #include "qapi/error.h"
14 #include "qemu/timer.h"
15 #include "sysemu/watchdog.h"
16 #include "hw/misc/aspeed_scu.h"
17 #include "hw/sysbus.h"
18 #include "hw/watchdog/wdt_aspeed.h"
20 #define WDT_STATUS (0x00 / 4)
21 #define WDT_RELOAD_VALUE (0x04 / 4)
22 #define WDT_RESTART (0x08 / 4)
23 #define WDT_CTRL (0x0C / 4)
24 #define WDT_CTRL_RESET_MODE_SOC (0x00 << 5)
25 #define WDT_CTRL_RESET_MODE_FULL_CHIP (0x01 << 5)
26 #define WDT_CTRL_1MHZ_CLK BIT(4)
27 #define WDT_CTRL_WDT_EXT BIT(3)
28 #define WDT_CTRL_WDT_INTR BIT(2)
29 #define WDT_CTRL_RESET_SYSTEM BIT(1)
30 #define WDT_CTRL_ENABLE BIT(0)
31 #define WDT_RESET_WIDTH (0x18 / 4)
32 #define WDT_RESET_WIDTH_ACTIVE_HIGH BIT(31)
33 #define WDT_POLARITY_MASK (0xFF << 24)
34 #define WDT_ACTIVE_HIGH_MAGIC (0xA5 << 24)
35 #define WDT_ACTIVE_LOW_MAGIC (0x5A << 24)
36 #define WDT_RESET_WIDTH_PUSH_PULL BIT(30)
37 #define WDT_DRIVE_TYPE_MASK (0xFF << 24)
38 #define WDT_PUSH_PULL_MAGIC (0xA8 << 24)
39 #define WDT_OPEN_DRAIN_MAGIC (0x8A << 24)
41 #define WDT_TIMEOUT_STATUS (0x10 / 4)
42 #define WDT_TIMEOUT_CLEAR (0x14 / 4)
44 #define WDT_RESTART_MAGIC 0x4755
46 static bool aspeed_wdt_is_enabled(const AspeedWDTState
*s
)
48 return s
->regs
[WDT_CTRL
] & WDT_CTRL_ENABLE
;
51 static bool is_ast2500(const AspeedWDTState
*s
)
53 switch (s
->silicon_rev
) {
54 case AST2500_A0_SILICON_REV
:
55 case AST2500_A1_SILICON_REV
:
57 case AST2400_A0_SILICON_REV
:
58 case AST2400_A1_SILICON_REV
:
66 static uint64_t aspeed_wdt_read(void *opaque
, hwaddr offset
, unsigned size
)
68 AspeedWDTState
*s
= ASPEED_WDT(opaque
);
74 return s
->regs
[WDT_STATUS
];
75 case WDT_RELOAD_VALUE
:
76 return s
->regs
[WDT_RELOAD_VALUE
];
78 qemu_log_mask(LOG_GUEST_ERROR
,
79 "%s: read from write-only reg at offset 0x%"
80 HWADDR_PRIx
"\n", __func__
, offset
);
83 return s
->regs
[WDT_CTRL
];
85 return s
->regs
[WDT_RESET_WIDTH
];
86 case WDT_TIMEOUT_STATUS
:
87 case WDT_TIMEOUT_CLEAR
:
88 qemu_log_mask(LOG_UNIMP
,
89 "%s: uninmplemented read at offset 0x%" HWADDR_PRIx
"\n",
93 qemu_log_mask(LOG_GUEST_ERROR
,
94 "%s: Out-of-bounds read at offset 0x%" HWADDR_PRIx
"\n",
101 static void aspeed_wdt_reload(AspeedWDTState
*s
, bool pclk
)
106 reload
= muldiv64(s
->regs
[WDT_RELOAD_VALUE
], NANOSECONDS_PER_SECOND
,
109 reload
= s
->regs
[WDT_RELOAD_VALUE
] * 1000ULL;
112 if (aspeed_wdt_is_enabled(s
)) {
113 timer_mod(s
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + reload
);
117 static void aspeed_wdt_write(void *opaque
, hwaddr offset
, uint64_t data
,
120 AspeedWDTState
*s
= ASPEED_WDT(opaque
);
121 bool enable
= data
& WDT_CTRL_ENABLE
;
127 qemu_log_mask(LOG_GUEST_ERROR
,
128 "%s: write to read-only reg at offset 0x%"
129 HWADDR_PRIx
"\n", __func__
, offset
);
131 case WDT_RELOAD_VALUE
:
132 s
->regs
[WDT_RELOAD_VALUE
] = data
;
135 if ((data
& 0xFFFF) == WDT_RESTART_MAGIC
) {
136 s
->regs
[WDT_STATUS
] = s
->regs
[WDT_RELOAD_VALUE
];
137 aspeed_wdt_reload(s
, !(data
& WDT_CTRL_1MHZ_CLK
));
141 if (enable
&& !aspeed_wdt_is_enabled(s
)) {
142 s
->regs
[WDT_CTRL
] = data
;
143 aspeed_wdt_reload(s
, !(data
& WDT_CTRL_1MHZ_CLK
));
144 } else if (!enable
&& aspeed_wdt_is_enabled(s
)) {
145 s
->regs
[WDT_CTRL
] = data
;
149 case WDT_RESET_WIDTH
:
151 uint32_t property
= data
& WDT_POLARITY_MASK
;
153 if (property
&& is_ast2500(s
)) {
154 if (property
== WDT_ACTIVE_HIGH_MAGIC
) {
155 s
->regs
[WDT_RESET_WIDTH
] |= WDT_RESET_WIDTH_ACTIVE_HIGH
;
156 } else if (property
== WDT_ACTIVE_LOW_MAGIC
) {
157 s
->regs
[WDT_RESET_WIDTH
] &= ~WDT_RESET_WIDTH_ACTIVE_HIGH
;
158 } else if (property
== WDT_PUSH_PULL_MAGIC
) {
159 s
->regs
[WDT_RESET_WIDTH
] |= WDT_RESET_WIDTH_PUSH_PULL
;
160 } else if (property
== WDT_OPEN_DRAIN_MAGIC
) {
161 s
->regs
[WDT_RESET_WIDTH
] &= ~WDT_RESET_WIDTH_PUSH_PULL
;
164 s
->regs
[WDT_RESET_WIDTH
] &= ~s
->ext_pulse_width_mask
;
165 s
->regs
[WDT_RESET_WIDTH
] |= data
& s
->ext_pulse_width_mask
;
168 case WDT_TIMEOUT_STATUS
:
169 case WDT_TIMEOUT_CLEAR
:
170 qemu_log_mask(LOG_UNIMP
,
171 "%s: uninmplemented write at offset 0x%" HWADDR_PRIx
"\n",
175 qemu_log_mask(LOG_GUEST_ERROR
,
176 "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx
"\n",
182 static WatchdogTimerModel model
= {
183 .wdt_name
= TYPE_ASPEED_WDT
,
184 .wdt_description
= "Aspeed watchdog device",
187 static const VMStateDescription vmstate_aspeed_wdt
= {
188 .name
= "vmstate_aspeed_wdt",
190 .minimum_version_id
= 0,
191 .fields
= (VMStateField
[]) {
192 VMSTATE_TIMER_PTR(timer
, AspeedWDTState
),
193 VMSTATE_UINT32_ARRAY(regs
, AspeedWDTState
, ASPEED_WDT_REGS_MAX
),
194 VMSTATE_END_OF_LIST()
198 static const MemoryRegionOps aspeed_wdt_ops
= {
199 .read
= aspeed_wdt_read
,
200 .write
= aspeed_wdt_write
,
201 .endianness
= DEVICE_LITTLE_ENDIAN
,
202 .valid
.min_access_size
= 4,
203 .valid
.max_access_size
= 4,
204 .valid
.unaligned
= false,
207 static void aspeed_wdt_reset(DeviceState
*dev
)
209 AspeedWDTState
*s
= ASPEED_WDT(dev
);
211 s
->regs
[WDT_STATUS
] = 0x3EF1480;
212 s
->regs
[WDT_RELOAD_VALUE
] = 0x03EF1480;
213 s
->regs
[WDT_RESTART
] = 0;
214 s
->regs
[WDT_CTRL
] = 0;
215 s
->regs
[WDT_RESET_WIDTH
] = 0xFF;
220 static void aspeed_wdt_timer_expired(void *dev
)
222 AspeedWDTState
*s
= ASPEED_WDT(dev
);
224 qemu_log_mask(CPU_LOG_RESET
, "Watchdog timer expired.\n");
225 watchdog_perform_action();
229 #define PCLK_HZ 24000000
231 static void aspeed_wdt_realize(DeviceState
*dev
, Error
**errp
)
233 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
234 AspeedWDTState
*s
= ASPEED_WDT(dev
);
236 if (!is_supported_silicon_rev(s
->silicon_rev
)) {
237 error_setg(errp
, "Unknown silicon revision: 0x%" PRIx32
,
242 switch (s
->silicon_rev
) {
243 case AST2400_A0_SILICON_REV
:
244 case AST2400_A1_SILICON_REV
:
245 s
->ext_pulse_width_mask
= 0xff;
247 case AST2500_A0_SILICON_REV
:
248 case AST2500_A1_SILICON_REV
:
249 s
->ext_pulse_width_mask
= 0xfffff;
252 g_assert_not_reached();
255 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, aspeed_wdt_timer_expired
, dev
);
257 /* FIXME: This setting should be derived from the SCU hw strapping
260 s
->pclk_freq
= PCLK_HZ
;
262 memory_region_init_io(&s
->iomem
, OBJECT(s
), &aspeed_wdt_ops
, s
,
263 TYPE_ASPEED_WDT
, ASPEED_WDT_REGS_MAX
* 4);
264 sysbus_init_mmio(sbd
, &s
->iomem
);
267 static Property aspeed_wdt_properties
[] = {
268 DEFINE_PROP_UINT32("silicon-rev", AspeedWDTState
, silicon_rev
, 0),
269 DEFINE_PROP_END_OF_LIST(),
272 static void aspeed_wdt_class_init(ObjectClass
*klass
, void *data
)
274 DeviceClass
*dc
= DEVICE_CLASS(klass
);
276 dc
->realize
= aspeed_wdt_realize
;
277 dc
->reset
= aspeed_wdt_reset
;
278 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
279 dc
->vmsd
= &vmstate_aspeed_wdt
;
280 dc
->props
= aspeed_wdt_properties
;
283 static const TypeInfo aspeed_wdt_info
= {
284 .parent
= TYPE_SYS_BUS_DEVICE
,
285 .name
= TYPE_ASPEED_WDT
,
286 .instance_size
= sizeof(AspeedWDTState
),
287 .class_init
= aspeed_wdt_class_init
,
290 static void wdt_aspeed_register_types(void)
292 watchdog_add_model(&model
);
293 type_register_static(&aspeed_wdt_info
);
296 type_init(wdt_aspeed_register_types
)