Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Wdt977 0.04: A Watchdog Device for Netwinder W83977AF chip
4 *
5 * (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
6 *
7 * -----------------------
8 *
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
16 * nwwatchdog_init.
17 * 25-Oct-2005 Woody Suwalski: Convert addresses to #defs, add spinlocks
18 * remove limitiation to be used on
19 * Netwinders only
20 */
21
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/types.h>
27#include <linux/kernel.h>
28#include <linux/fs.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>
35#include <linux/io.h>
36#include <linux/uaccess.h>
37
38#include <asm/mach-types.h>
39
40#define WATCHDOG_VERSION "0.04"
41#define WATCHDOG_NAME "Wdt977"
42
43#define IO_INDEX_PORT 0x370 /* on some systems it can be 0x3F0 */
44#define IO_DATA_PORT (IO_INDEX_PORT + 1)
45
46#define UNLOCK_DATA 0x87
47#define LOCK_DATA 0xAA
48#define DEVICE_REGISTER 0x07
49
50
51#define DEFAULT_TIMEOUT 60 /* default timeout in seconds */
52
53static int timeout = DEFAULT_TIMEOUT;
54static int timeoutM; /* timeout in minutes */
55static unsigned long timer_alive;
56static int testmode;
57static char expect_close;
58static DEFINE_SPINLOCK(spinlock);
59
60module_param(timeout, int, 0);
61MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (60..15300, default="
62 __MODULE_STRING(DEFAULT_TIMEOUT) ")");
63module_param(testmode, int, 0);
64MODULE_PARM_DESC(testmode, "Watchdog testmode (1 = no reboot), default=0");
65
66static bool nowayout = WATCHDOG_NOWAYOUT;
67module_param(nowayout, bool, 0);
68MODULE_PARM_DESC(nowayout,
69 "Watchdog cannot be stopped once started (default="
70 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
71
72/*
73 * Start the watchdog
74 */
75
76static int wdt977_start(void)
77{
78 unsigned long flags;
79
80 spin_lock_irqsave(&spinlock, flags);
81
82 /* unlock the SuperIO chip */
83 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
84 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
85
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)
91 */
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
98 kbd/mouse/LED */
99 outb_p(0xF4, IO_INDEX_PORT);
100 outb_p(0x00, IO_DATA_PORT);
101
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"
105 */
106 if (!testmode) {
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);
111 }
112
113 /* lock the SuperIO chip */
114 outb_p(LOCK_DATA, IO_INDEX_PORT);
115
116 spin_unlock_irqrestore(&spinlock, flags);
117 pr_info("activated\n");
118
119 return 0;
120}
121
122/*
123 * Stop the watchdog
124 */
125
126static int wdt977_stop(void)
127{
128 unsigned long flags;
129 spin_lock_irqsave(&spinlock, flags);
130
131 /* unlock the SuperIO chip */
132 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
133 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
134
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
139 */
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);
150
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);
157
158 /* lock the SuperIO chip */
159 outb_p(LOCK_DATA, IO_INDEX_PORT);
160
161 spin_unlock_irqrestore(&spinlock, flags);
162 pr_info("shutdown\n");
163
164 return 0;
165}
166
167/*
168 * Send a keepalive ping to the watchdog
169 * This is done by simply re-writing the timeout to reg. 0xF2
170 */
171
172static int wdt977_keepalive(void)
173{
174 unsigned long flags;
175 spin_lock_irqsave(&spinlock, flags);
176
177 /* unlock the SuperIO chip */
178 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
179 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
180
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);
187
188 /* lock the SuperIO chip */
189 outb_p(LOCK_DATA, IO_INDEX_PORT);
190 spin_unlock_irqrestore(&spinlock, flags);
191
192 return 0;
193}
194
195/*
196 * Set the watchdog timeout value
197 */
198
199static int wdt977_set_timeout(int t)
200{
201 int tmrval;
202
203 /* convert seconds to minutes, rounding up */
204 tmrval = (t + 59) / 60;
205
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...
210 */
211 tmrval += tmrval;
212 }
213
214 if (tmrval < 1 || tmrval > 255)
215 return -EINVAL;
216
217 /* timeout is the timeout in seconds, timeoutM is
218 the timeout in minutes) */
219 timeout = t;
220 timeoutM = tmrval;
221 return 0;
222}
223
224/*
225 * Get the watchdog status
226 */
227
228static int wdt977_get_status(int *status)
229{
230 int new_status;
231 unsigned long flags;
232
233 spin_lock_irqsave(&spinlock, flags);
234
235 /* unlock the SuperIO chip */
236 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
237 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
238
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);
244
245 /* lock the SuperIO chip */
246 outb_p(LOCK_DATA, IO_INDEX_PORT);
247
248 spin_unlock_irqrestore(&spinlock, flags);
249
250 *status = 0;
251 if (new_status & 1)
252 *status |= WDIOF_CARDRESET;
253
254 return 0;
255}
256
257
258/*
259 * /dev/watchdog handling
260 */
261
262static int wdt977_open(struct inode *inode, struct file *file)
263{
264 /* If the watchdog is alive we don't need to start it again */
265 if (test_and_set_bit(0, &timer_alive))
266 return -EBUSY;
267
268 if (nowayout)
269 __module_get(THIS_MODULE);
270
271 wdt977_start();
272 return stream_open(inode, file);
273}
274
275static int wdt977_release(struct inode *inode, struct file *file)
276{
277 /*
278 * Shut off the timer.
279 * Lock it in if it's a module and we set nowayout
280 */
281 if (expect_close == 42) {
282 wdt977_stop();
283 clear_bit(0, &timer_alive);
284 } else {
285 wdt977_keepalive();
286 pr_crit("Unexpected close, not stopping watchdog!\n");
287 }
288 expect_close = 0;
289 return 0;
290}
291
292
293/*
294 * wdt977_write:
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
299 *
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.
302 */
303
304static ssize_t wdt977_write(struct file *file, const char __user *buf,
305 size_t count, loff_t *ppos)
306{
307 if (count) {
308 if (!nowayout) {
309 size_t i;
310
311 /* In case it was set long ago */
312 expect_close = 0;
313
314 for (i = 0; i != count; i++) {
315 char c;
316 if (get_user(c, buf + i))
317 return -EFAULT;
318 if (c == 'V')
319 expect_close = 42;
320 }
321 }
322
323 /* someone wrote to us, we should restart timer */
324 wdt977_keepalive();
325 }
326 return count;
327}
328
329static const struct watchdog_info ident = {
330 .options = WDIOF_SETTIMEOUT |
331 WDIOF_MAGICCLOSE |
332 WDIOF_KEEPALIVEPING,
333 .firmware_version = 1,
334 .identity = WATCHDOG_NAME,
335};
336
337/*
338 * wdt977_ioctl:
339 * @inode: inode of the device
340 * @file: file handle to the device
341 * @cmd: watchdog command
342 * @arg: argument pointer
343 *
344 * The watchdog API defines a common set of functions for all watchdogs
345 * according to their available features.
346 */
347
348static long wdt977_ioctl(struct file *file, unsigned int cmd,
349 unsigned long arg)
350{
351 int status;
352 int new_options, retval = -EINVAL;
353 int new_timeout;
354 union {
355 struct watchdog_info __user *ident;
356 int __user *i;
357 } uarg;
358
359 uarg.i = (int __user *)arg;
360
361 switch (cmd) {
362 case WDIOC_GETSUPPORT:
363 return copy_to_user(uarg.ident, &ident,
364 sizeof(ident)) ? -EFAULT : 0;
365
366 case WDIOC_GETSTATUS:
367 wdt977_get_status(&status);
368 return put_user(status, uarg.i);
369
370 case WDIOC_GETBOOTSTATUS:
371 return put_user(0, uarg.i);
372
373 case WDIOC_SETOPTIONS:
374 if (get_user(new_options, uarg.i))
375 return -EFAULT;
376
377 if (new_options & WDIOS_DISABLECARD) {
378 wdt977_stop();
379 retval = 0;
380 }
381
382 if (new_options & WDIOS_ENABLECARD) {
383 wdt977_start();
384 retval = 0;
385 }
386
387 return retval;
388
389 case WDIOC_KEEPALIVE:
390 wdt977_keepalive();
391 return 0;
392
393 case WDIOC_SETTIMEOUT:
394 if (get_user(new_timeout, uarg.i))
395 return -EFAULT;
396
397 if (wdt977_set_timeout(new_timeout))
398 return -EINVAL;
399
400 wdt977_keepalive();
401 fallthrough;
402
403 case WDIOC_GETTIMEOUT:
404 return put_user(timeout, uarg.i);
405
406 default:
407 return -ENOTTY;
408
409 }
410}
411
412static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
413 void *unused)
414{
415 if (code == SYS_DOWN || code == SYS_HALT)
416 wdt977_stop();
417 return NOTIFY_DONE;
418}
419
420static const struct file_operations wdt977_fops = {
421 .owner = THIS_MODULE,
422 .llseek = no_llseek,
423 .write = wdt977_write,
424 .unlocked_ioctl = wdt977_ioctl,
425 .compat_ioctl = compat_ptr_ioctl,
426 .open = wdt977_open,
427 .release = wdt977_release,
428};
429
430static struct miscdevice wdt977_miscdev = {
431 .minor = WATCHDOG_MINOR,
432 .name = "watchdog",
433 .fops = &wdt977_fops,
434};
435
436static struct notifier_block wdt977_notifier = {
437 .notifier_call = wdt977_notify_sys,
438};
439
440static int __init wd977_init(void)
441{
442 int rc;
443
444 pr_info("driver v%s\n", WATCHDOG_VERSION);
445
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",
451 DEFAULT_TIMEOUT);
452 }
453
454 /* on Netwinder the IOports are already reserved by
455 * arch/arm/mach-footbridge/netwinder-hw.c
456 */
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",
460 IO_INDEX_PORT);
461 rc = -EIO;
462 goto err_out;
463 }
464 }
465
466 rc = register_reboot_notifier(&wdt977_notifier);
467 if (rc) {
468 pr_err("cannot register reboot notifier (err=%d)\n", rc);
469 goto err_out_region;
470 }
471
472 rc = misc_register(&wdt977_miscdev);
473 if (rc) {
474 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
475 wdt977_miscdev.minor, rc);
476 goto err_out_reboot;
477 }
478
479 pr_info("initialized. timeout=%d sec (nowayout=%d, testmode=%i)\n",
480 timeout, nowayout, testmode);
481
482 return 0;
483
484err_out_reboot:
485 unregister_reboot_notifier(&wdt977_notifier);
486err_out_region:
487 if (!machine_is_netwinder())
488 release_region(IO_INDEX_PORT, 2);
489err_out:
490 return rc;
491}
492
493static void __exit wd977_exit(void)
494{
495 wdt977_stop();
496 misc_deregister(&wdt977_miscdev);
497 unregister_reboot_notifier(&wdt977_notifier);
498 release_region(IO_INDEX_PORT, 2);
499}
500
501module_init(wd977_init);
502module_exit(wd977_exit);
503
504MODULE_AUTHOR("Woody Suwalski <woodys@xandros.com>");
505MODULE_DESCRIPTION("W83977AF Watchdog driver");
506MODULE_LICENSE("GPL");
1/*
2 * Wdt977 0.04: A Watchdog Device for Netwinder W83977AF chip
3 *
4 * (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
5 *
6 * -----------------------
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 * -----------------------
14 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
15 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
16 * 19-Dec-2001 Woody Suwalski: Netwinder fixes, ioctl interface
17 * 06-Jan-2002 Woody Suwalski: For compatibility, convert all timeouts
18 * from minutes to seconds.
19 * 07-Jul-2003 Daniele Bellucci: Audit return code of misc_register in
20 * nwwatchdog_init.
21 * 25-Oct-2005 Woody Suwalski: Convert addresses to #defs, add spinlocks
22 * remove limitiation to be used on
23 * Netwinders only
24 */
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/types.h>
29#include <linux/kernel.h>
30#include <linux/fs.h>
31#include <linux/miscdevice.h>
32#include <linux/init.h>
33#include <linux/ioport.h>
34#include <linux/watchdog.h>
35#include <linux/notifier.h>
36#include <linux/reboot.h>
37#include <linux/io.h>
38#include <linux/uaccess.h>
39
40#include <asm/system.h>
41#include <asm/mach-types.h>
42
43#define WATCHDOG_VERSION "0.04"
44#define WATCHDOG_NAME "Wdt977"
45#define PFX WATCHDOG_NAME ": "
46#define DRIVER_VERSION WATCHDOG_NAME " driver, v" WATCHDOG_VERSION "\n"
47
48#define IO_INDEX_PORT 0x370 /* on some systems it can be 0x3F0 */
49#define IO_DATA_PORT (IO_INDEX_PORT + 1)
50
51#define UNLOCK_DATA 0x87
52#define LOCK_DATA 0xAA
53#define DEVICE_REGISTER 0x07
54
55
56#define DEFAULT_TIMEOUT 60 /* default timeout in seconds */
57
58static int timeout = DEFAULT_TIMEOUT;
59static int timeoutM; /* timeout in minutes */
60static unsigned long timer_alive;
61static int testmode;
62static char expect_close;
63static DEFINE_SPINLOCK(spinlock);
64
65module_param(timeout, int, 0);
66MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (60..15300, default="
67 __MODULE_STRING(DEFAULT_TIMEOUT) ")");
68module_param(testmode, int, 0);
69MODULE_PARM_DESC(testmode, "Watchdog testmode (1 = no reboot), default=0");
70
71static int nowayout = WATCHDOG_NOWAYOUT;
72module_param(nowayout, int, 0);
73MODULE_PARM_DESC(nowayout,
74 "Watchdog cannot be stopped once started (default="
75 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
76
77/*
78 * Start the watchdog
79 */
80
81static int wdt977_start(void)
82{
83 unsigned long flags;
84
85 spin_lock_irqsave(&spinlock, flags);
86
87 /* unlock the SuperIO chip */
88 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
89 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
90
91 /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
92 * F2 has the timeout in minutes
93 * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
94 * at timeout, and to reset timer on kbd/mouse activity (not impl.)
95 * F4 is used to just clear the TIMEOUT'ed state (bit 0)
96 */
97 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
98 outb_p(0x08, IO_DATA_PORT);
99 outb_p(0xF2, IO_INDEX_PORT);
100 outb_p(timeoutM, IO_DATA_PORT);
101 outb_p(0xF3, IO_INDEX_PORT);
102 outb_p(0x00, IO_DATA_PORT); /* another setting is 0E for
103 kbd/mouse/LED */
104 outb_p(0xF4, IO_INDEX_PORT);
105 outb_p(0x00, IO_DATA_PORT);
106
107 /* At last select device Aux1 (dev=7) and set GP16 as a
108 * watchdog output. In test mode watch the bit 1 on F4 to
109 * indicate "triggered"
110 */
111 if (!testmode) {
112 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
113 outb_p(0x07, IO_DATA_PORT);
114 outb_p(0xE6, IO_INDEX_PORT);
115 outb_p(0x08, IO_DATA_PORT);
116 }
117
118 /* lock the SuperIO chip */
119 outb_p(LOCK_DATA, IO_INDEX_PORT);
120
121 spin_unlock_irqrestore(&spinlock, flags);
122 printk(KERN_INFO PFX "activated.\n");
123
124 return 0;
125}
126
127/*
128 * Stop the watchdog
129 */
130
131static int wdt977_stop(void)
132{
133 unsigned long flags;
134 spin_lock_irqsave(&spinlock, flags);
135
136 /* unlock the SuperIO chip */
137 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
138 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
139
140 /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
141 * F3 is reset to its default state
142 * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
143 * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
144 */
145 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
146 outb_p(0x08, IO_DATA_PORT);
147 outb_p(0xF2, IO_INDEX_PORT);
148 outb_p(0xFF, IO_DATA_PORT);
149 outb_p(0xF3, IO_INDEX_PORT);
150 outb_p(0x00, IO_DATA_PORT);
151 outb_p(0xF4, IO_INDEX_PORT);
152 outb_p(0x00, IO_DATA_PORT);
153 outb_p(0xF2, IO_INDEX_PORT);
154 outb_p(0x00, IO_DATA_PORT);
155
156 /* at last select device Aux1 (dev=7) and set
157 GP16 as a watchdog output */
158 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
159 outb_p(0x07, IO_DATA_PORT);
160 outb_p(0xE6, IO_INDEX_PORT);
161 outb_p(0x08, IO_DATA_PORT);
162
163 /* lock the SuperIO chip */
164 outb_p(LOCK_DATA, IO_INDEX_PORT);
165
166 spin_unlock_irqrestore(&spinlock, flags);
167 printk(KERN_INFO PFX "shutdown.\n");
168
169 return 0;
170}
171
172/*
173 * Send a keepalive ping to the watchdog
174 * This is done by simply re-writing the timeout to reg. 0xF2
175 */
176
177static int wdt977_keepalive(void)
178{
179 unsigned long flags;
180 spin_lock_irqsave(&spinlock, flags);
181
182 /* unlock the SuperIO chip */
183 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
184 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
185
186 /* select device Aux2 (device=8) and kicks watchdog reg F2 */
187 /* F2 has the timeout in minutes */
188 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
189 outb_p(0x08, IO_DATA_PORT);
190 outb_p(0xF2, IO_INDEX_PORT);
191 outb_p(timeoutM, IO_DATA_PORT);
192
193 /* lock the SuperIO chip */
194 outb_p(LOCK_DATA, IO_INDEX_PORT);
195 spin_unlock_irqrestore(&spinlock, flags);
196
197 return 0;
198}
199
200/*
201 * Set the watchdog timeout value
202 */
203
204static int wdt977_set_timeout(int t)
205{
206 int tmrval;
207
208 /* convert seconds to minutes, rounding up */
209 tmrval = (t + 59) / 60;
210
211 if (machine_is_netwinder()) {
212 /* we have a hw bug somewhere, so each 977 minute is actually
213 * only 30sec. This limits the max timeout to half of device
214 * max of 255 minutes...
215 */
216 tmrval += tmrval;
217 }
218
219 if (tmrval < 1 || tmrval > 255)
220 return -EINVAL;
221
222 /* timeout is the timeout in seconds, timeoutM is
223 the timeout in minutes) */
224 timeout = t;
225 timeoutM = tmrval;
226 return 0;
227}
228
229/*
230 * Get the watchdog status
231 */
232
233static int wdt977_get_status(int *status)
234{
235 int new_status;
236 unsigned long flags;
237
238 spin_lock_irqsave(&spinlock, flags);
239
240 /* unlock the SuperIO chip */
241 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
242 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
243
244 /* select device Aux2 (device=8) and read watchdog reg F4 */
245 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
246 outb_p(0x08, IO_DATA_PORT);
247 outb_p(0xF4, IO_INDEX_PORT);
248 new_status = inb_p(IO_DATA_PORT);
249
250 /* lock the SuperIO chip */
251 outb_p(LOCK_DATA, IO_INDEX_PORT);
252
253 spin_unlock_irqrestore(&spinlock, flags);
254
255 *status = 0;
256 if (new_status & 1)
257 *status |= WDIOF_CARDRESET;
258
259 return 0;
260}
261
262
263/*
264 * /dev/watchdog handling
265 */
266
267static int wdt977_open(struct inode *inode, struct file *file)
268{
269 /* If the watchdog is alive we don't need to start it again */
270 if (test_and_set_bit(0, &timer_alive))
271 return -EBUSY;
272
273 if (nowayout)
274 __module_get(THIS_MODULE);
275
276 wdt977_start();
277 return nonseekable_open(inode, file);
278}
279
280static int wdt977_release(struct inode *inode, struct file *file)
281{
282 /*
283 * Shut off the timer.
284 * Lock it in if it's a module and we set nowayout
285 */
286 if (expect_close == 42) {
287 wdt977_stop();
288 clear_bit(0, &timer_alive);
289 } else {
290 wdt977_keepalive();
291 printk(KERN_CRIT PFX
292 "Unexpected close, not stopping watchdog!\n");
293 }
294 expect_close = 0;
295 return 0;
296}
297
298
299/*
300 * wdt977_write:
301 * @file: file handle to the watchdog
302 * @buf: buffer to write (unused as data does not matter here
303 * @count: count of bytes
304 * @ppos: pointer to the position to write. No seeks allowed
305 *
306 * A write to a watchdog device is defined as a keepalive signal. Any
307 * write of data will do, as we we don't define content meaning.
308 */
309
310static ssize_t wdt977_write(struct file *file, const char __user *buf,
311 size_t count, loff_t *ppos)
312{
313 if (count) {
314 if (!nowayout) {
315 size_t i;
316
317 /* In case it was set long ago */
318 expect_close = 0;
319
320 for (i = 0; i != count; i++) {
321 char c;
322 if (get_user(c, buf + i))
323 return -EFAULT;
324 if (c == 'V')
325 expect_close = 42;
326 }
327 }
328
329 /* someone wrote to us, we should restart timer */
330 wdt977_keepalive();
331 }
332 return count;
333}
334
335static const struct watchdog_info ident = {
336 .options = WDIOF_SETTIMEOUT |
337 WDIOF_MAGICCLOSE |
338 WDIOF_KEEPALIVEPING,
339 .firmware_version = 1,
340 .identity = WATCHDOG_NAME,
341};
342
343/*
344 * wdt977_ioctl:
345 * @inode: inode of the device
346 * @file: file handle to the device
347 * @cmd: watchdog command
348 * @arg: argument pointer
349 *
350 * The watchdog API defines a common set of functions for all watchdogs
351 * according to their available features.
352 */
353
354static long wdt977_ioctl(struct file *file, unsigned int cmd,
355 unsigned long arg)
356{
357 int status;
358 int new_options, retval = -EINVAL;
359 int new_timeout;
360 union {
361 struct watchdog_info __user *ident;
362 int __user *i;
363 } uarg;
364
365 uarg.i = (int __user *)arg;
366
367 switch (cmd) {
368 case WDIOC_GETSUPPORT:
369 return copy_to_user(uarg.ident, &ident,
370 sizeof(ident)) ? -EFAULT : 0;
371
372 case WDIOC_GETSTATUS:
373 wdt977_get_status(&status);
374 return put_user(status, uarg.i);
375
376 case WDIOC_GETBOOTSTATUS:
377 return put_user(0, uarg.i);
378
379 case WDIOC_SETOPTIONS:
380 if (get_user(new_options, uarg.i))
381 return -EFAULT;
382
383 if (new_options & WDIOS_DISABLECARD) {
384 wdt977_stop();
385 retval = 0;
386 }
387
388 if (new_options & WDIOS_ENABLECARD) {
389 wdt977_start();
390 retval = 0;
391 }
392
393 return retval;
394
395 case WDIOC_KEEPALIVE:
396 wdt977_keepalive();
397 return 0;
398
399 case WDIOC_SETTIMEOUT:
400 if (get_user(new_timeout, uarg.i))
401 return -EFAULT;
402
403 if (wdt977_set_timeout(new_timeout))
404 return -EINVAL;
405
406 wdt977_keepalive();
407 /* Fall */
408
409 case WDIOC_GETTIMEOUT:
410 return put_user(timeout, uarg.i);
411
412 default:
413 return -ENOTTY;
414
415 }
416}
417
418static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
419 void *unused)
420{
421 if (code == SYS_DOWN || code == SYS_HALT)
422 wdt977_stop();
423 return NOTIFY_DONE;
424}
425
426static const struct file_operations wdt977_fops = {
427 .owner = THIS_MODULE,
428 .llseek = no_llseek,
429 .write = wdt977_write,
430 .unlocked_ioctl = wdt977_ioctl,
431 .open = wdt977_open,
432 .release = wdt977_release,
433};
434
435static struct miscdevice wdt977_miscdev = {
436 .minor = WATCHDOG_MINOR,
437 .name = "watchdog",
438 .fops = &wdt977_fops,
439};
440
441static struct notifier_block wdt977_notifier = {
442 .notifier_call = wdt977_notify_sys,
443};
444
445static int __init wd977_init(void)
446{
447 int rc;
448
449 printk(KERN_INFO PFX DRIVER_VERSION);
450
451 /* Check that the timeout value is within its range;
452 if not reset to the default */
453 if (wdt977_set_timeout(timeout)) {
454 wdt977_set_timeout(DEFAULT_TIMEOUT);
455 printk(KERN_INFO PFX
456 "timeout value must be 60 < timeout < 15300, using %d\n",
457 DEFAULT_TIMEOUT);
458 }
459
460 /* on Netwinder the IOports are already reserved by
461 * arch/arm/mach-footbridge/netwinder-hw.c
462 */
463 if (!machine_is_netwinder()) {
464 if (!request_region(IO_INDEX_PORT, 2, WATCHDOG_NAME)) {
465 printk(KERN_ERR PFX
466 "I/O address 0x%04x already in use\n",
467 IO_INDEX_PORT);
468 rc = -EIO;
469 goto err_out;
470 }
471 }
472
473 rc = register_reboot_notifier(&wdt977_notifier);
474 if (rc) {
475 printk(KERN_ERR PFX
476 "cannot register reboot notifier (err=%d)\n", rc);
477 goto err_out_region;
478 }
479
480 rc = misc_register(&wdt977_miscdev);
481 if (rc) {
482 printk(KERN_ERR PFX
483 "cannot register miscdev on minor=%d (err=%d)\n",
484 wdt977_miscdev.minor, rc);
485 goto err_out_reboot;
486 }
487
488 printk(KERN_INFO PFX
489 "initialized. timeout=%d sec (nowayout=%d, testmode=%i)\n",
490 timeout, nowayout, testmode);
491
492 return 0;
493
494err_out_reboot:
495 unregister_reboot_notifier(&wdt977_notifier);
496err_out_region:
497 if (!machine_is_netwinder())
498 release_region(IO_INDEX_PORT, 2);
499err_out:
500 return rc;
501}
502
503static void __exit wd977_exit(void)
504{
505 wdt977_stop();
506 misc_deregister(&wdt977_miscdev);
507 unregister_reboot_notifier(&wdt977_notifier);
508 release_region(IO_INDEX_PORT, 2);
509}
510
511module_init(wd977_init);
512module_exit(wd977_exit);
513
514MODULE_AUTHOR("Woody Suwalski <woodys@xandros.com>");
515MODULE_DESCRIPTION("W83977AF Watchdog driver");
516MODULE_LICENSE("GPL");
517MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);