xref: /linux/drivers/watchdog/ath79_wdt.c (revision b43ab901d671e3e3cad425ea5e9a3c74e266dcdd)
1 /*
2  * Atheros AR71XX/AR724X/AR913X built-in hardware watchdog timer.
3  *
4  * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
5  * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6  *
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.
10  *
11  * which again was based on sa1100 driver,
12  *	Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
13  *
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.
17  *
18  */
19 
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>
33 
34 #include <asm/mach-ath79/ath79.h>
35 #include <asm/mach-ath79/ar71xx_regs.h>
36 
37 #define DRIVER_NAME	"ath79-wdt"
38 
39 #define WDT_TIMEOUT	15	/* seconds */
40 
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 */
47 
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) ")");
52 
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)");
57 
58 static unsigned long wdt_flags;
59 
60 #define WDT_FLAGS_BUSY		0
61 #define WDT_FLAGS_EXPECT_CLOSE	1
62 
63 static struct clk *wdt_clk;
64 static unsigned long wdt_freq;
65 static int boot_status;
66 static int max_timeout;
67 
68 static inline void ath79_wdt_keepalive(void)
69 {
70 	ath79_reset_wr(AR71XX_RESET_REG_WDOG, wdt_freq * timeout);
71 	/* flush write */
72 	ath79_reset_rr(AR71XX_RESET_REG_WDOG);
73 }
74 
75 static inline void ath79_wdt_enable(void)
76 {
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);
81 }
82 
83 static inline void ath79_wdt_disable(void)
84 {
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);
88 }
89 
90 static int ath79_wdt_set_timeout(int val)
91 {
92 	if (val < 1 || val > max_timeout)
93 		return -EINVAL;
94 
95 	timeout = val;
96 	ath79_wdt_keepalive();
97 
98 	return 0;
99 }
100 
101 static int ath79_wdt_open(struct inode *inode, struct file *file)
102 {
103 	if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
104 		return -EBUSY;
105 
106 	clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
107 	ath79_wdt_enable();
108 
109 	return nonseekable_open(inode, file);
110 }
111 
112 static int ath79_wdt_release(struct inode *inode, struct file *file)
113 {
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();
120 	}
121 
122 	clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
123 	clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
124 
125 	return 0;
126 }
127 
128 static ssize_t ath79_wdt_write(struct file *file, const char *data,
129 				size_t len, loff_t *ppos)
130 {
131 	if (len) {
132 		if (!nowayout) {
133 			size_t i;
134 
135 			clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
136 
137 			for (i = 0; i != len; i++) {
138 				char c;
139 
140 				if (get_user(c, data + i))
141 					return -EFAULT;
142 
143 				if (c == 'V')
144 					set_bit(WDT_FLAGS_EXPECT_CLOSE,
145 						&wdt_flags);
146 			}
147 		}
148 
149 		ath79_wdt_keepalive();
150 	}
151 
152 	return len;
153 }
154 
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",
160 };
161 
162 static long ath79_wdt_ioctl(struct file *file, unsigned int cmd,
163 			    unsigned long arg)
164 {
165 	void __user *argp = (void __user *)arg;
166 	int __user *p = argp;
167 	int err;
168 	int t;
169 
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;
175 
176 	case WDIOC_GETSTATUS:
177 		err = put_user(0, p);
178 		break;
179 
180 	case WDIOC_GETBOOTSTATUS:
181 		err = put_user(boot_status, p);
182 		break;
183 
184 	case WDIOC_KEEPALIVE:
185 		ath79_wdt_keepalive();
186 		err = 0;
187 		break;
188 
189 	case WDIOC_SETTIMEOUT:
190 		err = get_user(t, p);
191 		if (err)
192 			break;
193 
194 		err = ath79_wdt_set_timeout(t);
195 		if (err)
196 			break;
197 
198 		/* fallthrough */
199 	case WDIOC_GETTIMEOUT:
200 		err = put_user(timeout, p);
201 		break;
202 
203 	default:
204 		err = -ENOTTY;
205 		break;
206 	}
207 
208 	return err;
209 }
210 
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,
218 };
219 
220 static struct miscdevice ath79_wdt_miscdev = {
221 	.minor = WATCHDOG_MINOR,
222 	.name = "watchdog",
223 	.fops = &ath79_wdt_fops,
224 };
225 
226 static int __devinit ath79_wdt_probe(struct platform_device *pdev)
227 {
228 	u32 ctrl;
229 	int err;
230 
231 	wdt_clk = clk_get(&pdev->dev, "wdt");
232 	if (IS_ERR(wdt_clk))
233 		return PTR_ERR(wdt_clk);
234 
235 	err = clk_enable(wdt_clk);
236 	if (err)
237 		goto err_clk_put;
238 
239 	wdt_freq = clk_get_rate(wdt_clk);
240 	if (!wdt_freq) {
241 		err = -EINVAL;
242 		goto err_clk_disable;
243 	}
244 
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);
251 	}
252 
253 	ctrl = ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL);
254 	boot_status = (ctrl & WDOG_CTRL_LAST_RESET) ? WDIOF_CARDRESET : 0;
255 
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;
261 	}
262 
263 	return 0;
264 
265 err_clk_disable:
266 	clk_disable(wdt_clk);
267 err_clk_put:
268 	clk_put(wdt_clk);
269 	return err;
270 }
271 
272 static int __devexit ath79_wdt_remove(struct platform_device *pdev)
273 {
274 	misc_deregister(&ath79_wdt_miscdev);
275 	clk_disable(wdt_clk);
276 	clk_put(wdt_clk);
277 	return 0;
278 }
279 
280 static void ath97_wdt_shutdown(struct platform_device *pdev)
281 {
282 	ath79_wdt_disable();
283 }
284 
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,
291 	},
292 };
293 
294 static int __init ath79_wdt_init(void)
295 {
296 	return platform_driver_probe(&ath79_wdt_driver, ath79_wdt_probe);
297 }
298 module_init(ath79_wdt_init);
299 
300 static void __exit ath79_wdt_exit(void)
301 {
302 	platform_driver_unregister(&ath79_wdt_driver);
303 }
304 module_exit(ath79_wdt_exit);
305 
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);
312