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