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