VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / char / watchdog / i8xx_tco.c
1 /*
2  *      i8xx_tco 0.06:  TCO timer driver for i8xx chipsets
3  *
4  *      (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights Reserved.
5  *                              http://www.kernelconcepts.de
6  *
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.
11  *
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.
15  *
16  *      (c) Copyright 2000      kernel concepts <nils@kernelconcepts.de>
17  *                              developed for
18  *                              Jentro AG, Haar/Munich (Germany)
19  *
20  *      TCO timer driver for i8xx chipsets
21  *      based on softdog.c by Alan Cox <alan@redhat.com>
22  *
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
30  *
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.
52  */
53
54 /*
55  *      Includes, defines, variables, module parameters, ...
56  */
57
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>
69
70 #include <asm/uaccess.h>
71 #include <asm/io.h>
72
73 #include "i8xx_tco.h"
74
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 ": "
80
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;
87
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) ")");
93
94 #ifdef CONFIG_WATCHDOG_NOWAYOUT
95 static int nowayout = 1;
96 #else
97 static int nowayout = 0;
98 #endif
99
100 module_param(nowayout, int, 0);
101 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
102
103 /*
104  * Some TCO specific functions
105  */
106
107 static inline unsigned char seconds_to_ticks(int seconds)
108 {
109         /* the internal timer is stored as ticks which decrement
110          * every 0.6 seconds */
111         return (seconds * 10) / 6;
112 }
113
114 static int tco_timer_start (void)
115 {
116         unsigned char val;
117
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);
124
125         if (val & 0x08)
126                 return -1;
127         return 0;
128 }
129
130 static int tco_timer_stop (void)
131 {
132         unsigned char val;
133
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);
140
141         if ((val & 0x08) == 0)
142                 return -1;
143         return 0;
144 }
145
146 static int tco_timer_keepalive (void)
147 {
148         spin_lock(&tco_lock);
149         outb (0x01, TCO1_RLD);
150         spin_unlock(&tco_lock);
151         return 0;
152 }
153
154 static int tco_timer_set_heartbeat (int t)
155 {
156         unsigned char val;
157         unsigned char tmrval;
158
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;
164
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);
173
174         if ((val & 0x3f) != tmrval)
175                 return -EINVAL;
176
177         heartbeat = t;
178         return 0;
179 }
180
181 /*
182  *      /dev/watchdog handling
183  */
184
185 static int i8xx_tco_open (struct inode *inode, struct file *file)
186 {
187         /* /dev/watchdog can only be opened once */
188         if (test_and_set_bit(0, &timer_alive))
189                 return -EBUSY;
190
191         /*
192          *      Reload and activate timer
193          */
194         tco_timer_keepalive ();
195         tco_timer_start ();
196         return nonseekable_open(inode, file);
197 }
198
199 static int i8xx_tco_release (struct inode *inode, struct file *file)
200 {
201         /*
202          *      Shut off the timer.
203          */
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 ();
209         }
210         clear_bit(0, &timer_alive);
211         tco_expect_close = 0;
212         return 0;
213 }
214
215 static ssize_t i8xx_tco_write (struct file *file, const char __user *data,
216                               size_t len, loff_t * ppos)
217 {
218         /* See if we got the magic character 'V' and reload the timer */
219         if (len) {
220                 if (!nowayout) {
221                         size_t i;
222
223                         /* note: just in case someone wrote the magic character
224                          * five months ago... */
225                         tco_expect_close = 0;
226
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;
234                         }
235                 }
236
237                 /* someone wrote to us, we should reload the timer */
238                 tco_timer_keepalive ();
239         }
240         return len;
241 }
242
243 static int i8xx_tco_ioctl (struct inode *inode, struct file *file,
244                           unsigned int cmd, unsigned long arg)
245 {
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,
256         };
257
258         switch (cmd) {
259                 case WDIOC_GETSUPPORT:
260                         return copy_to_user(argp, &ident,
261                                 sizeof (ident)) ? -EFAULT : 0;
262
263                 case WDIOC_GETSTATUS:
264                 case WDIOC_GETBOOTSTATUS:
265                         return put_user (0, p);
266
267                 case WDIOC_KEEPALIVE:
268                         tco_timer_keepalive ();
269                         return 0;
270
271                 case WDIOC_SETOPTIONS:
272                 {
273                         if (get_user (new_options, p))
274                                 return -EFAULT;
275
276                         if (new_options & WDIOS_DISABLECARD) {
277                                 tco_timer_stop ();
278                                 retval = 0;
279                         }
280
281                         if (new_options & WDIOS_ENABLECARD) {
282                                 tco_timer_keepalive ();
283                                 tco_timer_start ();
284                                 retval = 0;
285                         }
286
287                         return retval;
288                 }
289
290                 case WDIOC_SETTIMEOUT:
291                 {
292                         if (get_user(new_heartbeat, p))
293                                 return -EFAULT;
294
295                         if (tco_timer_set_heartbeat(new_heartbeat))
296                             return -EINVAL;
297
298                         tco_timer_keepalive ();
299                         /* Fall */
300                 }
301
302                 case WDIOC_GETTIMEOUT:
303                         return put_user(heartbeat, p);
304
305                 default:
306                         return -ENOIOCTLCMD;
307         }
308 }
309
310 /*
311  *      Notify system
312  */
313
314 static int i8xx_tco_notify_sys (struct notifier_block *this, unsigned long code, void *unused)
315 {
316         if (code==SYS_DOWN || code==SYS_HALT) {
317                 /* Turn the WDT off */
318                 tco_timer_stop ();
319         }
320
321         return NOTIFY_DONE;
322 }
323
324 /*
325  *      Kernel Interfaces
326  */
327
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,
335 };
336
337 static struct miscdevice i8xx_tco_miscdev = {
338         .minor =        WATCHDOG_MINOR,
339         .name =         "watchdog",
340         .fops =         &i8xx_tco_fops,
341 };
342
343 static struct notifier_block i8xx_tco_notifier = {
344         .notifier_call =        i8xx_tco_notify_sys,
345 };
346
347 /*
348  * Data for PCI driver interface
349  *
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.
354  */
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 */
366 };
367 MODULE_DEVICE_TABLE (pci, i8xx_tco_pci_tbl);
368
369 /*
370  *      Init & exit routines
371  */
372
373 static unsigned char __init i8xx_tco_getdevice (void)
374 {
375         struct pci_dev *dev = NULL;
376         u8 val1, val2;
377         u16 badr;
378         /*
379          *      Find the PCI device
380          */
381
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;
386                 }
387         }
388
389         if (i8xx_tco_pci) {
390                 /*
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
394                  */
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;
403                 }
404                 /*
405                  * Check chipset's NO_REBOOT bit
406                  */
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 */
415                         }
416                 }
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;
425         }
426         return 0;
427 }
428
429 static int __init watchdog_init (void)
430 {
431         int ret;
432
433         spin_lock_init(&tco_lock);
434
435         /* Check whether or not the hardware watchdog is there */
436         if (!i8xx_tco_getdevice () || i8xx_tco_pci == NULL)
437                 return -ENODEV;
438
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;
444         }
445
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);
452         }
453
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;
459         }
460
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;
466         }
467
468         tco_timer_keepalive ();
469
470         printk (KERN_INFO PFX "initialized (0x%04x). heartbeat=%d sec (nowayout=%d)\n",
471                 TCOBASE, heartbeat, nowayout);
472
473         return 0;
474
475 unreg_notifier:
476         unregister_reboot_notifier(&i8xx_tco_notifier);
477 unreg_region:
478         release_region (TCOBASE, 0x10);
479 out:
480         return ret;
481 }
482
483 static void __exit watchdog_cleanup (void)
484 {
485         u8 val;
486
487         /* Stop the timer before we leave */
488         if (!nowayout)
489                 tco_timer_stop ();
490
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);
495
496         /* Deregister */
497         misc_deregister (&i8xx_tco_miscdev);
498         unregister_reboot_notifier(&i8xx_tco_notifier);
499         release_region (TCOBASE, 0x10);
500 }
501
502 module_init(watchdog_init);
503 module_exit(watchdog_cleanup);
504
505 MODULE_AUTHOR("Nils Faerber");
506 MODULE_DESCRIPTION("TCO timer driver for i8xx chipsets");
507 MODULE_LICENSE("GPL");
508 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);