zorro: Fix reading of proc/bus/zorro/* in small chunks
[linux/fpc-iii.git] / drivers / watchdog / pc87413_wdt.c
blob3a56bc3609247f4fbb6e8fe5fffd5bacb2e209d1
1 /*
2 * NS pc87413-wdt Watchdog Timer driver for Linux 2.6.x.x
4 * This code is based on wdt.c with original copyright.
6 * (C) Copyright 2006 Sven Anders, <anders@anduras.de>
7 * and Marcus Junker, <junker@anduras.de>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 * Neither Sven Anders, Marcus Junker nor ANDURAS AG
15 * admit liability nor provide warranty for any of this software.
16 * This material is provided "AS-IS" and at no charge.
18 * Release 1.1
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/miscdevice.h>
24 #include <linux/watchdog.h>
25 #include <linux/ioport.h>
26 #include <linux/delay.h>
27 #include <linux/notifier.h>
28 #include <linux/fs.h>
29 #include <linux/reboot.h>
30 #include <linux/init.h>
31 #include <linux/spinlock.h>
32 #include <linux/moduleparam.h>
33 #include <linux/io.h>
34 #include <linux/uaccess.h>
36 #include <asm/system.h>
38 /* #define DEBUG 1 */
40 #define DEFAULT_TIMEOUT 1 /* 1 minute */
41 #define MAX_TIMEOUT 255
43 #define VERSION "1.1"
44 #define MODNAME "pc87413 WDT"
45 #define PFX MODNAME ": "
46 #define DPFX MODNAME " - DEBUG: "
48 #define WDT_INDEX_IO_PORT (io+0) /* I/O port base (index register) */
49 #define WDT_DATA_IO_PORT (WDT_INDEX_IO_PORT+1)
50 #define SWC_LDN 0x04
51 #define SIOCFG2 0x22 /* Serial IO register */
52 #define WDCTL 0x10 /* Watchdog-Timer-Controll-Register */
53 #define WDTO 0x11 /* Watchdog timeout register */
54 #define WDCFG 0x12 /* Watchdog config register */
56 #define IO_DEFAULT 0x2E /* Address used on Portwell Boards */
58 static int io = IO_DEFAULT;
60 static int timeout = DEFAULT_TIMEOUT; /* timeout value */
61 static unsigned long timer_enabled; /* is the timer enabled? */
63 static char expect_close; /* is the close expected? */
65 static DEFINE_SPINLOCK(io_lock); /* to guard us from io races */
67 static int nowayout = WATCHDOG_NOWAYOUT;
69 /* -- Low level function ----------------------------------------*/
71 /* Select pins for Watchdog output */
73 static inline void pc87413_select_wdt_out(void)
75 unsigned int cr_data = 0;
77 /* Step 1: Select multiple pin,pin55,as WDT output */
79 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
81 cr_data = inb(WDT_DATA_IO_PORT);
83 cr_data |= 0x80; /* Set Bit7 to 1*/
84 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
86 outb_p(cr_data, WDT_DATA_IO_PORT);
88 #ifdef DEBUG
89 printk(KERN_INFO DPFX
90 "Select multiple pin,pin55,as WDT output: Bit7 to 1: %d\n",
91 cr_data);
92 #endif
95 /* Enable SWC functions */
97 static inline void pc87413_enable_swc(void)
99 unsigned int cr_data = 0;
101 /* Step 2: Enable SWC functions */
103 outb_p(0x07, WDT_INDEX_IO_PORT); /* Point SWC_LDN (LDN=4) */
104 outb_p(SWC_LDN, WDT_DATA_IO_PORT);
106 outb_p(0x30, WDT_INDEX_IO_PORT); /* Read Index 0x30 First */
107 cr_data = inb(WDT_DATA_IO_PORT);
108 cr_data |= 0x01; /* Set Bit0 to 1 */
109 outb_p(0x30, WDT_INDEX_IO_PORT);
110 outb_p(cr_data, WDT_DATA_IO_PORT); /* Index0x30_bit0P1 */
112 #ifdef DEBUG
113 printk(KERN_INFO DPFX "pc87413 - Enable SWC functions\n");
114 #endif
117 /* Read SWC I/O base address */
119 static inline unsigned int pc87413_get_swc_base(void)
121 unsigned int swc_base_addr = 0;
122 unsigned char addr_l, addr_h = 0;
124 /* Step 3: Read SWC I/O Base Address */
126 outb_p(0x60, WDT_INDEX_IO_PORT); /* Read Index 0x60 */
127 addr_h = inb(WDT_DATA_IO_PORT);
129 outb_p(0x61, WDT_INDEX_IO_PORT); /* Read Index 0x61 */
131 addr_l = inb(WDT_DATA_IO_PORT);
133 swc_base_addr = (addr_h << 8) + addr_l;
134 #ifdef DEBUG
135 printk(KERN_INFO DPFX
136 "Read SWC I/O Base Address: low %d, high %d, res %d\n",
137 addr_l, addr_h, swc_base_addr);
138 #endif
139 return swc_base_addr;
142 /* Select Bank 3 of SWC */
144 static inline void pc87413_swc_bank3(unsigned int swc_base_addr)
146 /* Step 4: Select Bank3 of SWC */
147 outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
148 #ifdef DEBUG
149 printk(KERN_INFO DPFX "Select Bank3 of SWC\n");
150 #endif
153 /* Set watchdog timeout to x minutes */
155 static inline void pc87413_programm_wdto(unsigned int swc_base_addr,
156 char pc87413_time)
158 /* Step 5: Programm WDTO, Twd. */
159 outb_p(pc87413_time, swc_base_addr + WDTO);
160 #ifdef DEBUG
161 printk(KERN_INFO DPFX "Set WDTO to %d minutes\n", pc87413_time);
162 #endif
165 /* Enable WDEN */
167 static inline void pc87413_enable_wden(unsigned int swc_base_addr)
169 /* Step 6: Enable WDEN */
170 outb_p(inb(swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
171 #ifdef DEBUG
172 printk(KERN_INFO DPFX "Enable WDEN\n");
173 #endif
176 /* Enable SW_WD_TREN */
177 static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr)
179 /* Enable SW_WD_TREN */
180 outb_p(inb(swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
181 #ifdef DEBUG
182 printk(KERN_INFO DPFX "Enable SW_WD_TREN\n");
183 #endif
186 /* Disable SW_WD_TREN */
188 static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr)
190 /* Disable SW_WD_TREN */
191 outb_p(inb(swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
192 #ifdef DEBUG
193 printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN\n");
194 #endif
197 /* Enable SW_WD_TRG */
199 static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr)
201 /* Enable SW_WD_TRG */
202 outb_p(inb(swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
203 #ifdef DEBUG
204 printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG\n");
205 #endif
208 /* Disable SW_WD_TRG */
210 static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr)
212 /* Disable SW_WD_TRG */
213 outb_p(inb(swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
214 #ifdef DEBUG
215 printk(KERN_INFO DPFX "Disable SW_WD_TRG\n");
216 #endif
219 /* -- Higher level functions ------------------------------------*/
221 /* Enable the watchdog */
223 static void pc87413_enable(void)
225 unsigned int swc_base_addr;
227 spin_lock(&io_lock);
229 pc87413_select_wdt_out();
230 pc87413_enable_swc();
231 swc_base_addr = pc87413_get_swc_base();
232 pc87413_swc_bank3(swc_base_addr);
233 pc87413_programm_wdto(swc_base_addr, timeout);
234 pc87413_enable_wden(swc_base_addr);
235 pc87413_enable_sw_wd_tren(swc_base_addr);
236 pc87413_enable_sw_wd_trg(swc_base_addr);
238 spin_unlock(&io_lock);
241 /* Disable the watchdog */
243 static void pc87413_disable(void)
245 unsigned int swc_base_addr;
247 spin_lock(&io_lock);
249 pc87413_select_wdt_out();
250 pc87413_enable_swc();
251 swc_base_addr = pc87413_get_swc_base();
252 pc87413_swc_bank3(swc_base_addr);
253 pc87413_disable_sw_wd_tren(swc_base_addr);
254 pc87413_disable_sw_wd_trg(swc_base_addr);
255 pc87413_programm_wdto(swc_base_addr, 0);
257 spin_unlock(&io_lock);
260 /* Refresh the watchdog */
262 static void pc87413_refresh(void)
264 unsigned int swc_base_addr;
266 spin_lock(&io_lock);
268 pc87413_select_wdt_out();
269 pc87413_enable_swc();
270 swc_base_addr = pc87413_get_swc_base();
271 pc87413_swc_bank3(swc_base_addr);
272 pc87413_disable_sw_wd_tren(swc_base_addr);
273 pc87413_disable_sw_wd_trg(swc_base_addr);
274 pc87413_programm_wdto(swc_base_addr, timeout);
275 pc87413_enable_wden(swc_base_addr);
276 pc87413_enable_sw_wd_tren(swc_base_addr);
277 pc87413_enable_sw_wd_trg(swc_base_addr);
279 spin_unlock(&io_lock);
282 /* -- File operations -------------------------------------------*/
285 * pc87413_open:
286 * @inode: inode of device
287 * @file: file handle to device
291 static int pc87413_open(struct inode *inode, struct file *file)
293 /* /dev/watchdog can only be opened once */
295 if (test_and_set_bit(0, &timer_enabled))
296 return -EBUSY;
298 if (nowayout)
299 __module_get(THIS_MODULE);
301 /* Reload and activate timer */
302 pc87413_refresh();
304 printk(KERN_INFO MODNAME
305 "Watchdog enabled. Timeout set to %d minute(s).\n", timeout);
307 return nonseekable_open(inode, file);
311 * pc87413_release:
312 * @inode: inode to board
313 * @file: file handle to board
315 * The watchdog has a configurable API. There is a religious dispute
316 * between people who want their watchdog to be able to shut down and
317 * those who want to be sure if the watchdog manager dies the machine
318 * reboots. In the former case we disable the counters, in the latter
319 * case you have to open it again very soon.
322 static int pc87413_release(struct inode *inode, struct file *file)
324 /* Shut off the timer. */
326 if (expect_close == 42) {
327 pc87413_disable();
328 printk(KERN_INFO MODNAME
329 "Watchdog disabled, sleeping again...\n");
330 } else {
331 printk(KERN_CRIT MODNAME
332 "Unexpected close, not stopping watchdog!\n");
333 pc87413_refresh();
335 clear_bit(0, &timer_enabled);
336 expect_close = 0;
337 return 0;
341 * pc87413_status:
343 * return, if the watchdog is enabled (timeout is set...)
347 static int pc87413_status(void)
349 return 0; /* currently not supported */
353 * pc87413_write:
354 * @file: file handle to the watchdog
355 * @data: data buffer to write
356 * @len: length in bytes
357 * @ppos: pointer to the position to write. No seeks allowed
359 * A write to a watchdog device is defined as a keepalive signal. Any
360 * write of data will do, as we we don't define content meaning.
363 static ssize_t pc87413_write(struct file *file, const char __user *data,
364 size_t len, loff_t *ppos)
366 /* See if we got the magic character 'V' and reload the timer */
367 if (len) {
368 if (!nowayout) {
369 size_t i;
371 /* reset expect flag */
372 expect_close = 0;
374 /* scan to see whether or not we got the
375 magic character */
376 for (i = 0; i != len; i++) {
377 char c;
378 if (get_user(c, data + i))
379 return -EFAULT;
380 if (c == 'V')
381 expect_close = 42;
385 /* someone wrote to us, we should reload the timer */
386 pc87413_refresh();
388 return len;
392 * pc87413_ioctl:
393 * @file: file handle to the device
394 * @cmd: watchdog command
395 * @arg: argument pointer
397 * The watchdog API defines a common set of functions for all watchdogs
398 * according to their available features. We only actually usefully support
399 * querying capabilities and current status.
402 static long pc87413_ioctl(struct file *file, unsigned int cmd,
403 unsigned long arg)
405 int new_timeout;
407 union {
408 struct watchdog_info __user *ident;
409 int __user *i;
410 } uarg;
412 static const struct watchdog_info ident = {
413 .options = WDIOF_KEEPALIVEPING |
414 WDIOF_SETTIMEOUT |
415 WDIOF_MAGICCLOSE,
416 .firmware_version = 1,
417 .identity = "PC87413(HF/F) watchdog",
420 uarg.i = (int __user *)arg;
422 switch (cmd) {
423 case WDIOC_GETSUPPORT:
424 return copy_to_user(uarg.ident, &ident,
425 sizeof(ident)) ? -EFAULT : 0;
426 case WDIOC_GETSTATUS:
427 return put_user(pc87413_status(), uarg.i);
428 case WDIOC_GETBOOTSTATUS:
429 return put_user(0, uarg.i);
430 case WDIOC_SETOPTIONS:
432 int options, retval = -EINVAL;
433 if (get_user(options, uarg.i))
434 return -EFAULT;
435 if (options & WDIOS_DISABLECARD) {
436 pc87413_disable();
437 retval = 0;
439 if (options & WDIOS_ENABLECARD) {
440 pc87413_enable();
441 retval = 0;
443 return retval;
445 case WDIOC_KEEPALIVE:
446 pc87413_refresh();
447 #ifdef DEBUG
448 printk(KERN_INFO DPFX "keepalive\n");
449 #endif
450 return 0;
451 case WDIOC_SETTIMEOUT:
452 if (get_user(new_timeout, uarg.i))
453 return -EFAULT;
454 /* the API states this is given in secs */
455 new_timeout /= 60;
456 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
457 return -EINVAL;
458 timeout = new_timeout;
459 pc87413_refresh();
460 /* fall through and return the new timeout... */
461 case WDIOC_GETTIMEOUT:
462 new_timeout = timeout * 60;
463 return put_user(new_timeout, uarg.i);
464 default:
465 return -ENOTTY;
469 /* -- Notifier funtions -----------------------------------------*/
472 * notify_sys:
473 * @this: our notifier block
474 * @code: the event being reported
475 * @unused: unused
477 * Our notifier is called on system shutdowns. We want to turn the card
478 * off at reboot otherwise the machine will reboot again during memory
479 * test or worse yet during the following fsck. This would suck, in fact
480 * trust me - if it happens it does suck.
483 static int pc87413_notify_sys(struct notifier_block *this,
484 unsigned long code,
485 void *unused)
487 if (code == SYS_DOWN || code == SYS_HALT)
488 /* Turn the card off */
489 pc87413_disable();
490 return NOTIFY_DONE;
493 /* -- Module's structures ---------------------------------------*/
495 static const struct file_operations pc87413_fops = {
496 .owner = THIS_MODULE,
497 .llseek = no_llseek,
498 .write = pc87413_write,
499 .unlocked_ioctl = pc87413_ioctl,
500 .open = pc87413_open,
501 .release = pc87413_release,
504 static struct notifier_block pc87413_notifier = {
505 .notifier_call = pc87413_notify_sys,
508 static struct miscdevice pc87413_miscdev = {
509 .minor = WATCHDOG_MINOR,
510 .name = "watchdog",
511 .fops = &pc87413_fops,
514 /* -- Module init functions -------------------------------------*/
517 * pc87413_init: module's "constructor"
519 * Set up the WDT watchdog board. All we have to do is grab the
520 * resources we require and bitch if anyone beat us to them.
521 * The open() function will actually kick the board off.
524 static int __init pc87413_init(void)
526 int ret;
528 printk(KERN_INFO PFX "Version " VERSION " at io 0x%X\n",
529 WDT_INDEX_IO_PORT);
531 /* request_region(io, 2, "pc87413"); */
533 ret = register_reboot_notifier(&pc87413_notifier);
534 if (ret != 0) {
535 printk(KERN_ERR PFX
536 "cannot register reboot notifier (err=%d)\n", ret);
539 ret = misc_register(&pc87413_miscdev);
540 if (ret != 0) {
541 printk(KERN_ERR PFX
542 "cannot register miscdev on minor=%d (err=%d)\n",
543 WATCHDOG_MINOR, ret);
544 unregister_reboot_notifier(&pc87413_notifier);
545 return ret;
547 printk(KERN_INFO PFX "initialized. timeout=%d min \n", timeout);
548 pc87413_enable();
549 return 0;
553 * pc87413_exit: module's "destructor"
555 * Unload the watchdog. You cannot do this with any file handles open.
556 * If your watchdog is set to continue ticking on close and you unload
557 * it, well it keeps ticking. We won't get the interrupt but the board
558 * will not touch PC memory so all is fine. You just have to load a new
559 * module in 60 seconds or reboot.
562 static void __exit pc87413_exit(void)
564 /* Stop the timer before we leave */
565 if (!nowayout) {
566 pc87413_disable();
567 printk(KERN_INFO MODNAME "Watchdog disabled.\n");
570 misc_deregister(&pc87413_miscdev);
571 unregister_reboot_notifier(&pc87413_notifier);
572 /* release_region(io, 2); */
574 printk(KERN_INFO MODNAME " watchdog component driver removed.\n");
577 module_init(pc87413_init);
578 module_exit(pc87413_exit);
580 MODULE_AUTHOR("Sven Anders <anders@anduras.de>, "
581 "Marcus Junker <junker@anduras.de>,");
582 MODULE_DESCRIPTION("PC87413 WDT driver");
583 MODULE_LICENSE("GPL");
585 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
587 module_param(io, int, 0);
588 MODULE_PARM_DESC(io, MODNAME " I/O port (default: "
589 __MODULE_STRING(IO_DEFAULT) ").");
591 module_param(timeout, int, 0);
592 MODULE_PARM_DESC(timeout,
593 "Watchdog timeout in minutes (default="
594 __MODULE_STRING(DEFAULT_TIMEOUT) ").");
596 module_param(nowayout, int, 0);
597 MODULE_PARM_DESC(nowayout,
598 "Watchdog cannot be stopped once started (default="
599 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");