blob: 2ee2677f3648168daef037c94777de02375ba4b3 [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
7 * on acquirewdt.c which is based on wdt.c.
8 *
9 * (c) Copyright 2001 Charles Howes <chowes@vsol.net>
10 *
11 * Based on advantechwdt.c which is based on acquirewdt.c which
12 * is based on wdt.c.
13 *
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 *
19 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
20 * http://www.redhat.com
21 *
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 *
31 * (c) Copyright 1995 Alan Cox <alan@redhat.com>
32 *
33 * 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
36 *
37 */
38
Ian E. Morgan3809ad32005-09-01 22:49:17 +020039#include <linux/module.h>
40#include <linux/types.h>
41#include <linux/miscdevice.h>
42#include <linux/watchdog.h>
43#include <linux/ioport.h>
44#include <linux/delay.h>
45#include <linux/notifier.h>
46#include <linux/fs.h>
47#include <linux/reboot.h>
48#include <linux/init.h>
49#include <linux/spinlock.h>
50#include <linux/moduleparam.h>
51
52#include <asm/io.h>
53#include <asm/uaccess.h>
54#include <asm/system.h>
55
56static unsigned long sbc8360_is_open;
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -070057static DEFINE_SPINLOCK(sbc8360_lock);
Ian E. Morgan3809ad32005-09-01 22:49:17 +020058static char expect_close;
59
60#define PFX "sbc8360: "
61
62/*
63 *
64 * Watchdog Timer Configuration
65 *
66 * The function of the watchdog timer is to reset the system automatically
67 * and is defined at I/O port 0120H and 0121H. To enable the watchdog timer
68 * and allow the system to reset, write appropriate values from the table
69 * below to I/O port 0120H and 0121H. To disable the timer, write a zero
70 * value to I/O port 0121H for the system to stop the watchdog function.
71 *
72 * The following describes how the timer should be programmed (according to
73 * the vendor documentation)
74 *
75 * Enabling Watchdog:
76 * MOV AX,000AH (enable, phase I)
77 * MOV DX,0120H
78 * OUT DX,AX
79 * MOV AX,000BH (enable, phase II)
80 * MOV DX,0120H
81 * OUT DX,AX
82 * MOV AX,000nH (set multiplier n, from 1-4)
83 * MOV DX,0120H
84 * OUT DX,AX
85 * MOV AX,000mH (set base timer m, from 0-F)
86 * MOV DX,0121H
87 * OUT DX,AX
88 *
89 * Reset timer:
90 * MOV AX,000mH (same as set base timer, above)
91 * MOV DX,0121H
92 * OUT DX,AX
93 *
94 * Disabling Watchdog:
95 * MOV AX,0000H (a zero value)
96 * MOV DX,0120H
97 * OUT DX,AX
98 *
99 * Watchdog timeout configuration values:
100 * N
101 * M | 1 2 3 4
102 * --|----------------------------------
103 * 0 | 0.5s 5s 50s 100s
104 * 1 | 1s 10s 100s 200s
105 * 2 | 1.5s 15s 150s 300s
106 * 3 | 2s 20s 200s 400s
107 * 4 | 2.5s 25s 250s 500s
108 * 5 | 3s 30s 300s 600s
109 * 6 | 3.5s 35s 350s 700s
110 * 7 | 4s 40s 400s 800s
111 * 8 | 4.5s 45s 450s 900s
112 * 9 | 5s 50s 500s 1000s
113 * A | 5.5s 55s 550s 1100s
114 * B | 6s 60s 600s 1200s
115 * C | 6.5s 65s 650s 1300s
116 * D | 7s 70s 700s 1400s
117 * E | 7.5s 75s 750s 1500s
118 * F | 8s 80s 800s 1600s
119 *
120 * Another way to say the same things is:
121 * For N=1, Timeout = (M+1) * 0.5s
122 * For N=2, Timeout = (M+1) * 5s
123 * For N=3, Timeout = (M+1) * 50s
124 * For N=4, Timeout = (M+1) * 100s
125 *
126 */
127
128static int wd_times[64][2] = {
129 {0, 1}, /* 0 = 0.5s */
130 {1, 1}, /* 1 = 1s */
131 {2, 1}, /* 2 = 1.5s */
132 {3, 1}, /* 3 = 2s */
133 {4, 1}, /* 4 = 2.5s */
134 {5, 1}, /* 5 = 3s */
135 {6, 1}, /* 6 = 3.5s */
136 {7, 1}, /* 7 = 4s */
137 {8, 1}, /* 8 = 4.5s */
138 {9, 1}, /* 9 = 5s */
139 {0xA, 1}, /* 10 = 5.5s */
140 {0xB, 1}, /* 11 = 6s */
141 {0xC, 1}, /* 12 = 6.5s */
142 {0xD, 1}, /* 13 = 7s */
143 {0xE, 1}, /* 14 = 7.5s */
144 {0xF, 1}, /* 15 = 8s */
145 {0, 2}, /* 16 = 5s */
146 {1, 2}, /* 17 = 10s */
147 {2, 2}, /* 18 = 15s */
148 {3, 2}, /* 19 = 20s */
149 {4, 2}, /* 20 = 25s */
150 {5, 2}, /* 21 = 30s */
151 {6, 2}, /* 22 = 35s */
152 {7, 2}, /* 23 = 40s */
153 {8, 2}, /* 24 = 45s */
154 {9, 2}, /* 25 = 50s */
155 {0xA, 2}, /* 26 = 55s */
156 {0xB, 2}, /* 27 = 60s */
157 {0xC, 2}, /* 28 = 65s */
158 {0xD, 2}, /* 29 = 70s */
159 {0xE, 2}, /* 30 = 75s */
160 {0xF, 2}, /* 31 = 80s */
161 {0, 3}, /* 32 = 50s */
162 {1, 3}, /* 33 = 100s */
163 {2, 3}, /* 34 = 150s */
164 {3, 3}, /* 35 = 200s */
165 {4, 3}, /* 36 = 250s */
166 {5, 3}, /* 37 = 300s */
167 {6, 3}, /* 38 = 350s */
168 {7, 3}, /* 39 = 400s */
169 {8, 3}, /* 40 = 450s */
170 {9, 3}, /* 41 = 500s */
171 {0xA, 3}, /* 42 = 550s */
172 {0xB, 3}, /* 43 = 600s */
173 {0xC, 3}, /* 44 = 650s */
174 {0xD, 3}, /* 45 = 700s */
175 {0xE, 3}, /* 46 = 750s */
176 {0xF, 3}, /* 47 = 800s */
177 {0, 4}, /* 48 = 100s */
178 {1, 4}, /* 49 = 200s */
179 {2, 4}, /* 50 = 300s */
180 {3, 4}, /* 51 = 400s */
181 {4, 4}, /* 52 = 500s */
182 {5, 4}, /* 53 = 600s */
183 {6, 4}, /* 54 = 700s */
184 {7, 4}, /* 55 = 800s */
185 {8, 4}, /* 56 = 900s */
186 {9, 4}, /* 57 = 1000s */
187 {0xA, 4}, /* 58 = 1100s */
188 {0xB, 4}, /* 59 = 1200s */
189 {0xC, 4}, /* 60 = 1300s */
190 {0xD, 4}, /* 61 = 1400s */
191 {0xE, 4}, /* 62 = 1500s */
192 {0xF, 4} /* 63 = 1600s */
193};
194
195#define SBC8360_ENABLE 0x120
196#define SBC8360_BASETIME 0x121
197
198static int timeout = 27;
199static int wd_margin = 0xB;
200static int wd_multiplier = 2;
Wim Van Sebroeck3908bb12005-09-11 23:58:22 +0200201static int nowayout = WATCHDOG_NOWAYOUT;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200202
Ian E. Morgan290995f2006-08-31 21:27:42 -0700203module_param(timeout, int, 0);
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200204MODULE_PARM_DESC(timeout, "Index into timeout table (0-63) (default=27 (60s))");
205module_param(nowayout, int, 0);
206MODULE_PARM_DESC(nowayout,
Wim Van Sebroeckbffda5c2007-01-27 20:54:24 +0100207 "Watchdog cannot be stopped once started (default=" __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
234/* Userspace pings kernel driver, or requests clean close */
235static ssize_t sbc8360_write(struct file *file, const char __user * buf,
236 size_t count, loff_t * ppos)
237{
238 if (count) {
239 if (!nowayout) {
240 size_t i;
241
242 /* In case it was set long ago */
243 expect_close = 0;
244
245 for (i = 0; i != count; i++) {
246 char c;
247 if (get_user(c, buf + i))
248 return -EFAULT;
249 if (c == 'V')
250 expect_close = 42;
251 }
252 }
253 sbc8360_ping();
254 }
255 return count;
256}
257
258static int sbc8360_open(struct inode *inode, struct file *file)
259{
260 spin_lock(&sbc8360_lock);
261 if (test_and_set_bit(0, &sbc8360_is_open)) {
262 spin_unlock(&sbc8360_lock);
263 return -EBUSY;
264 }
265 if (nowayout)
266 __module_get(THIS_MODULE);
267
268 /* Activate and ping once to start the countdown */
269 spin_unlock(&sbc8360_lock);
270 sbc8360_activate();
271 sbc8360_ping();
272 return nonseekable_open(inode, file);
273}
274
275static int sbc8360_close(struct inode *inode, struct file *file)
276{
277 spin_lock(&sbc8360_lock);
278 if (expect_close == 42)
279 outb(0, SBC8360_ENABLE);
280 else
281 printk(KERN_CRIT PFX
282 "SBC8360 device closed unexpectedly. SBC8360 will not stop!\n");
283
284 clear_bit(0, &sbc8360_is_open);
285 expect_close = 0;
286 spin_unlock(&sbc8360_lock);
287 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{
297 if (code == SYS_DOWN || code == SYS_HALT) {
298 /* Disable the SBC8360 Watchdog */
299 outb(0, SBC8360_ENABLE);
300 }
301 return NOTIFY_DONE;
302}
303
304/*
305 * Kernel Interfaces
306 */
307
Arjan van de Ven62322d22006-07-03 00:24:21 -0700308static const struct file_operations sbc8360_fops = {
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200309 .owner = THIS_MODULE,
310 .llseek = no_llseek,
311 .write = sbc8360_write,
312 .open = sbc8360_open,
313 .release = sbc8360_close,
314};
315
316static struct miscdevice sbc8360_miscdev = {
317 .minor = WATCHDOG_MINOR,
318 .name = "watchdog",
319 .fops = &sbc8360_fops,
320};
321
322/*
323 * The SBC8360 needs to learn about soft shutdowns in order to
324 * turn the timebomb registers off.
325 */
326
327static struct notifier_block sbc8360_notifier = {
328 .notifier_call = sbc8360_notify_sys,
329};
330
331static int __init sbc8360_init(void)
332{
333 int res;
334 unsigned long int mseconds = 60000;
335
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300336 if (timeout < 0 || timeout > 63) {
337 printk(KERN_ERR PFX "Invalid timeout index (must be 0-63).\n");
338 res = -EINVAL;
339 goto out;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200340 }
341
342 if (!request_region(SBC8360_ENABLE, 1, "SBC8360")) {
343 printk(KERN_ERR PFX "ENABLE method I/O %X is not available.\n",
344 SBC8360_ENABLE);
345 res = -EIO;
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300346 goto out;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200347 }
348 if (!request_region(SBC8360_BASETIME, 1, "SBC8360")) {
349 printk(KERN_ERR PFX
350 "BASETIME method I/O %X is not available.\n",
351 SBC8360_BASETIME);
352 res = -EIO;
353 goto out_nobasetimereg;
354 }
355
356 res = register_reboot_notifier(&sbc8360_notifier);
357 if (res) {
358 printk(KERN_ERR PFX "Failed to register reboot notifier.\n");
359 goto out_noreboot;
360 }
361
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300362 res = misc_register(&sbc8360_miscdev);
363 if (res) {
364 printk(KERN_ERR PFX "failed to register misc device\n");
365 goto out_nomisc;
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200366 }
367
368 wd_margin = wd_times[timeout][0];
369 wd_multiplier = wd_times[timeout][1];
370
371 if (wd_multiplier == 1)
372 mseconds = (wd_margin + 1) * 500;
373 else if (wd_multiplier == 2)
374 mseconds = (wd_margin + 1) * 5000;
375 else if (wd_multiplier == 3)
376 mseconds = (wd_margin + 1) * 50000;
377 else if (wd_multiplier == 4)
378 mseconds = (wd_margin + 1) * 100000;
379
380 /* My kingdom for the ability to print "0.5 seconds" in the kernel! */
381 printk(KERN_INFO PFX "Timeout set at %ld ms.\n", mseconds);
382
383 return 0;
384
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200385 out_nomisc:
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300386 unregister_reboot_notifier(&sbc8360_notifier);
387 out_noreboot:
388 release_region(SBC8360_BASETIME, 1);
389 out_nobasetimereg:
390 release_region(SBC8360_ENABLE, 1);
391 out:
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200392 return res;
393}
394
395static void __exit sbc8360_exit(void)
396{
397 misc_deregister(&sbc8360_miscdev);
398 unregister_reboot_notifier(&sbc8360_notifier);
399 release_region(SBC8360_ENABLE, 1);
400 release_region(SBC8360_BASETIME, 1);
401}
402
403module_init(sbc8360_init);
404module_exit(sbc8360_exit);
405
406MODULE_AUTHOR("Ian E. Morgan <imorgan@webcon.ca>");
407MODULE_DESCRIPTION("SBC8360 watchdog driver");
408MODULE_LICENSE("GPL");
Ian E. Morgan290995f2006-08-31 21:27:42 -0700409MODULE_VERSION("1.01");
Ian E. Morgan3809ad32005-09-01 22:49:17 +0200410MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
411
412/* end of sbc8360.c */