[PATCH] kernel/hrtimer.c sparse warning fix
[linux-2.6/linux-loongson.git] / drivers / char / watchdog / i8xx_tco.c
bloba13395e2c372a9b27355040c9c94eae21040992b
1 /*
2 * i8xx_tco: TCO timer driver for i8xx chipsets
4 * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights Reserved.
5 * http://www.kernelconcepts.de
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Neither kernel concepts nor Nils Faerber admit liability nor provide
13 * warranty for any of this software. This material is provided
14 * "AS-IS" and at no charge.
16 * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>
17 * developed for
18 * Jentro AG, Haar/Munich (Germany)
20 * TCO timer driver for i8xx chipsets
21 * based on softdog.c by Alan Cox <alan@redhat.com>
23 * The TCO timer is implemented in the following I/O controller hubs:
24 * (See the intel documentation on http://developer.intel.com.)
25 * 82801AA (ICH) : document number 290655-003, 290677-014,
26 * 82801AB (ICHO) : document number 290655-003, 290677-014,
27 * 82801BA (ICH2) : document number 290687-002, 298242-027,
28 * 82801BAM (ICH2-M) : document number 290687-002, 298242-027,
29 * 82801CA (ICH3-S) : document number 290733-003, 290739-013,
30 * 82801CAM (ICH3-M) : document number 290716-001, 290718-007,
31 * 82801DB (ICH4) : document number 290744-001, 290745-020,
32 * 82801DBM (ICH4-M) : document number 252337-001, 252663-005,
33 * 82801E (C-ICH) : document number 273599-001, 273645-002,
34 * 82801EB (ICH5) : document number 252516-001, 252517-003,
35 * 82801ER (ICH5R) : document number 252516-001, 252517-003,
36 * 82801FB (ICH6) : document number 301473-002, 301474-007,
37 * 82801FR (ICH6R) : document number 301473-002, 301474-007,
38 * 82801FBM (ICH6-M) : document number 301473-002, 301474-007,
39 * 82801FW (ICH6W) : document number 301473-001, 301474-007,
40 * 82801FRW (ICH6RW) : document number 301473-001, 301474-007
42 * 20000710 Nils Faerber
43 * Initial Version 0.01
44 * 20000728 Nils Faerber
45 * 0.02 Fix for SMI_EN->TCO_EN bit, some cleanups
46 * 20011214 Matt Domsch <Matt_Domsch@dell.com>
47 * 0.03 Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
48 * Didn't add timeout option as i810_margin already exists.
49 * 20020224 Joel Becker, Wim Van Sebroeck
50 * 0.04 Support for 82801CA(M) chipset, timer margin needs to be > 3,
51 * add support for WDIOC_SETTIMEOUT and WDIOC_GETTIMEOUT.
52 * 20020412 Rob Radez <rob@osinvestor.com>, Wim Van Sebroeck
53 * 0.05 Fix possible timer_alive race, add expect close support,
54 * clean up ioctls (WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS and
55 * WDIOC_SETOPTIONS), made i810tco_getdevice __init,
56 * removed boot_status, removed tco_timer_read,
57 * added support for 82801DB and 82801E chipset,
58 * added support for 82801EB and 8280ER chipset,
59 * general cleanup.
60 * 20030921 Wim Van Sebroeck <wim@iguana.be>
61 * 0.06 change i810_margin to heartbeat, use module_param,
62 * added notify system support, renamed module to i8xx_tco.
63 * 20050128 Wim Van Sebroeck <wim@iguana.be>
64 * 0.07 Added support for the ICH4-M, ICH6, ICH6R, ICH6-M, ICH6W and ICH6RW
65 * chipsets. Also added support for the "undocumented" ICH7 chipset.
66 * 20050807 Wim Van Sebroeck <wim@iguana.be>
67 * 0.08 Make sure that the watchdog is only "armed" when started.
68 * (Kernel Bug 4251)
72 * Includes, defines, variables, module parameters, ...
75 #include <linux/module.h>
76 #include <linux/moduleparam.h>
77 #include <linux/types.h>
78 #include <linux/miscdevice.h>
79 #include <linux/watchdog.h>
80 #include <linux/notifier.h>
81 #include <linux/reboot.h>
82 #include <linux/init.h>
83 #include <linux/fs.h>
84 #include <linux/pci.h>
85 #include <linux/ioport.h>
87 #include <asm/uaccess.h>
88 #include <asm/io.h>
90 #include "i8xx_tco.h"
92 /* Module and version information */
93 #define TCO_VERSION "0.08"
94 #define TCO_MODULE_NAME "i8xx TCO timer"
95 #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION
96 #define PFX TCO_MODULE_NAME ": "
98 /* internal variables */
99 static unsigned int ACPIBASE;
100 static spinlock_t tco_lock; /* Guards the hardware */
101 static unsigned long timer_alive;
102 static char tco_expect_close;
103 static struct pci_dev *i8xx_tco_pci;
105 /* module parameters */
106 #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat (2<heartbeat<39) */
107 static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
108 module_param(heartbeat, int, 0);
109 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<heartbeat<39, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
111 static int nowayout = WATCHDOG_NOWAYOUT;
112 module_param(nowayout, int, 0);
113 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
116 * Some TCO specific functions
119 static inline unsigned char seconds_to_ticks(int seconds)
121 /* the internal timer is stored as ticks which decrement
122 * every 0.6 seconds */
123 return (seconds * 10) / 6;
126 static int tco_timer_start (void)
128 unsigned char val;
130 spin_lock(&tco_lock);
132 /* disable chipset's NO_REBOOT bit */
133 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val);
134 val &= 0xfd;
135 pci_write_config_byte (i8xx_tco_pci, 0xd4, val);
137 /* Bit 11: TCO Timer Halt -> 0 = The TCO timer is enabled to count */
138 val = inb (TCO1_CNT + 1);
139 val &= 0xf7;
140 outb (val, TCO1_CNT + 1);
141 val = inb (TCO1_CNT + 1);
143 spin_unlock(&tco_lock);
145 if (val & 0x08)
146 return -1;
147 return 0;
150 static int tco_timer_stop (void)
152 unsigned char val, val1;
154 spin_lock(&tco_lock);
155 /* Bit 11: TCO Timer Halt -> 1 = The TCO timer is disabled */
156 val = inb (TCO1_CNT + 1);
157 val |= 0x08;
158 outb (val, TCO1_CNT + 1);
159 val = inb (TCO1_CNT + 1);
161 /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
162 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
163 val1 |= 0x02;
164 pci_write_config_byte (i8xx_tco_pci, 0xd4, val1);
166 spin_unlock(&tco_lock);
168 if ((val & 0x08) == 0)
169 return -1;
170 return 0;
173 static int tco_timer_keepalive (void)
175 spin_lock(&tco_lock);
176 /* Reload the timer by writing to the TCO Timer Reload register */
177 outb (0x01, TCO1_RLD);
178 spin_unlock(&tco_lock);
179 return 0;
182 static int tco_timer_set_heartbeat (int t)
184 unsigned char val;
185 unsigned char tmrval;
187 tmrval = seconds_to_ticks(t);
188 /* from the specs: */
189 /* "Values of 0h-3h are ignored and should not be attempted" */
190 if (tmrval > 0x3f || tmrval < 0x04)
191 return -EINVAL;
193 /* Write new heartbeat to watchdog */
194 spin_lock(&tco_lock);
195 val = inb (TCO1_TMR);
196 val &= 0xc0;
197 val |= tmrval;
198 outb (val, TCO1_TMR);
199 val = inb (TCO1_TMR);
200 spin_unlock(&tco_lock);
202 if ((val & 0x3f) != tmrval)
203 return -EINVAL;
205 heartbeat = t;
206 return 0;
210 * /dev/watchdog handling
213 static int i8xx_tco_open (struct inode *inode, struct file *file)
215 /* /dev/watchdog can only be opened once */
216 if (test_and_set_bit(0, &timer_alive))
217 return -EBUSY;
220 * Reload and activate timer
222 tco_timer_keepalive ();
223 tco_timer_start ();
224 return nonseekable_open(inode, file);
227 static int i8xx_tco_release (struct inode *inode, struct file *file)
230 * Shut off the timer.
232 if (tco_expect_close == 42) {
233 tco_timer_stop ();
234 } else {
235 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
236 tco_timer_keepalive ();
238 clear_bit(0, &timer_alive);
239 tco_expect_close = 0;
240 return 0;
243 static ssize_t i8xx_tco_write (struct file *file, const char __user *data,
244 size_t len, loff_t * ppos)
246 /* See if we got the magic character 'V' and reload the timer */
247 if (len) {
248 if (!nowayout) {
249 size_t i;
251 /* note: just in case someone wrote the magic character
252 * five months ago... */
253 tco_expect_close = 0;
255 /* scan to see whether or not we got the magic character */
256 for (i = 0; i != len; i++) {
257 char c;
258 if(get_user(c, data+i))
259 return -EFAULT;
260 if (c == 'V')
261 tco_expect_close = 42;
265 /* someone wrote to us, we should reload the timer */
266 tco_timer_keepalive ();
268 return len;
271 static int i8xx_tco_ioctl (struct inode *inode, struct file *file,
272 unsigned int cmd, unsigned long arg)
274 int new_options, retval = -EINVAL;
275 int new_heartbeat;
276 void __user *argp = (void __user *)arg;
277 int __user *p = argp;
278 static struct watchdog_info ident = {
279 .options = WDIOF_SETTIMEOUT |
280 WDIOF_KEEPALIVEPING |
281 WDIOF_MAGICCLOSE,
282 .firmware_version = 0,
283 .identity = TCO_MODULE_NAME,
286 switch (cmd) {
287 case WDIOC_GETSUPPORT:
288 return copy_to_user(argp, &ident,
289 sizeof (ident)) ? -EFAULT : 0;
291 case WDIOC_GETSTATUS:
292 case WDIOC_GETBOOTSTATUS:
293 return put_user (0, p);
295 case WDIOC_KEEPALIVE:
296 tco_timer_keepalive ();
297 return 0;
299 case WDIOC_SETOPTIONS:
301 if (get_user (new_options, p))
302 return -EFAULT;
304 if (new_options & WDIOS_DISABLECARD) {
305 tco_timer_stop ();
306 retval = 0;
309 if (new_options & WDIOS_ENABLECARD) {
310 tco_timer_keepalive ();
311 tco_timer_start ();
312 retval = 0;
315 return retval;
318 case WDIOC_SETTIMEOUT:
320 if (get_user(new_heartbeat, p))
321 return -EFAULT;
323 if (tco_timer_set_heartbeat(new_heartbeat))
324 return -EINVAL;
326 tco_timer_keepalive ();
327 /* Fall */
330 case WDIOC_GETTIMEOUT:
331 return put_user(heartbeat, p);
333 default:
334 return -ENOIOCTLCMD;
339 * Notify system
342 static int i8xx_tco_notify_sys (struct notifier_block *this, unsigned long code, void *unused)
344 if (code==SYS_DOWN || code==SYS_HALT) {
345 /* Turn the WDT off */
346 tco_timer_stop ();
349 return NOTIFY_DONE;
353 * Kernel Interfaces
356 static struct file_operations i8xx_tco_fops = {
357 .owner = THIS_MODULE,
358 .llseek = no_llseek,
359 .write = i8xx_tco_write,
360 .ioctl = i8xx_tco_ioctl,
361 .open = i8xx_tco_open,
362 .release = i8xx_tco_release,
365 static struct miscdevice i8xx_tco_miscdev = {
366 .minor = WATCHDOG_MINOR,
367 .name = "watchdog",
368 .fops = &i8xx_tco_fops,
371 static struct notifier_block i8xx_tco_notifier = {
372 .notifier_call = i8xx_tco_notify_sys,
376 * Data for PCI driver interface
378 * This data only exists for exporting the supported
379 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
380 * register a pci_driver, because someone else might one day
381 * want to register another driver on the same PCI id.
383 static struct pci_device_id i8xx_tco_pci_tbl[] = {
384 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, PCI_ANY_ID, PCI_ANY_ID, },
385 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_0, PCI_ANY_ID, PCI_ANY_ID, },
386 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, PCI_ANY_ID, PCI_ANY_ID, },
387 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_10, PCI_ANY_ID, PCI_ANY_ID, },
388 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, PCI_ANY_ID, PCI_ANY_ID, },
389 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, PCI_ANY_ID, PCI_ANY_ID, },
390 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, PCI_ANY_ID, PCI_ANY_ID, },
391 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12, PCI_ANY_ID, PCI_ANY_ID, },
392 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801E_0, PCI_ANY_ID, PCI_ANY_ID, },
393 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, PCI_ANY_ID, PCI_ANY_ID, },
394 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_0, PCI_ANY_ID, PCI_ANY_ID, },
395 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1, PCI_ANY_ID, PCI_ANY_ID, },
396 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_2, PCI_ANY_ID, PCI_ANY_ID, },
397 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_0, PCI_ANY_ID, PCI_ANY_ID, },
398 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_1, PCI_ANY_ID, PCI_ANY_ID, },
399 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_1, PCI_ANY_ID, PCI_ANY_ID, },
400 { 0, }, /* End of list */
402 MODULE_DEVICE_TABLE (pci, i8xx_tco_pci_tbl);
405 * Init & exit routines
408 static unsigned char __init i8xx_tco_getdevice (void)
410 struct pci_dev *dev = NULL;
411 u8 val1, val2;
412 u16 badr;
414 * Find the PCI device
417 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
418 if (pci_match_id(i8xx_tco_pci_tbl, dev)) {
419 i8xx_tco_pci = dev;
420 break;
424 if (i8xx_tco_pci) {
426 * Find the ACPI base I/O address which is the base
427 * for the TCO registers (TCOBASE=ACPIBASE + 0x60)
428 * ACPIBASE is bits [15:7] from 0x40-0x43
430 pci_read_config_byte (i8xx_tco_pci, 0x40, &val1);
431 pci_read_config_byte (i8xx_tco_pci, 0x41, &val2);
432 badr = ((val2 << 1) | (val1 >> 7)) << 7;
433 ACPIBASE = badr;
434 /* Something's wrong here, ACPIBASE has to be set */
435 if (badr == 0x0001 || badr == 0x0000) {
436 printk (KERN_ERR PFX "failed to get TCOBASE address\n");
437 return 0;
440 /* Check chipset's NO_REBOOT bit */
441 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
442 if (val1 & 0x02) {
443 val1 &= 0xfd;
444 pci_write_config_byte (i8xx_tco_pci, 0xd4, val1);
445 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
446 if (val1 & 0x02) {
447 printk (KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
448 return 0; /* Cannot reset NO_REBOOT bit */
451 /* Disable reboots untill the watchdog starts */
452 val1 |= 0x02;
453 pci_write_config_byte (i8xx_tco_pci, 0xd4, val1);
455 /* Set the TCO_EN bit in SMI_EN register */
456 if (!request_region (SMI_EN + 1, 1, "i8xx TCO")) {
457 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
458 SMI_EN + 1);
459 return 0;
461 val1 = inb (SMI_EN + 1);
462 val1 &= 0xdf;
463 outb (val1, SMI_EN + 1);
464 release_region (SMI_EN + 1, 1);
465 return 1;
467 return 0;
470 static int __init watchdog_init (void)
472 int ret;
474 spin_lock_init(&tco_lock);
476 /* Check whether or not the hardware watchdog is there */
477 if (!i8xx_tco_getdevice () || i8xx_tco_pci == NULL)
478 return -ENODEV;
480 if (!request_region (TCOBASE, 0x10, "i8xx TCO")) {
481 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
482 TCOBASE);
483 ret = -EIO;
484 goto out;
487 /* Clear out the (probably old) status */
488 outb (0, TCO1_STS);
489 outb (3, TCO2_STS);
491 /* Check that the heartbeat value is within it's range ; if not reset to the default */
492 if (tco_timer_set_heartbeat (heartbeat)) {
493 heartbeat = WATCHDOG_HEARTBEAT;
494 tco_timer_set_heartbeat (heartbeat);
495 printk(KERN_INFO PFX "heartbeat value must be 2<heartbeat<39, using %d\n",
496 heartbeat);
499 ret = register_reboot_notifier(&i8xx_tco_notifier);
500 if (ret != 0) {
501 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
502 ret);
503 goto unreg_region;
506 ret = misc_register(&i8xx_tco_miscdev);
507 if (ret != 0) {
508 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
509 WATCHDOG_MINOR, ret);
510 goto unreg_notifier;
513 tco_timer_stop ();
515 printk (KERN_INFO PFX "initialized (0x%04x). heartbeat=%d sec (nowayout=%d)\n",
516 TCOBASE, heartbeat, nowayout);
518 return 0;
520 unreg_notifier:
521 unregister_reboot_notifier(&i8xx_tco_notifier);
522 unreg_region:
523 release_region (TCOBASE, 0x10);
524 out:
525 return ret;
528 static void __exit watchdog_cleanup (void)
530 /* Stop the timer before we leave */
531 if (!nowayout)
532 tco_timer_stop ();
534 /* Deregister */
535 misc_deregister (&i8xx_tco_miscdev);
536 unregister_reboot_notifier(&i8xx_tco_notifier);
537 release_region (TCOBASE, 0x10);
540 module_init(watchdog_init);
541 module_exit(watchdog_cleanup);
543 MODULE_AUTHOR("Nils Faerber");
544 MODULE_DESCRIPTION("TCO timer driver for i8xx chipsets");
545 MODULE_LICENSE("GPL");
546 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);