2 * Copyright 2016 IBM Corporation
4 * Joel Stanley <joel@jms.id.au>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/delay.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/watchdog.h>
21 struct watchdog_device wdd;
26 static const struct of_device_id aspeed_wdt_of_table[] = {
27 { .compatible = "aspeed,ast2400-wdt" },
28 { .compatible = "aspeed,ast2500-wdt" },
31 MODULE_DEVICE_TABLE(of, aspeed_wdt_of_table);
33 #define WDT_STATUS 0x00
34 #define WDT_RELOAD_VALUE 0x04
35 #define WDT_RESTART 0x08
37 #define WDT_CTRL_RESET_MODE_SOC (0x00 << 5)
38 #define WDT_CTRL_RESET_MODE_FULL_CHIP (0x01 << 5)
39 #define WDT_CTRL_1MHZ_CLK BIT(4)
40 #define WDT_CTRL_WDT_EXT BIT(3)
41 #define WDT_CTRL_WDT_INTR BIT(2)
42 #define WDT_CTRL_RESET_SYSTEM BIT(1)
43 #define WDT_CTRL_ENABLE BIT(0)
45 #define WDT_RESTART_MAGIC 0x4755
47 /* 32 bits at 1MHz, in milliseconds */
48 #define WDT_MAX_TIMEOUT_MS 4294967
49 #define WDT_DEFAULT_TIMEOUT 30
50 #define WDT_RATE_1MHZ 1000000
52 static struct aspeed_wdt *to_aspeed_wdt(struct watchdog_device *wdd)
54 return container_of(wdd, struct aspeed_wdt, wdd);
57 static void aspeed_wdt_enable(struct aspeed_wdt *wdt, int count)
59 wdt->ctrl |= WDT_CTRL_ENABLE;
61 writel(0, wdt->base + WDT_CTRL);
62 writel(count, wdt->base + WDT_RELOAD_VALUE);
63 writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
64 writel(wdt->ctrl, wdt->base + WDT_CTRL);
67 static int aspeed_wdt_start(struct watchdog_device *wdd)
69 struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
71 aspeed_wdt_enable(wdt, wdd->timeout * WDT_RATE_1MHZ);
76 static int aspeed_wdt_stop(struct watchdog_device *wdd)
78 struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
80 wdt->ctrl &= ~WDT_CTRL_ENABLE;
81 writel(wdt->ctrl, wdt->base + WDT_CTRL);
86 static int aspeed_wdt_ping(struct watchdog_device *wdd)
88 struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
90 writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
95 static int aspeed_wdt_set_timeout(struct watchdog_device *wdd,
98 struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
101 wdd->timeout = timeout;
103 actual = min(timeout, wdd->max_hw_heartbeat_ms * 1000);
105 writel(actual * WDT_RATE_1MHZ, wdt->base + WDT_RELOAD_VALUE);
106 writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
111 static int aspeed_wdt_restart(struct watchdog_device *wdd,
112 unsigned long action, void *data)
114 struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
116 aspeed_wdt_enable(wdt, 128 * WDT_RATE_1MHZ / 1000);
123 static const struct watchdog_ops aspeed_wdt_ops = {
124 .start = aspeed_wdt_start,
125 .stop = aspeed_wdt_stop,
126 .ping = aspeed_wdt_ping,
127 .set_timeout = aspeed_wdt_set_timeout,
128 .restart = aspeed_wdt_restart,
129 .owner = THIS_MODULE,
132 static const struct watchdog_info aspeed_wdt_info = {
133 .options = WDIOF_KEEPALIVEPING
136 .identity = KBUILD_MODNAME,
139 static int aspeed_wdt_remove(struct platform_device *pdev)
141 struct aspeed_wdt *wdt = platform_get_drvdata(pdev);
143 watchdog_unregister_device(&wdt->wdd);
148 static int aspeed_wdt_probe(struct platform_device *pdev)
150 struct aspeed_wdt *wdt;
151 struct resource *res;
154 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
158 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
159 wdt->base = devm_ioremap_resource(&pdev->dev, res);
160 if (IS_ERR(wdt->base))
161 return PTR_ERR(wdt->base);
164 * The ast2400 wdt can run at PCLK, or 1MHz. The ast2500 only
165 * runs at 1MHz. We chose to always run at 1MHz, as there's no
166 * good reason to have a faster watchdog counter.
168 wdt->wdd.info = &aspeed_wdt_info;
169 wdt->wdd.ops = &aspeed_wdt_ops;
170 wdt->wdd.max_hw_heartbeat_ms = WDT_MAX_TIMEOUT_MS;
171 wdt->wdd.parent = &pdev->dev;
173 wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
174 watchdog_init_timeout(&wdt->wdd, 0, &pdev->dev);
177 * Control reset on a per-device basis to ensure the
178 * host is not affected by a BMC reboot, so only reset
179 * the SOC and not the full chip
181 wdt->ctrl = WDT_CTRL_RESET_MODE_SOC |
183 WDT_CTRL_RESET_SYSTEM;
185 if (readl(wdt->base + WDT_CTRL) & WDT_CTRL_ENABLE) {
186 aspeed_wdt_start(&wdt->wdd);
187 set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
190 ret = watchdog_register_device(&wdt->wdd);
192 dev_err(&pdev->dev, "failed to register\n");
196 platform_set_drvdata(pdev, wdt);
201 static struct platform_driver aspeed_watchdog_driver = {
202 .probe = aspeed_wdt_probe,
203 .remove = aspeed_wdt_remove,
205 .name = KBUILD_MODNAME,
206 .of_match_table = of_match_ptr(aspeed_wdt_of_table),
209 module_platform_driver(aspeed_watchdog_driver);
211 MODULE_DESCRIPTION("Aspeed Watchdog Driver");
212 MODULE_LICENSE("GPL");