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 #include <linux/bitops.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/miscdevice.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/platform_device.h>
30 #include <linux/types.h>
31 #include <linux/watchdog.h>
32 #include <linux/clk.h>
33 #include <linux/err.h>
35 #include <asm/mach-ath79/ath79.h>
36 #include <asm/mach-ath79/ar71xx_regs.h>
38 #define DRIVER_NAME "ath79-wdt"
40 #define WDT_TIMEOUT 15 /* seconds */
42 #define WDOG_CTRL_LAST_RESET BIT(31)
43 #define WDOG_CTRL_ACTION_MASK 3
44 #define WDOG_CTRL_ACTION_NONE 0 /* no action */
45 #define WDOG_CTRL_ACTION_GPI 1 /* general purpose interrupt */
46 #define WDOG_CTRL_ACTION_NMI 2 /* NMI */
47 #define WDOG_CTRL_ACTION_FCR 3 /* full chip reset */
49 static int nowayout = WATCHDOG_NOWAYOUT;
50 module_param(nowayout, int, 0);
51 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
52 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
54 static int timeout = WDT_TIMEOUT;
55 module_param(timeout, int, 0);
56 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
57 "(default=" __MODULE_STRING(WDT_TIMEOUT) "s)");
59 static unsigned long wdt_flags;
61 #define WDT_FLAGS_BUSY 0
62 #define WDT_FLAGS_EXPECT_CLOSE 1
64 static struct clk *wdt_clk;
65 static unsigned long wdt_freq;
66 static int boot_status;
67 static int max_timeout;
69 static inline void ath79_wdt_keepalive(void)
71 ath79_reset_wr(AR71XX_RESET_REG_WDOG, wdt_freq * timeout);
74 static inline void ath79_wdt_enable(void)
76 ath79_wdt_keepalive();
79 * Updating the TIMER register requires a few microseconds
80 * on the AR934x SoCs at least. Use a small delay to ensure
81 * that the TIMER register is updated within the hardware
82 * before enabling the watchdog.
86 ath79_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_FCR);
89 static inline void ath79_wdt_disable(void)
91 ath79_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_NONE);
94 static int ath79_wdt_set_timeout(int val)
96 if (val < 1 || val > max_timeout)
100 ath79_wdt_keepalive();
105 static int ath79_wdt_open(struct inode *inode, struct file *file)
107 if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
110 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
113 return nonseekable_open(inode, file);
116 static int ath79_wdt_release(struct inode *inode, struct file *file)
118 if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags))
121 pr_crit(DRIVER_NAME ": device closed unexpectedly, "
122 "watchdog timer will not stop!\n");
123 ath79_wdt_keepalive();
126 clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
127 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
132 static ssize_t ath79_wdt_write(struct file *file, const char *data,
133 size_t len, loff_t *ppos)
139 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
141 for (i = 0; i != len; i++) {
144 if (get_user(c, data + i))
148 set_bit(WDT_FLAGS_EXPECT_CLOSE,
153 ath79_wdt_keepalive();
159 static const struct watchdog_info ath79_wdt_info = {
160 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
161 WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
162 .firmware_version = 0,
163 .identity = "ATH79 watchdog",
166 static long ath79_wdt_ioctl(struct file *file, unsigned int cmd,
169 void __user *argp = (void __user *)arg;
170 int __user *p = argp;
175 case WDIOC_GETSUPPORT:
176 err = copy_to_user(argp, &ath79_wdt_info,
177 sizeof(ath79_wdt_info)) ? -EFAULT : 0;
180 case WDIOC_GETSTATUS:
181 err = put_user(0, p);
184 case WDIOC_GETBOOTSTATUS:
185 err = put_user(boot_status, p);
188 case WDIOC_KEEPALIVE:
189 ath79_wdt_keepalive();
193 case WDIOC_SETTIMEOUT:
194 err = get_user(t, p);
198 err = ath79_wdt_set_timeout(t);
203 case WDIOC_GETTIMEOUT:
204 err = put_user(timeout, p);
215 static const struct file_operations ath79_wdt_fops = {
216 .owner = THIS_MODULE,
218 .write = ath79_wdt_write,
219 .unlocked_ioctl = ath79_wdt_ioctl,
220 .open = ath79_wdt_open,
221 .release = ath79_wdt_release,
224 static struct miscdevice ath79_wdt_miscdev = {
225 .minor = WATCHDOG_MINOR,
227 .fops = &ath79_wdt_fops,
230 static int __devinit ath79_wdt_probe(struct platform_device *pdev)
235 wdt_clk = clk_get(&pdev->dev, "wdt");
237 return PTR_ERR(wdt_clk);
239 err = clk_enable(wdt_clk);
243 wdt_freq = clk_get_rate(wdt_clk);
246 goto err_clk_disable;
249 max_timeout = (0xfffffffful / wdt_freq);
250 if (timeout < 1 || timeout > max_timeout) {
251 timeout = max_timeout;
253 "timeout value must be 0 < timeout < %d, using %d\n",
254 max_timeout, timeout);
257 ctrl = ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL);
258 boot_status = (ctrl & WDOG_CTRL_LAST_RESET) ? WDIOF_CARDRESET : 0;
260 err = misc_register(&ath79_wdt_miscdev);
263 "unable to register misc device, err=%d\n", err);
264 goto err_clk_disable;
270 clk_disable(wdt_clk);
276 static int __devexit ath79_wdt_remove(struct platform_device *pdev)
278 misc_deregister(&ath79_wdt_miscdev);
279 clk_disable(wdt_clk);
284 static void ath97_wdt_shutdown(struct platform_device *pdev)
289 static struct platform_driver ath79_wdt_driver = {
290 .remove = __devexit_p(ath79_wdt_remove),
291 .shutdown = ath97_wdt_shutdown,
294 .owner = THIS_MODULE,
298 static int __init ath79_wdt_init(void)
300 return platform_driver_probe(&ath79_wdt_driver, ath79_wdt_probe);
302 module_init(ath79_wdt_init);
304 static void __exit ath79_wdt_exit(void)
306 platform_driver_unregister(&ath79_wdt_driver);
308 module_exit(ath79_wdt_exit);
310 MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X hardware watchdog driver");
311 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
312 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
313 MODULE_LICENSE("GPL v2");
314 MODULE_ALIAS("platform:" DRIVER_NAME);
315 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);