fa2ba9ebe42aacf3478212143547c460d877402e
[deliverable/linux.git] / drivers / char / watchdog / i8xx_tco.c
1 /*
2 * i8xx_tco: 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 (ICH) : document number 290655-003, 290677-014,
26 * 82801AB (ICHO) : document number 290655-003, 290677-014,
27 * 82801BA (ICH2) : document number 290687-002, 298242-027,
28 * 82801BAM (ICH2-M) : document number 290687-002, 298242-027,
29 * 82801CA (ICH3-S) : document number 290733-003, 290739-013,
30 * 82801CAM (ICH3-M) : document number 290716-001, 290718-007,
31 * 82801DB (ICH4) : document number 290744-001, 290745-020,
32 * 82801DBM (ICH4-M) : document number 252337-001, 252663-005,
33 * 82801E (C-ICH) : document number 273599-001, 273645-002,
34 * 82801EB (ICH5) : document number 252516-001, 252517-003,
35 * 82801ER (ICH5R) : document number 252516-001, 252517-003,
36 *
37 * 20000710 Nils Faerber
38 * Initial Version 0.01
39 * 20000728 Nils Faerber
40 * 0.02 Fix for SMI_EN->TCO_EN bit, some cleanups
41 * 20011214 Matt Domsch <Matt_Domsch@dell.com>
42 * 0.03 Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
43 * Didn't add timeout option as i810_margin already exists.
44 * 20020224 Joel Becker, Wim Van Sebroeck
45 * 0.04 Support for 82801CA(M) chipset, timer margin needs to be > 3,
46 * add support for WDIOC_SETTIMEOUT and WDIOC_GETTIMEOUT.
47 * 20020412 Rob Radez <rob@osinvestor.com>, Wim Van Sebroeck
48 * 0.05 Fix possible timer_alive race, add expect close support,
49 * clean up ioctls (WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS and
50 * WDIOC_SETOPTIONS), made i810tco_getdevice __init,
51 * removed boot_status, removed tco_timer_read,
52 * added support for 82801DB and 82801E chipset,
53 * added support for 82801EB and 8280ER chipset,
54 * general cleanup.
55 * 20030921 Wim Van Sebroeck <wim@iguana.be>
56 * 0.06 change i810_margin to heartbeat, use module_param,
57 * added notify system support, renamed module to i8xx_tco.
58 * 20050128 Wim Van Sebroeck <wim@iguana.be>
59 * 0.07 Added support for the ICH4-M, ICH6, ICH6R, ICH6-M, ICH6W and ICH6RW
60 * chipsets. Also added support for the "undocumented" ICH7 chipset.
61 * 20050807 Wim Van Sebroeck <wim@iguana.be>
62 * 0.08 Make sure that the watchdog is only "armed" when started.
63 * (Kernel Bug 4251)
64 * 20060416 Wim Van Sebroeck <wim@iguana.be>
65 * 0.09 Remove support for the ICH6, ICH6R, ICH6-M, ICH6W and ICH6RW and
66 * ICH7 chipsets. (See Kernel Bug 6031 - other code will support these
67 * chipsets)
68 */
69
70 /*
71 * Includes, defines, variables, module parameters, ...
72 */
73
74 #include <linux/module.h>
75 #include <linux/moduleparam.h>
76 #include <linux/types.h>
77 #include <linux/miscdevice.h>
78 #include <linux/watchdog.h>
79 #include <linux/notifier.h>
80 #include <linux/reboot.h>
81 #include <linux/init.h>
82 #include <linux/fs.h>
83 #include <linux/pci.h>
84 #include <linux/ioport.h>
85
86 #include <asm/uaccess.h>
87 #include <asm/io.h>
88
89 #include "i8xx_tco.h"
90
91 /* Module and version information */
92 #define TCO_VERSION "0.09"
93 #define TCO_MODULE_NAME "i8xx TCO timer"
94 #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION
95 #define PFX TCO_MODULE_NAME ": "
96
97 /* internal variables */
98 static unsigned int ACPIBASE;
99 static spinlock_t tco_lock; /* Guards the hardware */
100 static unsigned long timer_alive;
101 static char tco_expect_close;
102 static struct pci_dev *i8xx_tco_pci;
103
104 /* module parameters */
105 #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat (2<heartbeat<39) */
106 static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
107 module_param(heartbeat, int, 0);
108 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<heartbeat<39, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
109
110 static int nowayout = WATCHDOG_NOWAYOUT;
111 module_param(nowayout, int, 0);
112 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
113
114 /*
115 * Some TCO specific functions
116 */
117
118 static inline unsigned char seconds_to_ticks(int seconds)
119 {
120 /* the internal timer is stored as ticks which decrement
121 * every 0.6 seconds */
122 return (seconds * 10) / 6;
123 }
124
125 static int tco_timer_start (void)
126 {
127 unsigned char val;
128
129 spin_lock(&tco_lock);
130
131 /* disable chipset's NO_REBOOT bit */
132 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val);
133 val &= 0xfd;
134 pci_write_config_byte (i8xx_tco_pci, 0xd4, val);
135
136 /* Bit 11: TCO Timer Halt -> 0 = The TCO timer is enabled to count */
137 val = inb (TCO1_CNT + 1);
138 val &= 0xf7;
139 outb (val, TCO1_CNT + 1);
140 val = inb (TCO1_CNT + 1);
141
142 spin_unlock(&tco_lock);
143
144 if (val & 0x08)
145 return -1;
146 return 0;
147 }
148
149 static int tco_timer_stop (void)
150 {
151 unsigned char val, val1;
152
153 spin_lock(&tco_lock);
154 /* Bit 11: TCO Timer Halt -> 1 = The TCO timer is disabled */
155 val = inb (TCO1_CNT + 1);
156 val |= 0x08;
157 outb (val, TCO1_CNT + 1);
158 val = inb (TCO1_CNT + 1);
159
160 /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
161 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
162 val1 |= 0x02;
163 pci_write_config_byte (i8xx_tco_pci, 0xd4, val1);
164
165 spin_unlock(&tco_lock);
166
167 if ((val & 0x08) == 0)
168 return -1;
169 return 0;
170 }
171
172 static int tco_timer_keepalive (void)
173 {
174 spin_lock(&tco_lock);
175 /* Reload the timer by writing to the TCO Timer Reload register */
176 outb (0x01, TCO1_RLD);
177 spin_unlock(&tco_lock);
178 return 0;
179 }
180
181 static int tco_timer_set_heartbeat (int t)
182 {
183 unsigned char val;
184 unsigned char tmrval;
185
186 tmrval = seconds_to_ticks(t);
187 /* from the specs: */
188 /* "Values of 0h-3h are ignored and should not be attempted" */
189 if (tmrval > 0x3f || tmrval < 0x04)
190 return -EINVAL;
191
192 /* Write new heartbeat to watchdog */
193 spin_lock(&tco_lock);
194 val = inb (TCO1_TMR);
195 val &= 0xc0;
196 val |= tmrval;
197 outb (val, TCO1_TMR);
198 val = inb (TCO1_TMR);
199 spin_unlock(&tco_lock);
200
201 if ((val & 0x3f) != tmrval)
202 return -EINVAL;
203
204 heartbeat = t;
205 return 0;
206 }
207
208 /*
209 * /dev/watchdog handling
210 */
211
212 static int i8xx_tco_open (struct inode *inode, struct file *file)
213 {
214 /* /dev/watchdog can only be opened once */
215 if (test_and_set_bit(0, &timer_alive))
216 return -EBUSY;
217
218 /*
219 * Reload and activate timer
220 */
221 tco_timer_keepalive ();
222 tco_timer_start ();
223 return nonseekable_open(inode, file);
224 }
225
226 static int i8xx_tco_release (struct inode *inode, struct file *file)
227 {
228 /*
229 * Shut off the timer.
230 */
231 if (tco_expect_close == 42) {
232 tco_timer_stop ();
233 } else {
234 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
235 tco_timer_keepalive ();
236 }
237 clear_bit(0, &timer_alive);
238 tco_expect_close = 0;
239 return 0;
240 }
241
242 static ssize_t i8xx_tco_write (struct file *file, const char __user *data,
243 size_t len, loff_t * ppos)
244 {
245 /* See if we got the magic character 'V' and reload the timer */
246 if (len) {
247 if (!nowayout) {
248 size_t i;
249
250 /* note: just in case someone wrote the magic character
251 * five months ago... */
252 tco_expect_close = 0;
253
254 /* scan to see whether or not we got the magic character */
255 for (i = 0; i != len; i++) {
256 char c;
257 if(get_user(c, data+i))
258 return -EFAULT;
259 if (c == 'V')
260 tco_expect_close = 42;
261 }
262 }
263
264 /* someone wrote to us, we should reload the timer */
265 tco_timer_keepalive ();
266 }
267 return len;
268 }
269
270 static int i8xx_tco_ioctl (struct inode *inode, struct file *file,
271 unsigned int cmd, unsigned long arg)
272 {
273 int new_options, retval = -EINVAL;
274 int new_heartbeat;
275 void __user *argp = (void __user *)arg;
276 int __user *p = argp;
277 static struct watchdog_info ident = {
278 .options = WDIOF_SETTIMEOUT |
279 WDIOF_KEEPALIVEPING |
280 WDIOF_MAGICCLOSE,
281 .firmware_version = 0,
282 .identity = TCO_MODULE_NAME,
283 };
284
285 switch (cmd) {
286 case WDIOC_GETSUPPORT:
287 return copy_to_user(argp, &ident,
288 sizeof (ident)) ? -EFAULT : 0;
289
290 case WDIOC_GETSTATUS:
291 case WDIOC_GETBOOTSTATUS:
292 return put_user (0, p);
293
294 case WDIOC_KEEPALIVE:
295 tco_timer_keepalive ();
296 return 0;
297
298 case WDIOC_SETOPTIONS:
299 {
300 if (get_user (new_options, p))
301 return -EFAULT;
302
303 if (new_options & WDIOS_DISABLECARD) {
304 tco_timer_stop ();
305 retval = 0;
306 }
307
308 if (new_options & WDIOS_ENABLECARD) {
309 tco_timer_keepalive ();
310 tco_timer_start ();
311 retval = 0;
312 }
313
314 return retval;
315 }
316
317 case WDIOC_SETTIMEOUT:
318 {
319 if (get_user(new_heartbeat, p))
320 return -EFAULT;
321
322 if (tco_timer_set_heartbeat(new_heartbeat))
323 return -EINVAL;
324
325 tco_timer_keepalive ();
326 /* Fall */
327 }
328
329 case WDIOC_GETTIMEOUT:
330 return put_user(heartbeat, p);
331
332 default:
333 return -ENOIOCTLCMD;
334 }
335 }
336
337 /*
338 * Notify system
339 */
340
341 static int i8xx_tco_notify_sys (struct notifier_block *this, unsigned long code, void *unused)
342 {
343 if (code==SYS_DOWN || code==SYS_HALT) {
344 /* Turn the WDT off */
345 tco_timer_stop ();
346 }
347
348 return NOTIFY_DONE;
349 }
350
351 /*
352 * Kernel Interfaces
353 */
354
355 static struct file_operations i8xx_tco_fops = {
356 .owner = THIS_MODULE,
357 .llseek = no_llseek,
358 .write = i8xx_tco_write,
359 .ioctl = i8xx_tco_ioctl,
360 .open = i8xx_tco_open,
361 .release = i8xx_tco_release,
362 };
363
364 static struct miscdevice i8xx_tco_miscdev = {
365 .minor = WATCHDOG_MINOR,
366 .name = "watchdog",
367 .fops = &i8xx_tco_fops,
368 };
369
370 static struct notifier_block i8xx_tco_notifier = {
371 .notifier_call = i8xx_tco_notify_sys,
372 };
373
374 /*
375 * Data for PCI driver interface
376 *
377 * This data only exists for exporting the supported
378 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
379 * register a pci_driver, because someone else might one day
380 * want to register another driver on the same PCI id.
381 */
382 static struct pci_device_id i8xx_tco_pci_tbl[] = {
383 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, PCI_ANY_ID, PCI_ANY_ID, },
384 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_0, PCI_ANY_ID, PCI_ANY_ID, },
385 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, PCI_ANY_ID, PCI_ANY_ID, },
386 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_10, PCI_ANY_ID, PCI_ANY_ID, },
387 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, PCI_ANY_ID, PCI_ANY_ID, },
388 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, PCI_ANY_ID, PCI_ANY_ID, },
389 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, PCI_ANY_ID, PCI_ANY_ID, },
390 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12, PCI_ANY_ID, PCI_ANY_ID, },
391 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801E_0, PCI_ANY_ID, PCI_ANY_ID, },
392 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, PCI_ANY_ID, PCI_ANY_ID, },
393 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_1, PCI_ANY_ID, PCI_ANY_ID, },
394 { 0, }, /* End of list */
395 };
396 MODULE_DEVICE_TABLE (pci, i8xx_tco_pci_tbl);
397
398 /*
399 * Init & exit routines
400 */
401
402 static unsigned char __init i8xx_tco_getdevice (void)
403 {
404 struct pci_dev *dev = NULL;
405 u8 val1, val2;
406 u16 badr;
407 /*
408 * Find the PCI device
409 */
410
411 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
412 if (pci_match_id(i8xx_tco_pci_tbl, dev)) {
413 i8xx_tco_pci = dev;
414 break;
415 }
416 }
417
418 if (i8xx_tco_pci) {
419 /*
420 * Find the ACPI base I/O address which is the base
421 * for the TCO registers (TCOBASE=ACPIBASE + 0x60)
422 * ACPIBASE is bits [15:7] from 0x40-0x43
423 */
424 pci_read_config_byte (i8xx_tco_pci, 0x40, &val1);
425 pci_read_config_byte (i8xx_tco_pci, 0x41, &val2);
426 badr = ((val2 << 1) | (val1 >> 7)) << 7;
427 ACPIBASE = badr;
428 /* Something's wrong here, ACPIBASE has to be set */
429 if (badr == 0x0001 || badr == 0x0000) {
430 printk (KERN_ERR PFX "failed to get TCOBASE address\n");
431 return 0;
432 }
433
434 /* Check chipset's NO_REBOOT bit */
435 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
436 if (val1 & 0x02) {
437 val1 &= 0xfd;
438 pci_write_config_byte (i8xx_tco_pci, 0xd4, val1);
439 pci_read_config_byte (i8xx_tco_pci, 0xd4, &val1);
440 if (val1 & 0x02) {
441 printk (KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
442 return 0; /* Cannot reset NO_REBOOT bit */
443 }
444 }
445 /* Disable reboots untill the watchdog starts */
446 val1 |= 0x02;
447 pci_write_config_byte (i8xx_tco_pci, 0xd4, val1);
448
449 /* Set the TCO_EN bit in SMI_EN register */
450 if (!request_region (SMI_EN + 1, 1, "i8xx TCO")) {
451 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
452 SMI_EN + 1);
453 return 0;
454 }
455 val1 = inb (SMI_EN + 1);
456 val1 &= 0xdf;
457 outb (val1, SMI_EN + 1);
458 release_region (SMI_EN + 1, 1);
459 return 1;
460 }
461 return 0;
462 }
463
464 static int __init watchdog_init (void)
465 {
466 int ret;
467
468 spin_lock_init(&tco_lock);
469
470 /* Check whether or not the hardware watchdog is there */
471 if (!i8xx_tco_getdevice () || i8xx_tco_pci == NULL)
472 return -ENODEV;
473
474 if (!request_region (TCOBASE, 0x10, "i8xx TCO")) {
475 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
476 TCOBASE);
477 ret = -EIO;
478 goto out;
479 }
480
481 /* Clear out the (probably old) status */
482 outb (0, TCO1_STS);
483 outb (3, TCO2_STS);
484
485 /* Check that the heartbeat value is within it's range ; if not reset to the default */
486 if (tco_timer_set_heartbeat (heartbeat)) {
487 heartbeat = WATCHDOG_HEARTBEAT;
488 tco_timer_set_heartbeat (heartbeat);
489 printk(KERN_INFO PFX "heartbeat value must be 2<heartbeat<39, using %d\n",
490 heartbeat);
491 }
492
493 ret = register_reboot_notifier(&i8xx_tco_notifier);
494 if (ret != 0) {
495 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
496 ret);
497 goto unreg_region;
498 }
499
500 ret = misc_register(&i8xx_tco_miscdev);
501 if (ret != 0) {
502 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
503 WATCHDOG_MINOR, ret);
504 goto unreg_notifier;
505 }
506
507 tco_timer_stop ();
508
509 printk (KERN_INFO PFX "initialized (0x%04x). heartbeat=%d sec (nowayout=%d)\n",
510 TCOBASE, heartbeat, nowayout);
511
512 return 0;
513
514 unreg_notifier:
515 unregister_reboot_notifier(&i8xx_tco_notifier);
516 unreg_region:
517 release_region (TCOBASE, 0x10);
518 out:
519 return ret;
520 }
521
522 static void __exit watchdog_cleanup (void)
523 {
524 /* Stop the timer before we leave */
525 if (!nowayout)
526 tco_timer_stop ();
527
528 /* Deregister */
529 misc_deregister (&i8xx_tco_miscdev);
530 unregister_reboot_notifier(&i8xx_tco_notifier);
531 release_region (TCOBASE, 0x10);
532 }
533
534 module_init(watchdog_init);
535 module_exit(watchdog_cleanup);
536
537 MODULE_AUTHOR("Nils Faerber");
538 MODULE_DESCRIPTION("TCO timer driver for i8xx chipsets");
539 MODULE_LICENSE("GPL");
540 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
This page took 0.043051 seconds and 4 git commands to generate.