xref: /openbmc/linux/drivers/watchdog/f71808e_wdt.c (revision baa7eb025ab14f3cba2e35c0a8648f9c9f01d24f)
1 /***************************************************************************
2  *   Copyright (C) 2006 by Hans Edgington <hans@edgington.nl>              *
3  *   Copyright (C) 2007-2009 Hans de Goede <hdegoede@redhat.com>           *
4  *   Copyright (C) 2010 Giel van Schijndel <me@mortis.eu>                  *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20  ***************************************************************************/
21 
22 #include <linux/err.h>
23 #include <linux/fs.h>
24 #include <linux/init.h>
25 #include <linux/io.h>
26 #include <linux/ioport.h>
27 #include <linux/miscdevice.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/notifier.h>
31 #include <linux/reboot.h>
32 #include <linux/uaccess.h>
33 #include <linux/watchdog.h>
34 
35 #define DRVNAME "f71808e_wdt"
36 
37 #define SIO_F71808FG_LD_WDT	0x07	/* Watchdog timer logical device */
38 #define SIO_UNLOCK_KEY		0x87	/* Key to enable Super-I/O */
39 #define SIO_LOCK_KEY		0xAA	/* Key to diasble Super-I/O */
40 
41 #define SIO_REG_LDSEL		0x07	/* Logical device select */
42 #define SIO_REG_DEVID		0x20	/* Device ID (2 bytes) */
43 #define SIO_REG_DEVREV		0x22	/* Device revision */
44 #define SIO_REG_MANID		0x23	/* Fintek ID (2 bytes) */
45 #define SIO_REG_ENABLE		0x30	/* Logical device enable */
46 #define SIO_REG_ADDR		0x60	/* Logical device address (2 bytes) */
47 
48 #define SIO_FINTEK_ID		0x1934	/* Manufacturers ID */
49 #define SIO_F71808_ID		0x0901  /* Chipset ID */
50 #define SIO_F71858_ID		0x0507  /* Chipset ID */
51 #define SIO_F71862_ID		0x0601	/* Chipset ID */
52 #define SIO_F71882_ID		0x0541	/* Chipset ID */
53 #define SIO_F71889_ID		0x0723	/* Chipset ID */
54 
55 #define	F71882FG_REG_START		0x01
56 
57 #define F71808FG_REG_WDO_CONF		0xf0
58 #define F71808FG_REG_WDT_CONF		0xf5
59 #define F71808FG_REG_WD_TIME		0xf6
60 
61 #define F71808FG_FLAG_WDOUT_EN		7
62 
63 #define F71808FG_FLAG_WDTMOUT_STS	5
64 #define F71808FG_FLAG_WD_EN		5
65 #define F71808FG_FLAG_WD_PULSE		4
66 #define F71808FG_FLAG_WD_UNIT		3
67 
68 /* Default values */
69 #define WATCHDOG_TIMEOUT	60	/* 1 minute default timeout */
70 #define WATCHDOG_MAX_TIMEOUT	(60 * 255)
71 #define WATCHDOG_PULSE_WIDTH	125	/* 125 ms, default pulse width for
72 					   watchdog signal */
73 
74 static unsigned short force_id;
75 module_param(force_id, ushort, 0);
76 MODULE_PARM_DESC(force_id, "Override the detected device ID");
77 
78 static const int max_timeout = WATCHDOG_MAX_TIMEOUT;
79 static int timeout = 60;	/* default timeout in seconds */
80 module_param(timeout, int, 0);
81 MODULE_PARM_DESC(timeout,
82 	"Watchdog timeout in seconds. 1<= timeout <="
83 			__MODULE_STRING(WATCHDOG_MAX_TIMEOUT) " (default="
84 			__MODULE_STRING(WATCHDOG_TIMEOUT) ")");
85 
86 static unsigned int pulse_width = WATCHDOG_PULSE_WIDTH;
87 module_param(pulse_width, uint, 0);
88 MODULE_PARM_DESC(pulse_width,
89 	"Watchdog signal pulse width. 0(=level), 1 ms, 25 ms, 125 ms or 5000 ms"
90 			" (default=" __MODULE_STRING(WATCHDOG_PULSE_WIDTH) ")");
91 
92 static int nowayout = WATCHDOG_NOWAYOUT;
93 module_param(nowayout, bool, 0444);
94 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
95 
96 static unsigned int start_withtimeout;
97 module_param(start_withtimeout, uint, 0);
98 MODULE_PARM_DESC(start_withtimeout, "Start watchdog timer on module load with"
99 	" given initial timeout. Zero (default) disables this feature.");
100 
101 enum chips { f71808fg, f71858fg, f71862fg, f71882fg, f71889fg };
102 
103 static const char *f71808e_names[] = {
104 	"f71808fg",
105 	"f71858fg",
106 	"f71862fg",
107 	"f71882fg",
108 	"f71889fg",
109 };
110 
111 /* Super-I/O Function prototypes */
112 static inline int superio_inb(int base, int reg);
113 static inline int superio_inw(int base, int reg);
114 static inline void superio_outb(int base, int reg, u8 val);
115 static inline void superio_set_bit(int base, int reg, int bit);
116 static inline void superio_clear_bit(int base, int reg, int bit);
117 static inline int superio_enter(int base);
118 static inline void superio_select(int base, int ld);
119 static inline void superio_exit(int base);
120 
121 struct watchdog_data {
122 	unsigned short	sioaddr;
123 	enum chips	type;
124 	unsigned long	opened;
125 	struct mutex	lock;
126 	char		expect_close;
127 	struct watchdog_info ident;
128 
129 	unsigned short	timeout;
130 	u8		timer_val;	/* content for the wd_time register */
131 	char		minutes_mode;
132 	u8		pulse_val;	/* pulse width flag */
133 	char		pulse_mode;	/* enable pulse output mode? */
134 	char		caused_reboot;	/* last reboot was by the watchdog */
135 };
136 
137 static struct watchdog_data watchdog = {
138 	.lock = __MUTEX_INITIALIZER(watchdog.lock),
139 };
140 
141 /* Super I/O functions */
142 static inline int superio_inb(int base, int reg)
143 {
144 	outb(reg, base);
145 	return inb(base + 1);
146 }
147 
148 static int superio_inw(int base, int reg)
149 {
150 	int val;
151 	val  = superio_inb(base, reg) << 8;
152 	val |= superio_inb(base, reg + 1);
153 	return val;
154 }
155 
156 static inline void superio_outb(int base, int reg, u8 val)
157 {
158 	outb(reg, base);
159 	outb(val, base + 1);
160 }
161 
162 static inline void superio_set_bit(int base, int reg, int bit)
163 {
164 	unsigned long val = superio_inb(base, reg);
165 	__set_bit(bit, &val);
166 	superio_outb(base, reg, val);
167 }
168 
169 static inline void superio_clear_bit(int base, int reg, int bit)
170 {
171 	unsigned long val = superio_inb(base, reg);
172 	__clear_bit(bit, &val);
173 	superio_outb(base, reg, val);
174 }
175 
176 static inline int superio_enter(int base)
177 {
178 	/* Don't step on other drivers' I/O space by accident */
179 	if (!request_muxed_region(base, 2, DRVNAME)) {
180 		printk(KERN_ERR DRVNAME ": I/O address 0x%04x already in use\n",
181 				(int)base);
182 		return -EBUSY;
183 	}
184 
185 	/* according to the datasheet the key must be send twice! */
186 	outb(SIO_UNLOCK_KEY, base);
187 	outb(SIO_UNLOCK_KEY, base);
188 
189 	return 0;
190 }
191 
192 static inline void superio_select(int base, int ld)
193 {
194 	outb(SIO_REG_LDSEL, base);
195 	outb(ld, base + 1);
196 }
197 
198 static inline void superio_exit(int base)
199 {
200 	outb(SIO_LOCK_KEY, base);
201 	release_region(base, 2);
202 }
203 
204 static int watchdog_set_timeout(int timeout)
205 {
206 	if (timeout <= 0
207 	 || timeout >  max_timeout) {
208 		printk(KERN_ERR DRVNAME ": watchdog timeout out of range\n");
209 		return -EINVAL;
210 	}
211 
212 	mutex_lock(&watchdog.lock);
213 
214 	watchdog.timeout = timeout;
215 	if (timeout > 0xff) {
216 		watchdog.timer_val = DIV_ROUND_UP(timeout, 60);
217 		watchdog.minutes_mode = true;
218 	} else {
219 		watchdog.timer_val = timeout;
220 		watchdog.minutes_mode = false;
221 	}
222 
223 	mutex_unlock(&watchdog.lock);
224 
225 	return 0;
226 }
227 
228 static int watchdog_set_pulse_width(unsigned int pw)
229 {
230 	int err = 0;
231 
232 	mutex_lock(&watchdog.lock);
233 
234 	if        (pw <=    1) {
235 		watchdog.pulse_val = 0;
236 	} else if (pw <=   25) {
237 		watchdog.pulse_val = 1;
238 	} else if (pw <=  125) {
239 		watchdog.pulse_val = 2;
240 	} else if (pw <= 5000) {
241 		watchdog.pulse_val = 3;
242 	} else {
243 		printk(KERN_ERR DRVNAME ": pulse width out of range\n");
244 		err = -EINVAL;
245 		goto exit_unlock;
246 	}
247 
248 	watchdog.pulse_mode = pw;
249 
250 exit_unlock:
251 	mutex_unlock(&watchdog.lock);
252 	return err;
253 }
254 
255 static int watchdog_keepalive(void)
256 {
257 	int err = 0;
258 
259 	mutex_lock(&watchdog.lock);
260 	err = superio_enter(watchdog.sioaddr);
261 	if (err)
262 		goto exit_unlock;
263 	superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
264 
265 	if (watchdog.minutes_mode)
266 		/* select minutes for timer units */
267 		superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
268 				F71808FG_FLAG_WD_UNIT);
269 	else
270 		/* select seconds for timer units */
271 		superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
272 				F71808FG_FLAG_WD_UNIT);
273 
274 	/* Set timer value */
275 	superio_outb(watchdog.sioaddr, F71808FG_REG_WD_TIME,
276 			   watchdog.timer_val);
277 
278 	superio_exit(watchdog.sioaddr);
279 
280 exit_unlock:
281 	mutex_unlock(&watchdog.lock);
282 	return err;
283 }
284 
285 static int watchdog_start(void)
286 {
287 	/* Make sure we don't die as soon as the watchdog is enabled below */
288 	int err = watchdog_keepalive();
289 	if (err)
290 		return err;
291 
292 	mutex_lock(&watchdog.lock);
293 	err = superio_enter(watchdog.sioaddr);
294 	if (err)
295 		goto exit_unlock;
296 	superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
297 
298 	/* Watchdog pin configuration */
299 	switch (watchdog.type) {
300 	case f71808fg:
301 		/* Set pin 21 to GPIO23/WDTRST#, then to WDTRST# */
302 		superio_clear_bit(watchdog.sioaddr, 0x2a, 3);
303 		superio_clear_bit(watchdog.sioaddr, 0x2b, 3);
304 		break;
305 
306 	case f71882fg:
307 		/* Set pin 56 to WDTRST# */
308 		superio_set_bit(watchdog.sioaddr, 0x29, 1);
309 		break;
310 
311 	case f71889fg:
312 		/* set pin 40 to WDTRST# */
313 		superio_outb(watchdog.sioaddr, 0x2b,
314 				superio_inb(watchdog.sioaddr, 0x2b) & 0xcf);
315 		break;
316 
317 	default:
318 		/*
319 		 * 'default' label to shut up the compiler and catch
320 		 * programmer errors
321 		 */
322 		err = -ENODEV;
323 		goto exit_superio;
324 	}
325 
326 	superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
327 	superio_set_bit(watchdog.sioaddr, SIO_REG_ENABLE, 0);
328 	superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDO_CONF,
329 			F71808FG_FLAG_WDOUT_EN);
330 
331 	superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
332 			F71808FG_FLAG_WD_EN);
333 
334 	if (watchdog.pulse_mode) {
335 		/* Select "pulse" output mode with given duration */
336 		u8 wdt_conf = superio_inb(watchdog.sioaddr,
337 				F71808FG_REG_WDT_CONF);
338 
339 		/* Set WD_PSWIDTH bits (1:0) */
340 		wdt_conf = (wdt_conf & 0xfc) | (watchdog.pulse_val & 0x03);
341 		/* Set WD_PULSE to "pulse" mode */
342 		wdt_conf |= BIT(F71808FG_FLAG_WD_PULSE);
343 
344 		superio_outb(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
345 				wdt_conf);
346 	} else {
347 		/* Select "level" output mode */
348 		superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
349 				F71808FG_FLAG_WD_PULSE);
350 	}
351 
352 exit_superio:
353 	superio_exit(watchdog.sioaddr);
354 exit_unlock:
355 	mutex_unlock(&watchdog.lock);
356 
357 	return err;
358 }
359 
360 static int watchdog_stop(void)
361 {
362 	int err = 0;
363 
364 	mutex_lock(&watchdog.lock);
365 	err = superio_enter(watchdog.sioaddr);
366 	if (err)
367 		goto exit_unlock;
368 	superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
369 
370 	superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
371 			F71808FG_FLAG_WD_EN);
372 
373 	superio_exit(watchdog.sioaddr);
374 
375 exit_unlock:
376 	mutex_unlock(&watchdog.lock);
377 
378 	return err;
379 }
380 
381 static int watchdog_get_status(void)
382 {
383 	int status = 0;
384 
385 	mutex_lock(&watchdog.lock);
386 	status = (watchdog.caused_reboot) ? WDIOF_CARDRESET : 0;
387 	mutex_unlock(&watchdog.lock);
388 
389 	return status;
390 }
391 
392 static bool watchdog_is_running(void)
393 {
394 	/*
395 	 * if we fail to determine the watchdog's status assume it to be
396 	 * running to be on the safe side
397 	 */
398 	bool is_running = true;
399 
400 	mutex_lock(&watchdog.lock);
401 	if (superio_enter(watchdog.sioaddr))
402 		goto exit_unlock;
403 	superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
404 
405 	is_running = (superio_inb(watchdog.sioaddr, SIO_REG_ENABLE) & BIT(0))
406 		&& (superio_inb(watchdog.sioaddr, F71808FG_REG_WDT_CONF)
407 			& F71808FG_FLAG_WD_EN);
408 
409 	superio_exit(watchdog.sioaddr);
410 
411 exit_unlock:
412 	mutex_unlock(&watchdog.lock);
413 	return is_running;
414 }
415 
416 /* /dev/watchdog api */
417 
418 static int watchdog_open(struct inode *inode, struct file *file)
419 {
420 	int err;
421 
422 	/* If the watchdog is alive we don't need to start it again */
423 	if (test_and_set_bit(0, &watchdog.opened))
424 		return -EBUSY;
425 
426 	err = watchdog_start();
427 	if (err) {
428 		clear_bit(0, &watchdog.opened);
429 		return err;
430 	}
431 
432 	if (nowayout)
433 		__module_get(THIS_MODULE);
434 
435 	watchdog.expect_close = 0;
436 	return nonseekable_open(inode, file);
437 }
438 
439 static int watchdog_release(struct inode *inode, struct file *file)
440 {
441 	clear_bit(0, &watchdog.opened);
442 
443 	if (!watchdog.expect_close) {
444 		watchdog_keepalive();
445 		printk(KERN_CRIT DRVNAME
446 			": Unexpected close, not stopping watchdog!\n");
447 	} else if (!nowayout) {
448 		watchdog_stop();
449 	}
450 	return 0;
451 }
452 
453 /*
454  *      watchdog_write:
455  *      @file: file handle to the watchdog
456  *      @buf: buffer to write
457  *      @count: count of bytes
458  *      @ppos: pointer to the position to write. No seeks allowed
459  *
460  *      A write to a watchdog device is defined as a keepalive signal. Any
461  *      write of data will do, as we we don't define content meaning.
462  */
463 
464 static ssize_t watchdog_write(struct file *file, const char __user *buf,
465 			    size_t count, loff_t *ppos)
466 {
467 	if (count) {
468 		if (!nowayout) {
469 			size_t i;
470 
471 			/* In case it was set long ago */
472 			bool expect_close = false;
473 
474 			for (i = 0; i != count; i++) {
475 				char c;
476 				if (get_user(c, buf + i))
477 					return -EFAULT;
478 				expect_close = (c == 'V');
479 			}
480 
481 			/* Properly order writes across fork()ed processes */
482 			mutex_lock(&watchdog.lock);
483 			watchdog.expect_close = expect_close;
484 			mutex_unlock(&watchdog.lock);
485 		}
486 
487 		/* someone wrote to us, we should restart timer */
488 		watchdog_keepalive();
489 	}
490 	return count;
491 }
492 
493 /*
494  *      watchdog_ioctl:
495  *      @inode: inode of the device
496  *      @file: file handle to the device
497  *      @cmd: watchdog command
498  *      @arg: argument pointer
499  *
500  *      The watchdog API defines a common set of functions for all watchdogs
501  *      according to their available features.
502  */
503 static long watchdog_ioctl(struct file *file, unsigned int cmd,
504 	unsigned long arg)
505 {
506 	int status;
507 	int new_options;
508 	int new_timeout;
509 	union {
510 		struct watchdog_info __user *ident;
511 		int __user *i;
512 	} uarg;
513 
514 	uarg.i = (int __user *)arg;
515 
516 	switch (cmd) {
517 	case WDIOC_GETSUPPORT:
518 		return copy_to_user(uarg.ident, &watchdog.ident,
519 			sizeof(watchdog.ident)) ? -EFAULT : 0;
520 
521 	case WDIOC_GETSTATUS:
522 		status = watchdog_get_status();
523 		if (status < 0)
524 			return status;
525 		return put_user(status, uarg.i);
526 
527 	case WDIOC_GETBOOTSTATUS:
528 		return put_user(0, uarg.i);
529 
530 	case WDIOC_SETOPTIONS:
531 		if (get_user(new_options, uarg.i))
532 			return -EFAULT;
533 
534 		if (new_options & WDIOS_DISABLECARD)
535 			watchdog_stop();
536 
537 		if (new_options & WDIOS_ENABLECARD)
538 			return watchdog_start();
539 
540 
541 	case WDIOC_KEEPALIVE:
542 		watchdog_keepalive();
543 		return 0;
544 
545 	case WDIOC_SETTIMEOUT:
546 		if (get_user(new_timeout, uarg.i))
547 			return -EFAULT;
548 
549 		if (watchdog_set_timeout(new_timeout))
550 			return -EINVAL;
551 
552 		watchdog_keepalive();
553 		/* Fall */
554 
555 	case WDIOC_GETTIMEOUT:
556 		return put_user(watchdog.timeout, uarg.i);
557 
558 	default:
559 		return -ENOTTY;
560 
561 	}
562 }
563 
564 static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
565 	void *unused)
566 {
567 	if (code == SYS_DOWN || code == SYS_HALT)
568 		watchdog_stop();
569 	return NOTIFY_DONE;
570 }
571 
572 static const struct file_operations watchdog_fops = {
573 	.owner		= THIS_MODULE,
574 	.llseek		= no_llseek,
575 	.open		= watchdog_open,
576 	.release	= watchdog_release,
577 	.write		= watchdog_write,
578 	.unlocked_ioctl	= watchdog_ioctl,
579 };
580 
581 static struct miscdevice watchdog_miscdev = {
582 	.minor		= WATCHDOG_MINOR,
583 	.name		= "watchdog",
584 	.fops		= &watchdog_fops,
585 };
586 
587 static struct notifier_block watchdog_notifier = {
588 	.notifier_call = watchdog_notify_sys,
589 };
590 
591 static int __init watchdog_init(int sioaddr)
592 {
593 	int wdt_conf, err = 0;
594 
595 	/* No need to lock watchdog.lock here because no entry points
596 	 * into the module have been registered yet.
597 	 */
598 	watchdog.sioaddr = sioaddr;
599 	watchdog.ident.options = WDIOC_SETTIMEOUT
600 				| WDIOF_MAGICCLOSE
601 				| WDIOF_KEEPALIVEPING;
602 
603 	snprintf(watchdog.ident.identity,
604 		sizeof(watchdog.ident.identity), "%s watchdog",
605 		f71808e_names[watchdog.type]);
606 
607 	err = superio_enter(sioaddr);
608 	if (err)
609 		return err;
610 	superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
611 
612 	wdt_conf = superio_inb(sioaddr, F71808FG_REG_WDT_CONF);
613 	watchdog.caused_reboot = wdt_conf & F71808FG_FLAG_WDTMOUT_STS;
614 
615 	superio_exit(sioaddr);
616 
617 	err = watchdog_set_timeout(timeout);
618 	if (err)
619 		return err;
620 	err = watchdog_set_pulse_width(pulse_width);
621 	if (err)
622 		return err;
623 
624 	err = register_reboot_notifier(&watchdog_notifier);
625 	if (err)
626 		return err;
627 
628 	err = misc_register(&watchdog_miscdev);
629 	if (err) {
630 		printk(KERN_ERR DRVNAME
631 			": cannot register miscdev on minor=%d\n",
632 				watchdog_miscdev.minor);
633 		goto exit_reboot;
634 	}
635 
636 	if (start_withtimeout) {
637 		if (start_withtimeout <= 0
638 		 || start_withtimeout >  max_timeout) {
639 			printk(KERN_ERR DRVNAME
640 				": starting timeout out of range\n");
641 			err = -EINVAL;
642 			goto exit_miscdev;
643 		}
644 
645 		err = watchdog_start();
646 		if (err) {
647 			printk(KERN_ERR DRVNAME
648 				": cannot start watchdog timer\n");
649 			goto exit_miscdev;
650 		}
651 
652 		mutex_lock(&watchdog.lock);
653 		err = superio_enter(sioaddr);
654 		if (err)
655 			goto exit_unlock;
656 		superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
657 
658 		if (start_withtimeout > 0xff) {
659 			/* select minutes for timer units */
660 			superio_set_bit(sioaddr, F71808FG_REG_WDT_CONF,
661 				F71808FG_FLAG_WD_UNIT);
662 			superio_outb(sioaddr, F71808FG_REG_WD_TIME,
663 				DIV_ROUND_UP(start_withtimeout, 60));
664 		} else {
665 			/* select seconds for timer units */
666 			superio_clear_bit(sioaddr, F71808FG_REG_WDT_CONF,
667 				F71808FG_FLAG_WD_UNIT);
668 			superio_outb(sioaddr, F71808FG_REG_WD_TIME,
669 				start_withtimeout);
670 		}
671 
672 		superio_exit(sioaddr);
673 		mutex_unlock(&watchdog.lock);
674 
675 		if (nowayout)
676 			__module_get(THIS_MODULE);
677 
678 		printk(KERN_INFO DRVNAME
679 			": watchdog started with initial timeout of %u sec\n",
680 			start_withtimeout);
681 	}
682 
683 	return 0;
684 
685 exit_unlock:
686 	mutex_unlock(&watchdog.lock);
687 exit_miscdev:
688 	misc_deregister(&watchdog_miscdev);
689 exit_reboot:
690 	unregister_reboot_notifier(&watchdog_notifier);
691 
692 	return err;
693 }
694 
695 static int __init f71808e_find(int sioaddr)
696 {
697 	u16 devid;
698 	int err = superio_enter(sioaddr);
699 	if (err)
700 		return err;
701 
702 	devid = superio_inw(sioaddr, SIO_REG_MANID);
703 	if (devid != SIO_FINTEK_ID) {
704 		pr_debug(DRVNAME ": Not a Fintek device\n");
705 		err = -ENODEV;
706 		goto exit;
707 	}
708 
709 	devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
710 	switch (devid) {
711 	case SIO_F71808_ID:
712 		watchdog.type = f71808fg;
713 		break;
714 	case SIO_F71882_ID:
715 		watchdog.type = f71882fg;
716 		break;
717 	case SIO_F71889_ID:
718 		watchdog.type = f71889fg;
719 		break;
720 	case SIO_F71862_ID:
721 		/* These have a watchdog, though it isn't implemented (yet). */
722 		err = -ENOSYS;
723 		goto exit;
724 	case SIO_F71858_ID:
725 		/* Confirmed (by datasheet) not to have a watchdog. */
726 		err = -ENODEV;
727 		goto exit;
728 	default:
729 		printk(KERN_INFO DRVNAME ": Unrecognized Fintek device: %04x\n",
730 		       (unsigned int)devid);
731 		err = -ENODEV;
732 		goto exit;
733 	}
734 
735 	printk(KERN_INFO DRVNAME ": Found %s watchdog chip, revision %d\n",
736 		f71808e_names[watchdog.type],
737 		(int)superio_inb(sioaddr, SIO_REG_DEVREV));
738 exit:
739 	superio_exit(sioaddr);
740 	return err;
741 }
742 
743 static int __init f71808e_init(void)
744 {
745 	static const unsigned short addrs[] = { 0x2e, 0x4e };
746 	int err = -ENODEV;
747 	int i;
748 
749 	for (i = 0; i < ARRAY_SIZE(addrs); i++) {
750 		err = f71808e_find(addrs[i]);
751 		if (err == 0)
752 			break;
753 	}
754 	if (i == ARRAY_SIZE(addrs))
755 		return err;
756 
757 	return watchdog_init(addrs[i]);
758 }
759 
760 static void __exit f71808e_exit(void)
761 {
762 	if (watchdog_is_running()) {
763 		printk(KERN_WARNING DRVNAME
764 			": Watchdog timer still running, stopping it\n");
765 		watchdog_stop();
766 	}
767 	misc_deregister(&watchdog_miscdev);
768 	unregister_reboot_notifier(&watchdog_notifier);
769 }
770 
771 MODULE_DESCRIPTION("F71808E Watchdog Driver");
772 MODULE_AUTHOR("Giel van Schijndel <me@mortis.eu>");
773 MODULE_LICENSE("GPL");
774 
775 module_init(f71808e_init);
776 module_exit(f71808e_exit);
777