1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2016 IBM Corporation
5 * Joel Stanley <joel@jms.id.au>
8 #include <linux/delay.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/watchdog.h>
17 struct watchdog_device wdd
;
22 struct aspeed_wdt_config
{
23 u32 ext_pulse_width_mask
;
26 static const struct aspeed_wdt_config ast2400_config
= {
27 .ext_pulse_width_mask
= 0xff,
30 static const struct aspeed_wdt_config ast2500_config
= {
31 .ext_pulse_width_mask
= 0xfffff,
34 static const struct of_device_id aspeed_wdt_of_table
[] = {
35 { .compatible
= "aspeed,ast2400-wdt", .data
= &ast2400_config
},
36 { .compatible
= "aspeed,ast2500-wdt", .data
= &ast2500_config
},
39 MODULE_DEVICE_TABLE(of
, aspeed_wdt_of_table
);
41 #define WDT_STATUS 0x00
42 #define WDT_RELOAD_VALUE 0x04
43 #define WDT_RESTART 0x08
45 #define WDT_CTRL_BOOT_SECONDARY BIT(7)
46 #define WDT_CTRL_RESET_MODE_SOC (0x00 << 5)
47 #define WDT_CTRL_RESET_MODE_FULL_CHIP (0x01 << 5)
48 #define WDT_CTRL_RESET_MODE_ARM_CPU (0x10 << 5)
49 #define WDT_CTRL_1MHZ_CLK BIT(4)
50 #define WDT_CTRL_WDT_EXT BIT(3)
51 #define WDT_CTRL_WDT_INTR BIT(2)
52 #define WDT_CTRL_RESET_SYSTEM BIT(1)
53 #define WDT_CTRL_ENABLE BIT(0)
54 #define WDT_TIMEOUT_STATUS 0x10
55 #define WDT_TIMEOUT_STATUS_BOOT_SECONDARY BIT(1)
58 * WDT_RESET_WIDTH controls the characteristics of the external pulse (if
59 * enabled), specifically:
62 * * Drive mode: push-pull vs open-drain
63 * * Polarity: Active high or active low
65 * Pulse duration configuration is available on both the AST2400 and AST2500,
66 * though the field changes between SoCs:
71 * This difference is captured in struct aspeed_wdt_config.
73 * The AST2500 exposes the drive mode and polarity options, but not in a
74 * regular fashion. For read purposes, bit 31 represents active high or low,
75 * and bit 30 represents push-pull or open-drain. With respect to write, magic
76 * values need to be written to the top byte to change the state of the drive
77 * mode and polarity bits. Any other value written to the top byte has no
78 * effect on the state of the drive mode or polarity bits. However, the pulse
79 * width value must be preserved (as desired) if written.
81 #define WDT_RESET_WIDTH 0x18
82 #define WDT_RESET_WIDTH_ACTIVE_HIGH BIT(31)
83 #define WDT_ACTIVE_HIGH_MAGIC (0xA5 << 24)
84 #define WDT_ACTIVE_LOW_MAGIC (0x5A << 24)
85 #define WDT_RESET_WIDTH_PUSH_PULL BIT(30)
86 #define WDT_PUSH_PULL_MAGIC (0xA8 << 24)
87 #define WDT_OPEN_DRAIN_MAGIC (0x8A << 24)
89 #define WDT_RESTART_MAGIC 0x4755
91 /* 32 bits at 1MHz, in milliseconds */
92 #define WDT_MAX_TIMEOUT_MS 4294967
93 #define WDT_DEFAULT_TIMEOUT 30
94 #define WDT_RATE_1MHZ 1000000
96 static struct aspeed_wdt
*to_aspeed_wdt(struct watchdog_device
*wdd
)
98 return container_of(wdd
, struct aspeed_wdt
, wdd
);
101 static void aspeed_wdt_enable(struct aspeed_wdt
*wdt
, int count
)
103 wdt
->ctrl
|= WDT_CTRL_ENABLE
;
105 writel(0, wdt
->base
+ WDT_CTRL
);
106 writel(count
, wdt
->base
+ WDT_RELOAD_VALUE
);
107 writel(WDT_RESTART_MAGIC
, wdt
->base
+ WDT_RESTART
);
108 writel(wdt
->ctrl
, wdt
->base
+ WDT_CTRL
);
111 static int aspeed_wdt_start(struct watchdog_device
*wdd
)
113 struct aspeed_wdt
*wdt
= to_aspeed_wdt(wdd
);
115 aspeed_wdt_enable(wdt
, wdd
->timeout
* WDT_RATE_1MHZ
);
120 static int aspeed_wdt_stop(struct watchdog_device
*wdd
)
122 struct aspeed_wdt
*wdt
= to_aspeed_wdt(wdd
);
124 wdt
->ctrl
&= ~WDT_CTRL_ENABLE
;
125 writel(wdt
->ctrl
, wdt
->base
+ WDT_CTRL
);
130 static int aspeed_wdt_ping(struct watchdog_device
*wdd
)
132 struct aspeed_wdt
*wdt
= to_aspeed_wdt(wdd
);
134 writel(WDT_RESTART_MAGIC
, wdt
->base
+ WDT_RESTART
);
139 static int aspeed_wdt_set_timeout(struct watchdog_device
*wdd
,
140 unsigned int timeout
)
142 struct aspeed_wdt
*wdt
= to_aspeed_wdt(wdd
);
145 wdd
->timeout
= timeout
;
147 actual
= min(timeout
, wdd
->max_hw_heartbeat_ms
* 1000);
149 writel(actual
* WDT_RATE_1MHZ
, wdt
->base
+ WDT_RELOAD_VALUE
);
150 writel(WDT_RESTART_MAGIC
, wdt
->base
+ WDT_RESTART
);
155 static int aspeed_wdt_restart(struct watchdog_device
*wdd
,
156 unsigned long action
, void *data
)
158 struct aspeed_wdt
*wdt
= to_aspeed_wdt(wdd
);
160 wdt
->ctrl
&= ~WDT_CTRL_BOOT_SECONDARY
;
161 aspeed_wdt_enable(wdt
, 128 * WDT_RATE_1MHZ
/ 1000);
168 static const struct watchdog_ops aspeed_wdt_ops
= {
169 .start
= aspeed_wdt_start
,
170 .stop
= aspeed_wdt_stop
,
171 .ping
= aspeed_wdt_ping
,
172 .set_timeout
= aspeed_wdt_set_timeout
,
173 .restart
= aspeed_wdt_restart
,
174 .owner
= THIS_MODULE
,
177 static const struct watchdog_info aspeed_wdt_info
= {
178 .options
= WDIOF_KEEPALIVEPING
181 .identity
= KBUILD_MODNAME
,
184 static int aspeed_wdt_probe(struct platform_device
*pdev
)
186 struct device
*dev
= &pdev
->dev
;
187 const struct aspeed_wdt_config
*config
;
188 const struct of_device_id
*ofdid
;
189 struct aspeed_wdt
*wdt
;
190 struct device_node
*np
;
191 const char *reset_type
;
196 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
200 wdt
->base
= devm_platform_ioremap_resource(pdev
, 0);
201 if (IS_ERR(wdt
->base
))
202 return PTR_ERR(wdt
->base
);
205 * The ast2400 wdt can run at PCLK, or 1MHz. The ast2500 only
206 * runs at 1MHz. We chose to always run at 1MHz, as there's no
207 * good reason to have a faster watchdog counter.
209 wdt
->wdd
.info
= &aspeed_wdt_info
;
210 wdt
->wdd
.ops
= &aspeed_wdt_ops
;
211 wdt
->wdd
.max_hw_heartbeat_ms
= WDT_MAX_TIMEOUT_MS
;
212 wdt
->wdd
.parent
= dev
;
214 wdt
->wdd
.timeout
= WDT_DEFAULT_TIMEOUT
;
215 watchdog_init_timeout(&wdt
->wdd
, 0, dev
);
219 ofdid
= of_match_node(aspeed_wdt_of_table
, np
);
222 config
= ofdid
->data
;
224 wdt
->ctrl
= WDT_CTRL_1MHZ_CLK
;
227 * Control reset on a per-device basis to ensure the
228 * host is not affected by a BMC reboot
230 ret
= of_property_read_string(np
, "aspeed,reset-type", &reset_type
);
232 wdt
->ctrl
|= WDT_CTRL_RESET_MODE_SOC
| WDT_CTRL_RESET_SYSTEM
;
234 if (!strcmp(reset_type
, "cpu"))
235 wdt
->ctrl
|= WDT_CTRL_RESET_MODE_ARM_CPU
|
236 WDT_CTRL_RESET_SYSTEM
;
237 else if (!strcmp(reset_type
, "soc"))
238 wdt
->ctrl
|= WDT_CTRL_RESET_MODE_SOC
|
239 WDT_CTRL_RESET_SYSTEM
;
240 else if (!strcmp(reset_type
, "system"))
241 wdt
->ctrl
|= WDT_CTRL_RESET_MODE_FULL_CHIP
|
242 WDT_CTRL_RESET_SYSTEM
;
243 else if (strcmp(reset_type
, "none"))
246 if (of_property_read_bool(np
, "aspeed,external-signal"))
247 wdt
->ctrl
|= WDT_CTRL_WDT_EXT
;
248 if (of_property_read_bool(np
, "aspeed,alt-boot"))
249 wdt
->ctrl
|= WDT_CTRL_BOOT_SECONDARY
;
251 if (readl(wdt
->base
+ WDT_CTRL
) & WDT_CTRL_ENABLE
) {
253 * The watchdog is running, but invoke aspeed_wdt_start() to
254 * write wdt->ctrl to WDT_CTRL to ensure the watchdog's
255 * configuration conforms to the driver's expectations.
256 * Primarily, ensure we're using the 1MHz clock source.
258 aspeed_wdt_start(&wdt
->wdd
);
259 set_bit(WDOG_HW_RUNNING
, &wdt
->wdd
.status
);
262 if (of_device_is_compatible(np
, "aspeed,ast2500-wdt")) {
263 u32 reg
= readl(wdt
->base
+ WDT_RESET_WIDTH
);
265 reg
&= config
->ext_pulse_width_mask
;
266 if (of_property_read_bool(np
, "aspeed,ext-push-pull"))
267 reg
|= WDT_PUSH_PULL_MAGIC
;
269 reg
|= WDT_OPEN_DRAIN_MAGIC
;
271 writel(reg
, wdt
->base
+ WDT_RESET_WIDTH
);
273 reg
&= config
->ext_pulse_width_mask
;
274 if (of_property_read_bool(np
, "aspeed,ext-active-high"))
275 reg
|= WDT_ACTIVE_HIGH_MAGIC
;
277 reg
|= WDT_ACTIVE_LOW_MAGIC
;
279 writel(reg
, wdt
->base
+ WDT_RESET_WIDTH
);
282 if (!of_property_read_u32(np
, "aspeed,ext-pulse-duration", &duration
)) {
283 u32 max_duration
= config
->ext_pulse_width_mask
+ 1;
285 if (duration
== 0 || duration
> max_duration
) {
286 dev_err(dev
, "Invalid pulse duration: %uus\n",
288 duration
= max(1U, min(max_duration
, duration
));
289 dev_info(dev
, "Pulse duration set to %uus\n",
294 * The watchdog is always configured with a 1MHz source, so
295 * there is no need to scale the microsecond value. However we
296 * need to offset it - from the datasheet:
298 * "This register decides the asserting duration of wdt_ext and
299 * wdt_rstarm signal. The default value is 0xFF. It means the
300 * default asserting duration of wdt_ext and wdt_rstarm is
303 * This implies a value of 0 gives a 1us pulse.
305 writel(duration
- 1, wdt
->base
+ WDT_RESET_WIDTH
);
308 status
= readl(wdt
->base
+ WDT_TIMEOUT_STATUS
);
309 if (status
& WDT_TIMEOUT_STATUS_BOOT_SECONDARY
)
310 wdt
->wdd
.bootstatus
= WDIOF_CARDRESET
;
312 return devm_watchdog_register_device(dev
, &wdt
->wdd
);
315 static struct platform_driver aspeed_watchdog_driver
= {
316 .probe
= aspeed_wdt_probe
,
318 .name
= KBUILD_MODNAME
,
319 .of_match_table
= of_match_ptr(aspeed_wdt_of_table
),
323 static int __init
aspeed_wdt_init(void)
325 return platform_driver_register(&aspeed_watchdog_driver
);
327 arch_initcall(aspeed_wdt_init
);
329 static void __exit
aspeed_wdt_exit(void)
331 platform_driver_unregister(&aspeed_watchdog_driver
);
333 module_exit(aspeed_wdt_exit
);
335 MODULE_DESCRIPTION("Aspeed Watchdog Driver");
336 MODULE_LICENSE("GPL");