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>
25 #include <linux/init.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 <asm/mach-ath79/ath79.h>
37 #include <asm/mach-ath79/ar71xx_regs.h>
39 #define DRIVER_NAME "ath79-wdt"
41 #define WDT_TIMEOUT 15 /* seconds */
43 #define WDOG_CTRL_LAST_RESET BIT(31)
44 #define WDOG_CTRL_ACTION_MASK 3
45 #define WDOG_CTRL_ACTION_NONE 0 /* no action */
46 #define WDOG_CTRL_ACTION_GPI 1 /* general purpose interrupt */
47 #define WDOG_CTRL_ACTION_NMI 2 /* NMI */
48 #define WDOG_CTRL_ACTION_FCR 3 /* full chip reset */
50 static bool nowayout = WATCHDOG_NOWAYOUT;
51 module_param(nowayout, bool, 0);
52 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
53 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
55 static int timeout = WDT_TIMEOUT;
56 module_param(timeout, int, 0);
57 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
58 "(default=" __MODULE_STRING(WDT_TIMEOUT) "s)");
60 static unsigned long wdt_flags;
62 #define WDT_FLAGS_BUSY 0
63 #define WDT_FLAGS_EXPECT_CLOSE 1
65 static struct clk *wdt_clk;
66 static unsigned long wdt_freq;
67 static int boot_status;
68 static int max_timeout;
70 static inline void ath79_wdt_keepalive(void)
72 ath79_reset_wr(AR71XX_RESET_REG_WDOG, wdt_freq * timeout);
74 ath79_reset_rr(AR71XX_RESET_REG_WDOG);
77 static inline void ath79_wdt_enable(void)
79 ath79_wdt_keepalive();
80 ath79_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_FCR);
82 ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL);
85 static inline void ath79_wdt_disable(void)
87 ath79_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_NONE);
89 ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL);
92 static int ath79_wdt_set_timeout(int val)
94 if (val < 1 || val > max_timeout)
98 ath79_wdt_keepalive();
103 static int ath79_wdt_open(struct inode *inode, struct file *file)
105 if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
108 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
111 return nonseekable_open(inode, file);
114 static int ath79_wdt_release(struct inode *inode, struct file *file)
116 if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags))
119 pr_crit("device closed unexpectedly, watchdog timer will not stop!\n");
120 ath79_wdt_keepalive();
123 clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
124 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
129 static ssize_t ath79_wdt_write(struct file *file, const char *data,
130 size_t len, loff_t *ppos)
136 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
138 for (i = 0; i != len; i++) {
141 if (get_user(c, data + i))
145 set_bit(WDT_FLAGS_EXPECT_CLOSE,
150 ath79_wdt_keepalive();
156 static const struct watchdog_info ath79_wdt_info = {
157 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
158 WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
159 .firmware_version = 0,
160 .identity = "ATH79 watchdog",
163 static long ath79_wdt_ioctl(struct file *file, unsigned int cmd,
166 void __user *argp = (void __user *)arg;
167 int __user *p = argp;
172 case WDIOC_GETSUPPORT:
173 err = copy_to_user(argp, &ath79_wdt_info,
174 sizeof(ath79_wdt_info)) ? -EFAULT : 0;
177 case WDIOC_GETSTATUS:
178 err = put_user(0, p);
181 case WDIOC_GETBOOTSTATUS:
182 err = put_user(boot_status, p);
185 case WDIOC_KEEPALIVE:
186 ath79_wdt_keepalive();
190 case WDIOC_SETTIMEOUT:
191 err = get_user(t, p);
195 err = ath79_wdt_set_timeout(t);
200 case WDIOC_GETTIMEOUT:
201 err = put_user(timeout, p);
212 static const struct file_operations ath79_wdt_fops = {
213 .owner = THIS_MODULE,
215 .write = ath79_wdt_write,
216 .unlocked_ioctl = ath79_wdt_ioctl,
217 .open = ath79_wdt_open,
218 .release = ath79_wdt_release,
221 static struct miscdevice ath79_wdt_miscdev = {
222 .minor = WATCHDOG_MINOR,
224 .fops = &ath79_wdt_fops,
227 static int __devinit ath79_wdt_probe(struct platform_device *pdev)
232 wdt_clk = clk_get(&pdev->dev, "wdt");
234 return PTR_ERR(wdt_clk);
236 err = clk_enable(wdt_clk);
240 wdt_freq = clk_get_rate(wdt_clk);
243 goto err_clk_disable;
246 max_timeout = (0xfffffffful / wdt_freq);
247 if (timeout < 1 || timeout > max_timeout) {
248 timeout = max_timeout;
250 "timeout value must be 0 < timeout < %d, using %d\n",
251 max_timeout, timeout);
254 ctrl = ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL);
255 boot_status = (ctrl & WDOG_CTRL_LAST_RESET) ? WDIOF_CARDRESET : 0;
257 err = misc_register(&ath79_wdt_miscdev);
260 "unable to register misc device, err=%d\n", err);
261 goto err_clk_disable;
267 clk_disable(wdt_clk);
273 static int __devexit ath79_wdt_remove(struct platform_device *pdev)
275 misc_deregister(&ath79_wdt_miscdev);
276 clk_disable(wdt_clk);
281 static void ath97_wdt_shutdown(struct platform_device *pdev)
286 static struct platform_driver ath79_wdt_driver = {
287 .remove = __devexit_p(ath79_wdt_remove),
288 .shutdown = ath97_wdt_shutdown,
291 .owner = THIS_MODULE,
295 static int __init ath79_wdt_init(void)
297 return platform_driver_probe(&ath79_wdt_driver, ath79_wdt_probe);
299 module_init(ath79_wdt_init);
301 static void __exit ath79_wdt_exit(void)
303 platform_driver_unregister(&ath79_wdt_driver);
305 module_exit(ath79_wdt_exit);
307 MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X hardware watchdog driver");
308 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
309 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
310 MODULE_LICENSE("GPL v2");
311 MODULE_ALIAS("platform:" DRIVER_NAME);
312 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);