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