blob: e24b2108287483803c82709d0feec1dae8b29907 [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
Guenter Roeckef0c1a62013-08-17 13:58:42 -070073#define W83627HF_LD_WDT 0x08
74
75static void superio_outb(int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Guenter Roeckef0c1a62013-08-17 13:58:42 -070077 outb(reg, WDT_EFER);
78 outb(val, WDT_EFDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Guenter Roeckef0c1a62013-08-17 13:58:42 -070081static inline int superio_inb(int reg)
82{
83 outb(reg, WDT_EFER);
84 return inb(WDT_EFDR);
85}
86
87static int superio_enter(void)
88{
89 if (!request_muxed_region(wdt_io, 2, WATCHDOG_NAME))
90 return -EBUSY;
91
92 outb_p(0x87, WDT_EFER); /* Enter extended function mode */
93 outb_p(0x87, WDT_EFER); /* Again according to manual */
94
95 return 0;
96}
97
98static void superio_select(int ld)
99{
100 superio_outb(0x07, ld);
101}
102
103static void superio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 outb_p(0xAA, WDT_EFER); /* Leave extended function mode */
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700106 release_region(wdt_io, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/* tyan motherboards seem to set F5 to 0x4C ?
110 * So explicitly init to appropriate value. */
Alan Cox46a39492008-05-19 14:09:18 +0100111
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700112static int w83627hf_init(struct watchdog_device *wdog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700114 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 unsigned char t;
116
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700117 ret = superio_enter();
118 if (ret)
119 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700121 superio_select(W83627HF_LD_WDT);
122 t = superio_inb(0x20); /* check chip version */
Guenter Roeck8f526382013-08-17 13:58:40 -0700123 if (t == 0x82) { /* W83627THF */
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700124 t = (superio_inb(0x2b) & 0xf7);
125 superio_outb(0x2b, t | 0x04); /* set GPIO3 to WDT0 */
Guenter Roeck8f526382013-08-17 13:58:40 -0700126 } else if (t == 0x88 || t == 0xa0) { /* W83627EHF / W83627DHG */
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700127 t = superio_inb(0x2d);
128 superio_outb(0x2d, t & ~0x01); /* set GPIO5 to WDT0 */
Guenter Roeck8f526382013-08-17 13:58:40 -0700129 }
130
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700131 /* set CR30 bit 0 to activate GPIO2 */
132 t = superio_inb(0x30);
Guenter Roeckac461102013-08-17 13:58:41 -0700133 if (!(t & 0x01))
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700134 superio_outb(0x30, t | 0x01);
Guenter Roeck8f526382013-08-17 13:58:40 -0700135
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700136 t = superio_inb(0xF6);
P@Draig Brady93642ec2005-08-17 09:06:07 +0200137 if (t != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800138 pr_info("Watchdog already running. Resetting timeout to %d sec\n",
Guenter Roeck30a836952013-10-28 19:43:57 -0700139 wdog->timeout);
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700140 superio_outb(0xF6, wdog->timeout);
P@Draig Brady93642ec2005-08-17 09:06:07 +0200141 }
Pádraig Brady28dd1b02007-07-24 11:49:27 +0100142
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700143 /* set second mode & disable keyboard turning off watchdog */
144 t = superio_inb(0xF5) & ~0x0C;
145 /* enable the WDTO# output low pulse to the KBRST# pin */
146 t |= 0x02;
147 superio_outb(0xF5, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700149 /* disable keyboard & mouse turning off watchdog */
150 t = superio_inb(0xF7) & ~0xC0;
151 superio_outb(0xF7, t);
Pádraig Brady28dd1b02007-07-24 11:49:27 +0100152
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700153 superio_exit();
154
155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Guenter Roeck30a836952013-10-28 19:43:57 -0700158static int wdt_set_time(unsigned int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700160 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700162 ret = superio_enter();
163 if (ret)
164 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700166 superio_select(W83627HF_LD_WDT);
167 superio_outb(0xF6, timeout);
168 superio_exit();
Wim Van Sebroeckab9d4412006-09-02 18:50:20 +0200169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
171}
172
Guenter Roeck30a836952013-10-28 19:43:57 -0700173static int wdt_start(struct watchdog_device *wdog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Guenter Roeck30a836952013-10-28 19:43:57 -0700175 return wdt_set_time(wdog->timeout);
176}
177
178static int wdt_stop(struct watchdog_device *wdog)
179{
180 return wdt_set_time(0);
181}
182
183static int wdt_set_timeout(struct watchdog_device *wdog, unsigned int timeout)
184{
185 wdog->timeout = timeout;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return 0;
188}
189
Guenter Roeck30a836952013-10-28 19:43:57 -0700190static unsigned int wdt_get_time(struct watchdog_device *wdog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Guenter Roeck30a836952013-10-28 19:43:57 -0700192 unsigned int timeleft;
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700193 int ret;
Greg Leec63b6d02011-09-12 20:28:46 -0400194
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700195 ret = superio_enter();
196 if (ret)
197 return 0;
Greg Leec63b6d02011-09-12 20:28:46 -0400198
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700199 superio_select(W83627HF_LD_WDT);
200 timeleft = superio_inb(0xF6);
201 superio_exit();
Greg Leec63b6d02011-09-12 20:28:46 -0400202
Greg Leec63b6d02011-09-12 20:28:46 -0400203 return timeleft;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
207 * Notifier for system down
208 */
Alan Cox46a39492008-05-19 14:09:18 +0100209static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 void *unused)
211{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000212 if (code == SYS_DOWN || code == SYS_HALT)
Guenter Roeck30a836952013-10-28 19:43:57 -0700213 wdt_set_time(0); /* Turn the WDT off */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return NOTIFY_DONE;
216}
217
218/*
219 * Kernel Interfaces
220 */
221
Guenter Roeck30a836952013-10-28 19:43:57 -0700222static struct watchdog_info wdt_info = {
223 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
224 .identity = "W83627HF Watchdog",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225};
226
Guenter Roeck30a836952013-10-28 19:43:57 -0700227static struct watchdog_ops wdt_ops = {
228 .owner = THIS_MODULE,
229 .start = wdt_start,
230 .stop = wdt_stop,
231 .set_timeout = wdt_set_timeout,
232 .get_timeleft = wdt_get_time,
233};
234
235static struct watchdog_device wdt_dev = {
236 .info = &wdt_info,
237 .ops = &wdt_ops,
238 .timeout = WATCHDOG_TIMEOUT,
239 .min_timeout = 1,
240 .max_timeout = 255,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241};
242
243/*
244 * The WDT needs to learn about soft shutdowns in order to
245 * turn the timebomb registers off.
246 */
247
248static struct notifier_block wdt_notifier = {
249 .notifier_call = wdt_notify_sys,
250};
251
Alan Cox46a39492008-05-19 14:09:18 +0100252static int __init wdt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 int ret;
255
Joe Perches27c766a2012-02-15 15:06:19 -0800256 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 -0700257
Guenter Roeck30a836952013-10-28 19:43:57 -0700258 watchdog_init_timeout(&wdt_dev, timeout, NULL);
259 watchdog_set_nowayout(&wdt_dev, nowayout);
260
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700261 ret = w83627hf_init(&wdt_dev);
262 if (ret) {
263 pr_err("failed to initialize watchdog (err=%d)\n", ret);
264 return ret;
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 ret = register_reboot_notifier(&wdt_notifier);
268 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800269 pr_err("cannot register reboot notifier (err=%d)\n", ret);
Guenter Roeckef0c1a62013-08-17 13:58:42 -0700270 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
Guenter Roeck30a836952013-10-28 19:43:57 -0700273 ret = watchdog_register_device(&wdt_dev);
274 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 goto unreg_reboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Joe Perches27c766a2012-02-15 15:06:19 -0800277 pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
Guenter Roeck30a836952013-10-28 19:43:57 -0700278 wdt_dev.timeout, nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return ret;
Guenter Roeck30a836952013-10-28 19:43:57 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282unreg_reboot:
283 unregister_reboot_notifier(&wdt_notifier);
Guenter Roeck30a836952013-10-28 19:43:57 -0700284 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Alan Cox46a39492008-05-19 14:09:18 +0100287static void __exit wdt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Guenter Roeck30a836952013-10-28 19:43:57 -0700289 watchdog_unregister_device(&wdt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 unregister_reboot_notifier(&wdt_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293module_init(wdt_init);
294module_exit(wdt_exit);
295
296MODULE_LICENSE("GPL");
Al Virod36b6912011-12-29 17:09:01 -0500297MODULE_AUTHOR("Pádraig Brady <P@draigBrady.com>");
Vlad Drukker0e94f2e2007-03-25 17:34:39 +0200298MODULE_DESCRIPTION("w83627hf/thf WDT driver");