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/module.h"
15 #include "qemu/timer.h"
16 #include "sysemu/watchdog.h"
17 #include "hw/misc/aspeed_scu.h"
18 #include "hw/qdev-properties.h"
19 #include "hw/sysbus.h"
20 #include "hw/watchdog/wdt_aspeed.h"
21 #include "migration/vmstate.h"
23 #define WDT_STATUS (0x00 / 4)
24 #define WDT_RELOAD_VALUE (0x04 / 4)
25 #define WDT_RESTART (0x08 / 4)
26 #define WDT_CTRL (0x0C / 4)
27 #define WDT_CTRL_RESET_MODE_SOC (0x00 << 5)
28 #define WDT_CTRL_RESET_MODE_FULL_CHIP (0x01 << 5)
29 #define WDT_CTRL_1MHZ_CLK BIT(4)
30 #define WDT_CTRL_WDT_EXT BIT(3)
31 #define WDT_CTRL_WDT_INTR BIT(2)
32 #define WDT_CTRL_RESET_SYSTEM BIT(1)
33 #define WDT_CTRL_ENABLE BIT(0)
34 #define WDT_RESET_WIDTH (0x18 / 4)
35 #define WDT_RESET_WIDTH_ACTIVE_HIGH BIT(31)
36 #define WDT_POLARITY_MASK (0xFF << 24)
37 #define WDT_ACTIVE_HIGH_MAGIC (0xA5 << 24)
38 #define WDT_ACTIVE_LOW_MAGIC (0x5A << 24)
39 #define WDT_RESET_WIDTH_PUSH_PULL BIT(30)
40 #define WDT_DRIVE_TYPE_MASK (0xFF << 24)
41 #define WDT_PUSH_PULL_MAGIC (0xA8 << 24)
42 #define WDT_OPEN_DRAIN_MAGIC (0x8A << 24)
44 #define WDT_TIMEOUT_STATUS (0x10 / 4)
45 #define WDT_TIMEOUT_CLEAR (0x14 / 4)
47 #define WDT_RESTART_MAGIC 0x4755
49 #define SCU_RESET_CONTROL1 (0x04 / 4)
50 #define SCU_RESET_SDRAM BIT(0)
52 static bool aspeed_wdt_is_enabled(const AspeedWDTState
*s
)
54 return s
->regs
[WDT_CTRL
] & WDT_CTRL_ENABLE
;
57 static bool is_ast2500(const AspeedWDTState
*s
)
59 switch (s
->silicon_rev
) {
60 case AST2500_A0_SILICON_REV
:
61 case AST2500_A1_SILICON_REV
:
63 case AST2400_A0_SILICON_REV
:
64 case AST2400_A1_SILICON_REV
:
72 static uint64_t aspeed_wdt_read(void *opaque
, hwaddr offset
, unsigned size
)
74 AspeedWDTState
*s
= ASPEED_WDT(opaque
);
80 return s
->regs
[WDT_STATUS
];
81 case WDT_RELOAD_VALUE
:
82 return s
->regs
[WDT_RELOAD_VALUE
];
84 qemu_log_mask(LOG_GUEST_ERROR
,
85 "%s: read from write-only reg at offset 0x%"
86 HWADDR_PRIx
"\n", __func__
, offset
);
89 return s
->regs
[WDT_CTRL
];
91 return s
->regs
[WDT_RESET_WIDTH
];
92 case WDT_TIMEOUT_STATUS
:
93 case WDT_TIMEOUT_CLEAR
:
94 qemu_log_mask(LOG_UNIMP
,
95 "%s: uninmplemented read at offset 0x%" HWADDR_PRIx
"\n",
99 qemu_log_mask(LOG_GUEST_ERROR
,
100 "%s: Out-of-bounds read at offset 0x%" HWADDR_PRIx
"\n",
107 static void aspeed_wdt_reload(AspeedWDTState
*s
, bool pclk
)
112 reload
= muldiv64(s
->regs
[WDT_RELOAD_VALUE
], NANOSECONDS_PER_SECOND
,
115 reload
= s
->regs
[WDT_RELOAD_VALUE
] * 1000ULL;
118 if (aspeed_wdt_is_enabled(s
)) {
119 timer_mod(s
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + reload
);
123 static void aspeed_wdt_write(void *opaque
, hwaddr offset
, uint64_t data
,
126 AspeedWDTState
*s
= ASPEED_WDT(opaque
);
127 bool enable
= data
& WDT_CTRL_ENABLE
;
133 qemu_log_mask(LOG_GUEST_ERROR
,
134 "%s: write to read-only reg at offset 0x%"
135 HWADDR_PRIx
"\n", __func__
, offset
);
137 case WDT_RELOAD_VALUE
:
138 s
->regs
[WDT_RELOAD_VALUE
] = data
;
141 if ((data
& 0xFFFF) == WDT_RESTART_MAGIC
) {
142 s
->regs
[WDT_STATUS
] = s
->regs
[WDT_RELOAD_VALUE
];
143 aspeed_wdt_reload(s
, !(data
& WDT_CTRL_1MHZ_CLK
));
147 if (enable
&& !aspeed_wdt_is_enabled(s
)) {
148 s
->regs
[WDT_CTRL
] = data
;
149 aspeed_wdt_reload(s
, !(data
& WDT_CTRL_1MHZ_CLK
));
150 } else if (!enable
&& aspeed_wdt_is_enabled(s
)) {
151 s
->regs
[WDT_CTRL
] = data
;
155 case WDT_RESET_WIDTH
:
157 uint32_t property
= data
& WDT_POLARITY_MASK
;
159 if (property
&& is_ast2500(s
)) {
160 if (property
== WDT_ACTIVE_HIGH_MAGIC
) {
161 s
->regs
[WDT_RESET_WIDTH
] |= WDT_RESET_WIDTH_ACTIVE_HIGH
;
162 } else if (property
== WDT_ACTIVE_LOW_MAGIC
) {
163 s
->regs
[WDT_RESET_WIDTH
] &= ~WDT_RESET_WIDTH_ACTIVE_HIGH
;
164 } else if (property
== WDT_PUSH_PULL_MAGIC
) {
165 s
->regs
[WDT_RESET_WIDTH
] |= WDT_RESET_WIDTH_PUSH_PULL
;
166 } else if (property
== WDT_OPEN_DRAIN_MAGIC
) {
167 s
->regs
[WDT_RESET_WIDTH
] &= ~WDT_RESET_WIDTH_PUSH_PULL
;
170 s
->regs
[WDT_RESET_WIDTH
] &= ~s
->ext_pulse_width_mask
;
171 s
->regs
[WDT_RESET_WIDTH
] |= data
& s
->ext_pulse_width_mask
;
174 case WDT_TIMEOUT_STATUS
:
175 case WDT_TIMEOUT_CLEAR
:
176 qemu_log_mask(LOG_UNIMP
,
177 "%s: uninmplemented write at offset 0x%" HWADDR_PRIx
"\n",
181 qemu_log_mask(LOG_GUEST_ERROR
,
182 "%s: Out-of-bounds write at offset 0x%" HWADDR_PRIx
"\n",
188 static WatchdogTimerModel model
= {
189 .wdt_name
= TYPE_ASPEED_WDT
,
190 .wdt_description
= "Aspeed watchdog device",
193 static const VMStateDescription vmstate_aspeed_wdt
= {
194 .name
= "vmstate_aspeed_wdt",
196 .minimum_version_id
= 0,
197 .fields
= (VMStateField
[]) {
198 VMSTATE_TIMER_PTR(timer
, AspeedWDTState
),
199 VMSTATE_UINT32_ARRAY(regs
, AspeedWDTState
, ASPEED_WDT_REGS_MAX
),
200 VMSTATE_END_OF_LIST()
204 static const MemoryRegionOps aspeed_wdt_ops
= {
205 .read
= aspeed_wdt_read
,
206 .write
= aspeed_wdt_write
,
207 .endianness
= DEVICE_LITTLE_ENDIAN
,
208 .valid
.min_access_size
= 4,
209 .valid
.max_access_size
= 4,
210 .valid
.unaligned
= false,
213 static void aspeed_wdt_reset(DeviceState
*dev
)
215 AspeedWDTState
*s
= ASPEED_WDT(dev
);
217 s
->regs
[WDT_STATUS
] = 0x3EF1480;
218 s
->regs
[WDT_RELOAD_VALUE
] = 0x03EF1480;
219 s
->regs
[WDT_RESTART
] = 0;
220 s
->regs
[WDT_CTRL
] = 0;
221 s
->regs
[WDT_RESET_WIDTH
] = 0xFF;
226 static void aspeed_wdt_timer_expired(void *dev
)
228 AspeedWDTState
*s
= ASPEED_WDT(dev
);
230 /* Do not reset on SDRAM controller reset */
231 if (s
->scu
->regs
[SCU_RESET_CONTROL1
] & SCU_RESET_SDRAM
) {
233 s
->regs
[WDT_CTRL
] = 0;
237 qemu_log_mask(CPU_LOG_RESET
, "Watchdog timer expired.\n");
238 watchdog_perform_action();
242 #define PCLK_HZ 24000000
244 static void aspeed_wdt_realize(DeviceState
*dev
, Error
**errp
)
246 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
247 AspeedWDTState
*s
= ASPEED_WDT(dev
);
251 obj
= object_property_get_link(OBJECT(dev
), "scu", &err
);
253 error_propagate(errp
, err
);
254 error_prepend(errp
, "required link 'scu' not found: ");
257 s
->scu
= ASPEED_SCU(obj
);
259 if (!is_supported_silicon_rev(s
->silicon_rev
)) {
260 error_setg(errp
, "Unknown silicon revision: 0x%" PRIx32
,
265 switch (s
->silicon_rev
) {
266 case AST2400_A0_SILICON_REV
:
267 case AST2400_A1_SILICON_REV
:
268 s
->ext_pulse_width_mask
= 0xff;
270 case AST2500_A0_SILICON_REV
:
271 case AST2500_A1_SILICON_REV
:
272 s
->ext_pulse_width_mask
= 0xfffff;
275 g_assert_not_reached();
278 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, aspeed_wdt_timer_expired
, dev
);
280 /* FIXME: This setting should be derived from the SCU hw strapping
283 s
->pclk_freq
= PCLK_HZ
;
285 memory_region_init_io(&s
->iomem
, OBJECT(s
), &aspeed_wdt_ops
, s
,
286 TYPE_ASPEED_WDT
, ASPEED_WDT_REGS_MAX
* 4);
287 sysbus_init_mmio(sbd
, &s
->iomem
);
290 static Property aspeed_wdt_properties
[] = {
291 DEFINE_PROP_UINT32("silicon-rev", AspeedWDTState
, silicon_rev
, 0),
292 DEFINE_PROP_END_OF_LIST(),
295 static void aspeed_wdt_class_init(ObjectClass
*klass
, void *data
)
297 DeviceClass
*dc
= DEVICE_CLASS(klass
);
299 dc
->realize
= aspeed_wdt_realize
;
300 dc
->reset
= aspeed_wdt_reset
;
301 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
302 dc
->vmsd
= &vmstate_aspeed_wdt
;
303 dc
->props
= aspeed_wdt_properties
;
306 static const TypeInfo aspeed_wdt_info
= {
307 .parent
= TYPE_SYS_BUS_DEVICE
,
308 .name
= TYPE_ASPEED_WDT
,
309 .instance_size
= sizeof(AspeedWDTState
),
310 .class_init
= aspeed_wdt_class_init
,
313 static void wdt_aspeed_register_types(void)
315 watchdog_add_model(&model
);
316 type_register_static(&aspeed_wdt_info
);
319 type_init(wdt_aspeed_register_types
)