b7b2dc9344183c8eaa714276d19ec616e2f7118f
[linux-flexiantxendom0-3.2.10.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/types.h>
47 #include <linux/miscdevice.h>
48 #include <linux/watchdog.h>
49 #include <linux/fs.h>
50 #include <linux/ioport.h>
51 #include <linux/notifier.h>
52 #include <linux/reboot.h>
53 #include <linux/init.h>
54
55 #include <asm/io.h>
56 #include <asm/uaccess.h>
57 #include <asm/system.h>
58
59 static unsigned long eurwdt_is_open;
60 static int eurwdt_timeout;
61 static char eur_expect_close;
62
63 /*
64  * You must set these - there is no sane way to probe for this board.
65  * You can use eurwdt=x,y to set these now.
66  */
67
68 static int io = 0x3f0;
69 static int irq = 10;
70 static char *ev = "int";
71
72 #define WDT_TIMEOUT             60                /* 1 minute */
73
74 #ifdef CONFIG_WATCHDOG_NOWAYOUT
75 static int nowayout = 1;
76 #else
77 static int nowayout = 0;
78 #endif
79
80 MODULE_PARM(nowayout,"i");
81 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
82
83 /*
84  * Some symbolic names
85  */
86
87 #define WDT_CTRL_REG            0x30
88 #define WDT_OUTPIN_CFG          0xe2
89 #define WDT_EVENT_INT           0x00
90 #define WDT_EVENT_REBOOT        0x08
91 #define WDT_UNIT_SEL            0xf1
92 #define WDT_UNIT_SECS           0x80
93 #define WDT_TIMEOUT_VAL         0xf2
94 #define WDT_TIMER_CFG           0xf3
95
96
97 #ifndef MODULE
98
99 /**
100  * eurwdt_setup:
101  * @str: command line string
102  *
103  * Setup options. The board isn't really probe-able so we have to
104  * get the user to tell us the configuration. Sane people build it
105  * modular but the others come here.
106  */
107
108 static int __init eurwdt_setup(char *str)
109 {
110         int ints[4];
111
112 str = get_options (str, ARRAY_SIZE(ints), ints);
113
114         if (ints[0] > 0) {
115                 io = ints[1];
116                 if (ints[0] > 1)
117                         irq = ints[2];
118         }
119
120         return 1;
121 }
122
123 __setup("eurwdt=", eurwdt_setup);
124
125 #endif /* !MODULE */
126
127 MODULE_PARM(io, "i");
128 MODULE_PARM_DESC(io, "Eurotech WDT io port (default=0x3f0)");
129 MODULE_PARM(irq, "i");
130 MODULE_PARM_DESC(irq, "Eurotech WDT irq (default=10)");
131 MODULE_PARM(ev, "s");
132 MODULE_PARM_DESC(ev, "Eurotech WDT event type (default is `int')");
133
134
135 /*
136  * Programming support
137  */
138
139 static inline void eurwdt_write_reg(u8 index, u8 data)
140 {
141         outb(index, io);
142         outb(data, io+1);
143 }
144
145 static inline void eurwdt_lock_chip(void)
146 {
147         outb(0xaa, io);
148 }
149
150 static inline void eurwdt_unlock_chip(void)
151 {
152         outb(0x55, io);
153         eurwdt_write_reg(0x07, 0x08);   /* set the logical device */
154 }
155
156 static inline void eurwdt_set_timeout(int timeout)
157 {
158         eurwdt_write_reg(WDT_TIMEOUT_VAL, (u8) timeout);
159 }
160
161 static inline void eurwdt_disable_timer(void)
162 {
163         eurwdt_set_timeout(0);
164 }
165
166 static void eurwdt_activate_timer(void)
167 {
168         eurwdt_disable_timer();
169         eurwdt_write_reg(WDT_CTRL_REG, 0x01);   /* activate the WDT */
170         eurwdt_write_reg(WDT_OUTPIN_CFG, !strcmp("int", ev) ? WDT_EVENT_INT : WDT_EVENT_REBOOT);
171
172         /* Setting interrupt line */
173         if (irq == 2 || irq > 15 || irq < 0) {
174                 printk(KERN_ERR ": invalid irq number\n");
175                 irq = 0;        /* if invalid we disable interrupt */
176         }
177         if (irq == 0)
178                 printk(KERN_INFO ": interrupt disabled\n");
179
180         eurwdt_write_reg(WDT_TIMER_CFG, irq<<4);
181
182         eurwdt_write_reg(WDT_UNIT_SEL, WDT_UNIT_SECS);  /* we use seconds */
183         eurwdt_set_timeout(0);  /* the default timeout */
184 }
185
186
187 /*
188  * Kernel methods.
189  */
190
191 static irqreturn_t eurwdt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
192 {
193         printk(KERN_CRIT "timeout WDT timeout\n");
194
195 #ifdef ONLY_TESTING
196         printk(KERN_CRIT "Would Reboot.\n");
197 #else
198         printk(KERN_CRIT "Initiating system reboot.\n");
199         machine_restart(NULL);
200 #endif
201         return IRQ_HANDLED;
202 }
203
204
205 /**
206  * eurwdt_ping:
207  *
208  * Reload counter one with the watchdog timeout.
209  */
210
211 static void eurwdt_ping(void)
212 {
213         /* Write the watchdog default value */
214         eurwdt_set_timeout(eurwdt_timeout);
215 }
216
217 /**
218  * eurwdt_write:
219  * @file: file handle to the watchdog
220  * @buf: buffer to write (unused as data does not matter here
221  * @count: count of bytes
222  * @ppos: pointer to the position to write. No seeks allowed
223  *
224  * A write to a watchdog device is defined as a keepalive signal. Any
225  * write of data will do, as we we don't define content meaning.
226  */
227
228 static ssize_t eurwdt_write(struct file *file, const char *buf, size_t count,
229 loff_t *ppos)
230 {
231         /*  Can't seek (pwrite) on this device  */
232         if (ppos != &file->f_pos)
233         return -ESPIPE;
234
235         if (count)      {
236                 if (!nowayout) {
237                         size_t i;
238
239                         eur_expect_close = 0;
240
241                         for (i = 0; i != count; i++) {
242                                 char c;
243                                 if(get_user(c, buf+i))
244                                         return -EFAULT;
245                                 if (c == 'V')
246                                         eur_expect_close = 42;
247                         }
248                 }
249                 eurwdt_ping();  /* the default timeout */
250         }
251
252         return count;
253 }
254
255 /**
256  * eurwdt_ioctl:
257  * @inode: inode of the device
258  * @file: file handle to the device
259  * @cmd: watchdog command
260  * @arg: argument pointer
261  *
262  * The watchdog API defines a common set of functions for all watchdogs
263  * according to their available features.
264  */
265
266 static int eurwdt_ioctl(struct inode *inode, struct file *file,
267         unsigned int cmd, unsigned long arg)
268 {
269         static struct watchdog_info ident = {
270                 .options          = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
271                 .firmware_version = 1,
272                 .identity         = "WDT Eurotech CPU-1220/1410",
273         };
274
275         int time;
276         int options, retval = -EINVAL;
277
278         switch(cmd) {
279         default:
280                 return -ENOIOCTLCMD;
281
282         case WDIOC_GETSUPPORT:
283                 return copy_to_user((struct watchdog_info *)arg, &ident,
284                         sizeof(ident)) ? -EFAULT : 0;
285
286         case WDIOC_GETSTATUS:
287         case WDIOC_GETBOOTSTATUS:
288                 return put_user(0, (int *) arg);
289
290         case WDIOC_KEEPALIVE:
291                 eurwdt_ping();
292                 return 0;
293
294         case WDIOC_SETTIMEOUT:
295                 if (copy_from_user(&time, (int *) arg, sizeof(int)))
296                         return -EFAULT;
297
298                 /* Sanity check */
299                 if (time < 0 || time > 255)
300                         return -EINVAL;
301
302                 eurwdt_timeout = time;
303                 eurwdt_set_timeout(time);
304                 /* Fall */
305
306         case WDIOC_GETTIMEOUT:
307                 return put_user(eurwdt_timeout, (int *)arg);
308
309         case WDIOC_SETOPTIONS:
310                 if (get_user(options, (int *)arg))
311                         return -EFAULT;
312                 if (options & WDIOS_DISABLECARD) {
313                         eurwdt_disable_timer();
314                         retval = 0;
315                 }
316                 if (options & WDIOS_ENABLECARD) {
317                         eurwdt_activate_timer();
318                         eurwdt_ping();
319                         retval = 0;
320                 }
321                 return retval;
322         }
323 }
324
325 /**
326  * eurwdt_open:
327  * @inode: inode of device
328  * @file: file handle to device
329  *
330  * The misc device has been opened. The watchdog device is single
331  * open and on opening we load the counter.
332  */
333
334 static int eurwdt_open(struct inode *inode, struct file *file)
335 {
336         if (test_and_set_bit(0, &eurwdt_is_open))
337                 return -EBUSY;
338         eurwdt_timeout = WDT_TIMEOUT;   /* initial timeout */
339         /* Activate the WDT */
340         eurwdt_activate_timer();
341         return 0;
342 }
343
344 /**
345  * eurwdt_release:
346  * @inode: inode to board
347  * @file: file handle to board
348  *
349  * The watchdog has a configurable API. There is a religious dispute
350  * between people who want their watchdog to be able to shut down and
351  * those who want to be sure if the watchdog manager dies the machine
352  * reboots. In the former case we disable the counters, in the latter
353  * case you have to open it again very soon.
354  */
355
356 static int eurwdt_release(struct inode *inode, struct file *file)
357 {
358         if (eur_expect_close == 42) {
359                 eurwdt_disable_timer();
360         } else {
361                 printk(KERN_CRIT "eurwdt: Unexpected close, not stopping watchdog!\n");
362                 eurwdt_ping();
363         }
364         clear_bit(0, &eurwdt_is_open);
365         eur_expect_close = 0;
366         return 0;
367 }
368
369 /**
370  * eurwdt_notify_sys:
371  * @this: our notifier block
372  * @code: the event being reported
373  * @unused: unused
374  *
375  * Our notifier is called on system shutdowns. We want to turn the card
376  * off at reboot otherwise the machine will reboot again during memory
377  * test or worse yet during the following fsck. This would suck, in fact
378  * trust me - if it happens it does suck.
379  */
380
381 static int eurwdt_notify_sys(struct notifier_block *this, unsigned long code,
382         void *unused)
383 {
384         if (code == SYS_DOWN || code == SYS_HALT) {
385                 /* Turn the card off */
386                 eurwdt_disable_timer();
387         }
388
389         return NOTIFY_DONE;
390 }
391
392 /*
393  * Kernel Interfaces
394  */
395
396
397 static struct file_operations eurwdt_fops = {
398         .owner  = THIS_MODULE,
399         .llseek = no_llseek,
400         .write  = eurwdt_write,
401         .ioctl  = eurwdt_ioctl,
402         .open   = eurwdt_open,
403         .release        = eurwdt_release,
404 };
405
406 static struct miscdevice eurwdt_miscdev = {
407         .minor  = WATCHDOG_MINOR,
408         .name   = "watchdog",
409         .fops   = &eurwdt_fops,
410 };
411
412 /*
413  * The WDT card needs to learn about soft shutdowns in order to
414  * turn the timebomb registers off.
415  */
416
417 static struct notifier_block eurwdt_notifier = {
418         .notifier_call = eurwdt_notify_sys,
419 };
420
421 /**
422  * cleanup_module:
423  *
424  * Unload the watchdog. You cannot do this with any file handles open.
425  * If your watchdog is set to continue ticking on close and you unload
426  * it, well it keeps ticking. We won't get the interrupt but the board
427  * will not touch PC memory so all is fine. You just have to load a new
428  * module in 60 seconds or reboot.
429  */
430
431 static void __exit eurwdt_exit(void)
432 {
433         eurwdt_lock_chip();
434
435         misc_deregister(&eurwdt_miscdev);
436
437         unregister_reboot_notifier(&eurwdt_notifier);
438         release_region(io, 2);
439         free_irq(irq, NULL);
440 }
441
442 /**
443  * eurwdt_init:
444  *
445  * Set up the WDT watchdog board. After grabbing the resources
446  * we require we need also to unlock the device.
447  * The open() function will actually kick the board off.
448  */
449
450 static int __init eurwdt_init(void)
451 {
452         int ret;
453
454         ret = misc_register(&eurwdt_miscdev);
455         if (ret) {
456                 printk(KERN_ERR "eurwdt: can't misc_register on minor=%d\n",
457                 WATCHDOG_MINOR);
458                 goto out;
459         }
460
461         ret = request_irq(irq, eurwdt_interrupt, SA_INTERRUPT, "eurwdt", NULL);
462         if(ret) {
463                 printk(KERN_ERR "eurwdt: IRQ %d is not free.\n", irq);
464                 goto outmisc;
465         }
466
467         if (!request_region(io, 2, "eurwdt")) {
468                 printk(KERN_ERR "eurwdt: IO %X is not free.\n", io);
469                 ret = -EBUSY;
470                 goto outirq;
471         }
472
473         ret = register_reboot_notifier(&eurwdt_notifier);
474         if (ret) {
475                 printk(KERN_ERR "eurwdt: can't register reboot notifier (err=%d)\n", ret);
476                 goto outreg;
477         }
478
479         eurwdt_unlock_chip();
480
481         ret = 0;
482         printk(KERN_INFO "Eurotech WDT driver 0.01 at %X (Interrupt %d)"
483                 " - timeout event: %s\n",
484                 io, irq, (!strcmp("int", ev) ? "int" : "reboot"));
485
486 out:
487         return ret;
488
489 outreg:
490         release_region(io, 2);
491
492 outirq:
493         free_irq(irq, NULL);
494
495 outmisc:
496         misc_deregister(&eurwdt_miscdev);
497         goto out;
498 }
499
500 module_init(eurwdt_init);
501 module_exit(eurwdt_exit);
502
503 MODULE_AUTHOR("Rodolfo Giometti");
504 MODULE_DESCRIPTION("Driver for Eurotech CPU-1220/1410 on board watchdog");
505 MODULE_LICENSE("GPL");