spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / watchdog / ath79_wdt.c
blob9db808349f8b1e6dc29a892920602fb497f5ef6d
1 /*
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/errno.h>
22 #include <linux/fs.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/miscdevice.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/platform_device.h>
29 #include <linux/types.h>
30 #include <linux/watchdog.h>
31 #include <linux/clk.h>
32 #include <linux/err.h>
34 #include <asm/mach-ath79/ath79.h>
35 #include <asm/mach-ath79/ar71xx_regs.h>
37 #define DRIVER_NAME "ath79-wdt"
39 #define WDT_TIMEOUT 15 /* seconds */
41 #define WDOG_CTRL_LAST_RESET BIT(31)
42 #define WDOG_CTRL_ACTION_MASK 3
43 #define WDOG_CTRL_ACTION_NONE 0 /* no action */
44 #define WDOG_CTRL_ACTION_GPI 1 /* general purpose interrupt */
45 #define WDOG_CTRL_ACTION_NMI 2 /* NMI */
46 #define WDOG_CTRL_ACTION_FCR 3 /* full chip reset */
48 static int nowayout = WATCHDOG_NOWAYOUT;
49 module_param(nowayout, int, 0);
50 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
51 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
53 static int timeout = WDT_TIMEOUT;
54 module_param(timeout, int, 0);
55 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
56 "(default=" __MODULE_STRING(WDT_TIMEOUT) "s)");
58 static unsigned long wdt_flags;
60 #define WDT_FLAGS_BUSY 0
61 #define WDT_FLAGS_EXPECT_CLOSE 1
63 static struct clk *wdt_clk;
64 static unsigned long wdt_freq;
65 static int boot_status;
66 static int max_timeout;
68 static inline void ath79_wdt_keepalive(void)
70 ath79_reset_wr(AR71XX_RESET_REG_WDOG, wdt_freq * timeout);
71 /* flush write */
72 ath79_reset_rr(AR71XX_RESET_REG_WDOG);
75 static inline void ath79_wdt_enable(void)
77 ath79_wdt_keepalive();
78 ath79_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_FCR);
79 /* flush write */
80 ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL);
83 static inline void ath79_wdt_disable(void)
85 ath79_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_NONE);
86 /* flush write */
87 ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL);
90 static int ath79_wdt_set_timeout(int val)
92 if (val < 1 || val > max_timeout)
93 return -EINVAL;
95 timeout = val;
96 ath79_wdt_keepalive();
98 return 0;
101 static int ath79_wdt_open(struct inode *inode, struct file *file)
103 if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
104 return -EBUSY;
106 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
107 ath79_wdt_enable();
109 return nonseekable_open(inode, file);
112 static int ath79_wdt_release(struct inode *inode, struct file *file)
114 if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags))
115 ath79_wdt_disable();
116 else {
117 pr_crit(DRIVER_NAME ": device closed unexpectedly, "
118 "watchdog timer will not stop!\n");
119 ath79_wdt_keepalive();
122 clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
123 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
125 return 0;
128 static ssize_t ath79_wdt_write(struct file *file, const char *data,
129 size_t len, loff_t *ppos)
131 if (len) {
132 if (!nowayout) {
133 size_t i;
135 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
137 for (i = 0; i != len; i++) {
138 char c;
140 if (get_user(c, data + i))
141 return -EFAULT;
143 if (c == 'V')
144 set_bit(WDT_FLAGS_EXPECT_CLOSE,
145 &wdt_flags);
149 ath79_wdt_keepalive();
152 return len;
155 static const struct watchdog_info ath79_wdt_info = {
156 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
157 WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
158 .firmware_version = 0,
159 .identity = "ATH79 watchdog",
162 static long ath79_wdt_ioctl(struct file *file, unsigned int cmd,
163 unsigned long arg)
165 void __user *argp = (void __user *)arg;
166 int __user *p = argp;
167 int err;
168 int t;
170 switch (cmd) {
171 case WDIOC_GETSUPPORT:
172 err = copy_to_user(argp, &ath79_wdt_info,
173 sizeof(ath79_wdt_info)) ? -EFAULT : 0;
174 break;
176 case WDIOC_GETSTATUS:
177 err = put_user(0, p);
178 break;
180 case WDIOC_GETBOOTSTATUS:
181 err = put_user(boot_status, p);
182 break;
184 case WDIOC_KEEPALIVE:
185 ath79_wdt_keepalive();
186 err = 0;
187 break;
189 case WDIOC_SETTIMEOUT:
190 err = get_user(t, p);
191 if (err)
192 break;
194 err = ath79_wdt_set_timeout(t);
195 if (err)
196 break;
198 /* fallthrough */
199 case WDIOC_GETTIMEOUT:
200 err = put_user(timeout, p);
201 break;
203 default:
204 err = -ENOTTY;
205 break;
208 return err;
211 static const struct file_operations ath79_wdt_fops = {
212 .owner = THIS_MODULE,
213 .llseek = no_llseek,
214 .write = ath79_wdt_write,
215 .unlocked_ioctl = ath79_wdt_ioctl,
216 .open = ath79_wdt_open,
217 .release = ath79_wdt_release,
220 static struct miscdevice ath79_wdt_miscdev = {
221 .minor = WATCHDOG_MINOR,
222 .name = "watchdog",
223 .fops = &ath79_wdt_fops,
226 static int __devinit ath79_wdt_probe(struct platform_device *pdev)
228 u32 ctrl;
229 int err;
231 wdt_clk = clk_get(&pdev->dev, "wdt");
232 if (IS_ERR(wdt_clk))
233 return PTR_ERR(wdt_clk);
235 err = clk_enable(wdt_clk);
236 if (err)
237 goto err_clk_put;
239 wdt_freq = clk_get_rate(wdt_clk);
240 if (!wdt_freq) {
241 err = -EINVAL;
242 goto err_clk_disable;
245 max_timeout = (0xfffffffful / wdt_freq);
246 if (timeout < 1 || timeout > max_timeout) {
247 timeout = max_timeout;
248 dev_info(&pdev->dev,
249 "timeout value must be 0 < timeout < %d, using %d\n",
250 max_timeout, timeout);
253 ctrl = ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL);
254 boot_status = (ctrl & WDOG_CTRL_LAST_RESET) ? WDIOF_CARDRESET : 0;
256 err = misc_register(&ath79_wdt_miscdev);
257 if (err) {
258 dev_err(&pdev->dev,
259 "unable to register misc device, err=%d\n", err);
260 goto err_clk_disable;
263 return 0;
265 err_clk_disable:
266 clk_disable(wdt_clk);
267 err_clk_put:
268 clk_put(wdt_clk);
269 return err;
272 static int __devexit ath79_wdt_remove(struct platform_device *pdev)
274 misc_deregister(&ath79_wdt_miscdev);
275 clk_disable(wdt_clk);
276 clk_put(wdt_clk);
277 return 0;
280 static void ath97_wdt_shutdown(struct platform_device *pdev)
282 ath79_wdt_disable();
285 static struct platform_driver ath79_wdt_driver = {
286 .remove = __devexit_p(ath79_wdt_remove),
287 .shutdown = ath97_wdt_shutdown,
288 .driver = {
289 .name = DRIVER_NAME,
290 .owner = THIS_MODULE,
294 static int __init ath79_wdt_init(void)
296 return platform_driver_probe(&ath79_wdt_driver, ath79_wdt_probe);
298 module_init(ath79_wdt_init);
300 static void __exit ath79_wdt_exit(void)
302 platform_driver_unregister(&ath79_wdt_driver);
304 module_exit(ath79_wdt_exit);
306 MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X hardware watchdog driver");
307 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
308 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
309 MODULE_LICENSE("GPL v2");
310 MODULE_ALIAS("platform:" DRIVER_NAME);
311 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);