V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / watchdog / pc87413_wdt.c
blob15e4f8887a9ee36b8c75afd37e0bb680f07f897e
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/version.h>
35 #include <asm/io.h>
36 #include <asm/uaccess.h>
37 #include <asm/system.h>
39 /* #define DEBUG 1 */
41 #define DEFAULT_TIMEOUT 1 /* 1 minute */
42 #define MAX_TIMEOUT 255
44 #define VERSION "1.1"
45 #define MODNAME "pc87413 WDT"
46 #define PFX MODNAME ": "
47 #define DPFX MODNAME " - DEBUG: "
49 #define WDT_INDEX_IO_PORT (io+0) /* I/O port base (index register) */
50 #define WDT_DATA_IO_PORT (WDT_INDEX_IO_PORT+1)
51 #define SWC_LDN 0x04
52 #define SIOCFG2 0x22 /* Serial IO register */
53 #define WDCTL 0x10 /* Watchdog-Timer-Controll-Register */
54 #define WDTO 0x11 /* Watchdog timeout register */
55 #define WDCFG 0x12 /* Watchdog config register */
57 static int io = 0x2E; /* Address used on Portwell Boards */
59 static int timeout = DEFAULT_TIMEOUT; /* timeout value */
60 static unsigned long timer_enabled = 0; /* is the timer enabled? */
62 static char expect_close; /* is the close expected? */
64 static DEFINE_SPINLOCK(io_lock);/* to guard the watchdog from io races */
66 static int nowayout = WATCHDOG_NOWAYOUT;
68 /* -- Low level function ----------------------------------------*/
70 /* Select pins for Watchdog output */
72 static inline void pc87413_select_wdt_out (void)
74 unsigned int cr_data = 0;
76 /* Step 1: Select multiple pin,pin55,as WDT output */
78 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
80 cr_data = inb (WDT_DATA_IO_PORT);
82 cr_data |= 0x80; /* Set Bit7 to 1*/
83 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
85 outb_p(cr_data, WDT_DATA_IO_PORT);
87 #ifdef DEBUG
88 printk(KERN_INFO DPFX "Select multiple pin,pin55,as WDT output:"
89 " Bit7 to 1: %d\n", cr_data);
90 #endif
93 /* Enable SWC functions */
95 static inline void pc87413_enable_swc(void)
97 unsigned int cr_data=0;
99 /* Step 2: Enable SWC functions */
101 outb_p(0x07, WDT_INDEX_IO_PORT); /* Point SWC_LDN (LDN=4) */
102 outb_p(SWC_LDN, WDT_DATA_IO_PORT);
104 outb_p(0x30, WDT_INDEX_IO_PORT); /* Read Index 0x30 First */
105 cr_data = inb(WDT_DATA_IO_PORT);
106 cr_data |= 0x01; /* Set Bit0 to 1 */
107 outb_p(0x30, WDT_INDEX_IO_PORT);
108 outb_p(cr_data, WDT_DATA_IO_PORT); /* Index0x30_bit0P1 */
110 #ifdef DEBUG
111 printk(KERN_INFO DPFX "pc87413 - Enable SWC functions\n");
112 #endif
115 /* Read SWC I/O base address */
117 static inline unsigned int pc87413_get_swc_base(void)
119 unsigned int swc_base_addr = 0;
120 unsigned char addr_l, addr_h = 0;
122 /* Step 3: Read SWC I/O Base Address */
124 outb_p(0x60, WDT_INDEX_IO_PORT); /* Read Index 0x60 */
125 addr_h = inb(WDT_DATA_IO_PORT);
127 outb_p(0x61, WDT_INDEX_IO_PORT); /* Read Index 0x61 */
129 addr_l = inb(WDT_DATA_IO_PORT);
131 swc_base_addr = (addr_h << 8) + addr_l;
133 #ifdef DEBUG
134 printk(KERN_INFO DPFX "Read SWC I/O Base Address: low %d, high %d,"
135 " res %d\n", addr_l, addr_h, swc_base_addr);
136 #endif
138 return swc_base_addr;
141 /* Select Bank 3 of SWC */
143 static inline void pc87413_swc_bank3(unsigned int swc_base_addr)
145 /* Step 4: Select Bank3 of SWC */
147 outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
149 #ifdef DEBUG
150 printk(KERN_INFO DPFX "Select Bank3 of SWC\n");
151 #endif
154 /* Set watchdog timeout to x minutes */
156 static inline void pc87413_programm_wdto(unsigned int swc_base_addr,
157 char pc87413_time)
159 /* Step 5: Programm WDTO, Twd. */
161 outb_p(pc87413_time, swc_base_addr + WDTO);
163 #ifdef DEBUG
164 printk(KERN_INFO DPFX "Set WDTO to %d minutes\n", pc87413_time);
165 #endif
168 /* Enable WDEN */
170 static inline void pc87413_enable_wden(unsigned int swc_base_addr)
172 /* Step 6: Enable WDEN */
174 outb_p(inb (swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
176 #ifdef DEBUG
177 printk(KERN_INFO DPFX "Enable WDEN\n");
178 #endif
181 /* Enable SW_WD_TREN */
182 static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr)
184 /* Enable SW_WD_TREN */
186 outb_p(inb (swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
188 #ifdef DEBUG
189 printk(KERN_INFO DPFX "Enable SW_WD_TREN\n");
190 #endif
193 /* Disable SW_WD_TREN */
195 static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr)
197 /* Disable SW_WD_TREN */
199 outb_p(inb (swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
201 #ifdef DEBUG
202 printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN\n");
203 #endif
206 /* Enable SW_WD_TRG */
208 static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr)
210 /* Enable SW_WD_TRG */
212 outb_p(inb (swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
214 #ifdef DEBUG
215 printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG\n");
216 #endif
219 /* Disable SW_WD_TRG */
221 static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr)
223 /* Disable SW_WD_TRG */
225 outb_p(inb (swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
227 #ifdef DEBUG
228 printk(KERN_INFO DPFX "Disable SW_WD_TRG\n");
229 #endif
232 /* -- Higher level functions ------------------------------------*/
234 /* Enable the watchdog */
236 static void pc87413_enable(void)
238 unsigned int swc_base_addr;
240 spin_lock(&io_lock);
242 pc87413_select_wdt_out();
243 pc87413_enable_swc();
244 swc_base_addr = pc87413_get_swc_base();
245 pc87413_swc_bank3(swc_base_addr);
246 pc87413_programm_wdto(swc_base_addr, timeout);
247 pc87413_enable_wden(swc_base_addr);
248 pc87413_enable_sw_wd_tren(swc_base_addr);
249 pc87413_enable_sw_wd_trg(swc_base_addr);
251 spin_unlock(&io_lock);
254 /* Disable the watchdog */
256 static void pc87413_disable(void)
258 unsigned int swc_base_addr;
260 spin_lock(&io_lock);
262 pc87413_select_wdt_out();
263 pc87413_enable_swc();
264 swc_base_addr = pc87413_get_swc_base();
265 pc87413_swc_bank3(swc_base_addr);
266 pc87413_disable_sw_wd_tren(swc_base_addr);
267 pc87413_disable_sw_wd_trg(swc_base_addr);
268 pc87413_programm_wdto(swc_base_addr, 0);
270 spin_unlock(&io_lock);
273 /* Refresh the watchdog */
275 static void pc87413_refresh(void)
277 unsigned int swc_base_addr;
279 spin_lock(&io_lock);
281 pc87413_select_wdt_out();
282 pc87413_enable_swc();
283 swc_base_addr = pc87413_get_swc_base();
284 pc87413_swc_bank3(swc_base_addr);
285 pc87413_disable_sw_wd_tren(swc_base_addr);
286 pc87413_disable_sw_wd_trg(swc_base_addr);
287 pc87413_programm_wdto(swc_base_addr, timeout);
288 pc87413_enable_wden(swc_base_addr);
289 pc87413_enable_sw_wd_tren(swc_base_addr);
290 pc87413_enable_sw_wd_trg(swc_base_addr);
292 spin_unlock(&io_lock);
295 /* -- File operations -------------------------------------------*/
298 * pc87413_open:
299 * @inode: inode of device
300 * @file: file handle to device
304 static int pc87413_open(struct inode *inode, struct file *file)
306 /* /dev/watchdog can only be opened once */
308 if (test_and_set_bit(0, &timer_enabled))
309 return -EBUSY;
311 if (nowayout)
312 __module_get(THIS_MODULE);
314 /* Reload and activate timer */
315 pc87413_refresh();
317 printk(KERN_INFO MODNAME "Watchdog enabled. Timeout set to"
318 " %d minute(s).\n", timeout);
320 return nonseekable_open(inode, file);
324 * pc87413_release:
325 * @inode: inode to board
326 * @file: file handle to board
328 * The watchdog has a configurable API. There is a religious dispute
329 * between people who want their watchdog to be able to shut down and
330 * those who want to be sure if the watchdog manager dies the machine
331 * reboots. In the former case we disable the counters, in the latter
332 * case you have to open it again very soon.
335 static int pc87413_release(struct inode *inode, struct file *file)
337 /* Shut off the timer. */
339 if (expect_close == 42) {
340 pc87413_disable();
341 printk(KERN_INFO MODNAME "Watchdog disabled,"
342 " sleeping again...\n");
343 } else {
344 printk(KERN_CRIT MODNAME "Unexpected close, not stopping"
345 " watchdog!\n");
346 pc87413_refresh();
349 clear_bit(0, &timer_enabled);
350 expect_close = 0;
352 return 0;
356 * pc87413_status:
358 * return, if the watchdog is enabled (timeout is set...)
362 static int pc87413_status(void)
364 return 0; /* currently not supported */
368 * pc87413_write:
369 * @file: file handle to the watchdog
370 * @data: data buffer to write
371 * @len: length in bytes
372 * @ppos: pointer to the position to write. No seeks allowed
374 * A write to a watchdog device is defined as a keepalive signal. Any
375 * write of data will do, as we we don't define content meaning.
378 static ssize_t pc87413_write(struct file *file, const char __user *data,
379 size_t len, loff_t *ppos)
381 /* See if we got the magic character 'V' and reload the timer */
382 if (len) {
383 if (!nowayout) {
384 size_t i;
386 /* reset expect flag */
387 expect_close = 0;
389 /* scan to see whether or not we got the magic character */
390 for (i = 0; i != len; i++) {
391 char c;
392 if (get_user(c, data+i))
393 return -EFAULT;
394 if (c == 'V')
395 expect_close = 42;
399 /* someone wrote to us, we should reload the timer */
400 pc87413_refresh();
402 return len;
406 * pc87413_ioctl:
407 * @inode: inode of the device
408 * @file: file handle to the device
409 * @cmd: watchdog command
410 * @arg: argument pointer
412 * The watchdog API defines a common set of functions for all watchdogs
413 * according to their available features. We only actually usefully support
414 * querying capabilities and current status.
417 static int pc87413_ioctl(struct inode *inode, struct file *file,
418 unsigned int cmd, unsigned long arg)
420 int new_timeout;
422 union {
423 struct watchdog_info __user *ident;
424 int __user *i;
425 } uarg;
427 static struct watchdog_info ident = {
428 .options = WDIOF_KEEPALIVEPING |
429 WDIOF_SETTIMEOUT |
430 WDIOF_MAGICCLOSE,
431 .firmware_version = 1,
432 .identity = "PC87413(HF/F) watchdog"
435 uarg.i = (int __user *)arg;
437 switch(cmd) {
438 default:
439 return -ENOTTY;
441 case WDIOC_GETSUPPORT:
442 return copy_to_user(uarg.ident, &ident,
443 sizeof(ident)) ? -EFAULT : 0;
445 case WDIOC_GETSTATUS:
446 return put_user(pc87413_status(), uarg.i);
448 case WDIOC_GETBOOTSTATUS:
449 return put_user(0, uarg.i);
451 case WDIOC_KEEPALIVE:
452 pc87413_refresh();
453 #ifdef DEBUG
454 printk(KERN_INFO DPFX "keepalive\n");
455 #endif
456 return 0;
458 case WDIOC_SETTIMEOUT:
459 if (get_user(new_timeout, uarg.i))
460 return -EFAULT;
462 // the API states this is given in secs
463 new_timeout /= 60;
465 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
466 return -EINVAL;
468 timeout = new_timeout;
469 pc87413_refresh();
471 // fall through and return the new timeout...
473 case WDIOC_GETTIMEOUT:
475 new_timeout = timeout * 60;
477 return put_user(new_timeout, uarg.i);
479 case WDIOC_SETOPTIONS:
481 int options, retval = -EINVAL;
483 if (get_user(options, uarg.i))
484 return -EFAULT;
486 if (options & WDIOS_DISABLECARD) {
487 pc87413_disable();
488 retval = 0;
491 if (options & WDIOS_ENABLECARD) {
492 pc87413_enable();
493 retval = 0;
496 return retval;
501 /* -- Notifier funtions -----------------------------------------*/
504 * notify_sys:
505 * @this: our notifier block
506 * @code: the event being reported
507 * @unused: unused
509 * Our notifier is called on system shutdowns. We want to turn the card
510 * off at reboot otherwise the machine will reboot again during memory
511 * test or worse yet during the following fsck. This would suck, in fact
512 * trust me - if it happens it does suck.
515 static int pc87413_notify_sys(struct notifier_block *this,
516 unsigned long code,
517 void *unused)
519 if (code == SYS_DOWN || code == SYS_HALT)
521 /* Turn the card off */
522 pc87413_disable();
524 return NOTIFY_DONE;
527 /* -- Module's structures ---------------------------------------*/
529 static const struct file_operations pc87413_fops = {
530 .owner = THIS_MODULE,
531 .llseek = no_llseek,
532 .write = pc87413_write,
533 .ioctl = pc87413_ioctl,
534 .open = pc87413_open,
535 .release = pc87413_release,
538 static struct notifier_block pc87413_notifier =
540 .notifier_call = pc87413_notify_sys,
543 static struct miscdevice pc87413_miscdev=
545 .minor = WATCHDOG_MINOR,
546 .name = "watchdog",
547 .fops = &pc87413_fops
550 /* -- Module init functions -------------------------------------*/
553 * pc87413_init: module's "constructor"
555 * Set up the WDT watchdog board. All we have to do is grab the
556 * resources we require and bitch if anyone beat us to them.
557 * The open() function will actually kick the board off.
560 static int __init pc87413_init(void)
562 int ret;
564 printk(KERN_INFO PFX "Version " VERSION " at io 0x%X\n", WDT_INDEX_IO_PORT);
566 /* request_region(io, 2, "pc87413"); */
568 ret = register_reboot_notifier(&pc87413_notifier);
569 if (ret != 0) {
570 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
571 ret);
574 ret = misc_register(&pc87413_miscdev);
576 if (ret != 0) {
577 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
578 WATCHDOG_MINOR, ret);
579 unregister_reboot_notifier(&pc87413_notifier);
580 return ret;
583 printk(KERN_INFO PFX "initialized. timeout=%d min \n", timeout);
585 pc87413_enable();
587 return 0;
591 * pc87413_exit: module's "destructor"
593 * Unload the watchdog. You cannot do this with any file handles open.
594 * If your watchdog is set to continue ticking on close and you unload
595 * it, well it keeps ticking. We won't get the interrupt but the board
596 * will not touch PC memory so all is fine. You just have to load a new
597 * module in 60 seconds or reboot.
600 static void __exit pc87413_exit(void)
602 /* Stop the timer before we leave */
603 if (!nowayout)
605 pc87413_disable();
606 printk(KERN_INFO MODNAME "Watchdog disabled.\n");
609 misc_deregister(&pc87413_miscdev);
610 unregister_reboot_notifier(&pc87413_notifier);
611 /* release_region(io,2); */
613 printk(MODNAME " watchdog component driver removed.\n");
616 module_init(pc87413_init);
617 module_exit(pc87413_exit);
619 MODULE_AUTHOR("Sven Anders <anders@anduras.de>, Marcus Junker <junker@anduras.de>,");
620 MODULE_DESCRIPTION("PC87413 WDT driver");
621 MODULE_LICENSE("GPL");
623 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
625 module_param(io, int, 0);
626 MODULE_PARM_DESC(io, MODNAME " I/O port (default: " __MODULE_STRING(io) ").");
628 module_param(timeout, int, 0);
629 MODULE_PARM_DESC(timeout, "Watchdog timeout in minutes (default=" __MODULE_STRING(timeout) ").");
631 module_param(nowayout, int, 0);
632 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");