blob: da60560ca446e7a1023c1707a0615c15e4f3af3c [file] [log] [blame]
Ian E. Morgan3809ad32005-09-01 22:49:17 +02001/*
2 * SBC8360 Watchdog driver
3 *
4 * (c) Copyright 2005 Webcon, Inc.
5 *
6 * Based on ib700wdt.c, which is based on advantechwdt.c which is based
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +00007 * on acquirewdt.c which is based on wdt.c.
Ian E. Morgan3809ad32005-09-01 22:49:17 +02008 *
9 * (c) Copyright 2001 Charles Howes <chowes@vsol.net>
10 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000011 * Based on advantechwdt.c which is based on acquirewdt.c which
12 * is based on wdt.c.
Ian E. Morgan3809ad32005-09-01 22:49:17 +020013 *
14 * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
15 *
16 * Based on acquirewdt.c which is based on wdt.c.
17 * Original copyright messages:
18 *
Alan Cox29fa0582008-10-27 15:17:56 +000019 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
20 * All Rights Reserved.
Ian E. Morgan3809ad32005-09-01 22:49:17 +020021 *
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * as published by the Free Software Foundation; either version
25 * 2 of the License, or (at your option) any later version.
26 *
27 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
28 * warranty for any of this software. This material is provided
29 * "AS-IS" and at no charge.
30 *
Alan Cox29fa0582008-10-27 15:17:56 +000031 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
Ian E. Morgan3809ad32005-09-01 22:49:17 +020032 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000033 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
34 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
35 * Added timeout module option to override default
Ian E. Morgan3809ad32005-09-01 22:49:17 +020036 *
37 */
38
Joe Perches27c766a2012-02-15 15:06:19 -080039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Ian E. Morgan3809ad32005-09-01 22:49:17 +020041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/miscdevice.h>
44#include <linux/watchdog.h>
45#include <linux/ioport.h>
46#include <linux/delay.h>
47#include <linux/notifier.h>
48#include <linux/fs.h>
49#include <linux/reboot.h>
50#include <linux/init.h>
51#include <linux/spinlock.h>
52#include <linux/moduleparam.h>
Alan Cox9f53c8d2008-05-19 14:08:22 +010053#include <linux/io.h>
54#include <linux/uaccess.h>
Ian E. Morgan3809ad32005-09-01 22:49:17 +020055
Ian E. Morgan3809ad32005-09-01 22:49:17 +020056
57static unsigned long sbc8360_is_open;
Ian E. Morgan3809ad32005-09-01 22:49:17 +020058static char expect_close;
59
Ian E. Morgan3809ad32005-09-01 22:49:17 +020060/*
61 *
62 * Watchdog Timer Configuration
63 *
64 * The function of the watchdog timer is to reset the system automatically
65 * and is defined at I/O port 0120H and 0121H. To enable the watchdog timer
66 * and allow the system to reset, write appropriate values from the table
67 * below to I/O port 0120H and 0121H. To disable the timer, write a zero
68 * value to I/O port 0121H for the system to stop the watchdog function.
69 *
70 * The following describes how the timer should be programmed (according to
71 * the vendor documentation)
72 *
73 * Enabling Watchdog:
74 * MOV AX,000AH (enable, phase I)
75 * MOV DX,0120H
76 * OUT DX,AX
77 * MOV AX,000BH (enable, phase II)
78 * MOV DX,0120H
79 * OUT DX,AX
80 * MOV AX,000nH (set multiplier n, from 1-4)
81 * MOV DX,0120H
82 * OUT DX,AX
83 * MOV AX,000mH (set base timer m, from 0-F)
84 * MOV DX,0121H
85 * OUT DX,AX
86 *
87 * Reset timer:
88 * MOV AX,000mH (same as set base timer, above)
89 * MOV DX,0121H
90 * OUT DX,AX
91 *
92 * Disabling Watchdog:
93 * MOV AX,0000H (a zero value)
94 * MOV DX,0120H
95 * OUT DX,AX
96 *
97 * Watchdog timeout configuration values:
98 * N
99 * M | 1 2 3 4
100 * --|----------------------------------
101 * 0 | 0.5s 5s 50s 100s
102 * 1 | 1s 10s 100s 200s
103 * 2 | 1.5s 15s 150s 300s
104 * 3 | 2s 20s 200s 400s
105 * 4 | 2.5s 25s 250s 500s
106 * 5 | 3s 30s 300s 600s
107 * 6 | 3.5s 35s 350s 700s
108 * 7 | 4s 40s 400s 800s
109 * 8 | 4.5s 45s 450s 900s
110 * 9 | 5s 50s 500s 1000s
111 * A | 5.5s 55s 550s 1100s
112 * B | 6s 60s 600s 1200s
113 * C | 6.5s 65s 650s 1300s
114 * D | 7s 70s 700s 1400s
115 * E | 7.5s 75s 750s 1500s
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000116 * F | 8s 80s 800s 1600s
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200117 *
118 * Another way to say the same things is:
119 * For N=1, Timeout = (M+1) * 0.5s
120 * For N=2, Timeout = (M+1) * 5s
121 * For N=3, Timeout = (M+1) * 50s
122 * For N=4, Timeout = (M+1) * 100s
123 *
124 */
125
126static int wd_times[64][2] = {
127 {0, 1}, /* 0 = 0.5s */
128 {1, 1}, /* 1 = 1s */
129 {2, 1}, /* 2 = 1.5s */
130 {3, 1}, /* 3 = 2s */
131 {4, 1}, /* 4 = 2.5s */
132 {5, 1}, /* 5 = 3s */
133 {6, 1}, /* 6 = 3.5s */
134 {7, 1}, /* 7 = 4s */
135 {8, 1}, /* 8 = 4.5s */
136 {9, 1}, /* 9 = 5s */
137 {0xA, 1}, /* 10 = 5.5s */
138 {0xB, 1}, /* 11 = 6s */
139 {0xC, 1}, /* 12 = 6.5s */
140 {0xD, 1}, /* 13 = 7s */
141 {0xE, 1}, /* 14 = 7.5s */
142 {0xF, 1}, /* 15 = 8s */
143 {0, 2}, /* 16 = 5s */
144 {1, 2}, /* 17 = 10s */
145 {2, 2}, /* 18 = 15s */
146 {3, 2}, /* 19 = 20s */
147 {4, 2}, /* 20 = 25s */
148 {5, 2}, /* 21 = 30s */
149 {6, 2}, /* 22 = 35s */
150 {7, 2}, /* 23 = 40s */
151 {8, 2}, /* 24 = 45s */
152 {9, 2}, /* 25 = 50s */
153 {0xA, 2}, /* 26 = 55s */
154 {0xB, 2}, /* 27 = 60s */
155 {0xC, 2}, /* 28 = 65s */
156 {0xD, 2}, /* 29 = 70s */
157 {0xE, 2}, /* 30 = 75s */
158 {0xF, 2}, /* 31 = 80s */
159 {0, 3}, /* 32 = 50s */
160 {1, 3}, /* 33 = 100s */
161 {2, 3}, /* 34 = 150s */
162 {3, 3}, /* 35 = 200s */
163 {4, 3}, /* 36 = 250s */
164 {5, 3}, /* 37 = 300s */
165 {6, 3}, /* 38 = 350s */
166 {7, 3}, /* 39 = 400s */
167 {8, 3}, /* 40 = 450s */
168 {9, 3}, /* 41 = 500s */
169 {0xA, 3}, /* 42 = 550s */
170 {0xB, 3}, /* 43 = 600s */
171 {0xC, 3}, /* 44 = 650s */
172 {0xD, 3}, /* 45 = 700s */
173 {0xE, 3}, /* 46 = 750s */
174 {0xF, 3}, /* 47 = 800s */
175 {0, 4}, /* 48 = 100s */
176 {1, 4}, /* 49 = 200s */
177 {2, 4}, /* 50 = 300s */
178 {3, 4}, /* 51 = 400s */
179 {4, 4}, /* 52 = 500s */
180 {5, 4}, /* 53 = 600s */
181 {6, 4}, /* 54 = 700s */
182 {7, 4}, /* 55 = 800s */
183 {8, 4}, /* 56 = 900s */
184 {9, 4}, /* 57 = 1000s */
185 {0xA, 4}, /* 58 = 1100s */
186 {0xB, 4}, /* 59 = 1200s */
187 {0xC, 4}, /* 60 = 1300s */
188 {0xD, 4}, /* 61 = 1400s */
189 {0xE, 4}, /* 62 = 1500s */
190 {0xF, 4} /* 63 = 1600s */
191};
192
193#define SBC8360_ENABLE 0x120
194#define SBC8360_BASETIME 0x121
195
196static int timeout = 27;
197static int wd_margin = 0xB;
198static int wd_multiplier = 2;
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100199static bool nowayout = WATCHDOG_NOWAYOUT;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200200
Ian E. Morgan290995f2006-08-31 21:27:42 -0700201module_param(timeout, int, 0);
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200202MODULE_PARM_DESC(timeout, "Index into timeout table (0-63) (default=27 (60s))");
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100203module_param(nowayout, bool, 0);
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200204MODULE_PARM_DESC(nowayout,
Alan Cox9f53c8d2008-05-19 14:08:22 +0100205 "Watchdog cannot be stopped once started (default="
206 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200207
208/*
209 * Kernel methods.
210 */
211
212/* Activate and pre-configure watchdog */
213static void sbc8360_activate(void)
214{
215 /* Enable the watchdog */
216 outb(0x0A, SBC8360_ENABLE);
217 msleep_interruptible(100);
218 outb(0x0B, SBC8360_ENABLE);
219 msleep_interruptible(100);
220 /* Set timeout multiplier */
221 outb(wd_multiplier, SBC8360_ENABLE);
222 msleep_interruptible(100);
223 /* Nothing happens until first sbc8360_ping() */
224}
225
226/* Kernel pings watchdog */
227static void sbc8360_ping(void)
228{
229 /* Write the base timer register */
230 outb(wd_margin, SBC8360_BASETIME);
231}
232
Wim Van Sebroeck12b7a152008-07-18 19:59:48 +0000233/* stop watchdog */
234static void sbc8360_stop(void)
235{
236 /* De-activate the watchdog */
237 outb(0, SBC8360_ENABLE);
238}
239
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200240/* Userspace pings kernel driver, or requests clean close */
Alan Cox9f53c8d2008-05-19 14:08:22 +0100241static ssize_t sbc8360_write(struct file *file, const char __user *buf,
242 size_t count, loff_t *ppos)
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200243{
244 if (count) {
245 if (!nowayout) {
246 size_t i;
247
248 /* In case it was set long ago */
249 expect_close = 0;
250
251 for (i = 0; i != count; i++) {
252 char c;
253 if (get_user(c, buf + i))
254 return -EFAULT;
255 if (c == 'V')
256 expect_close = 42;
257 }
258 }
259 sbc8360_ping();
260 }
261 return count;
262}
263
264static int sbc8360_open(struct inode *inode, struct file *file)
265{
Alan Cox9f53c8d2008-05-19 14:08:22 +0100266 if (test_and_set_bit(0, &sbc8360_is_open))
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200267 return -EBUSY;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200268 if (nowayout)
269 __module_get(THIS_MODULE);
270
271 /* Activate and ping once to start the countdown */
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200272 sbc8360_activate();
273 sbc8360_ping();
274 return nonseekable_open(inode, file);
275}
276
277static int sbc8360_close(struct inode *inode, struct file *file)
278{
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200279 if (expect_close == 42)
Wim Van Sebroeck12b7a152008-07-18 19:59:48 +0000280 sbc8360_stop();
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200281 else
Joe Perches27c766a2012-02-15 15:06:19 -0800282 pr_crit("SBC8360 device closed unexpectedly. SBC8360 will not stop!\n");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200283
284 clear_bit(0, &sbc8360_is_open);
285 expect_close = 0;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200286 return 0;
287}
288
289/*
290 * Notifier for system down
291 */
292
293static int sbc8360_notify_sys(struct notifier_block *this, unsigned long code,
294 void *unused)
295{
Wim Van Sebroeck12b7a152008-07-18 19:59:48 +0000296 if (code == SYS_DOWN || code == SYS_HALT)
297 sbc8360_stop(); /* Disable the SBC8360 Watchdog */
298
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200299 return NOTIFY_DONE;
300}
301
302/*
303 * Kernel Interfaces
304 */
305
Arjan van de Ven62322d22006-07-03 00:24:21 -0700306static const struct file_operations sbc8360_fops = {
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200307 .owner = THIS_MODULE,
308 .llseek = no_llseek,
309 .write = sbc8360_write,
310 .open = sbc8360_open,
311 .release = sbc8360_close,
312};
313
314static struct miscdevice sbc8360_miscdev = {
315 .minor = WATCHDOG_MINOR,
316 .name = "watchdog",
317 .fops = &sbc8360_fops,
318};
319
320/*
321 * The SBC8360 needs to learn about soft shutdowns in order to
322 * turn the timebomb registers off.
323 */
324
325static struct notifier_block sbc8360_notifier = {
326 .notifier_call = sbc8360_notify_sys,
327};
328
329static int __init sbc8360_init(void)
330{
331 int res;
332 unsigned long int mseconds = 60000;
333
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300334 if (timeout < 0 || timeout > 63) {
Joe Perches27c766a2012-02-15 15:06:19 -0800335 pr_err("Invalid timeout index (must be 0-63)\n");
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300336 res = -EINVAL;
337 goto out;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200338 }
339
340 if (!request_region(SBC8360_ENABLE, 1, "SBC8360")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800341 pr_err("ENABLE method I/O %X is not available\n",
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200342 SBC8360_ENABLE);
343 res = -EIO;
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300344 goto out;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200345 }
346 if (!request_region(SBC8360_BASETIME, 1, "SBC8360")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800347 pr_err("BASETIME method I/O %X is not available\n",
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200348 SBC8360_BASETIME);
349 res = -EIO;
350 goto out_nobasetimereg;
351 }
352
353 res = register_reboot_notifier(&sbc8360_notifier);
354 if (res) {
Joe Perches27c766a2012-02-15 15:06:19 -0800355 pr_err("Failed to register reboot notifier\n");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200356 goto out_noreboot;
357 }
358
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300359 res = misc_register(&sbc8360_miscdev);
360 if (res) {
Joe Perches27c766a2012-02-15 15:06:19 -0800361 pr_err("failed to register misc device\n");
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300362 goto out_nomisc;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200363 }
364
365 wd_margin = wd_times[timeout][0];
366 wd_multiplier = wd_times[timeout][1];
367
368 if (wd_multiplier == 1)
369 mseconds = (wd_margin + 1) * 500;
370 else if (wd_multiplier == 2)
371 mseconds = (wd_margin + 1) * 5000;
372 else if (wd_multiplier == 3)
373 mseconds = (wd_margin + 1) * 50000;
374 else if (wd_multiplier == 4)
375 mseconds = (wd_margin + 1) * 100000;
376
377 /* My kingdom for the ability to print "0.5 seconds" in the kernel! */
Joe Perches27c766a2012-02-15 15:06:19 -0800378 pr_info("Timeout set at %ld ms\n", mseconds);
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200379
380 return 0;
381
Alan Cox9f53c8d2008-05-19 14:08:22 +0100382out_nomisc:
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300383 unregister_reboot_notifier(&sbc8360_notifier);
Alan Cox9f53c8d2008-05-19 14:08:22 +0100384out_noreboot:
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300385 release_region(SBC8360_BASETIME, 1);
Alan Cox9f53c8d2008-05-19 14:08:22 +0100386out_nobasetimereg:
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300387 release_region(SBC8360_ENABLE, 1);
Alan Cox9f53c8d2008-05-19 14:08:22 +0100388out:
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200389 return res;
390}
391
392static void __exit sbc8360_exit(void)
393{
394 misc_deregister(&sbc8360_miscdev);
395 unregister_reboot_notifier(&sbc8360_notifier);
396 release_region(SBC8360_ENABLE, 1);
397 release_region(SBC8360_BASETIME, 1);
398}
399
400module_init(sbc8360_init);
401module_exit(sbc8360_exit);
402
403MODULE_AUTHOR("Ian E. Morgan <imorgan@webcon.ca>");
404MODULE_DESCRIPTION("SBC8360 watchdog driver");
405MODULE_LICENSE("GPL");
Ian E. Morgan290995f2006-08-31 21:27:42 -0700406MODULE_VERSION("1.01");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200407
408/* end of sbc8360.c */