2 * sunxi Watchdog Driver
4 * Copyright (c) 2013 Carlo Caione
5 * 2012 Henrik Nordstrom
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 * (c) Copyright 2010 Novell, Inc.
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/notifier.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/reboot.h>
29 #include <linux/types.h>
30 #include <linux/watchdog.h>
32 #define WDT_MAX_TIMEOUT 16
33 #define WDT_MIN_TIMEOUT 1
34 #define WDT_TIMEOUT_MASK 0x0F
36 #define WDT_CTRL_RELOAD ((1 << 0) | (0x0a57 << 1))
38 #define WDT_MODE_EN (1 << 0)
40 #define DRV_NAME "sunxi-wdt"
41 #define DRV_VERSION "1.0"
43 static bool nowayout = WATCHDOG_NOWAYOUT;
44 static unsigned int timeout = WDT_MAX_TIMEOUT;
47 * This structure stores the register offsets for different variants
48 * of Allwinner's watchdog hardware.
50 struct sunxi_wdt_reg {
59 struct sunxi_wdt_dev {
60 struct watchdog_device wdt_dev;
61 void __iomem *wdt_base;
62 const struct sunxi_wdt_reg *wdt_regs;
63 struct notifier_block restart_handler;
67 * wdt_timeout_map maps the watchdog timer interval value in seconds to
68 * the value of the register WDT_MODE at bits .wdt_timeout_shift ~ +3
70 * [timeout seconds] = register value
74 static const int wdt_timeout_map[] = {
89 static int sunxi_restart_handle(struct notifier_block *this, unsigned long mode,
92 struct sunxi_wdt_dev *sunxi_wdt = container_of(this,
95 void __iomem *wdt_base = sunxi_wdt->wdt_base;
96 const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
99 /* Set system reset function */
100 val = readl(wdt_base + regs->wdt_cfg);
101 val &= ~(regs->wdt_reset_mask);
102 val |= regs->wdt_reset_val;
103 writel(val, wdt_base + regs->wdt_cfg);
105 /* Set lowest timeout and enable watchdog */
106 val = readl(wdt_base + regs->wdt_mode);
107 val &= ~(WDT_TIMEOUT_MASK << regs->wdt_timeout_shift);
109 writel(val, wdt_base + regs->wdt_mode);
112 * Restart the watchdog. The default (and lowest) interval
113 * value for the watchdog is 0.5s.
115 writel(WDT_CTRL_RELOAD, wdt_base + regs->wdt_ctrl);
119 val = readl(wdt_base + regs->wdt_mode);
121 writel(val, wdt_base + regs->wdt_mode);
126 static int sunxi_wdt_ping(struct watchdog_device *wdt_dev)
128 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
129 void __iomem *wdt_base = sunxi_wdt->wdt_base;
130 const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
132 writel(WDT_CTRL_RELOAD, wdt_base + regs->wdt_ctrl);
137 static int sunxi_wdt_set_timeout(struct watchdog_device *wdt_dev,
138 unsigned int timeout)
140 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
141 void __iomem *wdt_base = sunxi_wdt->wdt_base;
142 const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
145 if (wdt_timeout_map[timeout] == 0)
148 sunxi_wdt->wdt_dev.timeout = timeout;
150 reg = readl(wdt_base + regs->wdt_mode);
151 reg &= ~(WDT_TIMEOUT_MASK << regs->wdt_timeout_shift);
152 reg |= wdt_timeout_map[timeout] << regs->wdt_timeout_shift;
153 writel(reg, wdt_base + regs->wdt_mode);
155 sunxi_wdt_ping(wdt_dev);
160 static int sunxi_wdt_stop(struct watchdog_device *wdt_dev)
162 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
163 void __iomem *wdt_base = sunxi_wdt->wdt_base;
164 const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
166 writel(0, wdt_base + regs->wdt_mode);
171 static int sunxi_wdt_start(struct watchdog_device *wdt_dev)
174 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
175 void __iomem *wdt_base = sunxi_wdt->wdt_base;
176 const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
179 ret = sunxi_wdt_set_timeout(&sunxi_wdt->wdt_dev,
180 sunxi_wdt->wdt_dev.timeout);
184 /* Set system reset function */
185 reg = readl(wdt_base + regs->wdt_cfg);
186 reg &= ~(regs->wdt_reset_mask);
187 reg |= ~(regs->wdt_reset_val);
188 writel(reg, wdt_base + regs->wdt_cfg);
190 /* Enable watchdog */
191 reg = readl(wdt_base + regs->wdt_mode);
193 writel(reg, wdt_base + regs->wdt_mode);
198 static const struct watchdog_info sunxi_wdt_info = {
199 .identity = DRV_NAME,
200 .options = WDIOF_SETTIMEOUT |
201 WDIOF_KEEPALIVEPING |
205 static const struct watchdog_ops sunxi_wdt_ops = {
206 .owner = THIS_MODULE,
207 .start = sunxi_wdt_start,
208 .stop = sunxi_wdt_stop,
209 .ping = sunxi_wdt_ping,
210 .set_timeout = sunxi_wdt_set_timeout,
213 static const struct sunxi_wdt_reg sun4i_wdt_reg = {
217 .wdt_timeout_shift = 3,
218 .wdt_reset_mask = 0x02,
219 .wdt_reset_val = 0x02,
222 static const struct of_device_id sunxi_wdt_dt_ids[] = {
223 { .compatible = "allwinner,sun4i-a10-wdt", .data = &sun4i_wdt_reg },
226 MODULE_DEVICE_TABLE(of, sunxi_wdt_dt_ids);
228 static int sunxi_wdt_probe(struct platform_device *pdev)
230 struct sunxi_wdt_dev *sunxi_wdt;
231 const struct of_device_id *device;
232 struct resource *res;
235 sunxi_wdt = devm_kzalloc(&pdev->dev, sizeof(*sunxi_wdt), GFP_KERNEL);
239 platform_set_drvdata(pdev, sunxi_wdt);
241 device = of_match_device(sunxi_wdt_dt_ids, &pdev->dev);
245 sunxi_wdt->wdt_regs = device->data;
247 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
248 sunxi_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
249 if (IS_ERR(sunxi_wdt->wdt_base))
250 return PTR_ERR(sunxi_wdt->wdt_base);
252 sunxi_wdt->wdt_dev.info = &sunxi_wdt_info;
253 sunxi_wdt->wdt_dev.ops = &sunxi_wdt_ops;
254 sunxi_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
255 sunxi_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
256 sunxi_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
257 sunxi_wdt->wdt_dev.parent = &pdev->dev;
259 watchdog_init_timeout(&sunxi_wdt->wdt_dev, timeout, &pdev->dev);
260 watchdog_set_nowayout(&sunxi_wdt->wdt_dev, nowayout);
262 watchdog_set_drvdata(&sunxi_wdt->wdt_dev, sunxi_wdt);
264 sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
266 err = watchdog_register_device(&sunxi_wdt->wdt_dev);
270 sunxi_wdt->restart_handler.notifier_call = sunxi_restart_handle;
271 sunxi_wdt->restart_handler.priority = 128;
272 err = register_restart_handler(&sunxi_wdt->restart_handler);
275 "cannot register restart handler (err=%d)\n", err);
277 dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
278 sunxi_wdt->wdt_dev.timeout, nowayout);
283 static int sunxi_wdt_remove(struct platform_device *pdev)
285 struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
287 unregister_restart_handler(&sunxi_wdt->restart_handler);
289 watchdog_unregister_device(&sunxi_wdt->wdt_dev);
290 watchdog_set_drvdata(&sunxi_wdt->wdt_dev, NULL);
295 static void sunxi_wdt_shutdown(struct platform_device *pdev)
297 struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
299 sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
302 static struct platform_driver sunxi_wdt_driver = {
303 .probe = sunxi_wdt_probe,
304 .remove = sunxi_wdt_remove,
305 .shutdown = sunxi_wdt_shutdown,
307 .owner = THIS_MODULE,
309 .of_match_table = sunxi_wdt_dt_ids,
313 module_platform_driver(sunxi_wdt_driver);
315 module_param(timeout, uint, 0);
316 MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
318 module_param(nowayout, bool, 0);
319 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
320 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
322 MODULE_LICENSE("GPL");
323 MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
324 MODULE_AUTHOR("Henrik Nordstrom <henrik@henriknordstrom.net>");
325 MODULE_DESCRIPTION("sunxi WatchDog Timer Driver");
326 MODULE_VERSION(DRV_VERSION);