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