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>
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,
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>
67 #include <linux/pci.h>
68 #include <linux/ioport.h>
70 #include <asm/uaccess.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;
97 static int nowayout = 0;
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)
118 spin_lock(&tco_lock);
119 val = inb (TCO1_CNT + 1);
121 outb (val, TCO1_CNT + 1);
122 val = inb (TCO1_CNT + 1);
123 spin_unlock(&tco_lock);
130 static int tco_timer_stop (void)
134 spin_lock(&tco_lock);
135 val = inb (TCO1_CNT + 1);
137 outb (val, TCO1_CNT + 1);
138 val = inb (TCO1_CNT + 1);
139 spin_unlock(&tco_lock);
141 if ((val & 0x08) == 0)
146 static int tco_timer_keepalive (void)
148 spin_lock(&tco_lock);
149 outb (0x01, TCO1_RLD);
150 spin_unlock(&tco_lock);
154 static int tco_timer_set_heartbeat (int t)
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)
165 /* Write new heartbeat to watchdog */
166 spin_lock(&tco_lock);
167 val = inb (TCO1_TMR);
170 outb (val, TCO1_TMR);
171 val = inb (TCO1_TMR);
172 spin_unlock(&tco_lock);
174 if ((val & 0x3f) != tmrval)
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))
192 * Reload and activate timer
194 tco_timer_keepalive ();
199 static int i8xx_tco_release (struct inode *inode, struct file *file)
202 * Shut off the timer.
204 if (tco_expect_close == 42) {
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;
215 static ssize_t i8xx_tco_write (struct file *file, const char *data,
216 size_t len, loff_t * ppos)
218 /* Can't seek (pwrite) on this device */
219 if (ppos != &file->f_pos)
222 /* See if we got the magic character 'V' and reload the timer */
227 /* note: just in case someone wrote the magic character
228 * five months ago... */
229 tco_expect_close = 0;
231 /* scan to see whether or not we got the magic character */
232 for (i = 0; i != len; i++) {
234 if(get_user(c, data+i))
237 tco_expect_close = 42;
241 /* someone wrote to us, we should reload the timer */
242 tco_timer_keepalive ();
247 static int i8xx_tco_ioctl (struct inode *inode, struct file *file,
248 unsigned int cmd, unsigned long arg)
250 int new_options, retval = -EINVAL;
252 static struct watchdog_info ident = {
253 .options = WDIOF_SETTIMEOUT |
254 WDIOF_KEEPALIVEPING |
256 .firmware_version = 0,
257 .identity = TCO_MODULE_NAME,
261 case WDIOC_GETSUPPORT:
262 return copy_to_user((struct watchdog_info *) arg, &ident,
263 sizeof (ident)) ? -EFAULT : 0;
265 case WDIOC_GETSTATUS:
266 case WDIOC_GETBOOTSTATUS:
267 return put_user (0, (int *) arg);
269 case WDIOC_KEEPALIVE:
270 tco_timer_keepalive ();
273 case WDIOC_SETOPTIONS:
275 if (get_user (new_options, (int *) arg))
278 if (new_options & WDIOS_DISABLECARD) {
283 if (new_options & WDIOS_ENABLECARD) {
284 tco_timer_keepalive ();
292 case WDIOC_SETTIMEOUT:
294 if (get_user(new_heartbeat, (int *) arg))
297 if (tco_timer_set_heartbeat(new_heartbeat))
300 tco_timer_keepalive ();
304 case WDIOC_GETTIMEOUT:
305 return put_user(heartbeat, (int *)arg);
316 static int i8xx_tco_notify_sys (struct notifier_block *this, unsigned long code, void *unused)
318 if (code==SYS_DOWN || code==SYS_HALT) {
319 /* Turn the WDT off */
330 static struct file_operations i8xx_tco_fops = {
331 .owner = THIS_MODULE,
333 .write = i8xx_tco_write,
334 .ioctl = i8xx_tco_ioctl,
335 .open = i8xx_tco_open,
336 .release = i8xx_tco_release,
339 static struct miscdevice i8xx_tco_miscdev = {
340 .minor = WATCHDOG_MINOR,
342 .fops = &i8xx_tco_fops,
345 static struct notifier_block i8xx_tco_notifier = {
346 .notifier_call = i8xx_tco_notify_sys,
350 * Data for PCI driver interface
352 * This data only exists for exporting the supported
353 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
354 * register a pci_driver, because someone else might one day
355 * want to register another driver on the same PCI id.
357 static struct pci_device_id i8xx_tco_pci_tbl[] = {
358 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, PCI_ANY_ID, PCI_ANY_ID, },
359 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_0, PCI_ANY_ID, PCI_ANY_ID, },
360 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, PCI_ANY_ID, PCI_ANY_ID, },
361 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_10, PCI_ANY_ID, PCI_ANY_ID, },
362 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, PCI_ANY_ID, PCI_ANY_ID, },
363 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, PCI_ANY_ID, PCI_ANY_ID, },
364 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, PCI_ANY_ID, PCI_ANY_ID, },
365 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801E_0, PCI_ANY_ID, PCI_ANY_ID, },
366 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, PCI_ANY_ID, PCI_ANY_ID, },
367 { 0, }, /* End of list */
369 MODULE_DEVICE_TABLE (pci, i8xx_tco_pci_tbl);
372 * Init & exit routines
375 static unsigned char __init i8xx_tco_getdevice (void)
377 struct pci_dev *dev = NULL;
381 * Find the PCI device
384 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
385 if (pci_match_device(i8xx_tco_pci_tbl, dev)) {
393 * Find the ACPI base I/O address which is the base
394 * for the TCO registers (TCOBASE=ACPIBASE + 0x60)
395 * ACPIBASE is bits [15:7] from 0x40-0x43
397 pci_read_config_byte (i8xx_tco_pci, 0x40, &val1);
398 pci_read_config_byte (i8xx_tco_pci, 0x41, &val2);
399 badr = ((val2 << 1) | (val1 >> 7)) << 7;
401 /* Something's wrong here, ACPIBASE has to be set */
402 if (badr == 0x0001 || badr == 0x0000) {
403 printk (KERN_ERR PFX "failed to get TCOBASE address\n");
407 * Check chipset's NO_REBOOT bit
409 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
412 pci_write_config_byte (i8xx_tco_pci, 0xd4, val1);
413 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
415 printk (KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
416 return 0; /* Cannot reset NO_REBOOT bit */
419 /* Set the TCO_EN bit in SMI_EN register */
420 val1 = inb (SMI_EN + 1);
422 outb (val1, SMI_EN + 1);
423 /* Clear out the (probably old) status */
431 static int __init watchdog_init (void)
435 spin_lock_init(&tco_lock);
437 /* Check whether or not the hardware watchdog is there */
438 if (!i8xx_tco_getdevice () || i8xx_tco_pci == NULL)
441 if (!request_region (TCOBASE, 0x10, "i8xx TCO")) {
442 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
448 /* Check that the heartbeat value is within it's range ; if not reset to the default */
449 if (tco_timer_set_heartbeat (heartbeat)) {
450 heartbeat = WATCHDOG_HEARTBEAT;
451 tco_timer_set_heartbeat (heartbeat);
452 printk(KERN_INFO PFX "heartbeat value must be 2<heartbeat<39, using %d\n",
456 ret = register_reboot_notifier(&i8xx_tco_notifier);
458 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
463 ret = misc_register(&i8xx_tco_miscdev);
465 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
466 WATCHDOG_MINOR, ret);
470 tco_timer_keepalive ();
472 printk (KERN_INFO PFX "initialized (0x%04x). heartbeat=%d sec (nowayout=%d)\n",
473 TCOBASE, heartbeat, nowayout);
478 unregister_reboot_notifier(&i8xx_tco_notifier);
480 release_region (TCOBASE, 0x10);
485 static void __exit watchdog_cleanup (void)
489 /* Stop the timer before we leave */
493 /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
494 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val);
496 pci_write_config_byte (i8xx_tco_pci, 0xd4, val);
499 misc_deregister (&i8xx_tco_miscdev);
500 unregister_reboot_notifier(&i8xx_tco_notifier);
501 release_region (TCOBASE, 0x10);
504 module_init(watchdog_init);
505 module_exit(watchdog_cleanup);
507 MODULE_AUTHOR("Nils Faerber");
508 MODULE_DESCRIPTION("TCO timer driver for i8xx chipsets");
509 MODULE_LICENSE("GPL");
510 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);