x86/bitops: Move BIT_64() for a wider use
[pandora-kernel.git] / drivers / watchdog / ath79_wdt.c
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/delay.h>
22 #include <linux/errno.h>
23 #include <linux/fs.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>
34
35 #include <asm/mach-ath79/ath79.h>
36 #include <asm/mach-ath79/ar71xx_regs.h>
37
38 #define DRIVER_NAME     "ath79-wdt"
39
40 #define WDT_TIMEOUT     15      /* seconds */
41
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 */
48
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) ")");
53
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)");
58
59 static unsigned long wdt_flags;
60
61 #define WDT_FLAGS_BUSY          0
62 #define WDT_FLAGS_EXPECT_CLOSE  1
63
64 static struct clk *wdt_clk;
65 static unsigned long wdt_freq;
66 static int boot_status;
67 static int max_timeout;
68
69 static inline void ath79_wdt_keepalive(void)
70 {
71         ath79_reset_wr(AR71XX_RESET_REG_WDOG, wdt_freq * timeout);
72 }
73
74 static inline void ath79_wdt_enable(void)
75 {
76         ath79_wdt_keepalive();
77
78         /*
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.
83          */
84         udelay(2);
85
86         ath79_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_FCR);
87 }
88
89 static inline void ath79_wdt_disable(void)
90 {
91         ath79_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_NONE);
92 }
93
94 static int ath79_wdt_set_timeout(int val)
95 {
96         if (val < 1 || val > max_timeout)
97                 return -EINVAL;
98
99         timeout = val;
100         ath79_wdt_keepalive();
101
102         return 0;
103 }
104
105 static int ath79_wdt_open(struct inode *inode, struct file *file)
106 {
107         if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
108                 return -EBUSY;
109
110         clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
111         ath79_wdt_enable();
112
113         return nonseekable_open(inode, file);
114 }
115
116 static int ath79_wdt_release(struct inode *inode, struct file *file)
117 {
118         if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags))
119                 ath79_wdt_disable();
120         else {
121                 pr_crit(DRIVER_NAME ": device closed unexpectedly, "
122                         "watchdog timer will not stop!\n");
123                 ath79_wdt_keepalive();
124         }
125
126         clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
127         clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
128
129         return 0;
130 }
131
132 static ssize_t ath79_wdt_write(struct file *file, const char *data,
133                                 size_t len, loff_t *ppos)
134 {
135         if (len) {
136                 if (!nowayout) {
137                         size_t i;
138
139                         clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
140
141                         for (i = 0; i != len; i++) {
142                                 char c;
143
144                                 if (get_user(c, data + i))
145                                         return -EFAULT;
146
147                                 if (c == 'V')
148                                         set_bit(WDT_FLAGS_EXPECT_CLOSE,
149                                                 &wdt_flags);
150                         }
151                 }
152
153                 ath79_wdt_keepalive();
154         }
155
156         return len;
157 }
158
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",
164 };
165
166 static long ath79_wdt_ioctl(struct file *file, unsigned int cmd,
167                             unsigned long arg)
168 {
169         void __user *argp = (void __user *)arg;
170         int __user *p = argp;
171         int err;
172         int t;
173
174         switch (cmd) {
175         case WDIOC_GETSUPPORT:
176                 err = copy_to_user(argp, &ath79_wdt_info,
177                                    sizeof(ath79_wdt_info)) ? -EFAULT : 0;
178                 break;
179
180         case WDIOC_GETSTATUS:
181                 err = put_user(0, p);
182                 break;
183
184         case WDIOC_GETBOOTSTATUS:
185                 err = put_user(boot_status, p);
186                 break;
187
188         case WDIOC_KEEPALIVE:
189                 ath79_wdt_keepalive();
190                 err = 0;
191                 break;
192
193         case WDIOC_SETTIMEOUT:
194                 err = get_user(t, p);
195                 if (err)
196                         break;
197
198                 err = ath79_wdt_set_timeout(t);
199                 if (err)
200                         break;
201
202                 /* fallthrough */
203         case WDIOC_GETTIMEOUT:
204                 err = put_user(timeout, p);
205                 break;
206
207         default:
208                 err = -ENOTTY;
209                 break;
210         }
211
212         return err;
213 }
214
215 static const struct file_operations ath79_wdt_fops = {
216         .owner          = THIS_MODULE,
217         .llseek         = no_llseek,
218         .write          = ath79_wdt_write,
219         .unlocked_ioctl = ath79_wdt_ioctl,
220         .open           = ath79_wdt_open,
221         .release        = ath79_wdt_release,
222 };
223
224 static struct miscdevice ath79_wdt_miscdev = {
225         .minor = WATCHDOG_MINOR,
226         .name = "watchdog",
227         .fops = &ath79_wdt_fops,
228 };
229
230 static int __devinit ath79_wdt_probe(struct platform_device *pdev)
231 {
232         u32 ctrl;
233         int err;
234
235         wdt_clk = clk_get(&pdev->dev, "wdt");
236         if (IS_ERR(wdt_clk))
237                 return PTR_ERR(wdt_clk);
238
239         err = clk_enable(wdt_clk);
240         if (err)
241                 goto err_clk_put;
242
243         wdt_freq = clk_get_rate(wdt_clk);
244         if (!wdt_freq) {
245                 err = -EINVAL;
246                 goto err_clk_disable;
247         }
248
249         max_timeout = (0xfffffffful / wdt_freq);
250         if (timeout < 1 || timeout > max_timeout) {
251                 timeout = max_timeout;
252                 dev_info(&pdev->dev,
253                         "timeout value must be 0 < timeout < %d, using %d\n",
254                         max_timeout, timeout);
255         }
256
257         ctrl = ath79_reset_rr(AR71XX_RESET_REG_WDOG_CTRL);
258         boot_status = (ctrl & WDOG_CTRL_LAST_RESET) ? WDIOF_CARDRESET : 0;
259
260         err = misc_register(&ath79_wdt_miscdev);
261         if (err) {
262                 dev_err(&pdev->dev,
263                         "unable to register misc device, err=%d\n", err);
264                 goto err_clk_disable;
265         }
266
267         return 0;
268
269 err_clk_disable:
270         clk_disable(wdt_clk);
271 err_clk_put:
272         clk_put(wdt_clk);
273         return err;
274 }
275
276 static int __devexit ath79_wdt_remove(struct platform_device *pdev)
277 {
278         misc_deregister(&ath79_wdt_miscdev);
279         clk_disable(wdt_clk);
280         clk_put(wdt_clk);
281         return 0;
282 }
283
284 static void ath97_wdt_shutdown(struct platform_device *pdev)
285 {
286         ath79_wdt_disable();
287 }
288
289 static struct platform_driver ath79_wdt_driver = {
290         .remove         = __devexit_p(ath79_wdt_remove),
291         .shutdown       = ath97_wdt_shutdown,
292         .driver         = {
293                 .name   = DRIVER_NAME,
294                 .owner  = THIS_MODULE,
295         },
296 };
297
298 static int __init ath79_wdt_init(void)
299 {
300         return platform_driver_probe(&ath79_wdt_driver, ath79_wdt_probe);
301 }
302 module_init(ath79_wdt_init);
303
304 static void __exit ath79_wdt_exit(void)
305 {
306         platform_driver_unregister(&ath79_wdt_driver);
307 }
308 module_exit(ath79_wdt_exit);
309
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);