1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Wdt977 0.04: A Watchdog Device for Netwinder W83977AF chip
5 * (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
7 * -----------------------
9 * -----------------------
10 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
11 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
12 * 19-Dec-2001 Woody Suwalski: Netwinder fixes, ioctl interface
13 * 06-Jan-2002 Woody Suwalski: For compatibility, convert all timeouts
14 * from minutes to seconds.
15 * 07-Jul-2003 Daniele Bellucci: Audit return code of misc_register in
17 * 25-Oct-2005 Woody Suwalski: Convert addresses to #defs, add spinlocks
18 * remove limitiation to be used on
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/types.h>
27 #include <linux/kernel.h>
29 #include <linux/miscdevice.h>
30 #include <linux/init.h>
31 #include <linux/ioport.h>
32 #include <linux/watchdog.h>
33 #include <linux/notifier.h>
34 #include <linux/reboot.h>
36 #include <linux/uaccess.h>
38 #include <asm/mach-types.h>
40 #define WATCHDOG_VERSION "0.04"
41 #define WATCHDOG_NAME "Wdt977"
43 #define IO_INDEX_PORT 0x370 /* on some systems it can be 0x3F0 */
44 #define IO_DATA_PORT (IO_INDEX_PORT + 1)
46 #define UNLOCK_DATA 0x87
47 #define LOCK_DATA 0xAA
48 #define DEVICE_REGISTER 0x07
51 #define DEFAULT_TIMEOUT 60 /* default timeout in seconds */
53 static int timeout
= DEFAULT_TIMEOUT
;
54 static int timeoutM
; /* timeout in minutes */
55 static unsigned long timer_alive
;
57 static char expect_close
;
58 static DEFINE_SPINLOCK(spinlock
);
60 module_param(timeout
, int, 0);
61 MODULE_PARM_DESC(timeout
, "Watchdog timeout in seconds (60..15300, default="
62 __MODULE_STRING(DEFAULT_TIMEOUT
) ")");
63 module_param(testmode
, int, 0);
64 MODULE_PARM_DESC(testmode
, "Watchdog testmode (1 = no reboot), default=0");
66 static bool nowayout
= WATCHDOG_NOWAYOUT
;
67 module_param(nowayout
, bool, 0);
68 MODULE_PARM_DESC(nowayout
,
69 "Watchdog cannot be stopped once started (default="
70 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
76 static int wdt977_start(void)
80 spin_lock_irqsave(&spinlock
, flags
);
82 /* unlock the SuperIO chip */
83 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
84 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
86 /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
87 * F2 has the timeout in minutes
88 * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
89 * at timeout, and to reset timer on kbd/mouse activity (not impl.)
90 * F4 is used to just clear the TIMEOUT'ed state (bit 0)
92 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
93 outb_p(0x08, IO_DATA_PORT
);
94 outb_p(0xF2, IO_INDEX_PORT
);
95 outb_p(timeoutM
, IO_DATA_PORT
);
96 outb_p(0xF3, IO_INDEX_PORT
);
97 outb_p(0x00, IO_DATA_PORT
); /* another setting is 0E for
99 outb_p(0xF4, IO_INDEX_PORT
);
100 outb_p(0x00, IO_DATA_PORT
);
102 /* At last select device Aux1 (dev=7) and set GP16 as a
103 * watchdog output. In test mode watch the bit 1 on F4 to
104 * indicate "triggered"
107 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
108 outb_p(0x07, IO_DATA_PORT
);
109 outb_p(0xE6, IO_INDEX_PORT
);
110 outb_p(0x08, IO_DATA_PORT
);
113 /* lock the SuperIO chip */
114 outb_p(LOCK_DATA
, IO_INDEX_PORT
);
116 spin_unlock_irqrestore(&spinlock
, flags
);
117 pr_info("activated\n");
126 static int wdt977_stop(void)
129 spin_lock_irqsave(&spinlock
, flags
);
131 /* unlock the SuperIO chip */
132 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
133 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
135 /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
136 * F3 is reset to its default state
137 * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
138 * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
140 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
141 outb_p(0x08, IO_DATA_PORT
);
142 outb_p(0xF2, IO_INDEX_PORT
);
143 outb_p(0xFF, IO_DATA_PORT
);
144 outb_p(0xF3, IO_INDEX_PORT
);
145 outb_p(0x00, IO_DATA_PORT
);
146 outb_p(0xF4, IO_INDEX_PORT
);
147 outb_p(0x00, IO_DATA_PORT
);
148 outb_p(0xF2, IO_INDEX_PORT
);
149 outb_p(0x00, IO_DATA_PORT
);
151 /* at last select device Aux1 (dev=7) and set
152 GP16 as a watchdog output */
153 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
154 outb_p(0x07, IO_DATA_PORT
);
155 outb_p(0xE6, IO_INDEX_PORT
);
156 outb_p(0x08, IO_DATA_PORT
);
158 /* lock the SuperIO chip */
159 outb_p(LOCK_DATA
, IO_INDEX_PORT
);
161 spin_unlock_irqrestore(&spinlock
, flags
);
162 pr_info("shutdown\n");
168 * Send a keepalive ping to the watchdog
169 * This is done by simply re-writing the timeout to reg. 0xF2
172 static int wdt977_keepalive(void)
175 spin_lock_irqsave(&spinlock
, flags
);
177 /* unlock the SuperIO chip */
178 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
179 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
181 /* select device Aux2 (device=8) and kicks watchdog reg F2 */
182 /* F2 has the timeout in minutes */
183 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
184 outb_p(0x08, IO_DATA_PORT
);
185 outb_p(0xF2, IO_INDEX_PORT
);
186 outb_p(timeoutM
, IO_DATA_PORT
);
188 /* lock the SuperIO chip */
189 outb_p(LOCK_DATA
, IO_INDEX_PORT
);
190 spin_unlock_irqrestore(&spinlock
, flags
);
196 * Set the watchdog timeout value
199 static int wdt977_set_timeout(int t
)
203 /* convert seconds to minutes, rounding up */
204 tmrval
= (t
+ 59) / 60;
206 if (machine_is_netwinder()) {
207 /* we have a hw bug somewhere, so each 977 minute is actually
208 * only 30sec. This limits the max timeout to half of device
209 * max of 255 minutes...
214 if (tmrval
< 1 || tmrval
> 255)
217 /* timeout is the timeout in seconds, timeoutM is
218 the timeout in minutes) */
225 * Get the watchdog status
228 static int wdt977_get_status(int *status
)
233 spin_lock_irqsave(&spinlock
, flags
);
235 /* unlock the SuperIO chip */
236 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
237 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
239 /* select device Aux2 (device=8) and read watchdog reg F4 */
240 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
241 outb_p(0x08, IO_DATA_PORT
);
242 outb_p(0xF4, IO_INDEX_PORT
);
243 new_status
= inb_p(IO_DATA_PORT
);
245 /* lock the SuperIO chip */
246 outb_p(LOCK_DATA
, IO_INDEX_PORT
);
248 spin_unlock_irqrestore(&spinlock
, flags
);
252 *status
|= WDIOF_CARDRESET
;
259 * /dev/watchdog handling
262 static int wdt977_open(struct inode
*inode
, struct file
*file
)
264 /* If the watchdog is alive we don't need to start it again */
265 if (test_and_set_bit(0, &timer_alive
))
269 __module_get(THIS_MODULE
);
272 return stream_open(inode
, file
);
275 static int wdt977_release(struct inode
*inode
, struct file
*file
)
278 * Shut off the timer.
279 * Lock it in if it's a module and we set nowayout
281 if (expect_close
== 42) {
283 clear_bit(0, &timer_alive
);
286 pr_crit("Unexpected close, not stopping watchdog!\n");
295 * @file: file handle to the watchdog
296 * @buf: buffer to write (unused as data does not matter here
297 * @count: count of bytes
298 * @ppos: pointer to the position to write. No seeks allowed
300 * A write to a watchdog device is defined as a keepalive signal. Any
301 * write of data will do, as we we don't define content meaning.
304 static ssize_t
wdt977_write(struct file
*file
, const char __user
*buf
,
305 size_t count
, loff_t
*ppos
)
311 /* In case it was set long ago */
314 for (i
= 0; i
!= count
; i
++) {
316 if (get_user(c
, buf
+ i
))
323 /* someone wrote to us, we should restart timer */
329 static const struct watchdog_info ident
= {
330 .options
= WDIOF_SETTIMEOUT
|
333 .firmware_version
= 1,
334 .identity
= WATCHDOG_NAME
,
339 * @inode: inode of the device
340 * @file: file handle to the device
341 * @cmd: watchdog command
342 * @arg: argument pointer
344 * The watchdog API defines a common set of functions for all watchdogs
345 * according to their available features.
348 static long wdt977_ioctl(struct file
*file
, unsigned int cmd
,
352 int new_options
, retval
= -EINVAL
;
355 struct watchdog_info __user
*ident
;
359 uarg
.i
= (int __user
*)arg
;
362 case WDIOC_GETSUPPORT
:
363 return copy_to_user(uarg
.ident
, &ident
,
364 sizeof(ident
)) ? -EFAULT
: 0;
366 case WDIOC_GETSTATUS
:
367 wdt977_get_status(&status
);
368 return put_user(status
, uarg
.i
);
370 case WDIOC_GETBOOTSTATUS
:
371 return put_user(0, uarg
.i
);
373 case WDIOC_SETOPTIONS
:
374 if (get_user(new_options
, uarg
.i
))
377 if (new_options
& WDIOS_DISABLECARD
) {
382 if (new_options
& WDIOS_ENABLECARD
) {
389 case WDIOC_KEEPALIVE
:
393 case WDIOC_SETTIMEOUT
:
394 if (get_user(new_timeout
, uarg
.i
))
397 if (wdt977_set_timeout(new_timeout
))
403 case WDIOC_GETTIMEOUT
:
404 return put_user(timeout
, uarg
.i
);
412 static int wdt977_notify_sys(struct notifier_block
*this, unsigned long code
,
415 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
420 static const struct file_operations wdt977_fops
= {
421 .owner
= THIS_MODULE
,
423 .write
= wdt977_write
,
424 .unlocked_ioctl
= wdt977_ioctl
,
425 .compat_ioctl
= compat_ptr_ioctl
,
427 .release
= wdt977_release
,
430 static struct miscdevice wdt977_miscdev
= {
431 .minor
= WATCHDOG_MINOR
,
433 .fops
= &wdt977_fops
,
436 static struct notifier_block wdt977_notifier
= {
437 .notifier_call
= wdt977_notify_sys
,
440 static int __init
wd977_init(void)
444 pr_info("driver v%s\n", WATCHDOG_VERSION
);
446 /* Check that the timeout value is within its range;
447 if not reset to the default */
448 if (wdt977_set_timeout(timeout
)) {
449 wdt977_set_timeout(DEFAULT_TIMEOUT
);
450 pr_info("timeout value must be 60 < timeout < 15300, using %d\n",
454 /* on Netwinder the IOports are already reserved by
455 * arch/arm/mach-footbridge/netwinder-hw.c
457 if (!machine_is_netwinder()) {
458 if (!request_region(IO_INDEX_PORT
, 2, WATCHDOG_NAME
)) {
459 pr_err("I/O address 0x%04x already in use\n",
466 rc
= register_reboot_notifier(&wdt977_notifier
);
468 pr_err("cannot register reboot notifier (err=%d)\n", rc
);
472 rc
= misc_register(&wdt977_miscdev
);
474 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
475 wdt977_miscdev
.minor
, rc
);
479 pr_info("initialized. timeout=%d sec (nowayout=%d, testmode=%i)\n",
480 timeout
, nowayout
, testmode
);
485 unregister_reboot_notifier(&wdt977_notifier
);
487 if (!machine_is_netwinder())
488 release_region(IO_INDEX_PORT
, 2);
493 static void __exit
wd977_exit(void)
496 misc_deregister(&wdt977_miscdev
);
497 unregister_reboot_notifier(&wdt977_notifier
);
498 release_region(IO_INDEX_PORT
, 2);
501 module_init(wd977_init
);
502 module_exit(wd977_exit
);
504 MODULE_AUTHOR("Woody Suwalski <woodys@xandros.com>");
505 MODULE_DESCRIPTION("W83977AF Watchdog driver");
506 MODULE_LICENSE("GPL");