2 * Watchdog for the 7101 PMU version found in the ALi M1535 chipsets
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/types.h>
13 #include <linux/miscdevice.h>
14 #include <linux/watchdog.h>
15 #include <linux/ioport.h>
16 #include <linux/notifier.h>
17 #include <linux/reboot.h>
18 #include <linux/init.h>
20 #include <linux/pci.h>
22 #include <asm/uaccess.h>
25 #define WATCHDOG_NAME "ALi_M1535"
26 #define PFX WATCHDOG_NAME ": "
27 #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
29 /* internal variables */
30 static unsigned long ali_is_open;
31 static char ali_expect_release;
32 static struct pci_dev *ali_pci;
33 static u32 ali_timeout_bits; /* stores the computed timeout */
34 static spinlock_t ali_lock; /* Guards the hardware */
36 /* module parameters */
37 static int timeout = WATCHDOG_TIMEOUT;
38 module_param(timeout, int, 0);
39 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (0<timeout<18000, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
41 #ifdef CONFIG_WATCHDOG_NOWAYOUT
42 static int nowayout = 1;
44 static int nowayout = 0;
47 module_param(nowayout, int, 0);
48 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
51 * ali_start - start watchdog countdown
53 * Starts the timer running providing the timer has a counter
57 static void ali_start(void)
63 pci_read_config_dword(ali_pci, 0xCC, &val);
64 val &= ~0x3F; /* Mask count */
65 val |= (1<<25) | ali_timeout_bits;
66 pci_write_config_dword(ali_pci, 0xCC, val);
68 spin_unlock(&ali_lock);
72 * ali_stop - stop the timer countdown
74 * Stop the ALi watchdog countdown
77 static void ali_stop(void)
83 pci_read_config_dword(ali_pci, 0xCC, &val);
84 val &= ~0x3F; /* Mask count to zero (disabled) */
85 val &= ~(1<<25);/* and for safety mask the reset enable */
86 pci_write_config_dword(ali_pci, 0xCC, val);
88 spin_unlock(&ali_lock);
92 * ali_keepalive - send a keepalive to the watchdog
94 * Send a keepalive to the timer (actually we restart the timer).
97 static void ali_keepalive(void)
103 * ali_settimer - compute the timer reload value
104 * @t: time in seconds
106 * Computes the timeout values needed
109 static int ali_settimer(int t)
114 ali_timeout_bits = t|(1<<6);
116 ali_timeout_bits = (t/60)|(1<<7);
118 ali_timeout_bits = (t/300)|(1<<6)|(1<<7);
126 * /dev/watchdog handling
130 * ali_write - writes to ALi watchdog
131 * @file: file from VFS
132 * @data: user address of data
133 * @len: length of data
134 * @ppos: pointer to the file offset
136 * Handle a write to the ALi watchdog. Writing to the file pings
137 * the watchdog and resets it. Writing the magic 'V' sequence allows
138 * the next close to turn off the watchdog.
141 static ssize_t ali_write(struct file *file, const char __user *data,
142 size_t len, loff_t * ppos)
144 /* Can't seek (pwrite) on this device */
145 if (ppos != &file->f_pos)
148 /* See if we got the magic character 'V' and reload the timer */
153 /* note: just in case someone wrote the magic character
154 * five months ago... */
155 ali_expect_release = 0;
157 /* scan to see whether or not we got the magic character */
158 for (i = 0; i != len; i++) {
160 if(get_user(c, data+i))
163 ali_expect_release = 42;
167 /* someone wrote to us, we should reload the timer */
174 * ali_ioctl - handle watchdog ioctls
176 * @file: VFS file pointer
178 * @arg: arguments to the ioctl
180 * Handle the watchdog ioctls supported by the ALi driver. Really
181 * we want an extension to enable irq ack monitoring and the like
184 static int ali_ioctl(struct inode *inode, struct file *file,
185 unsigned int cmd, unsigned long arg)
187 void __user *argp = (void __user *)arg;
188 int __user *p = argp;
189 static struct watchdog_info ident = {
190 .options = WDIOF_KEEPALIVEPING |
193 .firmware_version = 0,
194 .identity = "ALi M1535 WatchDog Timer",
198 case WDIOC_GETSUPPORT:
199 return copy_to_user(argp, &ident,
200 sizeof (ident)) ? -EFAULT : 0;
202 case WDIOC_GETSTATUS:
203 case WDIOC_GETBOOTSTATUS:
204 return put_user(0, p);
206 case WDIOC_KEEPALIVE:
210 case WDIOC_SETOPTIONS:
212 int new_options, retval = -EINVAL;
214 if (get_user (new_options, p))
217 if (new_options & WDIOS_DISABLECARD) {
222 if (new_options & WDIOS_ENABLECARD) {
230 case WDIOC_SETTIMEOUT:
234 if (get_user(new_timeout, p))
237 if (ali_settimer(new_timeout))
244 case WDIOC_GETTIMEOUT:
245 return put_user(timeout, p);
253 * ali_open - handle open of ali watchdog
254 * @inode: inode from VFS
255 * @file: file from VFS
257 * Open the ALi watchdog device. Ensure only one person opens it
258 * at a time. Also start the watchdog running.
261 static int ali_open(struct inode *inode, struct file *file)
263 /* /dev/watchdog can only be opened once */
264 if (test_and_set_bit(0, &ali_is_open))
273 * ali_release - close an ALi watchdog
274 * @inode: inode from VFS
275 * @file: file from VFS
277 * Close the ALi watchdog device. Actual shutdown of the timer
278 * only occurs if the magic sequence has been set.
281 static int ali_release(struct inode *inode, struct file *file)
284 * Shut off the timer.
286 if (ali_expect_release == 42) {
289 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
292 clear_bit(0, &ali_is_open);
293 ali_expect_release = 0;
298 * ali_notify_sys - System down notifier
300 * Notifier for system down
304 static int ali_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
306 if (code==SYS_DOWN || code==SYS_HALT) {
307 /* Turn the WDT off */
315 * Data for PCI driver interface
317 * This data only exists for exporting the supported
318 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
319 * register a pci_driver, because someone else might one day
320 * want to register another driver on the same PCI id.
323 static struct pci_device_id ali_pci_tbl[] = {
324 { PCI_VENDOR_ID_AL, 1535, PCI_ANY_ID, PCI_ANY_ID,},
327 MODULE_DEVICE_TABLE(pci, ali_pci_tbl);
330 * ali_find_watchdog - find a 1535 and 7101
332 * Scans the PCI hardware for a 1535 series bridge and matching 7101
333 * watchdog device. This may be overtight but it is better to be safe
336 static int __init ali_find_watchdog(void)
338 struct pci_dev *pdev;
341 /* Check for a 1535 series bridge */
342 pdev = pci_find_device(PCI_VENDOR_ID_AL, 0x1535, NULL);
346 /* Check for the a 7101 PMU */
347 pdev = pci_find_device(PCI_VENDOR_ID_AL, 0x7101, NULL);
351 if(pci_enable_device(pdev))
357 * Initialize the timer bits
359 pci_read_config_dword(pdev, 0xCC, &wdog);
361 wdog &= ~0x3F; /* Timer bits */
362 wdog &= ~((1<<27)|(1<<26)|(1<<25)|(1<<24)); /* Issued events */
363 wdog &= ~((1<<16)|(1<<13)|(1<<12)|(1<<11)|(1<<10)|(1<<9)); /* No monitor bits */
365 pci_write_config_dword(pdev, 0xCC, wdog);
374 static struct file_operations ali_fops = {
375 .owner = THIS_MODULE,
380 .release = ali_release,
383 static struct miscdevice ali_miscdev = {
384 .minor = WATCHDOG_MINOR,
389 static struct notifier_block ali_notifier = {
390 .notifier_call = ali_notify_sys,
394 * watchdog_init - module initialiser
396 * Scan for a suitable watchdog and if so initialize it. Return an error
397 * if we cannot, the error causes the module to unload
400 static int __init watchdog_init(void)
404 spin_lock_init(&ali_lock);
406 /* Check whether or not the hardware watchdog is there */
407 if (ali_find_watchdog() != 0) {
411 /* Check that the timeout value is within it's range ; if not reset to the default */
412 if (timeout < 1 || timeout >= 18000) {
413 timeout = WATCHDOG_TIMEOUT;
414 printk(KERN_INFO PFX "timeout value must be 0<timeout<18000, using %d\n",
418 /* Calculate the watchdog's timeout */
419 ali_settimer(timeout);
421 ret = misc_register(&ali_miscdev);
423 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
424 WATCHDOG_MINOR, ret);
428 ret = register_reboot_notifier(&ali_notifier);
430 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
435 printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
441 misc_deregister(&ali_miscdev);
446 * watchdog_exit - module de-initialiser
448 * Called while unloading a successfully installed watchdog module.
451 static void __exit watchdog_exit(void)
453 /* Stop the timer before we leave */
457 unregister_reboot_notifier(&ali_notifier);
458 misc_deregister(&ali_miscdev);
461 module_init(watchdog_init);
462 module_exit(watchdog_exit);
464 MODULE_AUTHOR("Alan Cox");
465 MODULE_DESCRIPTION("ALi M1535 PMU Watchdog Timer driver");
466 MODULE_LICENSE("GPL");
467 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);