blob: a9ea385fa1255c187616c1a9bc93d0f833e67f89 [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#include <asm/system.h>
57
58static unsigned long sbc8360_is_open;
Ian E. Morgan3809ad32005-09-01 22:49:17 +020059static char expect_close;
60
Ian E. Morgan3809ad32005-09-01 22:49:17 +020061/*
62 *
63 * Watchdog Timer Configuration
64 *
65 * The function of the watchdog timer is to reset the system automatically
66 * and is defined at I/O port 0120H and 0121H. To enable the watchdog timer
67 * and allow the system to reset, write appropriate values from the table
68 * below to I/O port 0120H and 0121H. To disable the timer, write a zero
69 * value to I/O port 0121H for the system to stop the watchdog function.
70 *
71 * The following describes how the timer should be programmed (according to
72 * the vendor documentation)
73 *
74 * Enabling Watchdog:
75 * MOV AX,000AH (enable, phase I)
76 * MOV DX,0120H
77 * OUT DX,AX
78 * MOV AX,000BH (enable, phase II)
79 * MOV DX,0120H
80 * OUT DX,AX
81 * MOV AX,000nH (set multiplier n, from 1-4)
82 * MOV DX,0120H
83 * OUT DX,AX
84 * MOV AX,000mH (set base timer m, from 0-F)
85 * MOV DX,0121H
86 * OUT DX,AX
87 *
88 * Reset timer:
89 * MOV AX,000mH (same as set base timer, above)
90 * MOV DX,0121H
91 * OUT DX,AX
92 *
93 * Disabling Watchdog:
94 * MOV AX,0000H (a zero value)
95 * MOV DX,0120H
96 * OUT DX,AX
97 *
98 * Watchdog timeout configuration values:
99 * N
100 * M | 1 2 3 4
101 * --|----------------------------------
102 * 0 | 0.5s 5s 50s 100s
103 * 1 | 1s 10s 100s 200s
104 * 2 | 1.5s 15s 150s 300s
105 * 3 | 2s 20s 200s 400s
106 * 4 | 2.5s 25s 250s 500s
107 * 5 | 3s 30s 300s 600s
108 * 6 | 3.5s 35s 350s 700s
109 * 7 | 4s 40s 400s 800s
110 * 8 | 4.5s 45s 450s 900s
111 * 9 | 5s 50s 500s 1000s
112 * A | 5.5s 55s 550s 1100s
113 * B | 6s 60s 600s 1200s
114 * C | 6.5s 65s 650s 1300s
115 * D | 7s 70s 700s 1400s
116 * E | 7.5s 75s 750s 1500s
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000117 * F | 8s 80s 800s 1600s
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200118 *
119 * Another way to say the same things is:
120 * For N=1, Timeout = (M+1) * 0.5s
121 * For N=2, Timeout = (M+1) * 5s
122 * For N=3, Timeout = (M+1) * 50s
123 * For N=4, Timeout = (M+1) * 100s
124 *
125 */
126
127static int wd_times[64][2] = {
128 {0, 1}, /* 0 = 0.5s */
129 {1, 1}, /* 1 = 1s */
130 {2, 1}, /* 2 = 1.5s */
131 {3, 1}, /* 3 = 2s */
132 {4, 1}, /* 4 = 2.5s */
133 {5, 1}, /* 5 = 3s */
134 {6, 1}, /* 6 = 3.5s */
135 {7, 1}, /* 7 = 4s */
136 {8, 1}, /* 8 = 4.5s */
137 {9, 1}, /* 9 = 5s */
138 {0xA, 1}, /* 10 = 5.5s */
139 {0xB, 1}, /* 11 = 6s */
140 {0xC, 1}, /* 12 = 6.5s */
141 {0xD, 1}, /* 13 = 7s */
142 {0xE, 1}, /* 14 = 7.5s */
143 {0xF, 1}, /* 15 = 8s */
144 {0, 2}, /* 16 = 5s */
145 {1, 2}, /* 17 = 10s */
146 {2, 2}, /* 18 = 15s */
147 {3, 2}, /* 19 = 20s */
148 {4, 2}, /* 20 = 25s */
149 {5, 2}, /* 21 = 30s */
150 {6, 2}, /* 22 = 35s */
151 {7, 2}, /* 23 = 40s */
152 {8, 2}, /* 24 = 45s */
153 {9, 2}, /* 25 = 50s */
154 {0xA, 2}, /* 26 = 55s */
155 {0xB, 2}, /* 27 = 60s */
156 {0xC, 2}, /* 28 = 65s */
157 {0xD, 2}, /* 29 = 70s */
158 {0xE, 2}, /* 30 = 75s */
159 {0xF, 2}, /* 31 = 80s */
160 {0, 3}, /* 32 = 50s */
161 {1, 3}, /* 33 = 100s */
162 {2, 3}, /* 34 = 150s */
163 {3, 3}, /* 35 = 200s */
164 {4, 3}, /* 36 = 250s */
165 {5, 3}, /* 37 = 300s */
166 {6, 3}, /* 38 = 350s */
167 {7, 3}, /* 39 = 400s */
168 {8, 3}, /* 40 = 450s */
169 {9, 3}, /* 41 = 500s */
170 {0xA, 3}, /* 42 = 550s */
171 {0xB, 3}, /* 43 = 600s */
172 {0xC, 3}, /* 44 = 650s */
173 {0xD, 3}, /* 45 = 700s */
174 {0xE, 3}, /* 46 = 750s */
175 {0xF, 3}, /* 47 = 800s */
176 {0, 4}, /* 48 = 100s */
177 {1, 4}, /* 49 = 200s */
178 {2, 4}, /* 50 = 300s */
179 {3, 4}, /* 51 = 400s */
180 {4, 4}, /* 52 = 500s */
181 {5, 4}, /* 53 = 600s */
182 {6, 4}, /* 54 = 700s */
183 {7, 4}, /* 55 = 800s */
184 {8, 4}, /* 56 = 900s */
185 {9, 4}, /* 57 = 1000s */
186 {0xA, 4}, /* 58 = 1100s */
187 {0xB, 4}, /* 59 = 1200s */
188 {0xC, 4}, /* 60 = 1300s */
189 {0xD, 4}, /* 61 = 1400s */
190 {0xE, 4}, /* 62 = 1500s */
191 {0xF, 4} /* 63 = 1600s */
192};
193
194#define SBC8360_ENABLE 0x120
195#define SBC8360_BASETIME 0x121
196
197static int timeout = 27;
198static int wd_margin = 0xB;
199static int wd_multiplier = 2;
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100200static bool nowayout = WATCHDOG_NOWAYOUT;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200201
Ian E. Morgan290995f2006-08-31 21:27:42 -0700202module_param(timeout, int, 0);
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200203MODULE_PARM_DESC(timeout, "Index into timeout table (0-63) (default=27 (60s))");
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100204module_param(nowayout, bool, 0);
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200205MODULE_PARM_DESC(nowayout,
Alan Cox9f53c8d2008-05-19 14:08:22 +0100206 "Watchdog cannot be stopped once started (default="
207 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200208
209/*
210 * Kernel methods.
211 */
212
213/* Activate and pre-configure watchdog */
214static void sbc8360_activate(void)
215{
216 /* Enable the watchdog */
217 outb(0x0A, SBC8360_ENABLE);
218 msleep_interruptible(100);
219 outb(0x0B, SBC8360_ENABLE);
220 msleep_interruptible(100);
221 /* Set timeout multiplier */
222 outb(wd_multiplier, SBC8360_ENABLE);
223 msleep_interruptible(100);
224 /* Nothing happens until first sbc8360_ping() */
225}
226
227/* Kernel pings watchdog */
228static void sbc8360_ping(void)
229{
230 /* Write the base timer register */
231 outb(wd_margin, SBC8360_BASETIME);
232}
233
Wim Van Sebroeck12b7a152008-07-18 19:59:48 +0000234/* stop watchdog */
235static void sbc8360_stop(void)
236{
237 /* De-activate the watchdog */
238 outb(0, SBC8360_ENABLE);
239}
240
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200241/* Userspace pings kernel driver, or requests clean close */
Alan Cox9f53c8d2008-05-19 14:08:22 +0100242static ssize_t sbc8360_write(struct file *file, const char __user *buf,
243 size_t count, loff_t *ppos)
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200244{
245 if (count) {
246 if (!nowayout) {
247 size_t i;
248
249 /* In case it was set long ago */
250 expect_close = 0;
251
252 for (i = 0; i != count; i++) {
253 char c;
254 if (get_user(c, buf + i))
255 return -EFAULT;
256 if (c == 'V')
257 expect_close = 42;
258 }
259 }
260 sbc8360_ping();
261 }
262 return count;
263}
264
265static int sbc8360_open(struct inode *inode, struct file *file)
266{
Alan Cox9f53c8d2008-05-19 14:08:22 +0100267 if (test_and_set_bit(0, &sbc8360_is_open))
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200268 return -EBUSY;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200269 if (nowayout)
270 __module_get(THIS_MODULE);
271
272 /* Activate and ping once to start the countdown */
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200273 sbc8360_activate();
274 sbc8360_ping();
275 return nonseekable_open(inode, file);
276}
277
278static int sbc8360_close(struct inode *inode, struct file *file)
279{
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200280 if (expect_close == 42)
Wim Van Sebroeck12b7a152008-07-18 19:59:48 +0000281 sbc8360_stop();
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200282 else
Joe Perches27c766a2012-02-15 15:06:19 -0800283 pr_crit("SBC8360 device closed unexpectedly. SBC8360 will not stop!\n");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200284
285 clear_bit(0, &sbc8360_is_open);
286 expect_close = 0;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200287 return 0;
288}
289
290/*
291 * Notifier for system down
292 */
293
294static int sbc8360_notify_sys(struct notifier_block *this, unsigned long code,
295 void *unused)
296{
Wim Van Sebroeck12b7a152008-07-18 19:59:48 +0000297 if (code == SYS_DOWN || code == SYS_HALT)
298 sbc8360_stop(); /* Disable the SBC8360 Watchdog */
299
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200300 return NOTIFY_DONE;
301}
302
303/*
304 * Kernel Interfaces
305 */
306
Arjan van de Ven62322d22006-07-03 00:24:21 -0700307static const struct file_operations sbc8360_fops = {
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200308 .owner = THIS_MODULE,
309 .llseek = no_llseek,
310 .write = sbc8360_write,
311 .open = sbc8360_open,
312 .release = sbc8360_close,
313};
314
315static struct miscdevice sbc8360_miscdev = {
316 .minor = WATCHDOG_MINOR,
317 .name = "watchdog",
318 .fops = &sbc8360_fops,
319};
320
321/*
322 * The SBC8360 needs to learn about soft shutdowns in order to
323 * turn the timebomb registers off.
324 */
325
326static struct notifier_block sbc8360_notifier = {
327 .notifier_call = sbc8360_notify_sys,
328};
329
330static int __init sbc8360_init(void)
331{
332 int res;
333 unsigned long int mseconds = 60000;
334
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300335 if (timeout < 0 || timeout > 63) {
Joe Perches27c766a2012-02-15 15:06:19 -0800336 pr_err("Invalid timeout index (must be 0-63)\n");
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300337 res = -EINVAL;
338 goto out;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200339 }
340
341 if (!request_region(SBC8360_ENABLE, 1, "SBC8360")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800342 pr_err("ENABLE method I/O %X is not available\n",
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200343 SBC8360_ENABLE);
344 res = -EIO;
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300345 goto out;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200346 }
347 if (!request_region(SBC8360_BASETIME, 1, "SBC8360")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800348 pr_err("BASETIME method I/O %X is not available\n",
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200349 SBC8360_BASETIME);
350 res = -EIO;
351 goto out_nobasetimereg;
352 }
353
354 res = register_reboot_notifier(&sbc8360_notifier);
355 if (res) {
Joe Perches27c766a2012-02-15 15:06:19 -0800356 pr_err("Failed to register reboot notifier\n");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200357 goto out_noreboot;
358 }
359
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300360 res = misc_register(&sbc8360_miscdev);
361 if (res) {
Joe Perches27c766a2012-02-15 15:06:19 -0800362 pr_err("failed to register misc device\n");
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300363 goto out_nomisc;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200364 }
365
366 wd_margin = wd_times[timeout][0];
367 wd_multiplier = wd_times[timeout][1];
368
369 if (wd_multiplier == 1)
370 mseconds = (wd_margin + 1) * 500;
371 else if (wd_multiplier == 2)
372 mseconds = (wd_margin + 1) * 5000;
373 else if (wd_multiplier == 3)
374 mseconds = (wd_margin + 1) * 50000;
375 else if (wd_multiplier == 4)
376 mseconds = (wd_margin + 1) * 100000;
377
378 /* My kingdom for the ability to print "0.5 seconds" in the kernel! */
Joe Perches27c766a2012-02-15 15:06:19 -0800379 pr_info("Timeout set at %ld ms\n", mseconds);
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200380
381 return 0;
382
Alan Cox9f53c8d2008-05-19 14:08:22 +0100383out_nomisc:
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300384 unregister_reboot_notifier(&sbc8360_notifier);
Alan Cox9f53c8d2008-05-19 14:08:22 +0100385out_noreboot:
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300386 release_region(SBC8360_BASETIME, 1);
Alan Cox9f53c8d2008-05-19 14:08:22 +0100387out_nobasetimereg:
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300388 release_region(SBC8360_ENABLE, 1);
Alan Cox9f53c8d2008-05-19 14:08:22 +0100389out:
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200390 return res;
391}
392
393static void __exit sbc8360_exit(void)
394{
395 misc_deregister(&sbc8360_miscdev);
396 unregister_reboot_notifier(&sbc8360_notifier);
397 release_region(SBC8360_ENABLE, 1);
398 release_region(SBC8360_BASETIME, 1);
399}
400
401module_init(sbc8360_init);
402module_exit(sbc8360_exit);
403
404MODULE_AUTHOR("Ian E. Morgan <imorgan@webcon.ca>");
405MODULE_DESCRIPTION("SBC8360 watchdog driver");
406MODULE_LICENSE("GPL");
Ian E. Morgan290995f2006-08-31 21:27:42 -0700407MODULE_VERSION("1.01");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200408MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
409
410/* end of sbc8360.c */