]> Pileus Git - ~andy/linux/blob - drivers/watchdog/w83697ug_wdt.c
Delete all instances of asm/system.h
[~andy/linux] / drivers / watchdog / w83697ug_wdt.c
1 /*
2  *      w83697ug/uf WDT driver
3  *
4  *      (c) Copyright 2008 Flemming Fransen <ff@nrvissing.net>
5  *              reused original code to support w83697ug/uf.
6  *
7  *      Based on w83627hf_wdt.c which is based on advantechwdt.c
8  *      which is based on wdt.c.
9  *      Original copyright messages:
10  *
11  *      (c) Copyright 2007 Vlad Drukker <vlad@storewiz.com>
12  *              added support for W83627THF.
13  *
14  *      (c) Copyright 2003 Pádraig Brady <P@draigBrady.com>
15  *
16  *      (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
17  *
18  *      (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
19  *                              http://www.redhat.com
20  *
21  *      This program is free software; you can redistribute it and/or
22  *      modify it under the terms of the GNU General Public License
23  *      as published by the Free Software Foundation; either version
24  *      2 of the License, or (at your option) any later version.
25  *
26  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
27  *      warranty for any of this software. This material is provided
28  *      "AS-IS" and at no charge.
29  *
30  *      (c) Copyright 1995    Alan Cox <alan@redhat.com>
31  */
32
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/types.h>
36 #include <linux/miscdevice.h>
37 #include <linux/watchdog.h>
38 #include <linux/fs.h>
39 #include <linux/ioport.h>
40 #include <linux/notifier.h>
41 #include <linux/reboot.h>
42 #include <linux/init.h>
43 #include <linux/spinlock.h>
44 #include <linux/io.h>
45 #include <linux/uaccess.h>
46
47
48 #define WATCHDOG_NAME "w83697ug/uf WDT"
49 #define PFX WATCHDOG_NAME ": "
50 #define WATCHDOG_TIMEOUT 60             /* 60 sec default timeout */
51
52 static unsigned long wdt_is_open;
53 static char expect_close;
54 static DEFINE_SPINLOCK(io_lock);
55
56 static int wdt_io = 0x2e;
57 module_param(wdt_io, int, 0);
58 MODULE_PARM_DESC(wdt_io, "w83697ug/uf WDT io port (default 0x2e)");
59
60 static int timeout = WATCHDOG_TIMEOUT;  /* in seconds */
61 module_param(timeout, int, 0);
62 MODULE_PARM_DESC(timeout,
63         "Watchdog timeout in seconds. 1<= timeout <=255 (default="
64                                 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
65
66 static int nowayout = WATCHDOG_NOWAYOUT;
67 module_param(nowayout, int, 0);
68 MODULE_PARM_DESC(nowayout,
69         "Watchdog cannot be stopped once started (default="
70                                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
71
72 /*
73  *      Kernel methods.
74  */
75
76 #define WDT_EFER (wdt_io+0)   /* Extended Function Enable Registers */
77 #define WDT_EFIR (wdt_io+0)   /* Extended Function Index Register
78                                                         (same as EFER) */
79 #define WDT_EFDR (WDT_EFIR+1) /* Extended Function Data Register */
80
81 static int w83697ug_select_wd_register(void)
82 {
83         unsigned char c;
84         unsigned char version;
85
86         outb_p(0x87, WDT_EFER); /* Enter extended function mode */
87         outb_p(0x87, WDT_EFER); /* Again according to manual */
88
89         outb(0x20, WDT_EFER);   /* check chip version   */
90         version = inb(WDT_EFDR);
91
92         if (version == 0x68) {  /* W83697UG             */
93                 printk(KERN_INFO PFX "Watchdog chip version 0x%02x = "
94                         "W83697UG/UF found at 0x%04x\n", version, wdt_io);
95
96                 outb_p(0x2b, WDT_EFER);
97                 c = inb_p(WDT_EFDR);    /* select WDT0 */
98                 c &= ~0x04;
99                 outb_p(0x2b, WDT_EFER);
100                 outb_p(c, WDT_EFDR);    /* set pin118 to WDT0 */
101
102         } else {
103                 printk(KERN_ERR PFX "No W83697UG/UF could be found\n");
104                 return -ENODEV;
105         }
106
107         outb_p(0x07, WDT_EFER); /* point to logical device number reg */
108         outb_p(0x08, WDT_EFDR); /* select logical device 8 (GPIO2) */
109         outb_p(0x30, WDT_EFER); /* select CR30 */
110         c = inb_p(WDT_EFDR);
111         outb_p(c | 0x01, WDT_EFDR); /* set bit 0 to activate GPIO2 */
112
113         return 0;
114 }
115
116 static void w83697ug_unselect_wd_register(void)
117 {
118         outb_p(0xAA, WDT_EFER); /* Leave extended function mode */
119 }
120
121 static int w83697ug_init(void)
122 {
123         int ret;
124         unsigned char t;
125
126         ret = w83697ug_select_wd_register();
127         if (ret != 0)
128                 return ret;
129
130         outb_p(0xF6, WDT_EFER); /* Select CRF6 */
131         t = inb_p(WDT_EFDR);    /* read CRF6 */
132         if (t != 0) {
133                 printk(KERN_INFO PFX "Watchdog already running."
134                         " Resetting timeout to %d sec\n", timeout);
135                 outb_p(timeout, WDT_EFDR);    /* Write back to CRF6 */
136         }
137         outb_p(0xF5, WDT_EFER); /* Select CRF5 */
138         t = inb_p(WDT_EFDR);    /* read CRF5 */
139         t &= ~0x0C;             /* set second mode &
140                                         disable keyboard turning off watchdog */
141         outb_p(t, WDT_EFDR);    /* Write back to CRF5 */
142
143         w83697ug_unselect_wd_register();
144         return 0;
145 }
146
147 static void wdt_ctrl(int timeout)
148 {
149         spin_lock(&io_lock);
150
151         if (w83697ug_select_wd_register() < 0) {
152                 spin_unlock(&io_lock);
153                 return;
154         }
155
156         outb_p(0xF4, WDT_EFER);    /* Select CRF4 */
157         outb_p(timeout, WDT_EFDR); /* Write Timeout counter to CRF4 */
158
159         w83697ug_unselect_wd_register();
160
161         spin_unlock(&io_lock);
162 }
163
164 static int wdt_ping(void)
165 {
166         wdt_ctrl(timeout);
167         return 0;
168 }
169
170 static int wdt_disable(void)
171 {
172         wdt_ctrl(0);
173         return 0;
174 }
175
176 static int wdt_set_heartbeat(int t)
177 {
178         if (t < 1 || t > 255)
179                 return -EINVAL;
180
181         timeout = t;
182         return 0;
183 }
184
185 static ssize_t wdt_write(struct file *file, const char __user *buf,
186                                                 size_t count, loff_t *ppos)
187 {
188         if (count) {
189                 if (!nowayout) {
190                         size_t i;
191
192                         expect_close = 0;
193
194                         for (i = 0; i != count; i++) {
195                                 char c;
196                                 if (get_user(c, buf + i))
197                                         return -EFAULT;
198                                 if (c == 'V')
199                                         expect_close = 42;
200                         }
201                 }
202                 wdt_ping();
203         }
204         return count;
205 }
206
207 static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
208 {
209         void __user *argp = (void __user *)arg;
210         int __user *p = argp;
211         int new_timeout;
212         static const struct watchdog_info ident = {
213                 .options =              WDIOF_KEEPALIVEPING |
214                                         WDIOF_SETTIMEOUT |
215                                         WDIOF_MAGICCLOSE,
216                 .firmware_version =     1,
217                 .identity =             "W83697UG WDT",
218         };
219
220         switch (cmd) {
221         case WDIOC_GETSUPPORT:
222                 if (copy_to_user(argp, &ident, sizeof(ident)))
223                         return -EFAULT;
224                 break;
225
226         case WDIOC_GETSTATUS:
227         case WDIOC_GETBOOTSTATUS:
228                 return put_user(0, p);
229
230         case WDIOC_SETOPTIONS:
231         {
232                 int options, retval = -EINVAL;
233
234                 if (get_user(options, p))
235                         return -EFAULT;
236
237                 if (options & WDIOS_DISABLECARD) {
238                         wdt_disable();
239                         retval = 0;
240                 }
241
242                 if (options & WDIOS_ENABLECARD) {
243                         wdt_ping();
244                         retval = 0;
245                 }
246
247                 return retval;
248         }
249
250         case WDIOC_KEEPALIVE:
251                 wdt_ping();
252                 break;
253
254         case WDIOC_SETTIMEOUT:
255                 if (get_user(new_timeout, p))
256                         return -EFAULT;
257                 if (wdt_set_heartbeat(new_timeout))
258                         return -EINVAL;
259                 wdt_ping();
260                 /* Fall */
261
262         case WDIOC_GETTIMEOUT:
263                 return put_user(timeout, p);
264
265         default:
266                 return -ENOTTY;
267         }
268         return 0;
269 }
270
271 static int wdt_open(struct inode *inode, struct file *file)
272 {
273         if (test_and_set_bit(0, &wdt_is_open))
274                 return -EBUSY;
275         /*
276          *      Activate
277          */
278
279         wdt_ping();
280         return nonseekable_open(inode, file);
281 }
282
283 static int wdt_close(struct inode *inode, struct file *file)
284 {
285         if (expect_close == 42)
286                 wdt_disable();
287         else {
288                 printk(KERN_CRIT PFX
289                         "Unexpected close, not stopping watchdog!\n");
290                 wdt_ping();
291         }
292         expect_close = 0;
293         clear_bit(0, &wdt_is_open);
294         return 0;
295 }
296
297 /*
298  *      Notifier for system down
299  */
300
301 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
302         void *unused)
303 {
304         if (code == SYS_DOWN || code == SYS_HALT)
305                 wdt_disable();  /* Turn the WDT off */
306
307         return NOTIFY_DONE;
308 }
309
310 /*
311  *      Kernel Interfaces
312  */
313
314 static const struct file_operations wdt_fops = {
315         .owner          = THIS_MODULE,
316         .llseek         = no_llseek,
317         .write          = wdt_write,
318         .unlocked_ioctl = wdt_ioctl,
319         .open           = wdt_open,
320         .release        = wdt_close,
321 };
322
323 static struct miscdevice wdt_miscdev = {
324         .minor = WATCHDOG_MINOR,
325         .name = "watchdog",
326         .fops = &wdt_fops,
327 };
328
329 /*
330  *      The WDT needs to learn about soft shutdowns in order to
331  *      turn the timebomb registers off.
332  */
333
334 static struct notifier_block wdt_notifier = {
335         .notifier_call = wdt_notify_sys,
336 };
337
338 static int __init wdt_init(void)
339 {
340         int ret;
341
342         printk(KERN_INFO "WDT driver for the Winbond(TM) W83697UG/UF Super I/O chip initialising.\n");
343
344         if (wdt_set_heartbeat(timeout)) {
345                 wdt_set_heartbeat(WATCHDOG_TIMEOUT);
346                 printk(KERN_INFO PFX
347                         "timeout value must be 1<=timeout<=255, using %d\n",
348                         WATCHDOG_TIMEOUT);
349         }
350
351         if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
352                 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
353                         wdt_io);
354                 ret = -EIO;
355                 goto out;
356         }
357
358         ret = w83697ug_init();
359         if (ret != 0)
360                 goto unreg_regions;
361
362         ret = register_reboot_notifier(&wdt_notifier);
363         if (ret != 0) {
364                 printk(KERN_ERR PFX
365                         "cannot register reboot notifier (err=%d)\n", ret);
366                 goto unreg_regions;
367         }
368
369         ret = misc_register(&wdt_miscdev);
370         if (ret != 0) {
371                 printk(KERN_ERR PFX
372                         "cannot register miscdev on minor=%d (err=%d)\n",
373                         WATCHDOG_MINOR, ret);
374                 goto unreg_reboot;
375         }
376
377         printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
378                 timeout, nowayout);
379
380 out:
381         return ret;
382 unreg_reboot:
383         unregister_reboot_notifier(&wdt_notifier);
384 unreg_regions:
385         release_region(wdt_io, 1);
386         goto out;
387 }
388
389 static void __exit wdt_exit(void)
390 {
391         misc_deregister(&wdt_miscdev);
392         unregister_reboot_notifier(&wdt_notifier);
393         release_region(wdt_io, 1);
394 }
395
396 module_init(wdt_init);
397 module_exit(wdt_exit);
398
399 MODULE_LICENSE("GPL");
400 MODULE_AUTHOR("Flemming Frandsen <ff@nrvissing.net>");
401 MODULE_DESCRIPTION("w83697ug/uf WDT driver");
402 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);