initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / char / watchdog / i8xx_tco.c
blob5fc054700c4dfbdbe95dd31b1749a6b1fb57e99a
1 /*
2 * i8xx_tco 0.06: 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 & 82801AB chip : document number 290655-003, 290677-004,
26 * 82801BA & 82801BAM chip : document number 290687-002, 298242-005,
27 * 82801CA & 82801CAM chip : document number 290716-001, 290718-001,
28 * 82801DB & 82801E chip : document number 290744-001, 273599-001,
29 * 82801EB & 82801ER chip : document number 252516-001
31 * 20000710 Nils Faerber
32 * Initial Version 0.01
33 * 20000728 Nils Faerber
34 * 0.02 Fix for SMI_EN->TCO_EN bit, some cleanups
35 * 20011214 Matt Domsch <Matt_Domsch@dell.com>
36 * 0.03 Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
37 * Didn't add timeout option as i810_margin already exists.
38 * 20020224 Joel Becker, Wim Van Sebroeck
39 * 0.04 Support for 82801CA(M) chipset, timer margin needs to be > 3,
40 * add support for WDIOC_SETTIMEOUT and WDIOC_GETTIMEOUT.
41 * 20020412 Rob Radez <rob@osinvestor.com>, Wim Van Sebroeck
42 * 0.05 Fix possible timer_alive race, add expect close support,
43 * clean up ioctls (WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS and
44 * WDIOC_SETOPTIONS), made i810tco_getdevice __init,
45 * removed boot_status, removed tco_timer_read,
46 * added support for 82801DB and 82801E chipset,
47 * added support for 82801EB and 8280ER chipset,
48 * general cleanup.
49 * 20030921 Wim Van Sebroeck <wim@iguana.be>
50 * 0.06 change i810_margin to heartbeat, use module_param,
51 * added notify system support, renamed module to i8xx_tco.
55 * Includes, defines, variables, module parameters, ...
58 #include <linux/module.h>
59 #include <linux/moduleparam.h>
60 #include <linux/types.h>
61 #include <linux/miscdevice.h>
62 #include <linux/watchdog.h>
63 #include <linux/notifier.h>
64 #include <linux/reboot.h>
65 #include <linux/init.h>
66 #include <linux/fs.h>
67 #include <linux/pci.h>
68 #include <linux/ioport.h>
70 #include <asm/uaccess.h>
71 #include <asm/io.h>
73 #include "i8xx_tco.h"
75 /* Module and version information */
76 #define TCO_VERSION "0.06"
77 #define TCO_MODULE_NAME "i8xx TCO timer"
78 #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION
79 #define PFX TCO_MODULE_NAME ": "
81 /* internal variables */
82 static unsigned int ACPIBASE;
83 static spinlock_t tco_lock; /* Guards the hardware */
84 static unsigned long timer_alive;
85 static char tco_expect_close;
86 static struct pci_dev *i8xx_tco_pci;
88 /* module parameters */
89 #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat (2<heartbeat<39) */
90 static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
91 module_param(heartbeat, int, 0);
92 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<heartbeat<39, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
94 #ifdef CONFIG_WATCHDOG_NOWAYOUT
95 static int nowayout = 1;
96 #else
97 static int nowayout = 0;
98 #endif
100 module_param(nowayout, int, 0);
101 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
104 * Some TCO specific functions
107 static inline unsigned char seconds_to_ticks(int seconds)
109 /* the internal timer is stored as ticks which decrement
110 * every 0.6 seconds */
111 return (seconds * 10) / 6;
114 static int tco_timer_start (void)
116 unsigned char val;
118 spin_lock(&tco_lock);
119 val = inb (TCO1_CNT + 1);
120 val &= 0xf7;
121 outb (val, TCO1_CNT + 1);
122 val = inb (TCO1_CNT + 1);
123 spin_unlock(&tco_lock);
125 if (val & 0x08)
126 return -1;
127 return 0;
130 static int tco_timer_stop (void)
132 unsigned char val;
134 spin_lock(&tco_lock);
135 val = inb (TCO1_CNT + 1);
136 val |= 0x08;
137 outb (val, TCO1_CNT + 1);
138 val = inb (TCO1_CNT + 1);
139 spin_unlock(&tco_lock);
141 if ((val & 0x08) == 0)
142 return -1;
143 return 0;
146 static int tco_timer_keepalive (void)
148 spin_lock(&tco_lock);
149 outb (0x01, TCO1_RLD);
150 spin_unlock(&tco_lock);
151 return 0;
154 static int tco_timer_set_heartbeat (int t)
156 unsigned char val;
157 unsigned char tmrval;
159 tmrval = seconds_to_ticks(t);
160 /* from the specs: */
161 /* "Values of 0h-3h are ignored and should not be attempted" */
162 if (tmrval > 0x3f || tmrval < 0x04)
163 return -EINVAL;
165 /* Write new heartbeat to watchdog */
166 spin_lock(&tco_lock);
167 val = inb (TCO1_TMR);
168 val &= 0xc0;
169 val |= tmrval;
170 outb (val, TCO1_TMR);
171 val = inb (TCO1_TMR);
172 spin_unlock(&tco_lock);
174 if ((val & 0x3f) != tmrval)
175 return -EINVAL;
177 heartbeat = t;
178 return 0;
182 * /dev/watchdog handling
185 static int i8xx_tco_open (struct inode *inode, struct file *file)
187 /* /dev/watchdog can only be opened once */
188 if (test_and_set_bit(0, &timer_alive))
189 return -EBUSY;
192 * Reload and activate timer
194 tco_timer_keepalive ();
195 tco_timer_start ();
196 return nonseekable_open(inode, file);
199 static int i8xx_tco_release (struct inode *inode, struct file *file)
202 * Shut off the timer.
204 if (tco_expect_close == 42) {
205 tco_timer_stop ();
206 } else {
207 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
208 tco_timer_keepalive ();
210 clear_bit(0, &timer_alive);
211 tco_expect_close = 0;
212 return 0;
215 static ssize_t i8xx_tco_write (struct file *file, const char __user *data,
216 size_t len, loff_t * ppos)
218 /* See if we got the magic character 'V' and reload the timer */
219 if (len) {
220 if (!nowayout) {
221 size_t i;
223 /* note: just in case someone wrote the magic character
224 * five months ago... */
225 tco_expect_close = 0;
227 /* scan to see whether or not we got the magic character */
228 for (i = 0; i != len; i++) {
229 char c;
230 if(get_user(c, data+i))
231 return -EFAULT;
232 if (c == 'V')
233 tco_expect_close = 42;
237 /* someone wrote to us, we should reload the timer */
238 tco_timer_keepalive ();
240 return len;
243 static int i8xx_tco_ioctl (struct inode *inode, struct file *file,
244 unsigned int cmd, unsigned long arg)
246 int new_options, retval = -EINVAL;
247 int new_heartbeat;
248 void __user *argp = (void __user *)arg;
249 int __user *p = argp;
250 static struct watchdog_info ident = {
251 .options = WDIOF_SETTIMEOUT |
252 WDIOF_KEEPALIVEPING |
253 WDIOF_MAGICCLOSE,
254 .firmware_version = 0,
255 .identity = TCO_MODULE_NAME,
258 switch (cmd) {
259 case WDIOC_GETSUPPORT:
260 return copy_to_user(argp, &ident,
261 sizeof (ident)) ? -EFAULT : 0;
263 case WDIOC_GETSTATUS:
264 case WDIOC_GETBOOTSTATUS:
265 return put_user (0, p);
267 case WDIOC_KEEPALIVE:
268 tco_timer_keepalive ();
269 return 0;
271 case WDIOC_SETOPTIONS:
273 if (get_user (new_options, p))
274 return -EFAULT;
276 if (new_options & WDIOS_DISABLECARD) {
277 tco_timer_stop ();
278 retval = 0;
281 if (new_options & WDIOS_ENABLECARD) {
282 tco_timer_keepalive ();
283 tco_timer_start ();
284 retval = 0;
287 return retval;
290 case WDIOC_SETTIMEOUT:
292 if (get_user(new_heartbeat, p))
293 return -EFAULT;
295 if (tco_timer_set_heartbeat(new_heartbeat))
296 return -EINVAL;
298 tco_timer_keepalive ();
299 /* Fall */
302 case WDIOC_GETTIMEOUT:
303 return put_user(heartbeat, p);
305 default:
306 return -ENOIOCTLCMD;
311 * Notify system
314 static int i8xx_tco_notify_sys (struct notifier_block *this, unsigned long code, void *unused)
316 if (code==SYS_DOWN || code==SYS_HALT) {
317 /* Turn the WDT off */
318 tco_timer_stop ();
321 return NOTIFY_DONE;
325 * Kernel Interfaces
328 static struct file_operations i8xx_tco_fops = {
329 .owner = THIS_MODULE,
330 .llseek = no_llseek,
331 .write = i8xx_tco_write,
332 .ioctl = i8xx_tco_ioctl,
333 .open = i8xx_tco_open,
334 .release = i8xx_tco_release,
337 static struct miscdevice i8xx_tco_miscdev = {
338 .minor = WATCHDOG_MINOR,
339 .name = "watchdog",
340 .fops = &i8xx_tco_fops,
343 static struct notifier_block i8xx_tco_notifier = {
344 .notifier_call = i8xx_tco_notify_sys,
348 * Data for PCI driver interface
350 * This data only exists for exporting the supported
351 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
352 * register a pci_driver, because someone else might one day
353 * want to register another driver on the same PCI id.
355 static struct pci_device_id i8xx_tco_pci_tbl[] = {
356 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, PCI_ANY_ID, PCI_ANY_ID, },
357 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_0, PCI_ANY_ID, PCI_ANY_ID, },
358 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, PCI_ANY_ID, PCI_ANY_ID, },
359 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_10, PCI_ANY_ID, PCI_ANY_ID, },
360 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, PCI_ANY_ID, PCI_ANY_ID, },
361 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, PCI_ANY_ID, PCI_ANY_ID, },
362 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, PCI_ANY_ID, PCI_ANY_ID, },
363 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801E_0, PCI_ANY_ID, PCI_ANY_ID, },
364 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, PCI_ANY_ID, PCI_ANY_ID, },
365 { 0, }, /* End of list */
367 MODULE_DEVICE_TABLE (pci, i8xx_tco_pci_tbl);
370 * Init & exit routines
373 static unsigned char __init i8xx_tco_getdevice (void)
375 struct pci_dev *dev = NULL;
376 u8 val1, val2;
377 u16 badr;
379 * Find the PCI device
382 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
383 if (pci_match_device(i8xx_tco_pci_tbl, dev)) {
384 i8xx_tco_pci = dev;
385 break;
389 if (i8xx_tco_pci) {
391 * Find the ACPI base I/O address which is the base
392 * for the TCO registers (TCOBASE=ACPIBASE + 0x60)
393 * ACPIBASE is bits [15:7] from 0x40-0x43
395 pci_read_config_byte (i8xx_tco_pci, 0x40, &val1);
396 pci_read_config_byte (i8xx_tco_pci, 0x41, &val2);
397 badr = ((val2 << 1) | (val1 >> 7)) << 7;
398 ACPIBASE = badr;
399 /* Something's wrong here, ACPIBASE has to be set */
400 if (badr == 0x0001 || badr == 0x0000) {
401 printk (KERN_ERR PFX "failed to get TCOBASE address\n");
402 return 0;
405 * Check chipset's NO_REBOOT bit
407 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
408 if (val1 & 0x02) {
409 val1 &= 0xfd;
410 pci_write_config_byte (i8xx_tco_pci, 0xd4, val1);
411 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
412 if (val1 & 0x02) {
413 printk (KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
414 return 0; /* Cannot reset NO_REBOOT bit */
417 /* Set the TCO_EN bit in SMI_EN register */
418 val1 = inb (SMI_EN + 1);
419 val1 &= 0xdf;
420 outb (val1, SMI_EN + 1);
421 /* Clear out the (probably old) status */
422 outb (0, TCO1_STS);
423 outb (3, TCO2_STS);
424 return 1;
426 return 0;
429 static int __init watchdog_init (void)
431 int ret;
433 spin_lock_init(&tco_lock);
435 /* Check whether or not the hardware watchdog is there */
436 if (!i8xx_tco_getdevice () || i8xx_tco_pci == NULL)
437 return -ENODEV;
439 if (!request_region (TCOBASE, 0x10, "i8xx TCO")) {
440 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
441 TCOBASE);
442 ret = -EIO;
443 goto out;
446 /* Check that the heartbeat value is within it's range ; if not reset to the default */
447 if (tco_timer_set_heartbeat (heartbeat)) {
448 heartbeat = WATCHDOG_HEARTBEAT;
449 tco_timer_set_heartbeat (heartbeat);
450 printk(KERN_INFO PFX "heartbeat value must be 2<heartbeat<39, using %d\n",
451 heartbeat);
454 ret = register_reboot_notifier(&i8xx_tco_notifier);
455 if (ret != 0) {
456 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
457 ret);
458 goto unreg_region;
461 ret = misc_register(&i8xx_tco_miscdev);
462 if (ret != 0) {
463 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
464 WATCHDOG_MINOR, ret);
465 goto unreg_notifier;
468 tco_timer_keepalive ();
470 printk (KERN_INFO PFX "initialized (0x%04x). heartbeat=%d sec (nowayout=%d)\n",
471 TCOBASE, heartbeat, nowayout);
473 return 0;
475 unreg_notifier:
476 unregister_reboot_notifier(&i8xx_tco_notifier);
477 unreg_region:
478 release_region (TCOBASE, 0x10);
479 out:
480 return ret;
483 static void __exit watchdog_cleanup (void)
485 u8 val;
487 /* Stop the timer before we leave */
488 if (!nowayout)
489 tco_timer_stop ();
491 /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
492 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val);
493 val |= 0x02;
494 pci_write_config_byte (i8xx_tco_pci, 0xd4, val);
496 /* Deregister */
497 misc_deregister (&i8xx_tco_miscdev);
498 unregister_reboot_notifier(&i8xx_tco_notifier);
499 release_region (TCOBASE, 0x10);
502 module_init(watchdog_init);
503 module_exit(watchdog_cleanup);
505 MODULE_AUTHOR("Nils Faerber");
506 MODULE_DESCRIPTION("TCO timer driver for i8xx chipsets");
507 MODULE_LICENSE("GPL");
508 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);