blob: 4f8b9912fc519c4cd7f59c89447a851578d25c1b [file] [log] [blame]
Guenter Roeckd0173272019-06-20 09:28:46 -07001// SPDX-License-Identifier: GPL-2.0+
Ian E. Morgan3809ad32005-09-01 22:49:17 +02002/*
3 * SBC8360 Watchdog driver
4 *
5 * (c) Copyright 2005 Webcon, Inc.
6 *
7 * Based on ib700wdt.c, which is based on advantechwdt.c which is based
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +00008 * on acquirewdt.c which is based on wdt.c.
Ian E. Morgan3809ad32005-09-01 22:49:17 +02009 *
10 * (c) Copyright 2001 Charles Howes <chowes@vsol.net>
11 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000012 * Based on advantechwdt.c which is based on acquirewdt.c which
13 * is based on wdt.c.
Ian E. Morgan3809ad32005-09-01 22:49:17 +020014 *
15 * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
16 *
17 * Based on acquirewdt.c which is based on wdt.c.
18 * Original copyright messages:
19 *
Alan Cox29fa0582008-10-27 15:17:56 +000020 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
21 * All Rights Reserved.
Ian E. Morgan3809ad32005-09-01 22:49:17 +020022 *
Ian E. Morgan3809ad32005-09-01 22:49:17 +020023 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
24 * warranty for any of this software. This material is provided
25 * "AS-IS" and at no charge.
26 *
Alan Cox29fa0582008-10-27 15:17:56 +000027 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
Ian E. Morgan3809ad32005-09-01 22:49:17 +020028 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000029 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
30 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
31 * Added timeout module option to override default
Ian E. Morgan3809ad32005-09-01 22:49:17 +020032 *
33 */
34
Joe Perches27c766a2012-02-15 15:06:19 -080035#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
Ian E. Morgan3809ad32005-09-01 22:49:17 +020037#include <linux/module.h>
38#include <linux/types.h>
39#include <linux/miscdevice.h>
40#include <linux/watchdog.h>
41#include <linux/ioport.h>
42#include <linux/delay.h>
43#include <linux/notifier.h>
44#include <linux/fs.h>
45#include <linux/reboot.h>
46#include <linux/init.h>
47#include <linux/spinlock.h>
48#include <linux/moduleparam.h>
Alan Cox9f53c8d2008-05-19 14:08:22 +010049#include <linux/io.h>
50#include <linux/uaccess.h>
Ian E. Morgan3809ad32005-09-01 22:49:17 +020051
Ian E. Morgan3809ad32005-09-01 22:49:17 +020052
53static unsigned long sbc8360_is_open;
Ian E. Morgan3809ad32005-09-01 22:49:17 +020054static char expect_close;
55
Ian E. Morgan3809ad32005-09-01 22:49:17 +020056/*
57 *
58 * Watchdog Timer Configuration
59 *
60 * The function of the watchdog timer is to reset the system automatically
61 * and is defined at I/O port 0120H and 0121H. To enable the watchdog timer
62 * and allow the system to reset, write appropriate values from the table
63 * below to I/O port 0120H and 0121H. To disable the timer, write a zero
64 * value to I/O port 0121H for the system to stop the watchdog function.
65 *
66 * The following describes how the timer should be programmed (according to
67 * the vendor documentation)
68 *
69 * Enabling Watchdog:
70 * MOV AX,000AH (enable, phase I)
71 * MOV DX,0120H
72 * OUT DX,AX
73 * MOV AX,000BH (enable, phase II)
74 * MOV DX,0120H
75 * OUT DX,AX
76 * MOV AX,000nH (set multiplier n, from 1-4)
77 * MOV DX,0120H
78 * OUT DX,AX
79 * MOV AX,000mH (set base timer m, from 0-F)
80 * MOV DX,0121H
81 * OUT DX,AX
82 *
83 * Reset timer:
84 * MOV AX,000mH (same as set base timer, above)
85 * MOV DX,0121H
86 * OUT DX,AX
87 *
88 * Disabling Watchdog:
89 * MOV AX,0000H (a zero value)
90 * MOV DX,0120H
91 * OUT DX,AX
92 *
93 * Watchdog timeout configuration values:
94 * N
95 * M | 1 2 3 4
96 * --|----------------------------------
97 * 0 | 0.5s 5s 50s 100s
98 * 1 | 1s 10s 100s 200s
99 * 2 | 1.5s 15s 150s 300s
100 * 3 | 2s 20s 200s 400s
101 * 4 | 2.5s 25s 250s 500s
102 * 5 | 3s 30s 300s 600s
103 * 6 | 3.5s 35s 350s 700s
104 * 7 | 4s 40s 400s 800s
105 * 8 | 4.5s 45s 450s 900s
106 * 9 | 5s 50s 500s 1000s
107 * A | 5.5s 55s 550s 1100s
108 * B | 6s 60s 600s 1200s
109 * C | 6.5s 65s 650s 1300s
110 * D | 7s 70s 700s 1400s
111 * E | 7.5s 75s 750s 1500s
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000112 * F | 8s 80s 800s 1600s
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200113 *
114 * Another way to say the same things is:
115 * For N=1, Timeout = (M+1) * 0.5s
116 * For N=2, Timeout = (M+1) * 5s
117 * For N=3, Timeout = (M+1) * 50s
118 * For N=4, Timeout = (M+1) * 100s
119 *
120 */
121
122static int wd_times[64][2] = {
123 {0, 1}, /* 0 = 0.5s */
124 {1, 1}, /* 1 = 1s */
125 {2, 1}, /* 2 = 1.5s */
126 {3, 1}, /* 3 = 2s */
127 {4, 1}, /* 4 = 2.5s */
128 {5, 1}, /* 5 = 3s */
129 {6, 1}, /* 6 = 3.5s */
130 {7, 1}, /* 7 = 4s */
131 {8, 1}, /* 8 = 4.5s */
132 {9, 1}, /* 9 = 5s */
133 {0xA, 1}, /* 10 = 5.5s */
134 {0xB, 1}, /* 11 = 6s */
135 {0xC, 1}, /* 12 = 6.5s */
136 {0xD, 1}, /* 13 = 7s */
137 {0xE, 1}, /* 14 = 7.5s */
138 {0xF, 1}, /* 15 = 8s */
139 {0, 2}, /* 16 = 5s */
140 {1, 2}, /* 17 = 10s */
141 {2, 2}, /* 18 = 15s */
142 {3, 2}, /* 19 = 20s */
143 {4, 2}, /* 20 = 25s */
144 {5, 2}, /* 21 = 30s */
145 {6, 2}, /* 22 = 35s */
146 {7, 2}, /* 23 = 40s */
147 {8, 2}, /* 24 = 45s */
148 {9, 2}, /* 25 = 50s */
149 {0xA, 2}, /* 26 = 55s */
150 {0xB, 2}, /* 27 = 60s */
151 {0xC, 2}, /* 28 = 65s */
152 {0xD, 2}, /* 29 = 70s */
153 {0xE, 2}, /* 30 = 75s */
154 {0xF, 2}, /* 31 = 80s */
155 {0, 3}, /* 32 = 50s */
156 {1, 3}, /* 33 = 100s */
157 {2, 3}, /* 34 = 150s */
158 {3, 3}, /* 35 = 200s */
159 {4, 3}, /* 36 = 250s */
160 {5, 3}, /* 37 = 300s */
161 {6, 3}, /* 38 = 350s */
162 {7, 3}, /* 39 = 400s */
163 {8, 3}, /* 40 = 450s */
164 {9, 3}, /* 41 = 500s */
165 {0xA, 3}, /* 42 = 550s */
166 {0xB, 3}, /* 43 = 600s */
167 {0xC, 3}, /* 44 = 650s */
168 {0xD, 3}, /* 45 = 700s */
169 {0xE, 3}, /* 46 = 750s */
170 {0xF, 3}, /* 47 = 800s */
171 {0, 4}, /* 48 = 100s */
172 {1, 4}, /* 49 = 200s */
173 {2, 4}, /* 50 = 300s */
174 {3, 4}, /* 51 = 400s */
175 {4, 4}, /* 52 = 500s */
176 {5, 4}, /* 53 = 600s */
177 {6, 4}, /* 54 = 700s */
178 {7, 4}, /* 55 = 800s */
179 {8, 4}, /* 56 = 900s */
180 {9, 4}, /* 57 = 1000s */
181 {0xA, 4}, /* 58 = 1100s */
182 {0xB, 4}, /* 59 = 1200s */
183 {0xC, 4}, /* 60 = 1300s */
184 {0xD, 4}, /* 61 = 1400s */
185 {0xE, 4}, /* 62 = 1500s */
186 {0xF, 4} /* 63 = 1600s */
187};
188
189#define SBC8360_ENABLE 0x120
190#define SBC8360_BASETIME 0x121
191
192static int timeout = 27;
193static int wd_margin = 0xB;
194static int wd_multiplier = 2;
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100195static bool nowayout = WATCHDOG_NOWAYOUT;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200196
Ian E. Morgan290995f2006-08-31 21:27:42 -0700197module_param(timeout, int, 0);
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200198MODULE_PARM_DESC(timeout, "Index into timeout table (0-63) (default=27 (60s))");
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100199module_param(nowayout, bool, 0);
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200200MODULE_PARM_DESC(nowayout,
Alan Cox9f53c8d2008-05-19 14:08:22 +0100201 "Watchdog cannot be stopped once started (default="
202 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200203
204/*
205 * Kernel methods.
206 */
207
208/* Activate and pre-configure watchdog */
209static void sbc8360_activate(void)
210{
211 /* Enable the watchdog */
212 outb(0x0A, SBC8360_ENABLE);
213 msleep_interruptible(100);
214 outb(0x0B, SBC8360_ENABLE);
215 msleep_interruptible(100);
216 /* Set timeout multiplier */
217 outb(wd_multiplier, SBC8360_ENABLE);
218 msleep_interruptible(100);
219 /* Nothing happens until first sbc8360_ping() */
220}
221
222/* Kernel pings watchdog */
223static void sbc8360_ping(void)
224{
225 /* Write the base timer register */
226 outb(wd_margin, SBC8360_BASETIME);
227}
228
Wim Van Sebroeck12b7a152008-07-18 19:59:48 +0000229/* stop watchdog */
230static void sbc8360_stop(void)
231{
232 /* De-activate the watchdog */
233 outb(0, SBC8360_ENABLE);
234}
235
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200236/* Userspace pings kernel driver, or requests clean close */
Alan Cox9f53c8d2008-05-19 14:08:22 +0100237static ssize_t sbc8360_write(struct file *file, const char __user *buf,
238 size_t count, loff_t *ppos)
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200239{
240 if (count) {
241 if (!nowayout) {
242 size_t i;
243
244 /* In case it was set long ago */
245 expect_close = 0;
246
247 for (i = 0; i != count; i++) {
248 char c;
249 if (get_user(c, buf + i))
250 return -EFAULT;
251 if (c == 'V')
252 expect_close = 42;
253 }
254 }
255 sbc8360_ping();
256 }
257 return count;
258}
259
260static int sbc8360_open(struct inode *inode, struct file *file)
261{
Alan Cox9f53c8d2008-05-19 14:08:22 +0100262 if (test_and_set_bit(0, &sbc8360_is_open))
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200263 return -EBUSY;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200264 if (nowayout)
265 __module_get(THIS_MODULE);
266
267 /* Activate and ping once to start the countdown */
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200268 sbc8360_activate();
269 sbc8360_ping();
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300270 return stream_open(inode, file);
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200271}
272
273static int sbc8360_close(struct inode *inode, struct file *file)
274{
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200275 if (expect_close == 42)
Wim Van Sebroeck12b7a152008-07-18 19:59:48 +0000276 sbc8360_stop();
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200277 else
Joe Perches27c766a2012-02-15 15:06:19 -0800278 pr_crit("SBC8360 device closed unexpectedly. SBC8360 will not stop!\n");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200279
280 clear_bit(0, &sbc8360_is_open);
281 expect_close = 0;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200282 return 0;
283}
284
285/*
286 * Notifier for system down
287 */
288
289static int sbc8360_notify_sys(struct notifier_block *this, unsigned long code,
290 void *unused)
291{
Wim Van Sebroeck12b7a152008-07-18 19:59:48 +0000292 if (code == SYS_DOWN || code == SYS_HALT)
293 sbc8360_stop(); /* Disable the SBC8360 Watchdog */
294
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200295 return NOTIFY_DONE;
296}
297
298/*
299 * Kernel Interfaces
300 */
301
Arjan van de Ven62322d22006-07-03 00:24:21 -0700302static const struct file_operations sbc8360_fops = {
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200303 .owner = THIS_MODULE,
304 .llseek = no_llseek,
305 .write = sbc8360_write,
306 .open = sbc8360_open,
307 .release = sbc8360_close,
308};
309
310static struct miscdevice sbc8360_miscdev = {
311 .minor = WATCHDOG_MINOR,
312 .name = "watchdog",
313 .fops = &sbc8360_fops,
314};
315
316/*
317 * The SBC8360 needs to learn about soft shutdowns in order to
318 * turn the timebomb registers off.
319 */
320
321static struct notifier_block sbc8360_notifier = {
322 .notifier_call = sbc8360_notify_sys,
323};
324
325static int __init sbc8360_init(void)
326{
327 int res;
328 unsigned long int mseconds = 60000;
329
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300330 if (timeout < 0 || timeout > 63) {
Joe Perches27c766a2012-02-15 15:06:19 -0800331 pr_err("Invalid timeout index (must be 0-63)\n");
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300332 res = -EINVAL;
333 goto out;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200334 }
335
336 if (!request_region(SBC8360_ENABLE, 1, "SBC8360")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800337 pr_err("ENABLE method I/O %X is not available\n",
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200338 SBC8360_ENABLE);
339 res = -EIO;
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300340 goto out;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200341 }
342 if (!request_region(SBC8360_BASETIME, 1, "SBC8360")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800343 pr_err("BASETIME method I/O %X is not available\n",
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200344 SBC8360_BASETIME);
345 res = -EIO;
346 goto out_nobasetimereg;
347 }
348
349 res = register_reboot_notifier(&sbc8360_notifier);
350 if (res) {
Joe Perches27c766a2012-02-15 15:06:19 -0800351 pr_err("Failed to register reboot notifier\n");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200352 goto out_noreboot;
353 }
354
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300355 res = misc_register(&sbc8360_miscdev);
356 if (res) {
Joe Perches27c766a2012-02-15 15:06:19 -0800357 pr_err("failed to register misc device\n");
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300358 goto out_nomisc;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200359 }
360
361 wd_margin = wd_times[timeout][0];
362 wd_multiplier = wd_times[timeout][1];
363
364 if (wd_multiplier == 1)
365 mseconds = (wd_margin + 1) * 500;
366 else if (wd_multiplier == 2)
367 mseconds = (wd_margin + 1) * 5000;
368 else if (wd_multiplier == 3)
369 mseconds = (wd_margin + 1) * 50000;
370 else if (wd_multiplier == 4)
371 mseconds = (wd_margin + 1) * 100000;
372
373 /* My kingdom for the ability to print "0.5 seconds" in the kernel! */
Joe Perches27c766a2012-02-15 15:06:19 -0800374 pr_info("Timeout set at %ld ms\n", mseconds);
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200375
376 return 0;
377
Alan Cox9f53c8d2008-05-19 14:08:22 +0100378out_nomisc:
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300379 unregister_reboot_notifier(&sbc8360_notifier);
Alan Cox9f53c8d2008-05-19 14:08:22 +0100380out_noreboot:
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300381 release_region(SBC8360_BASETIME, 1);
Alan Cox9f53c8d2008-05-19 14:08:22 +0100382out_nobasetimereg:
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300383 release_region(SBC8360_ENABLE, 1);
Alan Cox9f53c8d2008-05-19 14:08:22 +0100384out:
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200385 return res;
386}
387
388static void __exit sbc8360_exit(void)
389{
390 misc_deregister(&sbc8360_miscdev);
391 unregister_reboot_notifier(&sbc8360_notifier);
392 release_region(SBC8360_ENABLE, 1);
393 release_region(SBC8360_BASETIME, 1);
394}
395
396module_init(sbc8360_init);
397module_exit(sbc8360_exit);
398
399MODULE_AUTHOR("Ian E. Morgan <imorgan@webcon.ca>");
400MODULE_DESCRIPTION("SBC8360 watchdog driver");
401MODULE_LICENSE("GPL");
Ian E. Morgan290995f2006-08-31 21:27:42 -0700402MODULE_VERSION("1.01");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200403
404/* end of sbc8360.c */