2 * i6300esb 0.03: Watchdog timer driver for Intel 6300ESB chipset
4 * (c) Copyright 2004 Google Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * based on i810-tco.c which is
13 * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>
15 * Jentro AG, Haar/Munich (Germany)
17 * which is in turn based on softdog.c by Alan Cox <alan@redhat.com>
19 * The timer is implemented in the following I/O controller hubs:
20 * (See the intel documentation on http://developer.intel.com.)
21 * 6300ESB chip : document number 300641-003
24 * Initial version 0.01
27 * 20050210 David Härdeman <david@2gen.com>
28 * Ported driver to kernel 2.6
32 * Includes, defines, variables, module parameters, ...
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
40 #include <linux/miscdevice.h>
41 #include <linux/watchdog.h>
42 #include <linux/reboot.h>
43 #include <linux/init.h>
44 #include <linux/pci.h>
45 #include <linux/ioport.h>
47 #include <asm/uaccess.h>
52 /* Module and version information */
53 #define ESB_VERSION "0.03"
54 #define ESB_MODULE_NAME "i6300ESB timer"
55 #define ESB_DRIVER_NAME ESB_MODULE_NAME ", v" ESB_VERSION
56 #define PFX ESB_MODULE_NAME ": "
58 /* internal variables */
59 static void __iomem
*BASEADDR
;
60 static spinlock_t esb_lock
; /* Guards the hardware */
61 static unsigned long timer_alive
;
62 static struct pci_dev
*esb_pci
;
63 static unsigned short triggered
; /* The status of the watchdog upon boot */
64 static char esb_expect_close
;
66 /* module parameters */
67 #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat (1<heartbeat<2*1023) */
68 static int heartbeat
= WATCHDOG_HEARTBEAT
; /* in seconds */
69 module_param(heartbeat
, int, 0);
70 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeat in seconds. (1<heartbeat<2046, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT
) ")");
72 #ifdef CONFIG_WATCHDOG_NOWAYOUT
73 static int nowayout
= 1;
75 static int nowayout
= 0;
77 module_param(nowayout
, int, 0);
78 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
81 * Some i6300ESB specific functions
85 * Prepare for reloading the timer by unlocking the proper registers.
86 * This is performed by first writing 0x80 followed by 0x86 to the
87 * reload register. After this the appropriate registers can be written
88 * to once before they need to be unlocked again.
90 static inline void esb_unlock_registers(void) {
91 writeb(ESB_UNLOCK1
, ESB_RELOAD_REG
);
92 writeb(ESB_UNLOCK2
, ESB_RELOAD_REG
);
95 static void esb_timer_start(void)
99 /* Enable or Enable + Lock? */
100 val
= 0x02 | (nowayout
? 0x01 : 0x00);
102 pci_write_config_byte(esb_pci
, ESB_LOCK_REG
, val
);
105 static int esb_timer_stop(void)
109 spin_lock(&esb_lock
);
110 /* First, reset timers as suggested by the docs */
111 esb_unlock_registers();
112 writew(ESB_WDT_RELOAD
, ESB_RELOAD_REG
);
113 /* Then disable the WDT */
114 pci_write_config_byte(esb_pci
, ESB_LOCK_REG
, 0x0);
115 pci_read_config_byte(esb_pci
, ESB_LOCK_REG
, &val
);
116 spin_unlock(&esb_lock
);
118 /* Returns 0 if the timer was disabled, non-zero otherwise */
122 static void esb_timer_keepalive(void)
124 spin_lock(&esb_lock
);
125 esb_unlock_registers();
126 writew(ESB_WDT_RELOAD
, ESB_RELOAD_REG
);
127 /* FIXME: Do we need to flush anything here? */
128 spin_unlock(&esb_lock
);
131 static int esb_timer_set_heartbeat(int time
)
135 if (time
< 0x1 || time
> (2 * 0x03ff))
138 spin_lock(&esb_lock
);
140 /* We shift by 9, so if we are passed a value of 1 sec,
141 * val will be 1 << 9 = 512, then write that to two
142 * timers => 2 * 512 = 1024 (which is decremented at 1KHz)
147 esb_unlock_registers();
148 writel(val
, ESB_TIMER1_REG
);
151 esb_unlock_registers();
152 writel(val
, ESB_TIMER2_REG
);
155 esb_unlock_registers();
156 writew(ESB_WDT_RELOAD
, ESB_RELOAD_REG
);
158 /* FIXME: Do we need to flush everything out? */
162 spin_unlock(&esb_lock
);
166 static int esb_timer_read (void)
170 /* This isn't documented, and doesn't take into
171 * acount which stage is running, but it looks
172 * like a 20 bit count down, so we might as well report it.
174 pci_read_config_dword(esb_pci
, 0x64, &count
);
179 * /dev/watchdog handling
182 static int esb_open (struct inode
*inode
, struct file
*file
)
184 /* /dev/watchdog can only be opened once */
185 if (test_and_set_bit(0, &timer_alive
))
188 /* Reload and activate timer */
189 esb_timer_keepalive ();
192 return nonseekable_open(inode
, file
);
195 static int esb_release (struct inode
*inode
, struct file
*file
)
197 /* Shut off the timer. */
198 if (esb_expect_close
== 42) {
201 printk(KERN_CRIT PFX
"Unexpected close, not stopping watchdog!\n");
202 esb_timer_keepalive ();
204 clear_bit(0, &timer_alive
);
205 esb_expect_close
= 0;
209 static ssize_t
esb_write (struct file
*file
, const char __user
*data
,
210 size_t len
, loff_t
* ppos
)
212 /* See if we got the magic character 'V' and reload the timer */
217 /* note: just in case someone wrote the magic character
218 * five months ago... */
219 esb_expect_close
= 0;
221 /* scan to see whether or not we got the magic character */
222 for (i
= 0; i
!= len
; i
++) {
224 if(get_user(c
, data
+i
))
227 esb_expect_close
= 42;
231 /* someone wrote to us, we should reload the timer */
232 esb_timer_keepalive ();
237 static int esb_ioctl (struct inode
*inode
, struct file
*file
,
238 unsigned int cmd
, unsigned long arg
)
240 int new_options
, retval
= -EINVAL
;
242 void __user
*argp
= (void __user
*)arg
;
243 int __user
*p
= argp
;
244 static struct watchdog_info ident
= {
245 .options
= WDIOF_SETTIMEOUT
|
246 WDIOF_KEEPALIVEPING
|
248 .firmware_version
= 0,
249 .identity
= ESB_MODULE_NAME
,
253 case WDIOC_GETSUPPORT
:
254 return copy_to_user(argp
, &ident
,
255 sizeof (ident
)) ? -EFAULT
: 0;
257 case WDIOC_GETSTATUS
:
258 return put_user (esb_timer_read(), p
);
260 case WDIOC_GETBOOTSTATUS
:
261 return put_user (triggered
, p
);
263 case WDIOC_KEEPALIVE
:
264 esb_timer_keepalive ();
267 case WDIOC_SETOPTIONS
:
269 if (get_user (new_options
, p
))
272 if (new_options
& WDIOS_DISABLECARD
) {
277 if (new_options
& WDIOS_ENABLECARD
) {
278 esb_timer_keepalive ();
286 case WDIOC_SETTIMEOUT
:
288 if (get_user(new_heartbeat
, p
))
291 if (esb_timer_set_heartbeat(new_heartbeat
))
294 esb_timer_keepalive ();
298 case WDIOC_GETTIMEOUT
:
299 return put_user(heartbeat
, p
);
310 static int esb_notify_sys (struct notifier_block
*this, unsigned long code
, void *unused
)
312 if (code
==SYS_DOWN
|| code
==SYS_HALT
) {
313 /* Turn the WDT off */
324 static struct file_operations esb_fops
= {
325 .owner
= THIS_MODULE
,
330 .release
= esb_release
,
333 static struct miscdevice esb_miscdev
= {
334 .minor
= WATCHDOG_MINOR
,
339 static struct notifier_block esb_notifier
= {
340 .notifier_call
= esb_notify_sys
,
344 * Data for PCI driver interface
346 * This data only exists for exporting the supported
347 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
348 * register a pci_driver, because someone else might one day
349 * want to register another driver on the same PCI id.
351 static struct pci_device_id esb_pci_tbl
[] = {
352 { PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_ESB_9
, PCI_ANY_ID
, PCI_ANY_ID
, },
353 { 0, }, /* End of list */
355 MODULE_DEVICE_TABLE (pci
, esb_pci_tbl
);
358 * Init & exit routines
361 static unsigned char __init
esb_getdevice (void)
366 struct pci_dev
*dev
= NULL
;
368 * Find the PCI device
371 for_each_pci_dev(dev
)
372 if (pci_match_device(esb_pci_tbl
, dev
)) {
378 if (pci_enable_device(esb_pci
)) {
379 printk (KERN_ERR PFX
"failed to enable device\n");
383 if (pci_request_region(esb_pci
, 0, ESB_MODULE_NAME
)) {
384 printk (KERN_ERR PFX
"failed to request region\n");
388 BASEADDR
= ioremap(pci_resource_start(esb_pci
, 0),
389 pci_resource_len(esb_pci
, 0));
390 if (BASEADDR
== NULL
) {
391 /* Something's wrong here, BASEADDR has to be set */
392 printk (KERN_ERR PFX
"failed to get BASEADDR\n");
397 * The watchdog has two timers, it can be setup so that the
398 * expiry of timer1 results in an interrupt and the expiry of
399 * timer2 results in a reboot. We set it to not generate
400 * any interrupts as there is not much we can do with it
403 * We also enable reboots and set the timer frequency to
404 * the PCI clock divided by 2^15 (approx 1KHz).
406 pci_write_config_word(esb_pci
, ESB_CONFIG_REG
, 0x0003);
408 /* Check that the WDT isn't already locked */
409 pci_read_config_byte(esb_pci
, ESB_LOCK_REG
, &val1
);
410 if (val1
& ESB_WDT_LOCK
)
411 printk (KERN_WARNING PFX
"nowayout already set\n");
413 /* Set the timer to watchdog mode and disable it for now */
414 pci_write_config_byte(esb_pci
, ESB_LOCK_REG
, 0x00);
416 /* Check if the watchdog was previously triggered */
417 esb_unlock_registers();
418 val2
= readw(ESB_RELOAD_REG
);
419 triggered
= (val2
& (0x01 << 9) >> 9);
421 /* Reset trigger flag and timers */
422 esb_unlock_registers();
423 writew((0x11 << 8), ESB_RELOAD_REG
);
429 pci_release_region(esb_pci
, 0);
431 pci_disable_device(esb_pci
);
432 pci_dev_put(esb_pci
);
438 static int __init
watchdog_init (void)
442 spin_lock_init(&esb_lock
);
444 /* Check whether or not the hardware watchdog is there */
445 if (!esb_getdevice () || esb_pci
== NULL
)
448 /* Check that the heartbeat value is within it's range ; if not reset to the default */
449 if (esb_timer_set_heartbeat (heartbeat
)) {
450 esb_timer_set_heartbeat (WATCHDOG_HEARTBEAT
);
451 printk(KERN_INFO PFX
"heartbeat value must be 1<heartbeat<2046, using %d\n",
455 ret
= register_reboot_notifier(&esb_notifier
);
457 printk(KERN_ERR PFX
"cannot register reboot notifier (err=%d)\n",
462 ret
= misc_register(&esb_miscdev
);
464 printk(KERN_ERR PFX
"cannot register miscdev on minor=%d (err=%d)\n",
465 WATCHDOG_MINOR
, ret
);
471 printk (KERN_INFO PFX
"initialized (0x%p). heartbeat=%d sec (nowayout=%d)\n",
472 BASEADDR
, heartbeat
, nowayout
);
477 unregister_reboot_notifier(&esb_notifier
);
481 pci_release_region(esb_pci
, 0);
483 pci_disable_device(esb_pci
);
484 pci_dev_put(esb_pci
);
489 static void __exit
watchdog_cleanup (void)
491 /* Stop the timer before we leave */
496 misc_deregister(&esb_miscdev
);
497 unregister_reboot_notifier(&esb_notifier
);
499 pci_release_region(esb_pci
, 0);
500 pci_disable_device(esb_pci
);
501 pci_dev_put(esb_pci
);
504 module_init(watchdog_init
);
505 module_exit(watchdog_cleanup
);
507 MODULE_AUTHOR("Ross Biro and David Härdeman");
508 MODULE_DESCRIPTION("Watchdog driver for Intel 6300ESB chipsets");
509 MODULE_LICENSE("GPL");
510 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);