a6532b52c457a0805fd7cc83890b1b3f119a54fd
[sfrench/cifs-2.6.git] / drivers / watchdog / ep93xx_wdt.c
1 /*
2  * Watchdog driver for Cirrus Logic EP93xx family of devices.
3  *
4  * Copyright (c) 2004 Ray Lehtiniemi
5  * Copyright (c) 2006 Tower Technologies
6  * Based on ep93xx driver, bits from alim7101_wdt.c
7  *
8  * Authors: Ray Lehtiniemi <rayl@mail.com>,
9  *      Alessandro Zummo <a.zummo@towertech.it>
10  *
11  * This file is licensed under the terms of the GNU General Public
12  * License version 2. This program is licensed "as is" without any
13  * warranty of any kind, whether express or implied.
14  *
15  * This watchdog fires after 250msec, which is a too short interval
16  * for us to rely on the user space daemon alone. So we ping the
17  * wdt each ~200msec and eventually stop doing it if the user space
18  * daemon dies.
19  *
20  * TODO:
21  *
22  *      - Test last reset from watchdog status
23  *      - Add a few missing ioctls
24  */
25
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28 #include <linux/module.h>
29 #include <linux/fs.h>
30 #include <linux/miscdevice.h>
31 #include <linux/watchdog.h>
32 #include <linux/timer.h>
33 #include <linux/uaccess.h>
34 #include <linux/io.h>
35 #include <mach/hardware.h>
36
37 #define WDT_VERSION     "0.3"
38
39 /* default timeout (secs) */
40 #define WDT_TIMEOUT 30
41
42 static int nowayout = WATCHDOG_NOWAYOUT;
43 static int timeout = WDT_TIMEOUT;
44
45 static struct timer_list timer;
46 static unsigned long next_heartbeat;
47 static unsigned long wdt_status;
48 static unsigned long boot_status;
49
50 #define WDT_IN_USE              0
51 #define WDT_OK_TO_CLOSE         1
52
53 #define EP93XX_WDT_REG(x)       (EP93XX_WATCHDOG_BASE + (x))
54 #define EP93XX_WDT_WATCHDOG     EP93XX_WDT_REG(0x00)
55 #define EP93XX_WDT_WDSTATUS     EP93XX_WDT_REG(0x04)
56
57 /* reset the wdt every ~200ms */
58 #define WDT_INTERVAL (HZ/5)
59
60 static void wdt_enable(void)
61 {
62         __raw_writew(0xaaaa, EP93XX_WDT_WATCHDOG);
63 }
64
65 static void wdt_disable(void)
66 {
67         __raw_writew(0xaa55, EP93XX_WDT_WATCHDOG);
68 }
69
70 static inline void wdt_ping(void)
71 {
72         __raw_writew(0x5555, EP93XX_WDT_WATCHDOG);
73 }
74
75 static void wdt_startup(void)
76 {
77         next_heartbeat = jiffies + (timeout * HZ);
78
79         wdt_enable();
80         mod_timer(&timer, jiffies + WDT_INTERVAL);
81 }
82
83 static void wdt_shutdown(void)
84 {
85         del_timer_sync(&timer);
86         wdt_disable();
87 }
88
89 static void wdt_keepalive(void)
90 {
91         /* user land ping */
92         next_heartbeat = jiffies + (timeout * HZ);
93 }
94
95 static int ep93xx_wdt_open(struct inode *inode, struct file *file)
96 {
97         if (test_and_set_bit(WDT_IN_USE, &wdt_status))
98                 return -EBUSY;
99
100         clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
101
102         wdt_startup();
103
104         return nonseekable_open(inode, file);
105 }
106
107 static ssize_t
108 ep93xx_wdt_write(struct file *file, const char __user *data, size_t len,
109                  loff_t *ppos)
110 {
111         if (len) {
112                 if (!nowayout) {
113                         size_t i;
114
115                         clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
116
117                         for (i = 0; i != len; i++) {
118                                 char c;
119
120                                 if (get_user(c, data + i))
121                                         return -EFAULT;
122
123                                 if (c == 'V')
124                                         set_bit(WDT_OK_TO_CLOSE, &wdt_status);
125                                 else
126                                         clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
127                         }
128                 }
129                 wdt_keepalive();
130         }
131
132         return len;
133 }
134
135 static const struct watchdog_info ident = {
136         .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE,
137         .identity = "EP93xx Watchdog",
138 };
139
140 static long ep93xx_wdt_ioctl(struct file *file,
141                                         unsigned int cmd, unsigned long arg)
142 {
143         int ret = -ENOTTY;
144
145         switch (cmd) {
146         case WDIOC_GETSUPPORT:
147                 ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
148                                 sizeof(ident)) ? -EFAULT : 0;
149                 break;
150
151         case WDIOC_GETSTATUS:
152                 ret = put_user(0, (int __user *)arg);
153                 break;
154
155         case WDIOC_GETBOOTSTATUS:
156                 ret = put_user(boot_status, (int __user *)arg);
157                 break;
158
159         case WDIOC_KEEPALIVE:
160                 wdt_keepalive();
161                 ret = 0;
162                 break;
163
164         case WDIOC_GETTIMEOUT:
165                 /* actually, it is 0.250 seconds.... */
166                 ret = put_user(1, (int __user *)arg);
167                 break;
168         }
169         return ret;
170 }
171
172 static int ep93xx_wdt_release(struct inode *inode, struct file *file)
173 {
174         if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
175                 wdt_shutdown();
176         else
177                 pr_crit("Device closed unexpectedly - timer will not stop\n");
178
179         clear_bit(WDT_IN_USE, &wdt_status);
180         clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
181
182         return 0;
183 }
184
185 static const struct file_operations ep93xx_wdt_fops = {
186         .owner          = THIS_MODULE,
187         .write          = ep93xx_wdt_write,
188         .unlocked_ioctl = ep93xx_wdt_ioctl,
189         .open           = ep93xx_wdt_open,
190         .release        = ep93xx_wdt_release,
191         .llseek         = no_llseek,
192 };
193
194 static struct miscdevice ep93xx_wdt_miscdev = {
195         .minor          = WATCHDOG_MINOR,
196         .name           = "watchdog",
197         .fops           = &ep93xx_wdt_fops,
198 };
199
200 static void ep93xx_timer_ping(unsigned long data)
201 {
202         if (time_before(jiffies, next_heartbeat))
203                 wdt_ping();
204
205         /* Re-set the timer interval */
206         mod_timer(&timer, jiffies + WDT_INTERVAL);
207 }
208
209 static int __init ep93xx_wdt_init(void)
210 {
211         int err;
212
213         err = misc_register(&ep93xx_wdt_miscdev);
214
215         boot_status = __raw_readl(EP93XX_WDT_WATCHDOG) & 0x01 ? 1 : 0;
216
217         pr_info("EP93XX watchdog, driver version " WDT_VERSION "%s\n",
218                 (__raw_readl(EP93XX_WDT_WATCHDOG) & 0x08)
219                 ? " (nCS1 disable detected)" : "");
220
221         if (timeout < 1 || timeout > 3600) {
222                 timeout = WDT_TIMEOUT;
223                 pr_info("timeout value must be 1<=x<=3600, using %d\n",
224                         timeout);
225         }
226
227         setup_timer(&timer, ep93xx_timer_ping, 1);
228         return err;
229 }
230
231 static void __exit ep93xx_wdt_exit(void)
232 {
233         wdt_shutdown();
234         misc_deregister(&ep93xx_wdt_miscdev);
235 }
236
237 module_init(ep93xx_wdt_init);
238 module_exit(ep93xx_wdt_exit);
239
240 module_param(nowayout, int, 0);
241 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
242
243 module_param(timeout, int, 0);
244 MODULE_PARM_DESC(timeout,
245         "Watchdog timeout in seconds. (1<=timeout<=3600, default="
246                                 __MODULE_STRING(WDT_TIMEOUT) ")");
247
248 MODULE_AUTHOR("Ray Lehtiniemi <rayl@mail.com>,"
249                 "Alessandro Zummo <a.zummo@towertech.it>");
250 MODULE_DESCRIPTION("EP93xx Watchdog");
251 MODULE_LICENSE("GPL");
252 MODULE_VERSION(WDT_VERSION);
253 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);