ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 0;
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 *data,
216                               size_t len, loff_t * ppos)
217 {
218         /*  Can't seek (pwrite) on this device  */
219         if (ppos != &file->f_pos)
220                 return -ESPIPE;
221
222         /* See if we got the magic character 'V' and reload the timer */
223         if (len) {
224                 if (!nowayout) {
225                         size_t i;
226
227                         /* note: just in case someone wrote the magic character
228                          * five months ago... */
229                         tco_expect_close = 0;
230
231                         /* scan to see whether or not we got the magic character */
232                         for (i = 0; i != len; i++) {
233                                 char c;
234                                 if(get_user(c, data+i))
235                                         return -EFAULT;
236                                 if (c == 'V')
237                                         tco_expect_close = 42;
238                         }
239                 }
240
241                 /* someone wrote to us, we should reload the timer */
242                 tco_timer_keepalive ();
243         }
244         return len;
245 }
246
247 static int i8xx_tco_ioctl (struct inode *inode, struct file *file,
248                           unsigned int cmd, unsigned long arg)
249 {
250         int new_options, retval = -EINVAL;
251         int new_heartbeat;
252         static struct watchdog_info ident = {
253                 .options =              WDIOF_SETTIMEOUT |
254                                         WDIOF_KEEPALIVEPING |
255                                         WDIOF_MAGICCLOSE,
256                 .firmware_version =     0,
257                 .identity =             TCO_MODULE_NAME,
258         };
259
260         switch (cmd) {
261                 case WDIOC_GETSUPPORT:
262                         return copy_to_user((struct watchdog_info *) arg, &ident,
263                                 sizeof (ident)) ? -EFAULT : 0;
264
265                 case WDIOC_GETSTATUS:
266                 case WDIOC_GETBOOTSTATUS:
267                         return put_user (0, (int *) arg);
268
269                 case WDIOC_KEEPALIVE:
270                         tco_timer_keepalive ();
271                         return 0;
272
273                 case WDIOC_SETOPTIONS:
274                 {
275                         if (get_user (new_options, (int *) arg))
276                                 return -EFAULT;
277
278                         if (new_options & WDIOS_DISABLECARD) {
279                                 tco_timer_stop ();
280                                 retval = 0;
281                         }
282
283                         if (new_options & WDIOS_ENABLECARD) {
284                                 tco_timer_keepalive ();
285                                 tco_timer_start ();
286                                 retval = 0;
287                         }
288
289                         return retval;
290                 }
291
292                 case WDIOC_SETTIMEOUT:
293                 {
294                         if (get_user(new_heartbeat, (int *) arg))
295                                 return -EFAULT;
296
297                         if (tco_timer_set_heartbeat(new_heartbeat))
298                             return -EINVAL;
299
300                         tco_timer_keepalive ();
301                         /* Fall */
302                 }
303
304                 case WDIOC_GETTIMEOUT:
305                         return put_user(heartbeat, (int *)arg);
306
307                 default:
308                         return -ENOIOCTLCMD;
309         }
310 }
311
312 /*
313  *      Notify system
314  */
315
316 static int i8xx_tco_notify_sys (struct notifier_block *this, unsigned long code, void *unused)
317 {
318         if (code==SYS_DOWN || code==SYS_HALT) {
319                 /* Turn the WDT off */
320                 tco_timer_stop ();
321         }
322
323         return NOTIFY_DONE;
324 }
325
326 /*
327  *      Kernel Interfaces
328  */
329
330 static struct file_operations i8xx_tco_fops = {
331         .owner =        THIS_MODULE,
332         .llseek =       no_llseek,
333         .write =        i8xx_tco_write,
334         .ioctl =        i8xx_tco_ioctl,
335         .open =         i8xx_tco_open,
336         .release =      i8xx_tco_release,
337 };
338
339 static struct miscdevice i8xx_tco_miscdev = {
340         .minor =        WATCHDOG_MINOR,
341         .name =         "watchdog",
342         .fops =         &i8xx_tco_fops,
343 };
344
345 static struct notifier_block i8xx_tco_notifier = {
346         .notifier_call =        i8xx_tco_notify_sys,
347 };
348
349 /*
350  * Data for PCI driver interface
351  *
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.
356  */
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 */
368 };
369 MODULE_DEVICE_TABLE (pci, i8xx_tco_pci_tbl);
370
371 /*
372  *      Init & exit routines
373  */
374
375 static unsigned char __init i8xx_tco_getdevice (void)
376 {
377         struct pci_dev *dev = NULL;
378         u8 val1, val2;
379         u16 badr;
380         /*
381          *      Find the PCI device
382          */
383
384         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
385                 if (pci_match_device(i8xx_tco_pci_tbl, dev)) {
386                         i8xx_tco_pci = dev;
387                         break;
388                 }
389         }
390
391         if (i8xx_tco_pci) {
392                 /*
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
396                  */
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;
400                 ACPIBASE = badr;
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");
404                         return 0;
405                 }
406                 /*
407                  * Check chipset's NO_REBOOT bit
408                  */
409                 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
410                 if (val1 & 0x02) {
411                         val1 &= 0xfd;
412                         pci_write_config_byte (i8xx_tco_pci, 0xd4, val1);
413                         pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
414                         if (val1 & 0x02) {
415                                 printk (KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
416                                 return 0;       /* Cannot reset NO_REBOOT bit */
417                         }
418                 }
419                 /* Set the TCO_EN bit in SMI_EN register */
420                 val1 = inb (SMI_EN + 1);
421                 val1 &= 0xdf;
422                 outb (val1, SMI_EN + 1);
423                 /* Clear out the (probably old) status */
424                 outb (0, TCO1_STS);
425                 outb (3, TCO2_STS);
426                 return 1;
427         }
428         return 0;
429 }
430
431 static int __init watchdog_init (void)
432 {
433         int ret;
434
435         spin_lock_init(&tco_lock);
436
437         /* Check whether or not the hardware watchdog is there */
438         if (!i8xx_tco_getdevice () || i8xx_tco_pci == NULL)
439                 return -ENODEV;
440
441         if (!request_region (TCOBASE, 0x10, "i8xx TCO")) {
442                 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
443                         TCOBASE);
444                 ret = -EIO;
445                 goto out;
446         }
447
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",
453                         heartbeat);
454         }
455
456         ret = register_reboot_notifier(&i8xx_tco_notifier);
457         if (ret != 0) {
458                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
459                         ret);
460                 goto unreg_region;
461         }
462
463         ret = misc_register(&i8xx_tco_miscdev);
464         if (ret != 0) {
465                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
466                         WATCHDOG_MINOR, ret);
467                 goto unreg_notifier;
468         }
469
470         tco_timer_keepalive ();
471
472         printk (KERN_INFO PFX "initialized (0x%04x). heartbeat=%d sec (nowayout=%d)\n",
473                 TCOBASE, heartbeat, nowayout);
474
475         return 0;
476
477 unreg_notifier:
478         unregister_reboot_notifier(&i8xx_tco_notifier);
479 unreg_region:
480         release_region (TCOBASE, 0x10);
481 out:
482         return ret;
483 }
484
485 static void __exit watchdog_cleanup (void)
486 {
487         u8 val;
488
489         /* Stop the timer before we leave */
490         if (!nowayout)
491                 tco_timer_stop ();
492
493         /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
494         pci_read_config_byte (i8xx_tco_pci, 0xd4, &val);
495         val |= 0x02;
496         pci_write_config_byte (i8xx_tco_pci, 0xd4, val);
497
498         /* Deregister */
499         misc_deregister (&i8xx_tco_miscdev);
500         unregister_reboot_notifier(&i8xx_tco_notifier);
501         release_region (TCOBASE, 0x10);
502 }
503
504 module_init(watchdog_init);
505 module_exit(watchdog_cleanup);
506
507 MODULE_AUTHOR("Nils Faerber");
508 MODULE_DESCRIPTION("TCO timer driver for i8xx chipsets");
509 MODULE_LICENSE("GPL");
510 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);