This commit was manufactured by cvs2svn to create tag
[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 __user *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         void __user *argp = (void __user *)arg;
253         int __user *p = argp;
254         static struct watchdog_info ident = {
255                 .options =              WDIOF_SETTIMEOUT |
256                                         WDIOF_KEEPALIVEPING |
257                                         WDIOF_MAGICCLOSE,
258                 .firmware_version =     0,
259                 .identity =             TCO_MODULE_NAME,
260         };
261
262         switch (cmd) {
263                 case WDIOC_GETSUPPORT:
264                         return copy_to_user(argp, &ident,
265                                 sizeof (ident)) ? -EFAULT : 0;
266
267                 case WDIOC_GETSTATUS:
268                 case WDIOC_GETBOOTSTATUS:
269                         return put_user (0, p);
270
271                 case WDIOC_KEEPALIVE:
272                         tco_timer_keepalive ();
273                         return 0;
274
275                 case WDIOC_SETOPTIONS:
276                 {
277                         if (get_user (new_options, p))
278                                 return -EFAULT;
279
280                         if (new_options & WDIOS_DISABLECARD) {
281                                 tco_timer_stop ();
282                                 retval = 0;
283                         }
284
285                         if (new_options & WDIOS_ENABLECARD) {
286                                 tco_timer_keepalive ();
287                                 tco_timer_start ();
288                                 retval = 0;
289                         }
290
291                         return retval;
292                 }
293
294                 case WDIOC_SETTIMEOUT:
295                 {
296                         if (get_user(new_heartbeat, p))
297                                 return -EFAULT;
298
299                         if (tco_timer_set_heartbeat(new_heartbeat))
300                             return -EINVAL;
301
302                         tco_timer_keepalive ();
303                         /* Fall */
304                 }
305
306                 case WDIOC_GETTIMEOUT:
307                         return put_user(heartbeat, p);
308
309                 default:
310                         return -ENOIOCTLCMD;
311         }
312 }
313
314 /*
315  *      Notify system
316  */
317
318 static int i8xx_tco_notify_sys (struct notifier_block *this, unsigned long code, void *unused)
319 {
320         if (code==SYS_DOWN || code==SYS_HALT) {
321                 /* Turn the WDT off */
322                 tco_timer_stop ();
323         }
324
325         return NOTIFY_DONE;
326 }
327
328 /*
329  *      Kernel Interfaces
330  */
331
332 static struct file_operations i8xx_tco_fops = {
333         .owner =        THIS_MODULE,
334         .llseek =       no_llseek,
335         .write =        i8xx_tco_write,
336         .ioctl =        i8xx_tco_ioctl,
337         .open =         i8xx_tco_open,
338         .release =      i8xx_tco_release,
339 };
340
341 static struct miscdevice i8xx_tco_miscdev = {
342         .minor =        WATCHDOG_MINOR,
343         .name =         "watchdog",
344         .fops =         &i8xx_tco_fops,
345 };
346
347 static struct notifier_block i8xx_tco_notifier = {
348         .notifier_call =        i8xx_tco_notify_sys,
349 };
350
351 /*
352  * Data for PCI driver interface
353  *
354  * This data only exists for exporting the supported
355  * PCI ids via MODULE_DEVICE_TABLE.  We do not actually
356  * register a pci_driver, because someone else might one day
357  * want to register another driver on the same PCI id.
358  */
359 static struct pci_device_id i8xx_tco_pci_tbl[] = {
360         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0,   PCI_ANY_ID, PCI_ANY_ID, },
361         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_0,   PCI_ANY_ID, PCI_ANY_ID, },
362         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0,   PCI_ANY_ID, PCI_ANY_ID, },
363         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_10,  PCI_ANY_ID, PCI_ANY_ID, },
364         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0,   PCI_ANY_ID, PCI_ANY_ID, },
365         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12,  PCI_ANY_ID, PCI_ANY_ID, },
366         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0,   PCI_ANY_ID, PCI_ANY_ID, },
367         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801E_0,    PCI_ANY_ID, PCI_ANY_ID, },
368         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0,   PCI_ANY_ID, PCI_ANY_ID, },
369         { 0, },                 /* End of list */
370 };
371 MODULE_DEVICE_TABLE (pci, i8xx_tco_pci_tbl);
372
373 /*
374  *      Init & exit routines
375  */
376
377 static unsigned char __init i8xx_tco_getdevice (void)
378 {
379         struct pci_dev *dev = NULL;
380         u8 val1, val2;
381         u16 badr;
382         /*
383          *      Find the PCI device
384          */
385
386         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
387                 if (pci_match_device(i8xx_tco_pci_tbl, dev)) {
388                         i8xx_tco_pci = dev;
389                         break;
390                 }
391         }
392
393         if (i8xx_tco_pci) {
394                 /*
395                  *      Find the ACPI base I/O address which is the base
396                  *      for the TCO registers (TCOBASE=ACPIBASE + 0x60)
397                  *      ACPIBASE is bits [15:7] from 0x40-0x43
398                  */
399                 pci_read_config_byte (i8xx_tco_pci, 0x40, &val1);
400                 pci_read_config_byte (i8xx_tco_pci, 0x41, &val2);
401                 badr = ((val2 << 1) | (val1 >> 7)) << 7;
402                 ACPIBASE = badr;
403                 /* Something's wrong here, ACPIBASE has to be set */
404                 if (badr == 0x0001 || badr == 0x0000) {
405                         printk (KERN_ERR PFX "failed to get TCOBASE address\n");
406                         return 0;
407                 }
408                 /*
409                  * Check chipset's NO_REBOOT bit
410                  */
411                 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
412                 if (val1 & 0x02) {
413                         val1 &= 0xfd;
414                         pci_write_config_byte (i8xx_tco_pci, 0xd4, val1);
415                         pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
416                         if (val1 & 0x02) {
417                                 printk (KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
418                                 return 0;       /* Cannot reset NO_REBOOT bit */
419                         }
420                 }
421                 /* Set the TCO_EN bit in SMI_EN register */
422                 val1 = inb (SMI_EN + 1);
423                 val1 &= 0xdf;
424                 outb (val1, SMI_EN + 1);
425                 /* Clear out the (probably old) status */
426                 outb (0, TCO1_STS);
427                 outb (3, TCO2_STS);
428                 return 1;
429         }
430         return 0;
431 }
432
433 static int __init watchdog_init (void)
434 {
435         int ret;
436
437         spin_lock_init(&tco_lock);
438
439         /* Check whether or not the hardware watchdog is there */
440         if (!i8xx_tco_getdevice () || i8xx_tco_pci == NULL)
441                 return -ENODEV;
442
443         if (!request_region (TCOBASE, 0x10, "i8xx TCO")) {
444                 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
445                         TCOBASE);
446                 ret = -EIO;
447                 goto out;
448         }
449
450         /* Check that the heartbeat value is within it's range ; if not reset to the default */
451         if (tco_timer_set_heartbeat (heartbeat)) {
452                 heartbeat = WATCHDOG_HEARTBEAT;
453                 tco_timer_set_heartbeat (heartbeat);
454                 printk(KERN_INFO PFX "heartbeat value must be 2<heartbeat<39, using %d\n",
455                         heartbeat);
456         }
457
458         ret = register_reboot_notifier(&i8xx_tco_notifier);
459         if (ret != 0) {
460                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
461                         ret);
462                 goto unreg_region;
463         }
464
465         ret = misc_register(&i8xx_tco_miscdev);
466         if (ret != 0) {
467                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
468                         WATCHDOG_MINOR, ret);
469                 goto unreg_notifier;
470         }
471
472         tco_timer_keepalive ();
473
474         printk (KERN_INFO PFX "initialized (0x%04x). heartbeat=%d sec (nowayout=%d)\n",
475                 TCOBASE, heartbeat, nowayout);
476
477         return 0;
478
479 unreg_notifier:
480         unregister_reboot_notifier(&i8xx_tco_notifier);
481 unreg_region:
482         release_region (TCOBASE, 0x10);
483 out:
484         return ret;
485 }
486
487 static void __exit watchdog_cleanup (void)
488 {
489         u8 val;
490
491         /* Stop the timer before we leave */
492         if (!nowayout)
493                 tco_timer_stop ();
494
495         /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
496         pci_read_config_byte (i8xx_tco_pci, 0xd4, &val);
497         val |= 0x02;
498         pci_write_config_byte (i8xx_tco_pci, 0xd4, val);
499
500         /* Deregister */
501         misc_deregister (&i8xx_tco_miscdev);
502         unregister_reboot_notifier(&i8xx_tco_notifier);
503         release_region (TCOBASE, 0x10);
504 }
505
506 module_init(watchdog_init);
507 module_exit(watchdog_cleanup);
508
509 MODULE_AUTHOR("Nils Faerber");
510 MODULE_DESCRIPTION("TCO timer driver for i8xx chipsets");
511 MODULE_LICENSE("GPL");
512 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);