1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Watchdog for the 7101 PMU version found in the ALi M1535 chipsets
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/types.h>
11 #include <linux/miscdevice.h>
12 #include <linux/watchdog.h>
13 #include <linux/ioport.h>
14 #include <linux/notifier.h>
15 #include <linux/reboot.h>
16 #include <linux/init.h>
18 #include <linux/pci.h>
19 #include <linux/uaccess.h>
22 #define WATCHDOG_NAME "ALi_M1535"
23 #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
25 /* internal variables */
26 static unsigned long ali_is_open
;
27 static char ali_expect_release
;
28 static struct pci_dev
*ali_pci
;
29 static u32 ali_timeout_bits
; /* stores the computed timeout */
30 static DEFINE_SPINLOCK(ali_lock
); /* Guards the hardware */
32 /* module parameters */
33 static int timeout
= WATCHDOG_TIMEOUT
;
34 module_param(timeout
, int, 0);
35 MODULE_PARM_DESC(timeout
,
36 "Watchdog timeout in seconds. (0 < timeout < 18000, default="
37 __MODULE_STRING(WATCHDOG_TIMEOUT
) ")");
39 static bool nowayout
= WATCHDOG_NOWAYOUT
;
40 module_param(nowayout
, bool, 0);
41 MODULE_PARM_DESC(nowayout
,
42 "Watchdog cannot be stopped once started (default="
43 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
46 * ali_start - start watchdog countdown
48 * Starts the timer running providing the timer has a counter
52 static void ali_start(void)
58 pci_read_config_dword(ali_pci
, 0xCC, &val
);
59 val
&= ~0x3F; /* Mask count */
60 val
|= (1 << 25) | ali_timeout_bits
;
61 pci_write_config_dword(ali_pci
, 0xCC, val
);
63 spin_unlock(&ali_lock
);
67 * ali_stop - stop the timer countdown
69 * Stop the ALi watchdog countdown
72 static void ali_stop(void)
78 pci_read_config_dword(ali_pci
, 0xCC, &val
);
79 val
&= ~0x3F; /* Mask count to zero (disabled) */
80 val
&= ~(1 << 25); /* and for safety mask the reset enable */
81 pci_write_config_dword(ali_pci
, 0xCC, val
);
83 spin_unlock(&ali_lock
);
87 * ali_keepalive - send a keepalive to the watchdog
89 * Send a keepalive to the timer (actually we restart the timer).
92 static void ali_keepalive(void)
98 * ali_settimer - compute the timer reload value
101 * Computes the timeout values needed
104 static int ali_settimer(int t
)
109 ali_timeout_bits
= t
|(1 << 6);
111 ali_timeout_bits
= (t
/ 60)|(1 << 7);
113 ali_timeout_bits
= (t
/ 300)|(1 << 6)|(1 << 7);
122 * /dev/watchdog handling
126 * ali_write - writes to ALi watchdog
127 * @file: file from VFS
128 * @data: user address of data
129 * @len: length of data
130 * @ppos: pointer to the file offset
132 * Handle a write to the ALi watchdog. Writing to the file pings
133 * the watchdog and resets it. Writing the magic 'V' sequence allows
134 * the next close to turn off the watchdog.
137 static ssize_t
ali_write(struct file
*file
, const char __user
*data
,
138 size_t len
, loff_t
*ppos
)
140 /* See if we got the magic character 'V' and reload the timer */
145 /* note: just in case someone wrote the
146 magic character five months ago... */
147 ali_expect_release
= 0;
149 /* scan to see whether or not we got
150 the magic character */
151 for (i
= 0; i
!= len
; i
++) {
153 if (get_user(c
, data
+ i
))
156 ali_expect_release
= 42;
160 /* someone wrote to us, we should reload the timer */
167 * ali_ioctl - handle watchdog ioctls
168 * @file: VFS file pointer
170 * @arg: arguments to the ioctl
172 * Handle the watchdog ioctls supported by the ALi driver. Really
173 * we want an extension to enable irq ack monitoring and the like
176 static long ali_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
178 void __user
*argp
= (void __user
*)arg
;
179 int __user
*p
= argp
;
180 static const struct watchdog_info ident
= {
181 .options
= WDIOF_KEEPALIVEPING
|
184 .firmware_version
= 0,
185 .identity
= "ALi M1535 WatchDog Timer",
189 case WDIOC_GETSUPPORT
:
190 return copy_to_user(argp
, &ident
, sizeof(ident
)) ? -EFAULT
: 0;
192 case WDIOC_GETSTATUS
:
193 case WDIOC_GETBOOTSTATUS
:
194 return put_user(0, p
);
195 case WDIOC_SETOPTIONS
:
197 int new_options
, retval
= -EINVAL
;
199 if (get_user(new_options
, p
))
201 if (new_options
& WDIOS_DISABLECARD
) {
205 if (new_options
& WDIOS_ENABLECARD
) {
211 case WDIOC_KEEPALIVE
:
214 case WDIOC_SETTIMEOUT
:
217 if (get_user(new_timeout
, p
))
219 if (ali_settimer(new_timeout
))
224 case WDIOC_GETTIMEOUT
:
225 return put_user(timeout
, p
);
232 * ali_open - handle open of ali watchdog
233 * @inode: inode from VFS
234 * @file: file from VFS
236 * Open the ALi watchdog device. Ensure only one person opens it
237 * at a time. Also start the watchdog running.
240 static int ali_open(struct inode
*inode
, struct file
*file
)
242 /* /dev/watchdog can only be opened once */
243 if (test_and_set_bit(0, &ali_is_open
))
248 return stream_open(inode
, file
);
252 * ali_release - close an ALi watchdog
253 * @inode: inode from VFS
254 * @file: file from VFS
256 * Close the ALi watchdog device. Actual shutdown of the timer
257 * only occurs if the magic sequence has been set.
260 static int ali_release(struct inode
*inode
, struct file
*file
)
263 * Shut off the timer.
265 if (ali_expect_release
== 42)
268 pr_crit("Unexpected close, not stopping watchdog!\n");
271 clear_bit(0, &ali_is_open
);
272 ali_expect_release
= 0;
277 * ali_notify_sys - System down notifier
279 * Notifier for system down
283 static int ali_notify_sys(struct notifier_block
*this,
284 unsigned long code
, void *unused
)
286 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
287 ali_stop(); /* Turn the WDT off */
292 * Data for PCI driver interface
294 * This data only exists for exporting the supported
295 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
296 * register a pci_driver, because someone else might one day
297 * want to register another driver on the same PCI id.
300 static const struct pci_device_id ali_pci_tbl
[] __used
= {
301 { PCI_VENDOR_ID_AL
, 0x1533, PCI_ANY_ID
, PCI_ANY_ID
,},
302 { PCI_VENDOR_ID_AL
, 0x1535, PCI_ANY_ID
, PCI_ANY_ID
,},
305 MODULE_DEVICE_TABLE(pci
, ali_pci_tbl
);
308 * ali_find_watchdog - find a 1535 and 7101
310 * Scans the PCI hardware for a 1535 series bridge and matching 7101
311 * watchdog device. This may be overtight but it is better to be safe
314 static int __init
ali_find_watchdog(void)
316 struct pci_dev
*pdev
;
319 /* Check for a 1533/1535 series bridge */
320 pdev
= pci_get_device(PCI_VENDOR_ID_AL
, 0x1535, NULL
);
322 pdev
= pci_get_device(PCI_VENDOR_ID_AL
, 0x1533, NULL
);
327 /* Check for the a 7101 PMU */
328 pdev
= pci_get_device(PCI_VENDOR_ID_AL
, 0x7101, NULL
);
332 if (pci_enable_device(pdev
)) {
340 * Initialize the timer bits
342 pci_read_config_dword(pdev
, 0xCC, &wdog
);
347 wdog
&= ~((1 << 27)|(1 << 26)|(1 << 25)|(1 << 24));
348 /* No monitor bits */
349 wdog
&= ~((1 << 16)|(1 << 13)|(1 << 12)|(1 << 11)|(1 << 10)|(1 << 9));
351 pci_write_config_dword(pdev
, 0xCC, wdog
);
360 static const struct file_operations ali_fops
= {
361 .owner
= THIS_MODULE
,
364 .unlocked_ioctl
= ali_ioctl
,
365 .compat_ioctl
= compat_ptr_ioctl
,
367 .release
= ali_release
,
370 static struct miscdevice ali_miscdev
= {
371 .minor
= WATCHDOG_MINOR
,
376 static struct notifier_block ali_notifier
= {
377 .notifier_call
= ali_notify_sys
,
381 * watchdog_init - module initialiser
383 * Scan for a suitable watchdog and if so initialize it. Return an error
384 * if we cannot, the error causes the module to unload
387 static int __init
watchdog_init(void)
391 /* Check whether or not the hardware watchdog is there */
392 if (ali_find_watchdog() != 0)
395 /* Check that the timeout value is within it's range;
396 if not reset to the default */
397 if (timeout
< 1 || timeout
>= 18000) {
398 timeout
= WATCHDOG_TIMEOUT
;
399 pr_info("timeout value must be 0 < timeout < 18000, using %d\n",
403 /* Calculate the watchdog's timeout */
404 ali_settimer(timeout
);
406 ret
= register_reboot_notifier(&ali_notifier
);
408 pr_err("cannot register reboot notifier (err=%d)\n", ret
);
412 ret
= misc_register(&ali_miscdev
);
414 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
415 WATCHDOG_MINOR
, ret
);
419 pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
425 unregister_reboot_notifier(&ali_notifier
);
430 * watchdog_exit - module de-initialiser
432 * Called while unloading a successfully installed watchdog module.
435 static void __exit
watchdog_exit(void)
437 /* Stop the timer before we leave */
441 misc_deregister(&ali_miscdev
);
442 unregister_reboot_notifier(&ali_notifier
);
443 pci_dev_put(ali_pci
);
446 module_init(watchdog_init
);
447 module_exit(watchdog_exit
);
449 MODULE_AUTHOR("Alan Cox");
450 MODULE_DESCRIPTION("ALi M1535 PMU Watchdog Timer driver");
451 MODULE_LICENSE("GPL");