1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * W83977F Watchdog Timer Driver for Winbond W83977F I/O Chip
5 * (c) Copyright 2005 Jose Goncalves <jose.goncalves@inov.pt>
7 * Based on w83877f_wdt.c by Scott Jennings,
8 * and wdt977.c by Woody Suwalski
10 * -----------------------
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
20 #include <linux/miscdevice.h>
21 #include <linux/init.h>
22 #include <linux/ioport.h>
23 #include <linux/watchdog.h>
24 #include <linux/notifier.h>
25 #include <linux/reboot.h>
26 #include <linux/uaccess.h>
30 #define WATCHDOG_VERSION "1.00"
31 #define WATCHDOG_NAME "W83977F WDT"
33 #define IO_INDEX_PORT 0x3F0
34 #define IO_DATA_PORT (IO_INDEX_PORT+1)
36 #define UNLOCK_DATA 0x87
37 #define LOCK_DATA 0xAA
38 #define DEVICE_REGISTER 0x07
40 #define DEFAULT_TIMEOUT 45 /* default timeout in seconds */
42 static int timeout = DEFAULT_TIMEOUT;
43 static int timeoutW; /* timeout in watchdog counter units */
44 static unsigned long timer_alive;
46 static char expect_close;
47 static DEFINE_SPINLOCK(spinlock);
49 module_param(timeout, int, 0);
50 MODULE_PARM_DESC(timeout,
51 "Watchdog timeout in seconds (15..7635), default="
52 __MODULE_STRING(DEFAULT_TIMEOUT) ")");
53 module_param(testmode, int, 0);
54 MODULE_PARM_DESC(testmode, "Watchdog testmode (1 = no reboot), default=0");
56 static bool nowayout = WATCHDOG_NOWAYOUT;
57 module_param(nowayout, bool, 0);
58 MODULE_PARM_DESC(nowayout,
59 "Watchdog cannot be stopped once started (default="
60 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
66 static int wdt_start(void)
70 spin_lock_irqsave(&spinlock, flags);
72 /* Unlock the SuperIO chip */
73 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
74 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
77 * Select device Aux2 (device=8) to set watchdog regs F2, F3 and F4.
78 * F2 has the timeout in watchdog counter units.
79 * F3 is set to enable watchdog LED blink at timeout.
80 * F4 is used to just clear the TIMEOUT'ed state (bit 0).
82 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
83 outb_p(0x08, IO_DATA_PORT);
84 outb_p(0xF2, IO_INDEX_PORT);
85 outb_p(timeoutW, IO_DATA_PORT);
86 outb_p(0xF3, IO_INDEX_PORT);
87 outb_p(0x08, IO_DATA_PORT);
88 outb_p(0xF4, IO_INDEX_PORT);
89 outb_p(0x00, IO_DATA_PORT);
91 /* Set device Aux2 active */
92 outb_p(0x30, IO_INDEX_PORT);
93 outb_p(0x01, IO_DATA_PORT);
96 * Select device Aux1 (dev=7) to set GP16 as the watchdog output
97 * (in reg E6) and GP13 as the watchdog LED output (in reg E3).
98 * Map GP16 at pin 119.
99 * In test mode watch the bit 0 on F4 to indicate "triggered" or
100 * check watchdog LED on SBC.
102 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
103 outb_p(0x07, IO_DATA_PORT);
107 outb_p(0xE6, IO_INDEX_PORT);
108 outb_p(0x0A, IO_DATA_PORT);
109 outb_p(0x2C, IO_INDEX_PORT);
110 pin_map = inb_p(IO_DATA_PORT);
113 outb_p(0x2C, IO_INDEX_PORT);
114 outb_p(pin_map, IO_DATA_PORT);
116 outb_p(0xE3, IO_INDEX_PORT);
117 outb_p(0x08, IO_DATA_PORT);
119 /* Set device Aux1 active */
120 outb_p(0x30, IO_INDEX_PORT);
121 outb_p(0x01, IO_DATA_PORT);
123 /* Lock the SuperIO chip */
124 outb_p(LOCK_DATA, IO_INDEX_PORT);
126 spin_unlock_irqrestore(&spinlock, flags);
128 pr_info("activated\n");
137 static int wdt_stop(void)
141 spin_lock_irqsave(&spinlock, flags);
143 /* Unlock the SuperIO chip */
144 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
145 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
148 * Select device Aux2 (device=8) to set watchdog regs F2, F3 and F4.
149 * F2 is reset to its default value (watchdog timer disabled).
150 * F3 is reset to its default state.
151 * F4 clears the TIMEOUT'ed state (bit 0) - back to default.
153 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
154 outb_p(0x08, IO_DATA_PORT);
155 outb_p(0xF2, IO_INDEX_PORT);
156 outb_p(0xFF, IO_DATA_PORT);
157 outb_p(0xF3, IO_INDEX_PORT);
158 outb_p(0x00, IO_DATA_PORT);
159 outb_p(0xF4, IO_INDEX_PORT);
160 outb_p(0x00, IO_DATA_PORT);
161 outb_p(0xF2, IO_INDEX_PORT);
162 outb_p(0x00, IO_DATA_PORT);
165 * Select device Aux1 (dev=7) to set GP16 (in reg E6) and
166 * Gp13 (in reg E3) as inputs.
168 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
169 outb_p(0x07, IO_DATA_PORT);
171 outb_p(0xE6, IO_INDEX_PORT);
172 outb_p(0x01, IO_DATA_PORT);
174 outb_p(0xE3, IO_INDEX_PORT);
175 outb_p(0x01, IO_DATA_PORT);
177 /* Lock the SuperIO chip */
178 outb_p(LOCK_DATA, IO_INDEX_PORT);
180 spin_unlock_irqrestore(&spinlock, flags);
182 pr_info("shutdown\n");
188 * Send a keepalive ping to the watchdog
189 * This is done by simply re-writing the timeout to reg. 0xF2
192 static int wdt_keepalive(void)
196 spin_lock_irqsave(&spinlock, flags);
198 /* Unlock the SuperIO chip */
199 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
200 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
202 /* Select device Aux2 (device=8) to kick watchdog reg F2 */
203 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
204 outb_p(0x08, IO_DATA_PORT);
205 outb_p(0xF2, IO_INDEX_PORT);
206 outb_p(timeoutW, IO_DATA_PORT);
208 /* Lock the SuperIO chip */
209 outb_p(LOCK_DATA, IO_INDEX_PORT);
211 spin_unlock_irqrestore(&spinlock, flags);
217 * Set the watchdog timeout value
220 static int wdt_set_timeout(int t)
225 * Convert seconds to watchdog counter time units, rounding up.
226 * On PCM-5335 watchdog units are 30 seconds/step with 15 sec startup
227 * value. This information is supplied in the PCM-5335 manual and was
228 * checked by me on a real board. This is a bit strange because W83977f
229 * datasheet says counter unit is in minutes!
234 tmrval = ((t + 15) + 29) / 30;
240 * timeout is the timeout in seconds,
241 * timeoutW is the timeout in watchdog counter units.
244 timeout = (timeoutW * 30) - 15;
249 * Get the watchdog status
252 static int wdt_get_status(int *status)
257 spin_lock_irqsave(&spinlock, flags);
259 /* Unlock the SuperIO chip */
260 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
261 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
263 /* Select device Aux2 (device=8) to read watchdog reg F4 */
264 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
265 outb_p(0x08, IO_DATA_PORT);
266 outb_p(0xF4, IO_INDEX_PORT);
267 new_status = inb_p(IO_DATA_PORT);
269 /* Lock the SuperIO chip */
270 outb_p(LOCK_DATA, IO_INDEX_PORT);
272 spin_unlock_irqrestore(&spinlock, flags);
276 *status |= WDIOF_CARDRESET;
283 * /dev/watchdog handling
286 static int wdt_open(struct inode *inode, struct file *file)
288 /* If the watchdog is alive we don't need to start it again */
289 if (test_and_set_bit(0, &timer_alive))
293 __module_get(THIS_MODULE);
296 return stream_open(inode, file);
299 static int wdt_release(struct inode *inode, struct file *file)
302 * Shut off the timer.
303 * Lock it in if it's a module and we set nowayout
305 if (expect_close == 42) {
307 clear_bit(0, &timer_alive);
310 pr_crit("unexpected close, not stopping watchdog!\n");
318 * @file: file handle to the watchdog
319 * @buf: buffer to write (unused as data does not matter here
320 * @count: count of bytes
321 * @ppos: pointer to the position to write. No seeks allowed
323 * A write to a watchdog device is defined as a keepalive signal. Any
324 * write of data will do, as we we don't define content meaning.
327 static ssize_t wdt_write(struct file *file, const char __user *buf,
328 size_t count, loff_t *ppos)
330 /* See if we got the magic character 'V' and reload the timer */
335 /* note: just in case someone wrote the
336 magic character long ago */
339 /* scan to see whether or not we got the
341 for (ofs = 0; ofs != count; ofs++) {
343 if (get_user(c, buf + ofs))
350 /* someone wrote to us, we should restart timer */
358 * @inode: inode of the device
359 * @file: file handle to the device
360 * @cmd: watchdog command
361 * @arg: argument pointer
363 * The watchdog API defines a common set of functions for all watchdogs
364 * according to their available features.
367 static const struct watchdog_info ident = {
368 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
369 .firmware_version = 1,
370 .identity = WATCHDOG_NAME,
373 static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
376 int new_options, retval = -EINVAL;
379 struct watchdog_info __user *ident;
383 uarg.i = (int __user *)arg;
386 case WDIOC_GETSUPPORT:
387 return copy_to_user(uarg.ident, &ident,
388 sizeof(ident)) ? -EFAULT : 0;
390 case WDIOC_GETSTATUS:
391 wdt_get_status(&status);
392 return put_user(status, uarg.i);
394 case WDIOC_GETBOOTSTATUS:
395 return put_user(0, uarg.i);
397 case WDIOC_SETOPTIONS:
398 if (get_user(new_options, uarg.i))
401 if (new_options & WDIOS_DISABLECARD) {
406 if (new_options & WDIOS_ENABLECARD) {
413 case WDIOC_KEEPALIVE:
417 case WDIOC_SETTIMEOUT:
418 if (get_user(new_timeout, uarg.i))
421 if (wdt_set_timeout(new_timeout))
427 case WDIOC_GETTIMEOUT:
428 return put_user(timeout, uarg.i);
436 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
439 if (code == SYS_DOWN || code == SYS_HALT)
444 static const struct file_operations wdt_fops = {
445 .owner = THIS_MODULE,
448 .unlocked_ioctl = wdt_ioctl,
449 .compat_ioctl = compat_ptr_ioctl,
451 .release = wdt_release,
454 static struct miscdevice wdt_miscdev = {
455 .minor = WATCHDOG_MINOR,
460 static struct notifier_block wdt_notifier = {
461 .notifier_call = wdt_notify_sys,
464 static int __init w83977f_wdt_init(void)
468 pr_info("driver v%s\n", WATCHDOG_VERSION);
471 * Check that the timeout value is within it's range;
472 * if not reset to the default
474 if (wdt_set_timeout(timeout)) {
475 wdt_set_timeout(DEFAULT_TIMEOUT);
476 pr_info("timeout value must be 15 <= timeout <= 7635, using %d\n",
480 if (!request_region(IO_INDEX_PORT, 2, WATCHDOG_NAME)) {
481 pr_err("I/O address 0x%04x already in use\n", IO_INDEX_PORT);
486 rc = register_reboot_notifier(&wdt_notifier);
488 pr_err("cannot register reboot notifier (err=%d)\n", rc);
492 rc = misc_register(&wdt_miscdev);
494 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
495 wdt_miscdev.minor, rc);
499 pr_info("initialized. timeout=%d sec (nowayout=%d testmode=%d)\n",
500 timeout, nowayout, testmode);
505 unregister_reboot_notifier(&wdt_notifier);
507 release_region(IO_INDEX_PORT, 2);
512 static void __exit w83977f_wdt_exit(void)
515 misc_deregister(&wdt_miscdev);
516 unregister_reboot_notifier(&wdt_notifier);
517 release_region(IO_INDEX_PORT, 2);
520 module_init(w83977f_wdt_init);
521 module_exit(w83977f_wdt_exit);
523 MODULE_AUTHOR("Jose Goncalves <jose.goncalves@inov.pt>");
524 MODULE_DESCRIPTION("Driver for watchdog timer in W83977F I/O chip");
525 MODULE_LICENSE("GPL");