]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/watchdog/w83697hf_wdt.c
watchdog: nowayout is bool
[mirror_ubuntu-bionic-kernel.git] / drivers / watchdog / w83697hf_wdt.c
CommitLineData
f9a8c891 1/*
de710d68 2 * w83697hf/hg WDT driver
f9a8c891 3 *
3fdee8db 4 * (c) Copyright 2006 Samuel Tardieu <sam@rfc1149.net>
f9a8c891
MJ
5 * (c) Copyright 2006 Marcus Junker <junker@anduras.de>
6 *
de710d68
ST
7 * Based on w83627hf_wdt.c which is based on advantechwdt.c
8 * which is based on wdt.c.
f9a8c891
MJ
9 * Original copyright messages:
10 *
96de0e25 11 * (c) Copyright 2003 Pádraig Brady <P@draigBrady.com>
f9a8c891
MJ
12 *
13 * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
14 *
29fa0586
AC
15 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
16 * All Rights Reserved.
f9a8c891
MJ
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 *
23 * Neither Marcus Junker nor ANDURAS AG admit liability nor provide
24 * warranty for any of this software. This material is provided
25 * "AS-IS" and at no charge.
26 */
27
27c766aa
JP
28#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
f9a8c891
MJ
30#include <linux/module.h>
31#include <linux/moduleparam.h>
32#include <linux/types.h>
33#include <linux/miscdevice.h>
34#include <linux/watchdog.h>
35#include <linux/fs.h>
36#include <linux/ioport.h>
37#include <linux/notifier.h>
38#include <linux/reboot.h>
39#include <linux/init.h>
ab9d4414 40#include <linux/spinlock.h>
c1c8dd39
AC
41#include <linux/io.h>
42#include <linux/uaccess.h>
f9a8c891 43
f9a8c891
MJ
44#include <asm/system.h>
45
de710d68 46#define WATCHDOG_NAME "w83697hf/hg WDT"
f9a8c891 47#define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
6fd65601 48#define WATCHDOG_EARLY_DISABLE 1 /* Disable until userland kicks in */
f9a8c891
MJ
49
50static unsigned long wdt_is_open;
51static char expect_close;
c7dfd0cc 52static DEFINE_SPINLOCK(io_lock);
f9a8c891
MJ
53
54/* You must set this - there is no sane way to probe for this board. */
c81b2996 55static int wdt_io = 0x2e;
f9a8c891 56module_param(wdt_io, int, 0);
c1c8dd39
AC
57MODULE_PARM_DESC(wdt_io,
58 "w83697hf/hg WDT io port (default 0x2e, 0 = autodetect)");
f9a8c891
MJ
59
60static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
61module_param(timeout, int, 0);
c1c8dd39
AC
62MODULE_PARM_DESC(timeout,
63 "Watchdog timeout in seconds. 1<= timeout <=255 (default="
64 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
f9a8c891 65
86a1e189
WVS
66static bool nowayout = WATCHDOG_NOWAYOUT;
67module_param(nowayout, bool, 0);
c1c8dd39
AC
68MODULE_PARM_DESC(nowayout,
69 "Watchdog cannot be stopped once started (default="
70 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
f9a8c891 71
6fd65601
ST
72static int early_disable = WATCHDOG_EARLY_DISABLE;
73module_param(early_disable, int, 0);
c1c8dd39
AC
74MODULE_PARM_DESC(early_disable,
75 "Watchdog gets disabled at boot time (default="
76 __MODULE_STRING(WATCHDOG_EARLY_DISABLE) ")");
6fd65601 77
f9a8c891
MJ
78/*
79 * Kernel methods.
80 */
81
c1c8dd39
AC
82#define W83697HF_EFER (wdt_io + 0) /* Extended Function Enable Register */
83#define W83697HF_EFIR (wdt_io + 0) /* Extended Function Index Register
84 (same as EFER) */
85#define W83697HF_EFDR (wdt_io + 1) /* Extended Function Data Register */
f9a8c891 86
c1c8dd39 87static inline void w83697hf_unlock(void)
f9a8c891 88{
44d7d328
ST
89 outb_p(0x87, W83697HF_EFER); /* Enter extended function mode */
90 outb_p(0x87, W83697HF_EFER); /* Again according to manual */
f7be3328
ST
91}
92
c1c8dd39 93static inline void w83697hf_lock(void)
fe851eba
ST
94{
95 outb_p(0xAA, W83697HF_EFER); /* Leave extended function mode */
96}
97
0cd54476 98/*
d46ab596
ST
99 * The three functions w83697hf_get_reg(), w83697hf_set_reg() and
100 * w83697hf_write_timeout() must be called with the device unlocked.
0cd54476
ST
101 */
102
c1c8dd39 103static unsigned char w83697hf_get_reg(unsigned char reg)
0cd54476
ST
104{
105 outb_p(reg, W83697HF_EFIR);
106 return inb_p(W83697HF_EFDR);
107}
108
c1c8dd39 109static void w83697hf_set_reg(unsigned char reg, unsigned char data)
0cd54476
ST
110{
111 outb_p(reg, W83697HF_EFIR);
112 outb_p(data, W83697HF_EFDR);
113}
114
c1c8dd39 115static void w83697hf_write_timeout(int timeout)
d46ab596 116{
c1c8dd39
AC
117 /* Write Timeout counter to CRF4 */
118 w83697hf_set_reg(0xF4, timeout);
d46ab596
ST
119}
120
c1c8dd39 121static void w83697hf_select_wdt(void)
a7933e05
ST
122{
123 w83697hf_unlock();
124 w83697hf_set_reg(0x07, 0x08); /* Switch to logic device 8 (GPIO2) */
125}
126
c1c8dd39 127static inline void w83697hf_deselect_wdt(void)
a7933e05
ST
128{
129 w83697hf_lock();
130}
131
c1c8dd39 132static void w83697hf_init(void)
f9a8c891 133{
b7b9868b 134 unsigned char bbuf;
f9a8c891 135
fa69afd3
ST
136 w83697hf_select_wdt();
137
b7b9868b
ST
138 bbuf = w83697hf_get_reg(0x29);
139 bbuf &= ~0x60;
140 bbuf |= 0x20;
c1c8dd39
AC
141
142 /* Set pin 119 to WDTO# mode (= CR29, WDT0) */
143 w83697hf_set_reg(0x29, bbuf);
f9a8c891 144
b7b9868b
ST
145 bbuf = w83697hf_get_reg(0xF3);
146 bbuf &= ~0x04;
147 w83697hf_set_reg(0xF3, bbuf); /* Count mode is seconds */
f9a8c891 148
fa69afd3 149 w83697hf_deselect_wdt();
f9a8c891
MJ
150}
151
c1c8dd39 152static void wdt_ping(void)
f9a8c891 153{
ab9d4414 154 spin_lock(&io_lock);
a7933e05 155 w83697hf_select_wdt();
f9a8c891 156
d46ab596 157 w83697hf_write_timeout(timeout);
f9a8c891 158
a7933e05 159 w83697hf_deselect_wdt();
ab9d4414 160 spin_unlock(&io_lock);
f9a8c891
MJ
161}
162
c1c8dd39 163static void wdt_enable(void)
f9a8c891 164{
089d8139
ST
165 spin_lock(&io_lock);
166 w83697hf_select_wdt();
167
168 w83697hf_write_timeout(timeout);
169 w83697hf_set_reg(0x30, 1); /* Enable timer */
170
171 w83697hf_deselect_wdt();
172 spin_unlock(&io_lock);
f9a8c891
MJ
173}
174
c1c8dd39 175static void wdt_disable(void)
f9a8c891 176{
089d8139
ST
177 spin_lock(&io_lock);
178 w83697hf_select_wdt();
179
180 w83697hf_set_reg(0x30, 0); /* Disable timer */
181 w83697hf_write_timeout(0);
182
183 w83697hf_deselect_wdt();
184 spin_unlock(&io_lock);
f9a8c891
MJ
185}
186
c1c8dd39 187static unsigned char wdt_running(void)
6fd65601
ST
188{
189 unsigned char t;
190
191 spin_lock(&io_lock);
192 w83697hf_select_wdt();
193
194 t = w83697hf_get_reg(0xF4); /* Read timer */
195
196 w83697hf_deselect_wdt();
197 spin_unlock(&io_lock);
198
199 return t;
200}
201
c1c8dd39 202static int wdt_set_heartbeat(int t)
f9a8c891 203{
c1c8dd39 204 if (t < 1 || t > 255)
f9a8c891
MJ
205 return -EINVAL;
206
207 timeout = t;
208 return 0;
209}
210
c1c8dd39
AC
211static ssize_t wdt_write(struct file *file, const char __user *buf,
212 size_t count, loff_t *ppos)
f9a8c891
MJ
213{
214 if (count) {
215 if (!nowayout) {
216 size_t i;
217
218 expect_close = 0;
219
220 for (i = 0; i != count; i++) {
221 char c;
7944d3a5 222 if (get_user(c, buf + i))
f9a8c891
MJ
223 return -EFAULT;
224 if (c == 'V')
225 expect_close = 42;
226 }
227 }
228 wdt_ping();
229 }
230 return count;
231}
232
c1c8dd39 233static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
f9a8c891
MJ
234{
235 void __user *argp = (void __user *)arg;
236 int __user *p = argp;
237 int new_timeout;
c1c8dd39
AC
238 static const struct watchdog_info ident = {
239 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
240 | WDIOF_MAGICCLOSE,
f9a8c891
MJ
241 .firmware_version = 1,
242 .identity = "W83697HF WDT",
243 };
244
245 switch (cmd) {
246 case WDIOC_GETSUPPORT:
db16525e
ST
247 if (copy_to_user(argp, &ident, sizeof(ident)))
248 return -EFAULT;
249 break;
f9a8c891
MJ
250
251 case WDIOC_GETSTATUS:
252 case WDIOC_GETBOOTSTATUS:
db16525e 253 return put_user(0, p);
f9a8c891 254
f9a8c891
MJ
255 case WDIOC_SETOPTIONS:
256 {
db16525e 257 int options, retval = -EINVAL;
f9a8c891 258
db16525e
ST
259 if (get_user(options, p))
260 return -EFAULT;
f9a8c891 261
db16525e
ST
262 if (options & WDIOS_DISABLECARD) {
263 wdt_disable();
264 retval = 0;
265 }
f9a8c891 266
db16525e 267 if (options & WDIOS_ENABLECARD) {
089d8139 268 wdt_enable();
db16525e
ST
269 retval = 0;
270 }
f9a8c891 271
db16525e 272 return retval;
f9a8c891
MJ
273 }
274
0c06090c
WVS
275 case WDIOC_KEEPALIVE:
276 wdt_ping();
277 break;
278
279 case WDIOC_SETTIMEOUT:
280 if (get_user(new_timeout, p))
281 return -EFAULT;
282 if (wdt_set_heartbeat(new_timeout))
283 return -EINVAL;
284 wdt_ping();
285 /* Fall */
286
287 case WDIOC_GETTIMEOUT:
288 return put_user(timeout, p);
289
f9a8c891 290 default:
db16525e 291 return -ENOTTY;
f9a8c891
MJ
292 }
293 return 0;
294}
295
c1c8dd39 296static int wdt_open(struct inode *inode, struct file *file)
f9a8c891
MJ
297{
298 if (test_and_set_bit(0, &wdt_is_open))
299 return -EBUSY;
300 /*
301 * Activate
302 */
303
089d8139 304 wdt_enable();
f9a8c891
MJ
305 return nonseekable_open(inode, file);
306}
307
c1c8dd39 308static int wdt_close(struct inode *inode, struct file *file)
f9a8c891 309{
c1c8dd39 310 if (expect_close == 42)
f9a8c891 311 wdt_disable();
c1c8dd39 312 else {
27c766aa 313 pr_crit("Unexpected close, not stopping watchdog!\n");
f9a8c891
MJ
314 wdt_ping();
315 }
316 expect_close = 0;
317 clear_bit(0, &wdt_is_open);
318 return 0;
319}
320
321/*
322 * Notifier for system down
323 */
324
c1c8dd39 325static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
f9a8c891
MJ
326 void *unused)
327{
7944d3a5
WVS
328 if (code == SYS_DOWN || code == SYS_HALT)
329 wdt_disable(); /* Turn the WDT off */
330
f9a8c891
MJ
331 return NOTIFY_DONE;
332}
333
334/*
335 * Kernel Interfaces
336 */
337
2b8693c0 338static const struct file_operations wdt_fops = {
f9a8c891
MJ
339 .owner = THIS_MODULE,
340 .llseek = no_llseek,
341 .write = wdt_write,
c1c8dd39 342 .unlocked_ioctl = wdt_ioctl,
f9a8c891
MJ
343 .open = wdt_open,
344 .release = wdt_close,
345};
346
347static struct miscdevice wdt_miscdev = {
348 .minor = WATCHDOG_MINOR,
349 .name = "watchdog",
350 .fops = &wdt_fops,
351};
352
353/*
354 * The WDT needs to learn about soft shutdowns in order to
355 * turn the timebomb registers off.
356 */
357
358static struct notifier_block wdt_notifier = {
359 .notifier_call = wdt_notify_sys,
360};
361
c1c8dd39 362static int w83697hf_check_wdt(void)
c81b2996
ST
363{
364 if (!request_region(wdt_io, 2, WATCHDOG_NAME)) {
27c766aa 365 pr_err("I/O address 0x%x already in use\n", wdt_io);
c81b2996
ST
366 return -EIO;
367 }
368
27c766aa 369 pr_debug("Looking for watchdog at address 0x%x\n", wdt_io);
c81b2996
ST
370 w83697hf_unlock();
371 if (w83697hf_get_reg(0x20) == 0x60) {
27c766aa 372 pr_info("watchdog found at address 0x%x\n", wdt_io);
c81b2996
ST
373 w83697hf_lock();
374 return 0;
375 }
c1c8dd39
AC
376 /* Reprotect in case it was a compatible device */
377 w83697hf_lock();
c81b2996 378
27c766aa 379 pr_info("watchdog not found at address 0x%x\n", wdt_io);
c81b2996
ST
380 release_region(wdt_io, 2);
381 return -EIO;
382}
383
e223f01a
WVS
384static int w83697hf_ioports[] = { 0x2e, 0x4e, 0x00 };
385
c1c8dd39 386static int __init wdt_init(void)
f9a8c891 387{
e223f01a 388 int ret, i, found = 0;
f9a8c891 389
27c766aa 390 pr_info("WDT driver for W83697HF/HG initializing\n");
f9a8c891 391
e223f01a
WVS
392 if (wdt_io == 0) {
393 /* we will autodetect the W83697HF/HG watchdog */
394 for (i = 0; ((!found) && (w83697hf_ioports[i] != 0)); i++) {
395 wdt_io = w83697hf_ioports[i];
cde10ba3 396 if (!w83697hf_check_wdt())
e223f01a
WVS
397 found++;
398 }
399 } else {
c81b2996 400 if (!w83697hf_check_wdt())
e223f01a 401 found++;
c81b2996
ST
402 }
403
e223f01a 404 if (!found) {
27c766aa 405 pr_err("No W83697HF/HG could be found\n");
e223f01a
WVS
406 ret = -EIO;
407 goto out;
408 }
c81b2996 409
fa69afd3 410 w83697hf_init();
6fd65601
ST
411 if (early_disable) {
412 if (wdt_running())
27c766aa 413 pr_warn("Stopping previously enabled watchdog until userland kicks in\n");
6fd65601
ST
414 wdt_disable();
415 }
c81b2996 416
f9a8c891
MJ
417 if (wdt_set_heartbeat(timeout)) {
418 wdt_set_heartbeat(WATCHDOG_TIMEOUT);
27c766aa
JP
419 pr_info("timeout value must be 1 <= timeout <= 255, using %d\n",
420 WATCHDOG_TIMEOUT);
f9a8c891
MJ
421 }
422
f9a8c891
MJ
423 ret = register_reboot_notifier(&wdt_notifier);
424 if (ret != 0) {
27c766aa 425 pr_err("cannot register reboot notifier (err=%d)\n", ret);
f9a8c891
MJ
426 goto unreg_regions;
427 }
428
429 ret = misc_register(&wdt_miscdev);
430 if (ret != 0) {
27c766aa
JP
431 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
432 WATCHDOG_MINOR, ret);
f9a8c891
MJ
433 goto unreg_reboot;
434 }
435
27c766aa 436 pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
f9a8c891
MJ
437 timeout, nowayout);
438
439out:
440 return ret;
441unreg_reboot:
442 unregister_reboot_notifier(&wdt_notifier);
443unreg_regions:
b41a9f59 444 release_region(wdt_io, 2);
f9a8c891
MJ
445 goto out;
446}
447
c1c8dd39 448static void __exit wdt_exit(void)
f9a8c891
MJ
449{
450 misc_deregister(&wdt_miscdev);
451 unregister_reboot_notifier(&wdt_notifier);
b41a9f59 452 release_region(wdt_io, 2);
f9a8c891
MJ
453}
454
455module_init(wdt_init);
456module_exit(wdt_exit);
457
458MODULE_LICENSE("GPL");
143a2e54
WVS
459MODULE_AUTHOR("Marcus Junker <junker@anduras.de>, "
460 "Samuel Tardieu <sam@rfc1149.net>");
de710d68 461MODULE_DESCRIPTION("w83697hf/hg WDT driver");
f9a8c891 462MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);