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>
21 #include <linux/uaccess.h>
24 #define WATCHDOG_NAME "ALi_M1535"
25 #define PFX WATCHDOG_NAME ": "
26 #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
28 /* internal variables */
29 static unsigned long ali_is_open
;
30 static char ali_expect_release
;
31 static struct pci_dev
*ali_pci
;
32 static u32 ali_timeout_bits
; /* stores the computed timeout */
33 static DEFINE_SPINLOCK(ali_lock
); /* Guards the hardware */
35 /* module parameters */
36 static int timeout
= WATCHDOG_TIMEOUT
;
37 module_param(timeout
, int, 0);
38 MODULE_PARM_DESC(timeout
,
39 "Watchdog timeout in seconds. (0 < timeout < 18000, default="
40 __MODULE_STRING(WATCHDOG_TIMEOUT
) ")");
42 static int nowayout
= WATCHDOG_NOWAYOUT
;
43 module_param(nowayout
, int, 0);
44 MODULE_PARM_DESC(nowayout
,
45 "Watchdog cannot be stopped once started (default="
46 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
49 * ali_start - start watchdog countdown
51 * Starts the timer running providing the timer has a counter
55 static void ali_start(void)
61 pci_read_config_dword(ali_pci
, 0xCC, &val
);
62 val
&= ~0x3F; /* Mask count */
63 val
|= (1<<25) | ali_timeout_bits
;
64 pci_write_config_dword(ali_pci
, 0xCC, val
);
66 spin_unlock(&ali_lock
);
70 * ali_stop - stop the timer countdown
72 * Stop the ALi watchdog countdown
75 static void ali_stop(void)
81 pci_read_config_dword(ali_pci
, 0xCC, &val
);
82 val
&= ~0x3F; /* Mask count to zero (disabled) */
83 val
&= ~(1<<25);/* and for safety mask the reset enable */
84 pci_write_config_dword(ali_pci
, 0xCC, val
);
86 spin_unlock(&ali_lock
);
90 * ali_keepalive - send a keepalive to the watchdog
92 * Send a keepalive to the timer (actually we restart the timer).
95 static void ali_keepalive(void)
101 * ali_settimer - compute the timer reload value
102 * @t: time in seconds
104 * Computes the timeout values needed
107 static int ali_settimer(int t
)
112 ali_timeout_bits
= t
|(1<<6);
114 ali_timeout_bits
= (t
/60)|(1<<7);
116 ali_timeout_bits
= (t
/300)|(1<<6)|(1<<7);
125 * /dev/watchdog handling
129 * ali_write - writes to ALi watchdog
130 * @file: file from VFS
131 * @data: user address of data
132 * @len: length of data
133 * @ppos: pointer to the file offset
135 * Handle a write to the ALi watchdog. Writing to the file pings
136 * the watchdog and resets it. Writing the magic 'V' sequence allows
137 * the next close to turn off the watchdog.
140 static ssize_t
ali_write(struct file
*file
, const char __user
*data
,
141 size_t len
, loff_t
*ppos
)
143 /* See if we got the magic character 'V' and reload the timer */
148 /* note: just in case someone wrote the
149 magic character five months ago... */
150 ali_expect_release
= 0;
152 /* scan to see whether or not we got
153 the magic character */
154 for (i
= 0; i
!= len
; i
++) {
156 if (get_user(c
, data
+ i
))
159 ali_expect_release
= 42;
163 /* someone wrote to us, we should reload the timer */
170 * ali_ioctl - handle watchdog ioctls
171 * @file: VFS file pointer
173 * @arg: arguments to the ioctl
175 * Handle the watchdog ioctls supported by the ALi driver. Really
176 * we want an extension to enable irq ack monitoring and the like
179 static long ali_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
181 void __user
*argp
= (void __user
*)arg
;
182 int __user
*p
= argp
;
183 static struct watchdog_info ident
= {
184 .options
= WDIOF_KEEPALIVEPING
|
187 .firmware_version
= 0,
188 .identity
= "ALi M1535 WatchDog Timer",
192 case WDIOC_GETSUPPORT
:
193 return copy_to_user(argp
, &ident
, sizeof(ident
)) ? -EFAULT
: 0;
195 case WDIOC_GETSTATUS
:
196 case WDIOC_GETBOOTSTATUS
:
197 return put_user(0, p
);
198 case WDIOC_SETOPTIONS
:
200 int new_options
, retval
= -EINVAL
;
202 if (get_user(new_options
, p
))
204 if (new_options
& WDIOS_DISABLECARD
) {
208 if (new_options
& WDIOS_ENABLECARD
) {
214 case WDIOC_KEEPALIVE
:
217 case WDIOC_SETTIMEOUT
:
220 if (get_user(new_timeout
, p
))
222 if (ali_settimer(new_timeout
))
227 case WDIOC_GETTIMEOUT
:
228 return put_user(timeout
, p
);
235 * ali_open - handle open of ali watchdog
236 * @inode: inode from VFS
237 * @file: file from VFS
239 * Open the ALi watchdog device. Ensure only one person opens it
240 * at a time. Also start the watchdog running.
243 static int ali_open(struct inode
*inode
, struct file
*file
)
245 /* /dev/watchdog can only be opened once */
246 if (test_and_set_bit(0, &ali_is_open
))
251 return nonseekable_open(inode
, file
);
255 * ali_release - close an ALi watchdog
256 * @inode: inode from VFS
257 * @file: file from VFS
259 * Close the ALi watchdog device. Actual shutdown of the timer
260 * only occurs if the magic sequence has been set.
263 static int ali_release(struct inode
*inode
, struct file
*file
)
266 * Shut off the timer.
268 if (ali_expect_release
== 42)
272 "Unexpected close, not stopping watchdog!\n");
275 clear_bit(0, &ali_is_open
);
276 ali_expect_release
= 0;
281 * ali_notify_sys - System down notifier
283 * Notifier for system down
287 static int ali_notify_sys(struct notifier_block
*this,
288 unsigned long code
, void *unused
)
290 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
291 ali_stop(); /* Turn the WDT off */
296 * Data for PCI driver interface
298 * This data only exists for exporting the supported
299 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
300 * register a pci_driver, because someone else might one day
301 * want to register another driver on the same PCI id.
304 static struct pci_device_id ali_pci_tbl
[] = {
305 { PCI_VENDOR_ID_AL
, 0x1533, PCI_ANY_ID
, PCI_ANY_ID
,},
306 { PCI_VENDOR_ID_AL
, 0x1535, PCI_ANY_ID
, PCI_ANY_ID
,},
309 MODULE_STATIC_DEVICE_TABLE(pci
, ali_pci_tbl
);
312 * ali_find_watchdog - find a 1535 and 7101
314 * Scans the PCI hardware for a 1535 series bridge and matching 7101
315 * watchdog device. This may be overtight but it is better to be safe
318 static int __init
ali_find_watchdog(void)
320 struct pci_dev
*pdev
;
323 /* Check for a 1533/1535 series bridge */
324 pdev
= pci_get_device(PCI_VENDOR_ID_AL
, 0x1535, NULL
);
326 pdev
= pci_get_device(PCI_VENDOR_ID_AL
, 0x1533, NULL
);
331 /* Check for the a 7101 PMU */
332 pdev
= pci_get_device(PCI_VENDOR_ID_AL
, 0x7101, NULL
);
336 if (pci_enable_device(pdev
)) {
344 * Initialize the timer bits
346 pci_read_config_dword(pdev
, 0xCC, &wdog
);
351 wdog
&= ~((1<<27)|(1<<26)|(1<<25)|(1<<24));
352 /* No monitor bits */
353 wdog
&= ~((1<<16)|(1<<13)|(1<<12)|(1<<11)|(1<<10)|(1<<9));
355 pci_write_config_dword(pdev
, 0xCC, wdog
);
364 static const struct file_operations ali_fops
= {
365 .owner
= THIS_MODULE
,
368 .unlocked_ioctl
= ali_ioctl
,
370 .release
= ali_release
,
373 static struct miscdevice ali_miscdev
= {
374 .minor
= WATCHDOG_MINOR
,
379 static struct notifier_block ali_notifier
= {
380 .notifier_call
= ali_notify_sys
,
384 * watchdog_init - module initialiser
386 * Scan for a suitable watchdog and if so initialize it. Return an error
387 * if we cannot, the error causes the module to unload
390 static int __init
watchdog_init(void)
394 /* Check whether or not the hardware watchdog is there */
395 if (ali_find_watchdog() != 0)
398 /* Check that the timeout value is within it's range;
399 if not reset to the default */
400 if (timeout
< 1 || timeout
>= 18000) {
401 timeout
= WATCHDOG_TIMEOUT
;
403 "timeout value must be 0 < timeout < 18000, using %d\n",
407 /* Calculate the watchdog's timeout */
408 ali_settimer(timeout
);
410 ret
= register_reboot_notifier(&ali_notifier
);
413 "cannot register reboot notifier (err=%d)\n", ret
);
417 ret
= misc_register(&ali_miscdev
);
420 "cannot register miscdev on minor=%d (err=%d)\n",
421 WATCHDOG_MINOR
, ret
);
425 printk(KERN_INFO PFX
"initialized. timeout=%d sec (nowayout=%d)\n",
431 unregister_reboot_notifier(&ali_notifier
);
436 * watchdog_exit - module de-initialiser
438 * Called while unloading a successfully installed watchdog module.
441 static void __exit
watchdog_exit(void)
443 /* Stop the timer before we leave */
447 misc_deregister(&ali_miscdev
);
448 unregister_reboot_notifier(&ali_notifier
);
449 pci_dev_put(ali_pci
);
452 module_init(watchdog_init
);
453 module_exit(watchdog_exit
);
455 MODULE_AUTHOR("Alan Cox");
456 MODULE_DESCRIPTION("ALi M1535 PMU Watchdog Timer driver");
457 MODULE_LICENSE("GPL");
458 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);