2 * ALi M7101 PMU Computer Watchdog Timer driver
4 * Based on w83877f_wdt.c by Scott Jennings <linuxdrivers@oro.net>
5 * and the Cobalt kernel WDT timer driver by Tim Hockin
6 * <thockin@cobaltnet.com>
8 * (c)2002 Steve Hill <steve@navaho.co.uk>
10 * This WDT driver is different from most other Linux WDT
11 * drivers in that the driver will ping the watchdog by itself,
12 * because this particular WDT has a very short timeout (1.6
13 * seconds) and it would be insane to count on any userspace
14 * daemon always getting scheduled within that time frame.
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/types.h>
20 #include <linux/timer.h>
21 #include <linux/miscdevice.h>
22 #include <linux/watchdog.h>
23 #include <linux/ioport.h>
24 #include <linux/notifier.h>
25 #include <linux/reboot.h>
26 #include <linux/init.h>
28 #include <linux/pci.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
34 #define OUR_NAME "alim7101_wdt"
35 #define PFX OUR_NAME ": "
37 #define WDT_ENABLE 0x9C
38 #define WDT_DISABLE 0x8C
40 #define ALI_7101_WDT 0x92
41 #define ALI_WDT_ARM 0x01
44 * We're going to use a 1 second timeout.
45 * If we reset the watchdog every ~250ms we should be safe. */
47 #define WDT_INTERVAL (HZ/4+1)
50 * We must not require too good response from the userspace daemon.
51 * Here we require the userspace daemon to send us a heartbeat
52 * char to /dev/watchdog every 30 seconds.
55 #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
56 static int timeout = WATCHDOG_TIMEOUT; /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
57 module_param(timeout, int, 0);
58 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
60 static void wdt_timer_ping(unsigned long);
61 static struct timer_list timer;
62 static unsigned long next_heartbeat;
63 static unsigned long wdt_is_open;
64 static char wdt_expect_close;
65 static struct pci_dev *alim7101_pmu;
67 #ifdef CONFIG_WATCHDOG_NOWAYOUT
68 static int nowayout = 1;
70 static int nowayout = 0;
73 module_param(nowayout, int, 0);
74 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
80 static void wdt_timer_ping(unsigned long data)
82 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
83 * we agree to ping the WDT
87 if(time_before(jiffies, next_heartbeat))
89 /* Ping the WDT (this is actually a disarm/arm sequence) */
90 pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
91 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
92 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
94 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
96 /* Re-set the timer interval */
97 timer.expires = jiffies + WDT_INTERVAL;
105 static void wdt_change(int writeval)
109 pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
110 if (writeval == WDT_ENABLE)
111 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
113 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
116 static void wdt_startup(void)
118 next_heartbeat = jiffies + (timeout * HZ);
120 /* We must enable before we kick off the timer in case the timer
121 occurs as we ping it */
123 wdt_change(WDT_ENABLE);
125 /* Start the timer */
126 timer.expires = jiffies + WDT_INTERVAL;
130 printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
133 static void wdt_turnoff(void)
136 del_timer_sync(&timer);
137 wdt_change(WDT_DISABLE);
138 printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
141 static void wdt_keepalive(void)
144 next_heartbeat = jiffies + (timeout * HZ);
148 * /dev/watchdog handling
151 static ssize_t fop_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos)
153 /* See if we got the magic character 'V' and reload the timer */
158 /* note: just in case someone wrote the magic character
159 * five months ago... */
160 wdt_expect_close = 0;
163 for (ofs = 0; ofs != count; ofs++) {
165 if (get_user(c, buf+ofs))
168 wdt_expect_close = 42;
171 /* someone wrote to us, we should restart timer */
177 static int fop_open(struct inode * inode, struct file * file)
179 /* Just in case we're already talking to someone... */
180 if(test_and_set_bit(0, &wdt_is_open))
182 /* Good, fire up the show */
184 return nonseekable_open(inode, file);
187 static int fop_close(struct inode * inode, struct file * file)
189 if(wdt_expect_close == 42)
192 /* wim: shouldn't there be a: del_timer(&timer); */
193 printk(KERN_CRIT PFX "device file closed unexpectedly. Will not stop the WDT!\n");
195 clear_bit(0, &wdt_is_open);
196 wdt_expect_close = 0;
200 static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
202 void __user *argp = (void __user *)arg;
203 int __user *p = argp;
204 static struct watchdog_info ident =
206 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
207 .firmware_version = 1,
208 .identity = "ALiM7101",
213 case WDIOC_GETSUPPORT:
214 return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
215 case WDIOC_GETSTATUS:
216 case WDIOC_GETBOOTSTATUS:
217 return put_user(0, p);
218 case WDIOC_KEEPALIVE:
221 case WDIOC_SETOPTIONS:
223 int new_options, retval = -EINVAL;
225 if(get_user(new_options, p))
228 if(new_options & WDIOS_DISABLECARD) {
233 if(new_options & WDIOS_ENABLECARD) {
240 case WDIOC_SETTIMEOUT:
244 if(get_user(new_timeout, p))
247 if(new_timeout < 1 || new_timeout > 3600) /* arbitrary upper limit */
250 timeout = new_timeout;
254 case WDIOC_GETTIMEOUT:
255 return put_user(timeout, p);
261 static struct file_operations wdt_fops = {
270 static struct miscdevice wdt_miscdev = {
271 .minor=WATCHDOG_MINOR,
277 * Notifier for system down
280 static int wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
282 if (code==SYS_DOWN || code==SYS_HALT)
285 if (code==SYS_RESTART) {
287 * Cobalt devices have no way of rebooting themselves other than
288 * getting the watchdog to pull reset, so we restart the watchdog on
289 * reboot with no heartbeat
291 wdt_change(WDT_ENABLE);
292 printk(KERN_INFO PFX "Watchdog timer is now enabled with no heartbeat - should reboot in ~1 second.\n");
298 * The WDT needs to learn about soft shutdowns in order to
299 * turn the timebomb registers off.
302 static struct notifier_block wdt_notifier=
304 .notifier_call = wdt_notify_sys,
307 static void __exit alim7101_wdt_unload(void)
311 misc_deregister(&wdt_miscdev);
312 unregister_reboot_notifier(&wdt_notifier);
315 static int __init alim7101_wdt_init(void)
318 struct pci_dev *ali1543_south;
321 printk(KERN_INFO PFX "Steve Hill <steve@navaho.co.uk>.\n");
322 alim7101_pmu = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,NULL);
324 printk(KERN_INFO PFX "ALi M7101 PMU not present - WDT not set\n");
328 /* Set the WDT in the PMU to 1 second */
329 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02);
331 ali1543_south = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
332 if (!ali1543_south) {
333 printk(KERN_INFO PFX "ALi 1543 South-Bridge not present - WDT not set\n");
336 pci_read_config_byte(ali1543_south, 0x5e, &tmp);
337 if ((tmp & 0x1e) != 0x12) {
338 printk(KERN_INFO PFX "ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n");
342 if(timeout < 1 || timeout > 3600) /* arbitrary upper limit */
344 timeout = WATCHDOG_TIMEOUT;
345 printk(KERN_INFO PFX "timeout value must be 1<=x<=3600, using %d\n",
350 timer.function = wdt_timer_ping;
353 rc = misc_register(&wdt_miscdev);
355 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
356 wdt_miscdev.minor, rc);
360 rc = register_reboot_notifier(&wdt_notifier);
362 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
364 goto err_out_miscdev;
367 printk(KERN_INFO PFX "WDT driver for ALi M7101 initialised. timeout=%d sec (nowayout=%d)\n",
372 misc_deregister(&wdt_miscdev);
377 module_init(alim7101_wdt_init);
378 module_exit(alim7101_wdt_unload);
380 MODULE_AUTHOR("Steve Hill");
381 MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver");
382 MODULE_LICENSE("GPL");
383 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);