blob: ab5ec6d84b16d00d852d2d4f97e68a2a6343c2f0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Vlad Drukker0e94f2e2007-03-25 17:34:39 +02002 * w83627hf/thf WDT driver
3 *
Guenter Roeck30a836952013-10-28 19:43:57 -07004 * (c) Copyright 2013 Guenter Roeck
5 * converted to watchdog infrastructure
6 *
Vlad Drukker0e94f2e2007-03-25 17:34:39 +02007 * (c) Copyright 2007 Vlad Drukker <vlad@storewiz.com>
8 * added support for W83627THF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Al Virod36b6912011-12-29 17:09:01 -050010 * (c) Copyright 2003,2007 Pádraig Brady <P@draigBrady.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Based on advantechwdt.c which is based on wdt.c.
13 * Original copyright messages:
14 *
15 * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
16 *
Alan Cox29fa0582008-10-27 15:17:56 +000017 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
18 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * as published by the Free Software Foundation; either version
23 * 2 of the License, or (at your option) any later version.
24 *
25 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
26 * warranty for any of this software. This material is provided
27 * "AS-IS" and at no charge.
28 *
Alan Cox29fa0582008-10-27 15:17:56 +000029 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Joe Perches27c766a2012-02-15 15:06:19 -080032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioport.h>
39#include <linux/notifier.h>
40#include <linux/reboot.h>
41#include <linux/init.h>
Alan Cox46a39492008-05-19 14:09:18 +010042#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Benny Loenstrup Ammitzboell9c67bea2010-11-11 16:08:41 +010044#define WATCHDOG_NAME "w83627hf/thf/hg/dhg WDT"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* You must set this - there is no sane way to probe for this board. */
48static int wdt_io = 0x2E;
49module_param(wdt_io, int, 0);
Vlad Drukker0e94f2e2007-03-25 17:34:39 +020050MODULE_PARM_DESC(wdt_io, "w83627hf/thf WDT io port (default 0x2E)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Guenter Roeck30a836952013-10-28 19:43:57 -070052static int timeout; /* in seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053module_param(timeout, int, 0);
Alan Cox46a39492008-05-19 14:09:18 +010054MODULE_PARM_DESC(timeout,
55 "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
56 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010058static bool nowayout = WATCHDOG_NOWAYOUT;
59module_param(nowayout, bool, 0);
Alan Cox46a39492008-05-19 14:09:18 +010060MODULE_PARM_DESC(nowayout,
61 "Watchdog cannot be stopped once started (default="
62 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * Kernel methods.
66 */
67
68#define WDT_EFER (wdt_io+0) /* Extended Function Enable Registers */
Alan Cox46a39492008-05-19 14:09:18 +010069#define WDT_EFIR (wdt_io+0) /* Extended Function Index Register
70 (same as EFER) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define WDT_EFDR (WDT_EFIR+1) /* Extended Function Data Register */
72
Alan Cox46a39492008-05-19 14:09:18 +010073static void w83627hf_select_wd_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Vlad Drukker0e94f2e2007-03-25 17:34:39 +020075 unsigned char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 outb_p(0x87, WDT_EFER); /* Enter extended function mode */
77 outb_p(0x87, WDT_EFER); /* Again according to manual */
78
Pádraig Brady28dd1b02007-07-24 11:49:27 +010079 outb(0x20, WDT_EFER); /* check chip version */
Vlad Drukker0e94f2e2007-03-25 17:34:39 +020080 c = inb(WDT_EFDR);
Pádraig Brady28dd1b02007-07-24 11:49:27 +010081 if (c == 0x82) { /* W83627THF */
Vlad Drukker0e94f2e2007-03-25 17:34:39 +020082 outb_p(0x2b, WDT_EFER); /* select GPIO3 */
83 c = ((inb_p(WDT_EFDR) & 0xf7) | 0x04); /* select WDT0 */
84 outb_p(0x2b, WDT_EFER);
85 outb_p(c, WDT_EFDR); /* set GPIO3 to WDT0 */
Benny Loenstrup Ammitzboell9c67bea2010-11-11 16:08:41 +010086 } else if (c == 0x88 || c == 0xa0) { /* W83627EHF / W83627DHG */
Slobodan Tomićcf1eaab2009-06-28 21:20:36 +020087 outb_p(0x2d, WDT_EFER); /* select GPIO5 */
88 c = inb_p(WDT_EFDR) & ~0x01; /* PIN77 -> WDT0# */
89 outb_p(0x2d, WDT_EFER);
90 outb_p(c, WDT_EFDR); /* set GPIO5 to WDT0 */
Vlad Drukker0e94f2e2007-03-25 17:34:39 +020091 }
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 outb_p(0x07, WDT_EFER); /* point to logical device number reg */
94 outb_p(0x08, WDT_EFDR); /* select logical device 8 (GPIO2) */
95 outb_p(0x30, WDT_EFER); /* select CR30 */
96 outb_p(0x01, WDT_EFDR); /* set bit 0 to activate GPIO2 */
97}
98
Alan Cox46a39492008-05-19 14:09:18 +010099static void w83627hf_unselect_wd_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 outb_p(0xAA, WDT_EFER); /* Leave extended function mode */
102}
103
104/* tyan motherboards seem to set F5 to 0x4C ?
105 * So explicitly init to appropriate value. */
Alan Cox46a39492008-05-19 14:09:18 +0100106
Guenter Roeck30a836952013-10-28 19:43:57 -0700107static void w83627hf_init(struct watchdog_device *wdog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 unsigned char t;
110
111 w83627hf_select_wd_register();
112
P@Draig Brady93642ec2005-08-17 09:06:07 +0200113 outb_p(0xF6, WDT_EFER); /* Select CRF6 */
Alan Cox46a39492008-05-19 14:09:18 +0100114 t = inb_p(WDT_EFDR); /* read CRF6 */
P@Draig Brady93642ec2005-08-17 09:06:07 +0200115 if (t != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800116 pr_info("Watchdog already running. Resetting timeout to %d sec\n",
Guenter Roeck30a836952013-10-28 19:43:57 -0700117 wdog->timeout);
118 outb_p(wdog->timeout, WDT_EFDR); /* Write back to CRF6 */
P@Draig Brady93642ec2005-08-17 09:06:07 +0200119 }
Pádraig Brady28dd1b02007-07-24 11:49:27 +0100120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 outb_p(0xF5, WDT_EFER); /* Select CRF5 */
Alan Cox46a39492008-05-19 14:09:18 +0100122 t = inb_p(WDT_EFDR); /* read CRF5 */
123 t &= ~0x0C; /* set second mode & disable keyboard
124 turning off watchdog */
Herman Morsink Vollenbroek6d106e02010-12-06 21:24:56 +0100125 t |= 0x02; /* enable the WDTO# output low pulse
126 to the KBRST# pin (PIN60) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 outb_p(t, WDT_EFDR); /* Write back to CRF5 */
128
Pádraig Brady28dd1b02007-07-24 11:49:27 +0100129 outb_p(0xF7, WDT_EFER); /* Select CRF7 */
Alan Cox46a39492008-05-19 14:09:18 +0100130 t = inb_p(WDT_EFDR); /* read CRF7 */
131 t &= ~0xC0; /* disable keyboard & mouse turning off
132 watchdog */
Pádraig Brady28dd1b02007-07-24 11:49:27 +0100133 outb_p(t, WDT_EFDR); /* Write back to CRF7 */
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 w83627hf_unselect_wd_register();
136}
137
Guenter Roeck30a836952013-10-28 19:43:57 -0700138static int wdt_set_time(unsigned int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 w83627hf_select_wd_register();
141
142 outb_p(0xF6, WDT_EFER); /* Select CRF6 */
143 outb_p(timeout, WDT_EFDR); /* Write Timeout counter to CRF6 */
144
145 w83627hf_unselect_wd_register();
Wim Van Sebroeckab9d4412006-09-02 18:50:20 +0200146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return 0;
148}
149
Guenter Roeck30a836952013-10-28 19:43:57 -0700150static int wdt_start(struct watchdog_device *wdog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Guenter Roeck30a836952013-10-28 19:43:57 -0700152 return wdt_set_time(wdog->timeout);
153}
154
155static int wdt_stop(struct watchdog_device *wdog)
156{
157 return wdt_set_time(0);
158}
159
160static int wdt_set_timeout(struct watchdog_device *wdog, unsigned int timeout)
161{
162 wdog->timeout = timeout;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
165}
166
Guenter Roeck30a836952013-10-28 19:43:57 -0700167static unsigned int wdt_get_time(struct watchdog_device *wdog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Guenter Roeck30a836952013-10-28 19:43:57 -0700169 unsigned int timeleft;
Greg Leec63b6d02011-09-12 20:28:46 -0400170
171 w83627hf_select_wd_register();
172
173 outb_p(0xF6, WDT_EFER); /* Select CRF6 */
174 timeleft = inb_p(WDT_EFDR); /* Read Timeout counter to CRF6 */
175
176 w83627hf_unselect_wd_register();
177
Greg Leec63b6d02011-09-12 20:28:46 -0400178 return timeleft;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
182 * Notifier for system down
183 */
Alan Cox46a39492008-05-19 14:09:18 +0100184static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 void *unused)
186{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000187 if (code == SYS_DOWN || code == SYS_HALT)
Guenter Roeck30a836952013-10-28 19:43:57 -0700188 wdt_set_time(0); /* Turn the WDT off */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return NOTIFY_DONE;
191}
192
193/*
194 * Kernel Interfaces
195 */
196
Guenter Roeck30a836952013-10-28 19:43:57 -0700197static struct watchdog_info wdt_info = {
198 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
199 .identity = "W83627HF Watchdog",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
Guenter Roeck30a836952013-10-28 19:43:57 -0700202static struct watchdog_ops wdt_ops = {
203 .owner = THIS_MODULE,
204 .start = wdt_start,
205 .stop = wdt_stop,
206 .set_timeout = wdt_set_timeout,
207 .get_timeleft = wdt_get_time,
208};
209
210static struct watchdog_device wdt_dev = {
211 .info = &wdt_info,
212 .ops = &wdt_ops,
213 .timeout = WATCHDOG_TIMEOUT,
214 .min_timeout = 1,
215 .max_timeout = 255,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
218/*
219 * The WDT needs to learn about soft shutdowns in order to
220 * turn the timebomb registers off.
221 */
222
223static struct notifier_block wdt_notifier = {
224 .notifier_call = wdt_notify_sys,
225};
226
Alan Cox46a39492008-05-19 14:09:18 +0100227static int __init wdt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 int ret;
230
Joe Perches27c766a2012-02-15 15:06:19 -0800231 pr_info("WDT driver for the Winbond(TM) W83627HF/THF/HG/DHG Super I/O chip initialising\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800234 pr_err("I/O address 0x%04x already in use\n", wdt_io);
Guenter Roeck30a836952013-10-28 19:43:57 -0700235 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Guenter Roeck30a836952013-10-28 19:43:57 -0700238 watchdog_init_timeout(&wdt_dev, timeout, NULL);
239 watchdog_set_nowayout(&wdt_dev, nowayout);
240
241 w83627hf_init(&wdt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 ret = register_reboot_notifier(&wdt_notifier);
244 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800245 pr_err("cannot register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 goto unreg_regions;
247 }
248
Guenter Roeck30a836952013-10-28 19:43:57 -0700249 ret = watchdog_register_device(&wdt_dev);
250 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 goto unreg_reboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Joe Perches27c766a2012-02-15 15:06:19 -0800253 pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
Guenter Roeck30a836952013-10-28 19:43:57 -0700254 wdt_dev.timeout, nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return ret;
Guenter Roeck30a836952013-10-28 19:43:57 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258unreg_reboot:
259 unregister_reboot_notifier(&wdt_notifier);
260unreg_regions:
261 release_region(wdt_io, 1);
Guenter Roeck30a836952013-10-28 19:43:57 -0700262 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Alan Cox46a39492008-05-19 14:09:18 +0100265static void __exit wdt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Guenter Roeck30a836952013-10-28 19:43:57 -0700267 watchdog_unregister_device(&wdt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unregister_reboot_notifier(&wdt_notifier);
Alan Cox46a39492008-05-19 14:09:18 +0100269 release_region(wdt_io, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272module_init(wdt_init);
273module_exit(wdt_exit);
274
275MODULE_LICENSE("GPL");
Al Virod36b6912011-12-29 17:09:01 -0500276MODULE_AUTHOR("Pádraig Brady <P@draigBrady.com>");
Vlad Drukker0e94f2e2007-03-25 17:34:39 +0200277MODULE_DESCRIPTION("w83627hf/thf WDT driver");