2 * Atheros AR71XX/AR724X/AR913X built-in hardware watchdog timer.
4 * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 * This driver was based on: drivers/watchdog/ixp4xx_wdt.c
8 * Author: Deepak Saxena <dsaxena@plexity.net>
9 * Copyright 2004 (c) MontaVista, Software, Inc.
11 * which again was based on sa1100 driver,
12 * Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License version 2 as published
16 * by the Free Software Foundation.
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/bitops.h>
23 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/miscdevice.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/platform_device.h>
31 #include <linux/types.h>
32 #include <linux/watchdog.h>
33 #include <linux/clk.h>
34 #include <linux/err.h>
36 #include <linux/of_platform.h>
38 #define DRIVER_NAME "ath79-wdt"
40 #define WDT_TIMEOUT 15 /* seconds */
42 #define WDOG_REG_CTRL 0x00
43 #define WDOG_REG_TIMER 0x04
45 #define WDOG_CTRL_LAST_RESET BIT(31)
46 #define WDOG_CTRL_ACTION_MASK 3
47 #define WDOG_CTRL_ACTION_NONE 0 /* no action */
48 #define WDOG_CTRL_ACTION_GPI 1 /* general purpose interrupt */
49 #define WDOG_CTRL_ACTION_NMI 2 /* NMI */
50 #define WDOG_CTRL_ACTION_FCR 3 /* full chip reset */
52 static bool nowayout = WATCHDOG_NOWAYOUT;
53 module_param(nowayout, bool, 0);
54 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
55 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
57 static int timeout = WDT_TIMEOUT;
58 module_param(timeout, int, 0);
59 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
60 "(default=" __MODULE_STRING(WDT_TIMEOUT) "s)");
62 static unsigned long wdt_flags;
64 #define WDT_FLAGS_BUSY 0
65 #define WDT_FLAGS_EXPECT_CLOSE 1
67 static struct clk *wdt_clk;
68 static unsigned long wdt_freq;
69 static int boot_status;
70 static int max_timeout;
71 static void __iomem *wdt_base;
73 static inline void ath79_wdt_wr(unsigned reg, u32 val)
75 iowrite32(val, wdt_base + reg);
78 static inline u32 ath79_wdt_rr(unsigned reg)
80 return ioread32(wdt_base + reg);
83 static inline void ath79_wdt_keepalive(void)
85 ath79_wdt_wr(WDOG_REG_TIMER, wdt_freq * timeout);
87 ath79_wdt_rr(WDOG_REG_TIMER);
90 static inline void ath79_wdt_enable(void)
92 ath79_wdt_keepalive();
93 ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_FCR);
95 ath79_wdt_rr(WDOG_REG_CTRL);
98 static inline void ath79_wdt_disable(void)
100 ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_NONE);
102 ath79_wdt_rr(WDOG_REG_CTRL);
105 static int ath79_wdt_set_timeout(int val)
107 if (val < 1 || val > max_timeout)
111 ath79_wdt_keepalive();
116 static int ath79_wdt_open(struct inode *inode, struct file *file)
118 if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
121 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
124 return nonseekable_open(inode, file);
127 static int ath79_wdt_release(struct inode *inode, struct file *file)
129 if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags))
132 pr_crit("device closed unexpectedly, watchdog timer will not stop!\n");
133 ath79_wdt_keepalive();
136 clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
137 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
142 static ssize_t ath79_wdt_write(struct file *file, const char *data,
143 size_t len, loff_t *ppos)
149 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
151 for (i = 0; i != len; i++) {
154 if (get_user(c, data + i))
158 set_bit(WDT_FLAGS_EXPECT_CLOSE,
163 ath79_wdt_keepalive();
169 static const struct watchdog_info ath79_wdt_info = {
170 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
171 WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
172 .firmware_version = 0,
173 .identity = "ATH79 watchdog",
176 static long ath79_wdt_ioctl(struct file *file, unsigned int cmd,
179 void __user *argp = (void __user *)arg;
180 int __user *p = argp;
185 case WDIOC_GETSUPPORT:
186 err = copy_to_user(argp, &ath79_wdt_info,
187 sizeof(ath79_wdt_info)) ? -EFAULT : 0;
190 case WDIOC_GETSTATUS:
191 err = put_user(0, p);
194 case WDIOC_GETBOOTSTATUS:
195 err = put_user(boot_status, p);
198 case WDIOC_KEEPALIVE:
199 ath79_wdt_keepalive();
203 case WDIOC_SETTIMEOUT:
204 err = get_user(t, p);
208 err = ath79_wdt_set_timeout(t);
213 case WDIOC_GETTIMEOUT:
214 err = put_user(timeout, p);
225 static const struct file_operations ath79_wdt_fops = {
226 .owner = THIS_MODULE,
228 .write = ath79_wdt_write,
229 .unlocked_ioctl = ath79_wdt_ioctl,
230 .open = ath79_wdt_open,
231 .release = ath79_wdt_release,
234 static struct miscdevice ath79_wdt_miscdev = {
235 .minor = WATCHDOG_MINOR,
237 .fops = &ath79_wdt_fops,
240 static int ath79_wdt_probe(struct platform_device *pdev)
242 struct resource *res;
249 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
250 wdt_base = devm_ioremap_resource(&pdev->dev, res);
251 if (IS_ERR(wdt_base))
252 return PTR_ERR(wdt_base);
254 wdt_clk = devm_clk_get(&pdev->dev, "wdt");
256 return PTR_ERR(wdt_clk);
258 err = clk_enable(wdt_clk);
262 wdt_freq = clk_get_rate(wdt_clk);
265 goto err_clk_disable;
268 max_timeout = (0xfffffffful / wdt_freq);
269 if (timeout < 1 || timeout > max_timeout) {
270 timeout = max_timeout;
272 "timeout value must be 0 < timeout < %d, using %d\n",
273 max_timeout, timeout);
276 ctrl = ath79_wdt_rr(WDOG_REG_CTRL);
277 boot_status = (ctrl & WDOG_CTRL_LAST_RESET) ? WDIOF_CARDRESET : 0;
279 err = misc_register(&ath79_wdt_miscdev);
282 "unable to register misc device, err=%d\n", err);
283 goto err_clk_disable;
289 clk_disable(wdt_clk);
293 static int ath79_wdt_remove(struct platform_device *pdev)
295 misc_deregister(&ath79_wdt_miscdev);
296 clk_disable(wdt_clk);
300 static void ath97_wdt_shutdown(struct platform_device *pdev)
306 static const struct of_device_id ath79_wdt_match[] = {
307 { .compatible = "qca,ar7130-wdt" },
310 MODULE_DEVICE_TABLE(of, ath79_wdt_match);
313 static struct platform_driver ath79_wdt_driver = {
314 .probe = ath79_wdt_probe,
315 .remove = ath79_wdt_remove,
316 .shutdown = ath97_wdt_shutdown,
319 .owner = THIS_MODULE,
320 .of_match_table = of_match_ptr(ath79_wdt_match),
324 module_platform_driver(ath79_wdt_driver);
326 MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X hardware watchdog driver");
327 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
328 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
329 MODULE_LICENSE("GPL v2");
330 MODULE_ALIAS("platform:" DRIVER_NAME);