ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / watchdog / eurotechwdt.c
1 /*
2  *      Eurotech CPU-1220/1410 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@redhat.com>, All Rights Reserved.
12  *                              http://www.redhat.com
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  * 2002/04/25 - Rob Radez
29  *      clean up #includes
30  *      clean up locking
31  *      make __setup param unique
32  *      proper options in watchdog_info
33  *      add WDIOC_GETSTATUS and WDIOC_SETOPTIONS ioctls
34  *      add expect_close support
35  *
36  * 2001 - Rodolfo Giometti
37  *      Initial release
38  *
39  * 2002.05.30 - Joel Becker <joel.becker@oracle.com>
40  *      Added Matt Domsch's nowayout module option.
41  */
42
43 #include <linux/config.h>
44 #include <linux/interrupt.h>
45 #include <linux/module.h>
46 #include <linux/moduleparam.h>
47 #include <linux/types.h>
48 #include <linux/miscdevice.h>
49 #include <linux/watchdog.h>
50 #include <linux/fs.h>
51 #include <linux/ioport.h>
52 #include <linux/notifier.h>
53 #include <linux/reboot.h>
54 #include <linux/init.h>
55
56 #include <asm/io.h>
57 #include <asm/uaccess.h>
58 #include <asm/system.h>
59
60 static unsigned long eurwdt_is_open;
61 static int eurwdt_timeout;
62 static char eur_expect_close;
63
64 /*
65  * You must set these - there is no sane way to probe for this board.
66  * You can use eurwdt=x,y to set these now.
67  */
68
69 static int io = 0x3f0;
70 static int irq = 10;
71 static char *ev = "int";
72
73 #define WDT_TIMEOUT             60                /* 1 minute */
74
75 #ifdef CONFIG_WATCHDOG_NOWAYOUT
76 static int nowayout = 1;
77 #else
78 static int nowayout = 0;
79 #endif
80
81 module_param(nowayout, int, 0);
82 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_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, !strcmp("int", ev) ? WDT_EVENT_INT : WDT_EVENT_REBOOT);
142
143         /* Setting interrupt line */
144         if (irq == 2 || irq > 15 || irq < 0) {
145                 printk(KERN_ERR ": invalid irq number\n");
146                 irq = 0;        /* if invalid we disable interrupt */
147         }
148         if (irq == 0)
149                 printk(KERN_INFO ": interrupt disabled\n");
150
151         eurwdt_write_reg(WDT_TIMER_CFG, irq<<4);
152
153         eurwdt_write_reg(WDT_UNIT_SEL, WDT_UNIT_SECS);  /* we use seconds */
154         eurwdt_set_timeout(0);  /* the default timeout */
155 }
156
157
158 /*
159  * Kernel methods.
160  */
161
162 static irqreturn_t eurwdt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
163 {
164         printk(KERN_CRIT "timeout WDT timeout\n");
165
166 #ifdef ONLY_TESTING
167         printk(KERN_CRIT "Would Reboot.\n");
168 #else
169         printk(KERN_CRIT "Initiating system reboot.\n");
170         machine_restart(NULL);
171 #endif
172         return IRQ_HANDLED;
173 }
174
175
176 /**
177  * eurwdt_ping:
178  *
179  * Reload counter one with the watchdog timeout.
180  */
181
182 static void eurwdt_ping(void)
183 {
184         /* Write the watchdog default value */
185         eurwdt_set_timeout(eurwdt_timeout);
186 }
187
188 /**
189  * eurwdt_write:
190  * @file: file handle to the watchdog
191  * @buf: buffer to write (unused as data does not matter here
192  * @count: count of bytes
193  * @ppos: pointer to the position to write. No seeks allowed
194  *
195  * A write to a watchdog device is defined as a keepalive signal. Any
196  * write of data will do, as we we don't define content meaning.
197  */
198
199 static ssize_t eurwdt_write(struct file *file, const char *buf, size_t count,
200 loff_t *ppos)
201 {
202         /*  Can't seek (pwrite) on this device  */
203         if (ppos != &file->f_pos)
204         return -ESPIPE;
205
206         if (count)      {
207                 if (!nowayout) {
208                         size_t i;
209
210                         eur_expect_close = 0;
211
212                         for (i = 0; i != count; i++) {
213                                 char c;
214                                 if(get_user(c, buf+i))
215                                         return -EFAULT;
216                                 if (c == 'V')
217                                         eur_expect_close = 42;
218                         }
219                 }
220                 eurwdt_ping();  /* the default timeout */
221         }
222
223         return count;
224 }
225
226 /**
227  * eurwdt_ioctl:
228  * @inode: inode of the device
229  * @file: file handle to the device
230  * @cmd: watchdog command
231  * @arg: argument pointer
232  *
233  * The watchdog API defines a common set of functions for all watchdogs
234  * according to their available features.
235  */
236
237 static int eurwdt_ioctl(struct inode *inode, struct file *file,
238         unsigned int cmd, unsigned long arg)
239 {
240         static struct watchdog_info ident = {
241                 .options          = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | 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         default:
251                 return -ENOIOCTLCMD;
252
253         case WDIOC_GETSUPPORT:
254                 return copy_to_user((struct watchdog_info *)arg, &ident,
255                         sizeof(ident)) ? -EFAULT : 0;
256
257         case WDIOC_GETSTATUS:
258         case WDIOC_GETBOOTSTATUS:
259                 return put_user(0, (int *) arg);
260
261         case WDIOC_KEEPALIVE:
262                 eurwdt_ping();
263                 return 0;
264
265         case WDIOC_SETTIMEOUT:
266                 if (copy_from_user(&time, (int *) arg, sizeof(int)))
267                         return -EFAULT;
268
269                 /* Sanity check */
270                 if (time < 0 || time > 255)
271                         return -EINVAL;
272
273                 eurwdt_timeout = time;
274                 eurwdt_set_timeout(time);
275                 /* Fall */
276
277         case WDIOC_GETTIMEOUT:
278                 return put_user(eurwdt_timeout, (int *)arg);
279
280         case WDIOC_SETOPTIONS:
281                 if (get_user(options, (int *)arg))
282                         return -EFAULT;
283                 if (options & WDIOS_DISABLECARD) {
284                         eurwdt_disable_timer();
285                         retval = 0;
286                 }
287                 if (options & WDIOS_ENABLECARD) {
288                         eurwdt_activate_timer();
289                         eurwdt_ping();
290                         retval = 0;
291                 }
292                 return retval;
293         }
294 }
295
296 /**
297  * eurwdt_open:
298  * @inode: inode of device
299  * @file: file handle to device
300  *
301  * The misc device has been opened. The watchdog device is single
302  * open and on opening we load the counter.
303  */
304
305 static int eurwdt_open(struct inode *inode, struct file *file)
306 {
307         if (test_and_set_bit(0, &eurwdt_is_open))
308                 return -EBUSY;
309         eurwdt_timeout = WDT_TIMEOUT;   /* initial timeout */
310         /* Activate the WDT */
311         eurwdt_activate_timer();
312         return 0;
313 }
314
315 /**
316  * eurwdt_release:
317  * @inode: inode to board
318  * @file: file handle to board
319  *
320  * The watchdog has a configurable API. There is a religious dispute
321  * between people who want their watchdog to be able to shut down and
322  * those who want to be sure if the watchdog manager dies the machine
323  * reboots. In the former case we disable the counters, in the latter
324  * case you have to open it again very soon.
325  */
326
327 static int eurwdt_release(struct inode *inode, struct file *file)
328 {
329         if (eur_expect_close == 42) {
330                 eurwdt_disable_timer();
331         } else {
332                 printk(KERN_CRIT "eurwdt: Unexpected close, not stopping watchdog!\n");
333                 eurwdt_ping();
334         }
335         clear_bit(0, &eurwdt_is_open);
336         eur_expect_close = 0;
337         return 0;
338 }
339
340 /**
341  * eurwdt_notify_sys:
342  * @this: our notifier block
343  * @code: the event being reported
344  * @unused: unused
345  *
346  * Our notifier is called on system shutdowns. We want to turn the card
347  * off at reboot otherwise the machine will reboot again during memory
348  * test or worse yet during the following fsck. This would suck, in fact
349  * trust me - if it happens it does suck.
350  */
351
352 static int eurwdt_notify_sys(struct notifier_block *this, unsigned long code,
353         void *unused)
354 {
355         if (code == SYS_DOWN || code == SYS_HALT) {
356                 /* Turn the card off */
357                 eurwdt_disable_timer();
358         }
359
360         return NOTIFY_DONE;
361 }
362
363 /*
364  * Kernel Interfaces
365  */
366
367
368 static struct file_operations eurwdt_fops = {
369         .owner  = THIS_MODULE,
370         .llseek = no_llseek,
371         .write  = eurwdt_write,
372         .ioctl  = eurwdt_ioctl,
373         .open   = eurwdt_open,
374         .release        = eurwdt_release,
375 };
376
377 static struct miscdevice eurwdt_miscdev = {
378         .minor  = WATCHDOG_MINOR,
379         .name   = "watchdog",
380         .fops   = &eurwdt_fops,
381 };
382
383 /*
384  * The WDT card needs to learn about soft shutdowns in order to
385  * turn the timebomb registers off.
386  */
387
388 static struct notifier_block eurwdt_notifier = {
389         .notifier_call = eurwdt_notify_sys,
390 };
391
392 /**
393  * cleanup_module:
394  *
395  * Unload the watchdog. You cannot do this with any file handles open.
396  * If your watchdog is set to continue ticking on close and you unload
397  * it, well it keeps ticking. We won't get the interrupt but the board
398  * will not touch PC memory so all is fine. You just have to load a new
399  * module in 60 seconds or reboot.
400  */
401
402 static void __exit eurwdt_exit(void)
403 {
404         eurwdt_lock_chip();
405
406         misc_deregister(&eurwdt_miscdev);
407
408         unregister_reboot_notifier(&eurwdt_notifier);
409         release_region(io, 2);
410         free_irq(irq, NULL);
411 }
412
413 /**
414  * eurwdt_init:
415  *
416  * Set up the WDT watchdog board. After grabbing the resources
417  * we require we need also to unlock the device.
418  * The open() function will actually kick the board off.
419  */
420
421 static int __init eurwdt_init(void)
422 {
423         int ret;
424
425         ret = misc_register(&eurwdt_miscdev);
426         if (ret) {
427                 printk(KERN_ERR "eurwdt: can't misc_register on minor=%d\n",
428                 WATCHDOG_MINOR);
429                 goto out;
430         }
431
432         ret = request_irq(irq, eurwdt_interrupt, SA_INTERRUPT, "eurwdt", NULL);
433         if(ret) {
434                 printk(KERN_ERR "eurwdt: IRQ %d is not free.\n", irq);
435                 goto outmisc;
436         }
437
438         if (!request_region(io, 2, "eurwdt")) {
439                 printk(KERN_ERR "eurwdt: IO %X is not free.\n", io);
440                 ret = -EBUSY;
441                 goto outirq;
442         }
443
444         ret = register_reboot_notifier(&eurwdt_notifier);
445         if (ret) {
446                 printk(KERN_ERR "eurwdt: can't register reboot notifier (err=%d)\n", ret);
447                 goto outreg;
448         }
449
450         eurwdt_unlock_chip();
451
452         ret = 0;
453         printk(KERN_INFO "Eurotech WDT driver 0.01 at %X (Interrupt %d)"
454                 " - timeout event: %s\n",
455                 io, irq, (!strcmp("int", ev) ? "int" : "reboot"));
456
457 out:
458         return ret;
459
460 outreg:
461         release_region(io, 2);
462
463 outirq:
464         free_irq(irq, NULL);
465
466 outmisc:
467         misc_deregister(&eurwdt_miscdev);
468         goto out;
469 }
470
471 module_init(eurwdt_init);
472 module_exit(eurwdt_exit);
473
474 MODULE_AUTHOR("Rodolfo Giometti");
475 MODULE_DESCRIPTION("Driver for Eurotech CPU-1220/1410 on board watchdog");
476 MODULE_LICENSE("GPL");
477 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);