2 * Eurotech CPU-1220/1410 on board WDT driver
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>
9 * Original copyright messages:
11 * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
12 * http://www.redhat.com
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.
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.
23 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>*
28 * 2002/04/25 - Rob Radez
31 * make __setup param unique
32 * proper options in watchdog_info
33 * add WDIOC_GETSTATUS and WDIOC_SETOPTIONS ioctls
34 * add expect_close support
36 * 2001 - Rodolfo Giometti
39 * 2002.05.30 - Joel Becker <joel.becker@oracle.com>
40 * Added Matt Domsch's nowayout module option.
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>
50 #include <linux/ioport.h>
51 #include <linux/notifier.h>
52 #include <linux/reboot.h>
53 #include <linux/init.h>
56 #include <asm/uaccess.h>
57 #include <asm/system.h>
59 static unsigned long eurwdt_is_open;
60 static int eurwdt_timeout;
61 static char eur_expect_close;
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.
68 static int io = 0x3f0;
70 static char *ev = "int";
72 #define WDT_TIMEOUT 60 /* 1 minute */
74 #ifdef CONFIG_WATCHDOG_NOWAYOUT
75 static int nowayout = 1;
77 static int nowayout = 0;
80 MODULE_PARM(nowayout,"i");
81 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
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
101 * @str: command line string
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.
108 static int __init eurwdt_setup(char *str)
112 str = get_options (str, ARRAY_SIZE(ints), ints);
123 __setup("eurwdt=", eurwdt_setup);
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')");
136 * Programming support
139 static inline void eurwdt_write_reg(u8 index, u8 data)
145 static inline void eurwdt_lock_chip(void)
150 static inline void eurwdt_unlock_chip(void)
153 eurwdt_write_reg(0x07, 0x08); /* set the logical device */
156 static inline void eurwdt_set_timeout(int timeout)
158 eurwdt_write_reg(WDT_TIMEOUT_VAL, (u8) timeout);
161 static inline void eurwdt_disable_timer(void)
163 eurwdt_set_timeout(0);
166 static void eurwdt_activate_timer(void)
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);
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 */
178 printk(KERN_INFO ": interrupt disabled\n");
180 eurwdt_write_reg(WDT_TIMER_CFG, irq<<4);
182 eurwdt_write_reg(WDT_UNIT_SEL, WDT_UNIT_SECS); /* we use seconds */
183 eurwdt_set_timeout(0); /* the default timeout */
191 static irqreturn_t eurwdt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
193 printk(KERN_CRIT "timeout WDT timeout\n");
196 printk(KERN_CRIT "Would Reboot.\n");
198 printk(KERN_CRIT "Initiating system reboot.\n");
199 machine_restart(NULL);
208 * Reload counter one with the watchdog timeout.
211 static void eurwdt_ping(void)
213 /* Write the watchdog default value */
214 eurwdt_set_timeout(eurwdt_timeout);
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
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.
228 static ssize_t eurwdt_write(struct file *file, const char *buf, size_t count,
231 /* Can't seek (pwrite) on this device */
232 if (ppos != &file->f_pos)
239 eur_expect_close = 0;
241 for (i = 0; i != count; i++) {
243 if(get_user(c, buf+i))
246 eur_expect_close = 42;
249 eurwdt_ping(); /* the default timeout */
257 * @inode: inode of the device
258 * @file: file handle to the device
259 * @cmd: watchdog command
260 * @arg: argument pointer
262 * The watchdog API defines a common set of functions for all watchdogs
263 * according to their available features.
266 static int eurwdt_ioctl(struct inode *inode, struct file *file,
267 unsigned int cmd, unsigned long arg)
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",
276 int options, retval = -EINVAL;
282 case WDIOC_GETSUPPORT:
283 return copy_to_user((struct watchdog_info *)arg, &ident,
284 sizeof(ident)) ? -EFAULT : 0;
286 case WDIOC_GETSTATUS:
287 case WDIOC_GETBOOTSTATUS:
288 return put_user(0, (int *) arg);
290 case WDIOC_KEEPALIVE:
294 case WDIOC_SETTIMEOUT:
295 if (copy_from_user(&time, (int *) arg, sizeof(int)))
299 if (time < 0 || time > 255)
302 eurwdt_timeout = time;
303 eurwdt_set_timeout(time);
306 case WDIOC_GETTIMEOUT:
307 return put_user(eurwdt_timeout, (int *)arg);
309 case WDIOC_SETOPTIONS:
310 if (get_user(options, (int *)arg))
312 if (options & WDIOS_DISABLECARD) {
313 eurwdt_disable_timer();
316 if (options & WDIOS_ENABLECARD) {
317 eurwdt_activate_timer();
327 * @inode: inode of device
328 * @file: file handle to device
330 * The misc device has been opened. The watchdog device is single
331 * open and on opening we load the counter.
334 static int eurwdt_open(struct inode *inode, struct file *file)
336 if (test_and_set_bit(0, &eurwdt_is_open))
338 eurwdt_timeout = WDT_TIMEOUT; /* initial timeout */
339 /* Activate the WDT */
340 eurwdt_activate_timer();
346 * @inode: inode to board
347 * @file: file handle to board
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.
356 static int eurwdt_release(struct inode *inode, struct file *file)
358 if (eur_expect_close == 42) {
359 eurwdt_disable_timer();
361 printk(KERN_CRIT "eurwdt: Unexpected close, not stopping watchdog!\n");
364 clear_bit(0, &eurwdt_is_open);
365 eur_expect_close = 0;
371 * @this: our notifier block
372 * @code: the event being reported
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.
381 static int eurwdt_notify_sys(struct notifier_block *this, unsigned long code,
384 if (code == SYS_DOWN || code == SYS_HALT) {
385 /* Turn the card off */
386 eurwdt_disable_timer();
397 static struct file_operations eurwdt_fops = {
398 .owner = THIS_MODULE,
400 .write = eurwdt_write,
401 .ioctl = eurwdt_ioctl,
403 .release = eurwdt_release,
406 static struct miscdevice eurwdt_miscdev = {
407 .minor = WATCHDOG_MINOR,
409 .fops = &eurwdt_fops,
413 * The WDT card needs to learn about soft shutdowns in order to
414 * turn the timebomb registers off.
417 static struct notifier_block eurwdt_notifier = {
418 .notifier_call = eurwdt_notify_sys,
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.
431 static void __exit eurwdt_exit(void)
435 misc_deregister(&eurwdt_miscdev);
437 unregister_reboot_notifier(&eurwdt_notifier);
438 release_region(io, 2);
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.
450 static int __init eurwdt_init(void)
454 ret = misc_register(&eurwdt_miscdev);
456 printk(KERN_ERR "eurwdt: can't misc_register on minor=%d\n",
461 ret = request_irq(irq, eurwdt_interrupt, SA_INTERRUPT, "eurwdt", NULL);
463 printk(KERN_ERR "eurwdt: IRQ %d is not free.\n", irq);
467 if (!request_region(io, 2, "eurwdt")) {
468 printk(KERN_ERR "eurwdt: IO %X is not free.\n", io);
473 ret = register_reboot_notifier(&eurwdt_notifier);
475 printk(KERN_ERR "eurwdt: can't register reboot notifier (err=%d)\n", ret);
479 eurwdt_unlock_chip();
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"));
490 release_region(io, 2);
496 misc_deregister(&eurwdt_miscdev);
500 module_init(eurwdt_init);
501 module_exit(eurwdt_exit);
503 MODULE_AUTHOR("Rodolfo Giometti");
504 MODULE_DESCRIPTION("Driver for Eurotech CPU-1220/1410 on board watchdog");
505 MODULE_LICENSE("GPL");