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/platform_device.h>
27 #include <linux/reboot.h>
28 #include <linux/types.h>
29 #include <linux/watchdog.h>
31 #define WDT_MAX_TIMEOUT 16
32 #define WDT_MIN_TIMEOUT 1
33 #define WDT_MODE_TIMEOUT(n) ((n) << 3)
34 #define WDT_TIMEOUT_MASK WDT_MODE_TIMEOUT(0x0F)
37 #define WDT_CTRL_RELOAD ((1 << 0) | (0x0a57 << 1))
40 #define WDT_MODE_EN (1 << 0)
41 #define WDT_MODE_RST_EN (1 << 1)
43 #define DRV_NAME "sunxi-wdt"
44 #define DRV_VERSION "1.0"
46 static bool nowayout = WATCHDOG_NOWAYOUT;
47 static unsigned int timeout = WDT_MAX_TIMEOUT;
49 struct sunxi_wdt_dev {
50 struct watchdog_device wdt_dev;
51 void __iomem *wdt_base;
52 struct notifier_block restart_handler;
56 * wdt_timeout_map maps the watchdog timer interval value in seconds to
57 * the value of the register WDT_MODE bit 3:6
59 * [timeout seconds] = register value
63 static const int wdt_timeout_map[] = {
78 static int sunxi_restart_handle(struct notifier_block *this, unsigned long mode,
81 struct sunxi_wdt_dev *sunxi_wdt = container_of(this,
84 void __iomem *wdt_base = sunxi_wdt->wdt_base;
86 /* Enable timer and set reset bit in the watchdog */
87 writel(WDT_MODE_EN | WDT_MODE_RST_EN, wdt_base + WDT_MODE);
90 * Restart the watchdog. The default (and lowest) interval
91 * value for the watchdog is 0.5s.
93 writel(WDT_CTRL_RELOAD, wdt_base + WDT_CTRL);
97 writel(WDT_MODE_EN | WDT_MODE_RST_EN, wdt_base + WDT_MODE);
102 static int sunxi_wdt_ping(struct watchdog_device *wdt_dev)
104 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
105 void __iomem *wdt_base = sunxi_wdt->wdt_base;
107 iowrite32(WDT_CTRL_RELOAD, wdt_base + WDT_CTRL);
112 static int sunxi_wdt_set_timeout(struct watchdog_device *wdt_dev,
113 unsigned int timeout)
115 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
116 void __iomem *wdt_base = sunxi_wdt->wdt_base;
119 if (wdt_timeout_map[timeout] == 0)
122 sunxi_wdt->wdt_dev.timeout = timeout;
124 reg = ioread32(wdt_base + WDT_MODE);
125 reg &= ~WDT_TIMEOUT_MASK;
126 reg |= WDT_MODE_TIMEOUT(wdt_timeout_map[timeout]);
127 iowrite32(reg, wdt_base + WDT_MODE);
129 sunxi_wdt_ping(wdt_dev);
134 static int sunxi_wdt_stop(struct watchdog_device *wdt_dev)
136 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
137 void __iomem *wdt_base = sunxi_wdt->wdt_base;
139 iowrite32(0, wdt_base + WDT_MODE);
144 static int sunxi_wdt_start(struct watchdog_device *wdt_dev)
147 struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
148 void __iomem *wdt_base = sunxi_wdt->wdt_base;
151 ret = sunxi_wdt_set_timeout(&sunxi_wdt->wdt_dev,
152 sunxi_wdt->wdt_dev.timeout);
156 reg = ioread32(wdt_base + WDT_MODE);
157 reg |= (WDT_MODE_RST_EN | WDT_MODE_EN);
158 iowrite32(reg, wdt_base + WDT_MODE);
163 static const struct watchdog_info sunxi_wdt_info = {
164 .identity = DRV_NAME,
165 .options = WDIOF_SETTIMEOUT |
166 WDIOF_KEEPALIVEPING |
170 static const struct watchdog_ops sunxi_wdt_ops = {
171 .owner = THIS_MODULE,
172 .start = sunxi_wdt_start,
173 .stop = sunxi_wdt_stop,
174 .ping = sunxi_wdt_ping,
175 .set_timeout = sunxi_wdt_set_timeout,
178 static int sunxi_wdt_probe(struct platform_device *pdev)
180 struct sunxi_wdt_dev *sunxi_wdt;
181 struct resource *res;
184 sunxi_wdt = devm_kzalloc(&pdev->dev, sizeof(*sunxi_wdt), GFP_KERNEL);
188 platform_set_drvdata(pdev, sunxi_wdt);
190 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
191 sunxi_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
192 if (IS_ERR(sunxi_wdt->wdt_base))
193 return PTR_ERR(sunxi_wdt->wdt_base);
195 sunxi_wdt->wdt_dev.info = &sunxi_wdt_info;
196 sunxi_wdt->wdt_dev.ops = &sunxi_wdt_ops;
197 sunxi_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
198 sunxi_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
199 sunxi_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
200 sunxi_wdt->wdt_dev.parent = &pdev->dev;
202 watchdog_init_timeout(&sunxi_wdt->wdt_dev, timeout, &pdev->dev);
203 watchdog_set_nowayout(&sunxi_wdt->wdt_dev, nowayout);
205 watchdog_set_drvdata(&sunxi_wdt->wdt_dev, sunxi_wdt);
207 sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
209 err = watchdog_register_device(&sunxi_wdt->wdt_dev);
213 sunxi_wdt->restart_handler.notifier_call = sunxi_restart_handle;
214 sunxi_wdt->restart_handler.priority = 128;
215 err = register_restart_handler(&sunxi_wdt->restart_handler);
218 "cannot register restart handler (err=%d)\n", err);
220 dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
221 sunxi_wdt->wdt_dev.timeout, nowayout);
226 static int sunxi_wdt_remove(struct platform_device *pdev)
228 struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
230 unregister_restart_handler(&sunxi_wdt->restart_handler);
232 watchdog_unregister_device(&sunxi_wdt->wdt_dev);
233 watchdog_set_drvdata(&sunxi_wdt->wdt_dev, NULL);
238 static void sunxi_wdt_shutdown(struct platform_device *pdev)
240 struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
242 sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
245 static const struct of_device_id sunxi_wdt_dt_ids[] = {
246 { .compatible = "allwinner,sun4i-a10-wdt" },
249 MODULE_DEVICE_TABLE(of, sunxi_wdt_dt_ids);
251 static struct platform_driver sunxi_wdt_driver = {
252 .probe = sunxi_wdt_probe,
253 .remove = sunxi_wdt_remove,
254 .shutdown = sunxi_wdt_shutdown,
256 .owner = THIS_MODULE,
258 .of_match_table = sunxi_wdt_dt_ids,
262 module_platform_driver(sunxi_wdt_driver);
264 module_param(timeout, uint, 0);
265 MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
267 module_param(nowayout, bool, 0);
268 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
269 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
271 MODULE_LICENSE("GPL");
272 MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
273 MODULE_AUTHOR("Henrik Nordstrom <henrik@henriknordstrom.net>");
274 MODULE_DESCRIPTION("sunxi WatchDog Timer Driver");
275 MODULE_VERSION(DRV_VERSION);