Remove all #inclusions of asm/system.h
[pandora-kernel.git] / drivers / watchdog / eurotechwdt.c
1 /*
2  *      Eurotech CPU-1220/1410/1420 on board WDT driver
3  *
4  *      (c) Copyright 2001 Ascensit <support@ascensit.com>
5  *      (c) Copyright 2001 Rodolfo Giometti <giometti@ascensit.com>
6  *      (c) Copyright 2002 Rob Radez <rob@osinvestor.com>
7  *
8  *      Based on wdt.c.
9  *      Original copyright messages:
10  *
11  *      (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
12  *                                              All Rights Reserved.
13  *
14  *      This program is free software; you can redistribute it and/or
15  *      modify it under the terms of the GNU General Public License
16  *      as published by the Free Software Foundation; either version
17  *      2 of the License, or (at your option) any later version.
18  *
19  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
20  *      warranty for any of this software. This material is provided
21  *      "AS-IS" and at no charge.
22  *
23  *      (c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>*
24  */
25
26 /* Changelog:
27  *
28  * 2001 - Rodolfo Giometti
29  *      Initial release
30  *
31  * 2002/04/25 - Rob Radez
32  *      clean up #includes
33  *      clean up locking
34  *      make __setup param unique
35  *      proper options in watchdog_info
36  *      add WDIOC_GETSTATUS and WDIOC_SETOPTIONS ioctls
37  *      add expect_close support
38  *
39  * 2002.05.30 - Joel Becker <joel.becker@oracle.com>
40  *      Added Matt Domsch's nowayout module option.
41  */
42
43 /*
44  *      The eurotech CPU-1220/1410/1420's watchdog is a part
45  *      of the on-board SUPER I/O device SMSC FDC 37B782.
46  */
47
48 #include <linux/interrupt.h>
49 #include <linux/module.h>
50 #include <linux/moduleparam.h>
51 #include <linux/types.h>
52 #include <linux/miscdevice.h>
53 #include <linux/watchdog.h>
54 #include <linux/fs.h>
55 #include <linux/ioport.h>
56 #include <linux/notifier.h>
57 #include <linux/reboot.h>
58 #include <linux/init.h>
59 #include <linux/io.h>
60 #include <linux/uaccess.h>
61
62
63 static unsigned long eurwdt_is_open;
64 static int eurwdt_timeout;
65 static char eur_expect_close;
66 static DEFINE_SPINLOCK(eurwdt_lock);
67
68 /*
69  * You must set these - there is no sane way to probe for this board.
70  */
71
72 static int io = 0x3f0;
73 static int irq = 10;
74 static char *ev = "int";
75
76 #define WDT_TIMEOUT             60                /* 1 minute */
77
78 static int nowayout = WATCHDOG_NOWAYOUT;
79 module_param(nowayout, int, 0);
80 MODULE_PARM_DESC(nowayout,
81                 "Watchdog cannot be stopped once started (default="
82                                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
83
84 /*
85  * Some symbolic names
86  */
87
88 #define WDT_CTRL_REG            0x30
89 #define WDT_OUTPIN_CFG          0xe2
90 #define WDT_EVENT_INT           0x00
91 #define WDT_EVENT_REBOOT        0x08
92 #define WDT_UNIT_SEL            0xf1
93 #define WDT_UNIT_SECS           0x80
94 #define WDT_TIMEOUT_VAL         0xf2
95 #define WDT_TIMER_CFG           0xf3
96
97
98 module_param(io, int, 0);
99 MODULE_PARM_DESC(io, "Eurotech WDT io port (default=0x3f0)");
100 module_param(irq, int, 0);
101 MODULE_PARM_DESC(irq, "Eurotech WDT irq (default=10)");
102 module_param(ev, charp, 0);
103 MODULE_PARM_DESC(ev, "Eurotech WDT event type (default is `int')");
104
105
106 /*
107  * Programming support
108  */
109
110 static inline void eurwdt_write_reg(u8 index, u8 data)
111 {
112         outb(index, io);
113         outb(data, io+1);
114 }
115
116 static inline void eurwdt_lock_chip(void)
117 {
118         outb(0xaa, io);
119 }
120
121 static inline void eurwdt_unlock_chip(void)
122 {
123         outb(0x55, io);
124         eurwdt_write_reg(0x07, 0x08);   /* set the logical device */
125 }
126
127 static inline void eurwdt_set_timeout(int timeout)
128 {
129         eurwdt_write_reg(WDT_TIMEOUT_VAL, (u8) timeout);
130 }
131
132 static inline void eurwdt_disable_timer(void)
133 {
134         eurwdt_set_timeout(0);
135 }
136
137 static void eurwdt_activate_timer(void)
138 {
139         eurwdt_disable_timer();
140         eurwdt_write_reg(WDT_CTRL_REG, 0x01);   /* activate the WDT */
141         eurwdt_write_reg(WDT_OUTPIN_CFG,
142                 !strcmp("int", ev) ? WDT_EVENT_INT : WDT_EVENT_REBOOT);
143
144         /* Setting interrupt line */
145         if (irq == 2 || irq > 15 || irq < 0) {
146                 printk(KERN_ERR ": invalid irq number\n");
147                 irq = 0;        /* if invalid we disable interrupt */
148         }
149         if (irq == 0)
150                 printk(KERN_INFO ": interrupt disabled\n");
151
152         eurwdt_write_reg(WDT_TIMER_CFG, irq << 4);
153
154         eurwdt_write_reg(WDT_UNIT_SEL, WDT_UNIT_SECS);  /* we use seconds */
155         eurwdt_set_timeout(0);  /* the default timeout */
156 }
157
158
159 /*
160  * Kernel methods.
161  */
162
163 static irqreturn_t eurwdt_interrupt(int irq, void *dev_id)
164 {
165         printk(KERN_CRIT "timeout WDT timeout\n");
166
167 #ifdef ONLY_TESTING
168         printk(KERN_CRIT "Would Reboot.\n");
169 #else
170         printk(KERN_CRIT "Initiating system reboot.\n");
171         emergency_restart();
172 #endif
173         return IRQ_HANDLED;
174 }
175
176
177 /**
178  * eurwdt_ping:
179  *
180  * Reload counter one with the watchdog timeout.
181  */
182
183 static void eurwdt_ping(void)
184 {
185         /* Write the watchdog default value */
186         eurwdt_set_timeout(eurwdt_timeout);
187 }
188
189 /**
190  * eurwdt_write:
191  * @file: file handle to the watchdog
192  * @buf: buffer to write (unused as data does not matter here
193  * @count: count of bytes
194  * @ppos: pointer to the position to write. No seeks allowed
195  *
196  * A write to a watchdog device is defined as a keepalive signal. Any
197  * write of data will do, as we we don't define content meaning.
198  */
199
200 static ssize_t eurwdt_write(struct file *file, const char __user *buf,
201 size_t count, loff_t *ppos)
202 {
203         if (count) {
204                 if (!nowayout) {
205                         size_t i;
206
207                         eur_expect_close = 0;
208
209                         for (i = 0; i != count; i++) {
210                                 char c;
211                                 if (get_user(c, buf + i))
212                                         return -EFAULT;
213                                 if (c == 'V')
214                                         eur_expect_close = 42;
215                         }
216                 }
217                 spin_lock(&eurwdt_lock);
218                 eurwdt_ping();  /* the default timeout */
219                 spin_unlock(&eurwdt_lock);
220         }
221         return count;
222 }
223
224 /**
225  * eurwdt_ioctl:
226  * @file: file handle to the device
227  * @cmd: watchdog command
228  * @arg: argument pointer
229  *
230  * The watchdog API defines a common set of functions for all watchdogs
231  * according to their available features.
232  */
233
234 static long eurwdt_ioctl(struct file *file,
235                                         unsigned int cmd, unsigned long arg)
236 {
237         void __user *argp = (void __user *)arg;
238         int __user *p = argp;
239         static const struct watchdog_info ident = {
240                 .options          = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
241                                                         | WDIOF_MAGICCLOSE,
242                 .firmware_version = 1,
243                 .identity         = "WDT Eurotech CPU-1220/1410",
244         };
245
246         int time;
247         int options, retval = -EINVAL;
248
249         switch (cmd) {
250         case WDIOC_GETSUPPORT:
251                 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
252
253         case WDIOC_GETSTATUS:
254         case WDIOC_GETBOOTSTATUS:
255                 return put_user(0, p);
256
257         case WDIOC_SETOPTIONS:
258                 if (get_user(options, p))
259                         return -EFAULT;
260                 spin_lock(&eurwdt_lock);
261                 if (options & WDIOS_DISABLECARD) {
262                         eurwdt_disable_timer();
263                         retval = 0;
264                 }
265                 if (options & WDIOS_ENABLECARD) {
266                         eurwdt_activate_timer();
267                         eurwdt_ping();
268                         retval = 0;
269                 }
270                 spin_unlock(&eurwdt_lock);
271                 return retval;
272
273         case WDIOC_KEEPALIVE:
274                 spin_lock(&eurwdt_lock);
275                 eurwdt_ping();
276                 spin_unlock(&eurwdt_lock);
277                 return 0;
278
279         case WDIOC_SETTIMEOUT:
280                 if (copy_from_user(&time, p, sizeof(int)))
281                         return -EFAULT;
282
283                 /* Sanity check */
284                 if (time < 0 || time > 255)
285                         return -EINVAL;
286
287                 spin_lock(&eurwdt_lock);
288                 eurwdt_timeout = time;
289                 eurwdt_set_timeout(time);
290                 spin_unlock(&eurwdt_lock);
291                 /* Fall */
292
293         case WDIOC_GETTIMEOUT:
294                 return put_user(eurwdt_timeout, p);
295
296         default:
297                 return -ENOTTY;
298         }
299 }
300
301 /**
302  * eurwdt_open:
303  * @inode: inode of device
304  * @file: file handle to device
305  *
306  * The misc device has been opened. The watchdog device is single
307  * open and on opening we load the counter.
308  */
309
310 static int eurwdt_open(struct inode *inode, struct file *file)
311 {
312         if (test_and_set_bit(0, &eurwdt_is_open))
313                 return -EBUSY;
314         eurwdt_timeout = WDT_TIMEOUT;   /* initial timeout */
315         /* Activate the WDT */
316         eurwdt_activate_timer();
317         return nonseekable_open(inode, file);
318 }
319
320 /**
321  * eurwdt_release:
322  * @inode: inode to board
323  * @file: file handle to board
324  *
325  * The watchdog has a configurable API. There is a religious dispute
326  * between people who want their watchdog to be able to shut down and
327  * those who want to be sure if the watchdog manager dies the machine
328  * reboots. In the former case we disable the counters, in the latter
329  * case you have to open it again very soon.
330  */
331
332 static int eurwdt_release(struct inode *inode, struct file *file)
333 {
334         if (eur_expect_close == 42)
335                 eurwdt_disable_timer();
336         else {
337                 printk(KERN_CRIT
338                         "eurwdt: Unexpected close, not stopping watchdog!\n");
339                 eurwdt_ping();
340         }
341         clear_bit(0, &eurwdt_is_open);
342         eur_expect_close = 0;
343         return 0;
344 }
345
346 /**
347  * eurwdt_notify_sys:
348  * @this: our notifier block
349  * @code: the event being reported
350  * @unused: unused
351  *
352  * Our notifier is called on system shutdowns. We want to turn the card
353  * off at reboot otherwise the machine will reboot again during memory
354  * test or worse yet during the following fsck. This would suck, in fact
355  * trust me - if it happens it does suck.
356  */
357
358 static int eurwdt_notify_sys(struct notifier_block *this, unsigned long code,
359         void *unused)
360 {
361         if (code == SYS_DOWN || code == SYS_HALT)
362                 eurwdt_disable_timer(); /* Turn the card off */
363
364         return NOTIFY_DONE;
365 }
366
367 /*
368  * Kernel Interfaces
369  */
370
371
372 static const struct file_operations eurwdt_fops = {
373         .owner          = THIS_MODULE,
374         .llseek         = no_llseek,
375         .write          = eurwdt_write,
376         .unlocked_ioctl = eurwdt_ioctl,
377         .open           = eurwdt_open,
378         .release        = eurwdt_release,
379 };
380
381 static struct miscdevice eurwdt_miscdev = {
382         .minor  = WATCHDOG_MINOR,
383         .name   = "watchdog",
384         .fops   = &eurwdt_fops,
385 };
386
387 /*
388  * The WDT card needs to learn about soft shutdowns in order to
389  * turn the timebomb registers off.
390  */
391
392 static struct notifier_block eurwdt_notifier = {
393         .notifier_call = eurwdt_notify_sys,
394 };
395
396 /**
397  * cleanup_module:
398  *
399  * Unload the watchdog. You cannot do this with any file handles open.
400  * If your watchdog is set to continue ticking on close and you unload
401  * it, well it keeps ticking. We won't get the interrupt but the board
402  * will not touch PC memory so all is fine. You just have to load a new
403  * module in 60 seconds or reboot.
404  */
405
406 static void __exit eurwdt_exit(void)
407 {
408         eurwdt_lock_chip();
409
410         misc_deregister(&eurwdt_miscdev);
411
412         unregister_reboot_notifier(&eurwdt_notifier);
413         release_region(io, 2);
414         free_irq(irq, NULL);
415 }
416
417 /**
418  * eurwdt_init:
419  *
420  * Set up the WDT watchdog board. After grabbing the resources
421  * we require we need also to unlock the device.
422  * The open() function will actually kick the board off.
423  */
424
425 static int __init eurwdt_init(void)
426 {
427         int ret;
428
429         ret = request_irq(irq, eurwdt_interrupt, 0, "eurwdt", NULL);
430         if (ret) {
431                 printk(KERN_ERR "eurwdt: IRQ %d is not free.\n", irq);
432                 goto out;
433         }
434
435         if (!request_region(io, 2, "eurwdt")) {
436                 printk(KERN_ERR "eurwdt: IO %X is not free.\n", io);
437                 ret = -EBUSY;
438                 goto outirq;
439         }
440
441         ret = register_reboot_notifier(&eurwdt_notifier);
442         if (ret) {
443                 printk(KERN_ERR
444                     "eurwdt: can't register reboot notifier (err=%d)\n", ret);
445                 goto outreg;
446         }
447
448         ret = misc_register(&eurwdt_miscdev);
449         if (ret) {
450                 printk(KERN_ERR "eurwdt: can't misc_register on minor=%d\n",
451                 WATCHDOG_MINOR);
452                 goto outreboot;
453         }
454
455         eurwdt_unlock_chip();
456
457         ret = 0;
458         printk(KERN_INFO "Eurotech WDT driver 0.01 at %X (Interrupt %d)"
459                 " - timeout event: %s\n",
460                 io, irq, (!strcmp("int", ev) ? "int" : "reboot"));
461
462 out:
463         return ret;
464
465 outreboot:
466         unregister_reboot_notifier(&eurwdt_notifier);
467
468 outreg:
469         release_region(io, 2);
470
471 outirq:
472         free_irq(irq, NULL);
473         goto out;
474 }
475
476 module_init(eurwdt_init);
477 module_exit(eurwdt_exit);
478
479 MODULE_AUTHOR("Rodolfo Giometti");
480 MODULE_DESCRIPTION("Driver for Eurotech CPU-1220/1410 on board watchdog");
481 MODULE_LICENSE("GPL");
482 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);