1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
34#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/types.h>
37#include <linux/watchdog.h>
38#include <linux/ioport.h>
39#include <linux/notifier.h>
40#include <linux/reboot.h>
41#include <linux/init.h>
42#include <linux/io.h>
43
44#define WATCHDOG_NAME "w83627hf/thf/hg/dhg WDT"
45#define WATCHDOG_TIMEOUT 60
46
47
48static int wdt_io = 0x2E;
49module_param(wdt_io, int, 0);
50MODULE_PARM_DESC(wdt_io, "w83627hf/thf WDT io port (default 0x2E)");
51
52static int timeout;
53module_param(timeout, int, 0);
54MODULE_PARM_DESC(timeout,
55 "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
56 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
57
58static bool nowayout = WATCHDOG_NOWAYOUT;
59module_param(nowayout, bool, 0);
60MODULE_PARM_DESC(nowayout,
61 "Watchdog cannot be stopped once started (default="
62 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
63
64
65
66
67
68#define WDT_EFER (wdt_io+0)
69#define WDT_EFIR (wdt_io+0)
70
71#define WDT_EFDR (WDT_EFIR+1)
72
73#define W83627HF_LD_WDT 0x08
74
75static void superio_outb(int reg, int val)
76{
77 outb(reg, WDT_EFER);
78 outb(val, WDT_EFDR);
79}
80
81static 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);
93 outb_p(0x87, WDT_EFER);
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)
104{
105 outb_p(0xAA, WDT_EFER);
106 release_region(wdt_io, 2);
107}
108
109
110
111
112static int w83627hf_init(struct watchdog_device *wdog)
113{
114 int ret;
115 unsigned char t;
116
117 ret = superio_enter();
118 if (ret)
119 return ret;
120
121 superio_select(W83627HF_LD_WDT);
122 t = superio_inb(0x20);
123 if (t == 0x82) {
124 t = (superio_inb(0x2b) & 0xf7);
125 superio_outb(0x2b, t | 0x04);
126 } else if (t == 0x88 || t == 0xa0) {
127 t = superio_inb(0x2d);
128 superio_outb(0x2d, t & ~0x01);
129 }
130
131
132 t = superio_inb(0x30);
133 if (!(t & 0x01))
134 superio_outb(0x30, t | 0x01);
135
136 t = superio_inb(0xF6);
137 if (t != 0) {
138 pr_info("Watchdog already running. Resetting timeout to %d sec\n",
139 wdog->timeout);
140 superio_outb(0xF6, wdog->timeout);
141 }
142
143
144 t = superio_inb(0xF5) & ~0x0C;
145
146 t |= 0x02;
147 superio_outb(0xF5, t);
148
149
150 t = superio_inb(0xF7) & ~0xC0;
151 superio_outb(0xF7, t);
152
153 superio_exit();
154
155 return 0;
156}
157
158static int wdt_set_time(unsigned int timeout)
159{
160 int ret;
161
162 ret = superio_enter();
163 if (ret)
164 return ret;
165
166 superio_select(W83627HF_LD_WDT);
167 superio_outb(0xF6, timeout);
168 superio_exit();
169
170 return 0;
171}
172
173static int wdt_start(struct watchdog_device *wdog)
174{
175 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
187 return 0;
188}
189
190static unsigned int wdt_get_time(struct watchdog_device *wdog)
191{
192 unsigned int timeleft;
193 int ret;
194
195 ret = superio_enter();
196 if (ret)
197 return 0;
198
199 superio_select(W83627HF_LD_WDT);
200 timeleft = superio_inb(0xF6);
201 superio_exit();
202
203 return timeleft;
204}
205
206
207
208
209static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
210 void *unused)
211{
212 if (code == SYS_DOWN || code == SYS_HALT)
213 wdt_set_time(0);
214
215 return NOTIFY_DONE;
216}
217
218
219
220
221
222static struct watchdog_info wdt_info = {
223 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
224 .identity = "W83627HF Watchdog",
225};
226
227static 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,
241};
242
243
244
245
246
247
248static struct notifier_block wdt_notifier = {
249 .notifier_call = wdt_notify_sys,
250};
251
252static int __init wdt_init(void)
253{
254 int ret;
255
256 pr_info("WDT driver for the Winbond(TM) W83627HF/THF/HG/DHG Super I/O chip initialising\n");
257
258 watchdog_init_timeout(&wdt_dev, timeout, NULL);
259 watchdog_set_nowayout(&wdt_dev, nowayout);
260
261 ret = w83627hf_init(&wdt_dev);
262 if (ret) {
263 pr_err("failed to initialize watchdog (err=%d)\n", ret);
264 return ret;
265 }
266
267 ret = register_reboot_notifier(&wdt_notifier);
268 if (ret != 0) {
269 pr_err("cannot register reboot notifier (err=%d)\n", ret);
270 return ret;
271 }
272
273 ret = watchdog_register_device(&wdt_dev);
274 if (ret)
275 goto unreg_reboot;
276
277 pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
278 wdt_dev.timeout, nowayout);
279
280 return ret;
281
282unreg_reboot:
283 unregister_reboot_notifier(&wdt_notifier);
284 return ret;
285}
286
287static void __exit wdt_exit(void)
288{
289 watchdog_unregister_device(&wdt_dev);
290 unregister_reboot_notifier(&wdt_notifier);
291}
292
293module_init(wdt_init);
294module_exit(wdt_exit);
295
296MODULE_LICENSE("GPL");
297MODULE_AUTHOR("Pádraig Brady <P@draigBrady.com>");
298MODULE_DESCRIPTION("w83627hf/thf WDT driver");
299