1b7e04f8cSWim Van Sebroeck /*
2b7e04f8cSWim Van Sebroeck  *      NS pc87413-wdt Watchdog Timer driver for Linux 2.6.x.x
3b7e04f8cSWim Van Sebroeck  *
4b7e04f8cSWim Van Sebroeck  *      This code is based on wdt.c with original copyright.
5b7e04f8cSWim Van Sebroeck  *
6b7e04f8cSWim Van Sebroeck  *      (C) Copyright 2006 Sven Anders, <anders@anduras.de>
7b7e04f8cSWim Van Sebroeck  *                     and Marcus Junker, <junker@anduras.de>
8b7e04f8cSWim Van Sebroeck  *
9b7e04f8cSWim Van Sebroeck  *      This program is free software; you can redistribute it and/or
10b7e04f8cSWim Van Sebroeck  *      modify it under the terms of the GNU General Public License
11b7e04f8cSWim Van Sebroeck  *      as published by the Free Software Foundation; either version
12b7e04f8cSWim Van Sebroeck  *      2 of the License, or (at your option) any later version.
13b7e04f8cSWim Van Sebroeck  *
14b7e04f8cSWim Van Sebroeck  *      Neither Sven Anders, Marcus Junker nor ANDURAS AG
15b7e04f8cSWim Van Sebroeck  *      admit liability nor provide warranty for any of this software.
16b7e04f8cSWim Van Sebroeck  *      This material is provided "AS-IS" and at no charge.
17b7e04f8cSWim Van Sebroeck  *
18b7e04f8cSWim Van Sebroeck  *      Release 1.1
19b7e04f8cSWim Van Sebroeck  */
20b7e04f8cSWim Van Sebroeck 
21b7e04f8cSWim Van Sebroeck #include <linux/module.h>
22b7e04f8cSWim Van Sebroeck #include <linux/types.h>
23b7e04f8cSWim Van Sebroeck #include <linux/miscdevice.h>
24b7e04f8cSWim Van Sebroeck #include <linux/watchdog.h>
25b7e04f8cSWim Van Sebroeck #include <linux/ioport.h>
26b7e04f8cSWim Van Sebroeck #include <linux/delay.h>
27b7e04f8cSWim Van Sebroeck #include <linux/notifier.h>
28b7e04f8cSWim Van Sebroeck #include <linux/fs.h>
29b7e04f8cSWim Van Sebroeck #include <linux/reboot.h>
30b7e04f8cSWim Van Sebroeck #include <linux/init.h>
31b7e04f8cSWim Van Sebroeck #include <linux/spinlock.h>
32b7e04f8cSWim Van Sebroeck #include <linux/moduleparam.h>
33aee334c2SAlan Cox #include <linux/io.h>
34aee334c2SAlan Cox #include <linux/uaccess.h>
35b7e04f8cSWim Van Sebroeck 
36b7e04f8cSWim Van Sebroeck #include <asm/system.h>
37b7e04f8cSWim Van Sebroeck 
38b7e04f8cSWim Van Sebroeck /* #define DEBUG 1 */
39b7e04f8cSWim Van Sebroeck 
40b7e04f8cSWim Van Sebroeck #define DEFAULT_TIMEOUT     1		/* 1 minute */
41b7e04f8cSWim Van Sebroeck #define MAX_TIMEOUT         255
42b7e04f8cSWim Van Sebroeck 
43b7e04f8cSWim Van Sebroeck #define VERSION             "1.1"
44b7e04f8cSWim Van Sebroeck #define MODNAME             "pc87413 WDT"
45b7e04f8cSWim Van Sebroeck #define PFX                 MODNAME ": "
46b7e04f8cSWim Van Sebroeck #define DPFX                MODNAME " - DEBUG: "
47b7e04f8cSWim Van Sebroeck 
48b7e04f8cSWim Van Sebroeck #define WDT_INDEX_IO_PORT   (io+0)	/* I/O port base (index register) */
49b7e04f8cSWim Van Sebroeck #define WDT_DATA_IO_PORT    (WDT_INDEX_IO_PORT+1)
50b7e04f8cSWim Van Sebroeck #define SWC_LDN             0x04
51b7e04f8cSWim Van Sebroeck #define SIOCFG2             0x22	/* Serial IO register */
5225985edcSLucas De Marchi #define WDCTL               0x10	/* Watchdog-Timer-Control-Register */
53b7e04f8cSWim Van Sebroeck #define WDTO                0x11	/* Watchdog timeout register */
54b7e04f8cSWim Van Sebroeck #define WDCFG               0x12	/* Watchdog config register */
55b7e04f8cSWim Van Sebroeck 
5676550d32SRandy Dunlap #define IO_DEFAULT	0x2E		/* Address used on Portwell Boards */
5776550d32SRandy Dunlap 
5876550d32SRandy Dunlap static int io = IO_DEFAULT;
59b7e04f8cSWim Van Sebroeck 
60b7e04f8cSWim Van Sebroeck static int timeout = DEFAULT_TIMEOUT;	/* timeout value */
61aee334c2SAlan Cox static unsigned long timer_enabled;	/* is the timer enabled? */
62b7e04f8cSWim Van Sebroeck 
63b7e04f8cSWim Van Sebroeck static char expect_close;		/* is the close expected? */
64b7e04f8cSWim Van Sebroeck 
65aee334c2SAlan Cox static DEFINE_SPINLOCK(io_lock);	/* to guard us from io races */
66b7e04f8cSWim Van Sebroeck 
67b7e04f8cSWim Van Sebroeck static int nowayout = WATCHDOG_NOWAYOUT;
68b7e04f8cSWim Van Sebroeck 
69b7e04f8cSWim Van Sebroeck /* -- Low level function ----------------------------------------*/
70b7e04f8cSWim Van Sebroeck 
71b7e04f8cSWim Van Sebroeck /* Select pins for Watchdog output */
72b7e04f8cSWim Van Sebroeck 
73b7e04f8cSWim Van Sebroeck static inline void pc87413_select_wdt_out(void)
74b7e04f8cSWim Van Sebroeck {
75b7e04f8cSWim Van Sebroeck 	unsigned int cr_data = 0;
76b7e04f8cSWim Van Sebroeck 
77b7e04f8cSWim Van Sebroeck 	/* Step 1: Select multiple pin,pin55,as WDT output */
78b7e04f8cSWim Van Sebroeck 
79b7e04f8cSWim Van Sebroeck 	outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
80b7e04f8cSWim Van Sebroeck 
81b7e04f8cSWim Van Sebroeck 	cr_data = inb(WDT_DATA_IO_PORT);
82b7e04f8cSWim Van Sebroeck 
83b7e04f8cSWim Van Sebroeck 	cr_data |= 0x80; /* Set Bit7 to 1*/
84b7e04f8cSWim Van Sebroeck 	outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
85b7e04f8cSWim Van Sebroeck 
86b7e04f8cSWim Van Sebroeck 	outb_p(cr_data, WDT_DATA_IO_PORT);
87b7e04f8cSWim Van Sebroeck 
88b7e04f8cSWim Van Sebroeck #ifdef DEBUG
89aee334c2SAlan Cox 	printk(KERN_INFO DPFX
90aee334c2SAlan Cox 		"Select multiple pin,pin55,as WDT output: Bit7 to 1: %d\n",
91aee334c2SAlan Cox 								cr_data);
92b7e04f8cSWim Van Sebroeck #endif
93b7e04f8cSWim Van Sebroeck }
94b7e04f8cSWim Van Sebroeck 
95b7e04f8cSWim Van Sebroeck /* Enable SWC functions */
96b7e04f8cSWim Van Sebroeck 
97b7e04f8cSWim Van Sebroeck static inline void pc87413_enable_swc(void)
98b7e04f8cSWim Van Sebroeck {
99b7e04f8cSWim Van Sebroeck 	unsigned int cr_data = 0;
100b7e04f8cSWim Van Sebroeck 
101b7e04f8cSWim Van Sebroeck 	/* Step 2: Enable SWC functions */
102b7e04f8cSWim Van Sebroeck 
103b7e04f8cSWim Van Sebroeck 	outb_p(0x07, WDT_INDEX_IO_PORT);	/* Point SWC_LDN (LDN=4) */
104b7e04f8cSWim Van Sebroeck 	outb_p(SWC_LDN, WDT_DATA_IO_PORT);
105b7e04f8cSWim Van Sebroeck 
106b7e04f8cSWim Van Sebroeck 	outb_p(0x30, WDT_INDEX_IO_PORT);	/* Read Index 0x30 First */
107b7e04f8cSWim Van Sebroeck 	cr_data = inb(WDT_DATA_IO_PORT);
108b7e04f8cSWim Van Sebroeck 	cr_data |= 0x01;			/* Set Bit0 to 1 */
109b7e04f8cSWim Van Sebroeck 	outb_p(0x30, WDT_INDEX_IO_PORT);
110b7e04f8cSWim Van Sebroeck 	outb_p(cr_data, WDT_DATA_IO_PORT);	/* Index0x30_bit0P1 */
111b7e04f8cSWim Van Sebroeck 
112b7e04f8cSWim Van Sebroeck #ifdef DEBUG
113b7e04f8cSWim Van Sebroeck 	printk(KERN_INFO DPFX "pc87413 - Enable SWC functions\n");
114b7e04f8cSWim Van Sebroeck #endif
115b7e04f8cSWim Van Sebroeck }
116b7e04f8cSWim Van Sebroeck 
117b7e04f8cSWim Van Sebroeck /* Read SWC I/O base address */
118b7e04f8cSWim Van Sebroeck 
119b7e04f8cSWim Van Sebroeck static inline unsigned int pc87413_get_swc_base(void)
120b7e04f8cSWim Van Sebroeck {
121b7e04f8cSWim Van Sebroeck 	unsigned int  swc_base_addr = 0;
122b7e04f8cSWim Van Sebroeck 	unsigned char addr_l, addr_h = 0;
123b7e04f8cSWim Van Sebroeck 
124b7e04f8cSWim Van Sebroeck 	/* Step 3: Read SWC I/O Base Address */
125b7e04f8cSWim Van Sebroeck 
126b7e04f8cSWim Van Sebroeck 	outb_p(0x60, WDT_INDEX_IO_PORT);	/* Read Index 0x60 */
127b7e04f8cSWim Van Sebroeck 	addr_h = inb(WDT_DATA_IO_PORT);
128b7e04f8cSWim Van Sebroeck 
129b7e04f8cSWim Van Sebroeck 	outb_p(0x61, WDT_INDEX_IO_PORT);	/* Read Index 0x61 */
130b7e04f8cSWim Van Sebroeck 
131b7e04f8cSWim Van Sebroeck 	addr_l = inb(WDT_DATA_IO_PORT);
132b7e04f8cSWim Van Sebroeck 
133b7e04f8cSWim Van Sebroeck 	swc_base_addr = (addr_h << 8) + addr_l;
134b7e04f8cSWim Van Sebroeck #ifdef DEBUG
135aee334c2SAlan Cox 	printk(KERN_INFO DPFX
136aee334c2SAlan Cox 		"Read SWC I/O Base Address: low %d, high %d, res %d\n",
137aee334c2SAlan Cox 						addr_l, addr_h, swc_base_addr);
138b7e04f8cSWim Van Sebroeck #endif
139b7e04f8cSWim Van Sebroeck 	return swc_base_addr;
140b7e04f8cSWim Van Sebroeck }
141b7e04f8cSWim Van Sebroeck 
142b7e04f8cSWim Van Sebroeck /* Select Bank 3 of SWC */
143b7e04f8cSWim Van Sebroeck 
144b7e04f8cSWim Van Sebroeck static inline void pc87413_swc_bank3(unsigned int swc_base_addr)
145b7e04f8cSWim Van Sebroeck {
146b7e04f8cSWim Van Sebroeck 	/* Step 4: Select Bank3 of SWC */
147b7e04f8cSWim Van Sebroeck 	outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
148b7e04f8cSWim Van Sebroeck #ifdef DEBUG
149b7e04f8cSWim Van Sebroeck 	printk(KERN_INFO DPFX "Select Bank3 of SWC\n");
150b7e04f8cSWim Van Sebroeck #endif
151b7e04f8cSWim Van Sebroeck }
152b7e04f8cSWim Van Sebroeck 
153b7e04f8cSWim Van Sebroeck /* Set watchdog timeout to x minutes */
154b7e04f8cSWim Van Sebroeck 
155b7e04f8cSWim Van Sebroeck static inline void pc87413_programm_wdto(unsigned int swc_base_addr,
156b7e04f8cSWim Van Sebroeck 					 char pc87413_time)
157b7e04f8cSWim Van Sebroeck {
158b7e04f8cSWim Van Sebroeck 	/* Step 5: Programm WDTO, Twd. */
159b7e04f8cSWim Van Sebroeck 	outb_p(pc87413_time, swc_base_addr + WDTO);
160b7e04f8cSWim Van Sebroeck #ifdef DEBUG
161b7e04f8cSWim Van Sebroeck 	printk(KERN_INFO DPFX "Set WDTO to %d minutes\n", pc87413_time);
162b7e04f8cSWim Van Sebroeck #endif
163b7e04f8cSWim Van Sebroeck }
164b7e04f8cSWim Van Sebroeck 
165b7e04f8cSWim Van Sebroeck /* Enable WDEN */
166b7e04f8cSWim Van Sebroeck 
167b7e04f8cSWim Van Sebroeck static inline void pc87413_enable_wden(unsigned int swc_base_addr)
168b7e04f8cSWim Van Sebroeck {
169b7e04f8cSWim Van Sebroeck 	/* Step 6: Enable WDEN */
170b7e04f8cSWim Van Sebroeck 	outb_p(inb(swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
171b7e04f8cSWim Van Sebroeck #ifdef DEBUG
172b7e04f8cSWim Van Sebroeck 	printk(KERN_INFO DPFX "Enable WDEN\n");
173b7e04f8cSWim Van Sebroeck #endif
174b7e04f8cSWim Van Sebroeck }
175b7e04f8cSWim Van Sebroeck 
176b7e04f8cSWim Van Sebroeck /* Enable SW_WD_TREN */
177b7e04f8cSWim Van Sebroeck static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr)
178b7e04f8cSWim Van Sebroeck {
179b7e04f8cSWim Van Sebroeck 	/* Enable SW_WD_TREN */
180b7e04f8cSWim Van Sebroeck 	outb_p(inb(swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
181b7e04f8cSWim Van Sebroeck #ifdef DEBUG
182b7e04f8cSWim Van Sebroeck 	printk(KERN_INFO DPFX "Enable SW_WD_TREN\n");
183b7e04f8cSWim Van Sebroeck #endif
184b7e04f8cSWim Van Sebroeck }
185b7e04f8cSWim Van Sebroeck 
186b7e04f8cSWim Van Sebroeck /* Disable SW_WD_TREN */
187b7e04f8cSWim Van Sebroeck 
188b7e04f8cSWim Van Sebroeck static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr)
189b7e04f8cSWim Van Sebroeck {
190b7e04f8cSWim Van Sebroeck 	/* Disable SW_WD_TREN */
191b7e04f8cSWim Van Sebroeck 	outb_p(inb(swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
192b7e04f8cSWim Van Sebroeck #ifdef DEBUG
193b7e04f8cSWim Van Sebroeck 	printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN\n");
194b7e04f8cSWim Van Sebroeck #endif
195b7e04f8cSWim Van Sebroeck }
196b7e04f8cSWim Van Sebroeck 
197b7e04f8cSWim Van Sebroeck /* Enable SW_WD_TRG */
198b7e04f8cSWim Van Sebroeck 
199b7e04f8cSWim Van Sebroeck static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr)
200b7e04f8cSWim Van Sebroeck {
201b7e04f8cSWim Van Sebroeck 	/* Enable SW_WD_TRG */
202b7e04f8cSWim Van Sebroeck 	outb_p(inb(swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
203b7e04f8cSWim Van Sebroeck #ifdef DEBUG
204b7e04f8cSWim Van Sebroeck 	printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG\n");
205b7e04f8cSWim Van Sebroeck #endif
206b7e04f8cSWim Van Sebroeck }
207b7e04f8cSWim Van Sebroeck 
208b7e04f8cSWim Van Sebroeck /* Disable SW_WD_TRG */
209b7e04f8cSWim Van Sebroeck 
210b7e04f8cSWim Van Sebroeck static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr)
211b7e04f8cSWim Van Sebroeck {
212b7e04f8cSWim Van Sebroeck 	/* Disable SW_WD_TRG */
213b7e04f8cSWim Van Sebroeck 	outb_p(inb(swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
214b7e04f8cSWim Van Sebroeck #ifdef DEBUG
215b7e04f8cSWim Van Sebroeck 	printk(KERN_INFO DPFX "Disable SW_WD_TRG\n");
216b7e04f8cSWim Van Sebroeck #endif
217b7e04f8cSWim Van Sebroeck }
218b7e04f8cSWim Van Sebroeck 
219b7e04f8cSWim Van Sebroeck /* -- Higher level functions ------------------------------------*/
220b7e04f8cSWim Van Sebroeck 
221b7e04f8cSWim Van Sebroeck /* Enable the watchdog */
222b7e04f8cSWim Van Sebroeck 
223b7e04f8cSWim Van Sebroeck static void pc87413_enable(void)
224b7e04f8cSWim Van Sebroeck {
225b7e04f8cSWim Van Sebroeck 	unsigned int swc_base_addr;
226b7e04f8cSWim Van Sebroeck 
227b7e04f8cSWim Van Sebroeck 	spin_lock(&io_lock);
228b7e04f8cSWim Van Sebroeck 
229b7e04f8cSWim Van Sebroeck 	pc87413_select_wdt_out();
230b7e04f8cSWim Van Sebroeck 	pc87413_enable_swc();
231b7e04f8cSWim Van Sebroeck 	swc_base_addr = pc87413_get_swc_base();
232b7e04f8cSWim Van Sebroeck 	pc87413_swc_bank3(swc_base_addr);
233b7e04f8cSWim Van Sebroeck 	pc87413_programm_wdto(swc_base_addr, timeout);
234b7e04f8cSWim Van Sebroeck 	pc87413_enable_wden(swc_base_addr);
235b7e04f8cSWim Van Sebroeck 	pc87413_enable_sw_wd_tren(swc_base_addr);
236b7e04f8cSWim Van Sebroeck 	pc87413_enable_sw_wd_trg(swc_base_addr);
237b7e04f8cSWim Van Sebroeck 
238b7e04f8cSWim Van Sebroeck 	spin_unlock(&io_lock);
239b7e04f8cSWim Van Sebroeck }
240b7e04f8cSWim Van Sebroeck 
241b7e04f8cSWim Van Sebroeck /* Disable the watchdog */
242b7e04f8cSWim Van Sebroeck 
243b7e04f8cSWim Van Sebroeck static void pc87413_disable(void)
244b7e04f8cSWim Van Sebroeck {
245b7e04f8cSWim Van Sebroeck 	unsigned int swc_base_addr;
246b7e04f8cSWim Van Sebroeck 
247b7e04f8cSWim Van Sebroeck 	spin_lock(&io_lock);
248b7e04f8cSWim Van Sebroeck 
249b7e04f8cSWim Van Sebroeck 	pc87413_select_wdt_out();
250b7e04f8cSWim Van Sebroeck 	pc87413_enable_swc();
251b7e04f8cSWim Van Sebroeck 	swc_base_addr = pc87413_get_swc_base();
252b7e04f8cSWim Van Sebroeck 	pc87413_swc_bank3(swc_base_addr);
253b7e04f8cSWim Van Sebroeck 	pc87413_disable_sw_wd_tren(swc_base_addr);
254b7e04f8cSWim Van Sebroeck 	pc87413_disable_sw_wd_trg(swc_base_addr);
255b7e04f8cSWim Van Sebroeck 	pc87413_programm_wdto(swc_base_addr, 0);
256b7e04f8cSWim Van Sebroeck 
257b7e04f8cSWim Van Sebroeck 	spin_unlock(&io_lock);
258b7e04f8cSWim Van Sebroeck }
259b7e04f8cSWim Van Sebroeck 
260b7e04f8cSWim Van Sebroeck /* Refresh the watchdog */
261b7e04f8cSWim Van Sebroeck 
262b7e04f8cSWim Van Sebroeck static void pc87413_refresh(void)
263b7e04f8cSWim Van Sebroeck {
264b7e04f8cSWim Van Sebroeck 	unsigned int swc_base_addr;
265b7e04f8cSWim Van Sebroeck 
266b7e04f8cSWim Van Sebroeck 	spin_lock(&io_lock);
267b7e04f8cSWim Van Sebroeck 
268b7e04f8cSWim Van Sebroeck 	pc87413_select_wdt_out();
269b7e04f8cSWim Van Sebroeck 	pc87413_enable_swc();
270b7e04f8cSWim Van Sebroeck 	swc_base_addr = pc87413_get_swc_base();
271b7e04f8cSWim Van Sebroeck 	pc87413_swc_bank3(swc_base_addr);
272b7e04f8cSWim Van Sebroeck 	pc87413_disable_sw_wd_tren(swc_base_addr);
273b7e04f8cSWim Van Sebroeck 	pc87413_disable_sw_wd_trg(swc_base_addr);
274b7e04f8cSWim Van Sebroeck 	pc87413_programm_wdto(swc_base_addr, timeout);
275b7e04f8cSWim Van Sebroeck 	pc87413_enable_wden(swc_base_addr);
276b7e04f8cSWim Van Sebroeck 	pc87413_enable_sw_wd_tren(swc_base_addr);
277b7e04f8cSWim Van Sebroeck 	pc87413_enable_sw_wd_trg(swc_base_addr);
278b7e04f8cSWim Van Sebroeck 
279b7e04f8cSWim Van Sebroeck 	spin_unlock(&io_lock);
280b7e04f8cSWim Van Sebroeck }
281b7e04f8cSWim Van Sebroeck 
282b7e04f8cSWim Van Sebroeck /* -- File operations -------------------------------------------*/
283b7e04f8cSWim Van Sebroeck 
284b7e04f8cSWim Van Sebroeck /**
285b7e04f8cSWim Van Sebroeck  *	pc87413_open:
286b7e04f8cSWim Van Sebroeck  *	@inode: inode of device
287b7e04f8cSWim Van Sebroeck  *	@file: file handle to device
288b7e04f8cSWim Van Sebroeck  *
289b7e04f8cSWim Van Sebroeck  */
290b7e04f8cSWim Van Sebroeck 
291b7e04f8cSWim Van Sebroeck static int pc87413_open(struct inode *inode, struct file *file)
292b7e04f8cSWim Van Sebroeck {
293b7e04f8cSWim Van Sebroeck 	/* /dev/watchdog can only be opened once */
294b7e04f8cSWim Van Sebroeck 
295b7e04f8cSWim Van Sebroeck 	if (test_and_set_bit(0, &timer_enabled))
296b7e04f8cSWim Van Sebroeck 		return -EBUSY;
297b7e04f8cSWim Van Sebroeck 
298b7e04f8cSWim Van Sebroeck 	if (nowayout)
299b7e04f8cSWim Van Sebroeck 		__module_get(THIS_MODULE);
300b7e04f8cSWim Van Sebroeck 
301b7e04f8cSWim Van Sebroeck 	/* Reload and activate timer */
302b7e04f8cSWim Van Sebroeck 	pc87413_refresh();
303b7e04f8cSWim Van Sebroeck 
304aee334c2SAlan Cox 	printk(KERN_INFO MODNAME
305aee334c2SAlan Cox 		"Watchdog enabled. Timeout set to %d minute(s).\n", timeout);
306b7e04f8cSWim Van Sebroeck 
307b7e04f8cSWim Van Sebroeck 	return nonseekable_open(inode, file);
308b7e04f8cSWim Van Sebroeck }
309b7e04f8cSWim Van Sebroeck 
310b7e04f8cSWim Van Sebroeck /**
311b7e04f8cSWim Van Sebroeck  *	pc87413_release:
312b7e04f8cSWim Van Sebroeck  *	@inode: inode to board
313b7e04f8cSWim Van Sebroeck  *	@file: file handle to board
314b7e04f8cSWim Van Sebroeck  *
315b7e04f8cSWim Van Sebroeck  *	The watchdog has a configurable API. There is a religious dispute
316b7e04f8cSWim Van Sebroeck  *	between people who want their watchdog to be able to shut down and
317b7e04f8cSWim Van Sebroeck  *	those who want to be sure if the watchdog manager dies the machine
318b7e04f8cSWim Van Sebroeck  *	reboots. In the former case we disable the counters, in the latter
319b7e04f8cSWim Van Sebroeck  *	case you have to open it again very soon.
320b7e04f8cSWim Van Sebroeck  */
321b7e04f8cSWim Van Sebroeck 
322b7e04f8cSWim Van Sebroeck static int pc87413_release(struct inode *inode, struct file *file)
323b7e04f8cSWim Van Sebroeck {
324b7e04f8cSWim Van Sebroeck 	/* Shut off the timer. */
325b7e04f8cSWim Van Sebroeck 
326b7e04f8cSWim Van Sebroeck 	if (expect_close == 42) {
327b7e04f8cSWim Van Sebroeck 		pc87413_disable();
328aee334c2SAlan Cox 		printk(KERN_INFO MODNAME
329aee334c2SAlan Cox 				"Watchdog disabled, sleeping again...\n");
330b7e04f8cSWim Van Sebroeck 	} else {
331aee334c2SAlan Cox 		printk(KERN_CRIT MODNAME
332aee334c2SAlan Cox 				"Unexpected close, not stopping watchdog!\n");
333b7e04f8cSWim Van Sebroeck 		pc87413_refresh();
334b7e04f8cSWim Van Sebroeck 	}
335b7e04f8cSWim Van Sebroeck 	clear_bit(0, &timer_enabled);
336b7e04f8cSWim Van Sebroeck 	expect_close = 0;
337b7e04f8cSWim Van Sebroeck 	return 0;
338b7e04f8cSWim Van Sebroeck }
339b7e04f8cSWim Van Sebroeck 
340b7e04f8cSWim Van Sebroeck /**
341b7e04f8cSWim Van Sebroeck  *	pc87413_status:
342b7e04f8cSWim Van Sebroeck  *
343b7e04f8cSWim Van Sebroeck  *      return, if the watchdog is enabled (timeout is set...)
344b7e04f8cSWim Van Sebroeck  */
345b7e04f8cSWim Van Sebroeck 
346b7e04f8cSWim Van Sebroeck 
347b7e04f8cSWim Van Sebroeck static int pc87413_status(void)
348b7e04f8cSWim Van Sebroeck {
349b7e04f8cSWim Van Sebroeck 	  return 0; /* currently not supported */
350b7e04f8cSWim Van Sebroeck }
351b7e04f8cSWim Van Sebroeck 
352b7e04f8cSWim Van Sebroeck /**
353b7e04f8cSWim Van Sebroeck  *	pc87413_write:
354b7e04f8cSWim Van Sebroeck  *	@file: file handle to the watchdog
355b7e04f8cSWim Van Sebroeck  *	@data: data buffer to write
356b7e04f8cSWim Van Sebroeck  *	@len: length in bytes
357b7e04f8cSWim Van Sebroeck  *	@ppos: pointer to the position to write. No seeks allowed
358b7e04f8cSWim Van Sebroeck  *
359b7e04f8cSWim Van Sebroeck  *	A write to a watchdog device is defined as a keepalive signal. Any
360b7e04f8cSWim Van Sebroeck  *	write of data will do, as we we don't define content meaning.
361b7e04f8cSWim Van Sebroeck  */
362b7e04f8cSWim Van Sebroeck 
363b7e04f8cSWim Van Sebroeck static ssize_t pc87413_write(struct file *file, const char __user *data,
364b7e04f8cSWim Van Sebroeck 			     size_t len, loff_t *ppos)
365b7e04f8cSWim Van Sebroeck {
366b7e04f8cSWim Van Sebroeck 	/* See if we got the magic character 'V' and reload the timer */
367b7e04f8cSWim Van Sebroeck 	if (len) {
368b7e04f8cSWim Van Sebroeck 		if (!nowayout) {
369b7e04f8cSWim Van Sebroeck 			size_t i;
370b7e04f8cSWim Van Sebroeck 
371b7e04f8cSWim Van Sebroeck 			/* reset expect flag */
372b7e04f8cSWim Van Sebroeck 			expect_close = 0;
373b7e04f8cSWim Van Sebroeck 
374aee334c2SAlan Cox 			/* scan to see whether or not we got the
375aee334c2SAlan Cox 			   magic character */
376b7e04f8cSWim Van Sebroeck 			for (i = 0; i != len; i++) {
377b7e04f8cSWim Van Sebroeck 				char c;
378b7e04f8cSWim Van Sebroeck 				if (get_user(c, data + i))
379b7e04f8cSWim Van Sebroeck 					return -EFAULT;
380b7e04f8cSWim Van Sebroeck 				if (c == 'V')
381b7e04f8cSWim Van Sebroeck 					expect_close = 42;
382b7e04f8cSWim Van Sebroeck 			}
383b7e04f8cSWim Van Sebroeck 		}
384b7e04f8cSWim Van Sebroeck 
385b7e04f8cSWim Van Sebroeck 		/* someone wrote to us, we should reload the timer */
386b7e04f8cSWim Van Sebroeck 		pc87413_refresh();
387b7e04f8cSWim Van Sebroeck 	}
388b7e04f8cSWim Van Sebroeck 	return len;
389b7e04f8cSWim Van Sebroeck }
390b7e04f8cSWim Van Sebroeck 
391b7e04f8cSWim Van Sebroeck /**
392b7e04f8cSWim Van Sebroeck  *	pc87413_ioctl:
393b7e04f8cSWim Van Sebroeck  *	@file: file handle to the device
394b7e04f8cSWim Van Sebroeck  *	@cmd: watchdog command
395b7e04f8cSWim Van Sebroeck  *	@arg: argument pointer
396b7e04f8cSWim Van Sebroeck  *
397b7e04f8cSWim Van Sebroeck  *	The watchdog API defines a common set of functions for all watchdogs
398b7e04f8cSWim Van Sebroeck  *	according to their available features. We only actually usefully support
399b7e04f8cSWim Van Sebroeck  *	querying capabilities and current status.
400b7e04f8cSWim Van Sebroeck  */
401b7e04f8cSWim Van Sebroeck 
402aee334c2SAlan Cox static long pc87413_ioctl(struct file *file, unsigned int cmd,
403aee334c2SAlan Cox 						unsigned long arg)
404b7e04f8cSWim Van Sebroeck {
405b7e04f8cSWim Van Sebroeck 	int new_timeout;
406b7e04f8cSWim Van Sebroeck 
407b7e04f8cSWim Van Sebroeck 	union {
408b7e04f8cSWim Van Sebroeck 		struct watchdog_info __user *ident;
409b7e04f8cSWim Van Sebroeck 		int __user *i;
410b7e04f8cSWim Van Sebroeck 	} uarg;
411b7e04f8cSWim Van Sebroeck 
41242747d71SWim Van Sebroeck 	static const struct watchdog_info ident = {
413b7e04f8cSWim Van Sebroeck 		.options          = WDIOF_KEEPALIVEPING |
414b7e04f8cSWim Van Sebroeck 				    WDIOF_SETTIMEOUT |
415b7e04f8cSWim Van Sebroeck 				    WDIOF_MAGICCLOSE,
416b7e04f8cSWim Van Sebroeck 		.firmware_version = 1,
4177944d3a5SWim Van Sebroeck 		.identity         = "PC87413(HF/F) watchdog",
418b7e04f8cSWim Van Sebroeck 	};
419b7e04f8cSWim Van Sebroeck 
420b7e04f8cSWim Van Sebroeck 	uarg.i = (int __user *)arg;
421b7e04f8cSWim Van Sebroeck 
422b7e04f8cSWim Van Sebroeck 	switch (cmd) {
423b7e04f8cSWim Van Sebroeck 	case WDIOC_GETSUPPORT:
424b7e04f8cSWim Van Sebroeck 		return copy_to_user(uarg.ident, &ident,
425b7e04f8cSWim Van Sebroeck 					sizeof(ident)) ? -EFAULT : 0;
426b7e04f8cSWim Van Sebroeck 	case WDIOC_GETSTATUS:
427b7e04f8cSWim Van Sebroeck 		return put_user(pc87413_status(), uarg.i);
428b7e04f8cSWim Van Sebroeck 	case WDIOC_GETBOOTSTATUS:
429b7e04f8cSWim Van Sebroeck 		return put_user(0, uarg.i);
4300c06090cSWim Van Sebroeck 	case WDIOC_SETOPTIONS:
4310c06090cSWim Van Sebroeck 	{
4320c06090cSWim Van Sebroeck 		int options, retval = -EINVAL;
4330c06090cSWim Van Sebroeck 		if (get_user(options, uarg.i))
4340c06090cSWim Van Sebroeck 			return -EFAULT;
4350c06090cSWim Van Sebroeck 		if (options & WDIOS_DISABLECARD) {
4360c06090cSWim Van Sebroeck 			pc87413_disable();
4370c06090cSWim Van Sebroeck 			retval = 0;
4380c06090cSWim Van Sebroeck 		}
4390c06090cSWim Van Sebroeck 		if (options & WDIOS_ENABLECARD) {
4400c06090cSWim Van Sebroeck 			pc87413_enable();
4410c06090cSWim Van Sebroeck 			retval = 0;
4420c06090cSWim Van Sebroeck 		}
4430c06090cSWim Van Sebroeck 		return retval;
4440c06090cSWim Van Sebroeck 	}
445b7e04f8cSWim Van Sebroeck 	case WDIOC_KEEPALIVE:
446b7e04f8cSWim Van Sebroeck 		pc87413_refresh();
447b7e04f8cSWim Van Sebroeck #ifdef DEBUG
448b7e04f8cSWim Van Sebroeck 		printk(KERN_INFO DPFX "keepalive\n");
449b7e04f8cSWim Van Sebroeck #endif
450b7e04f8cSWim Van Sebroeck 		return 0;
451b7e04f8cSWim Van Sebroeck 	case WDIOC_SETTIMEOUT:
452b7e04f8cSWim Van Sebroeck 		if (get_user(new_timeout, uarg.i))
453b7e04f8cSWim Van Sebroeck 			return -EFAULT;
454aee334c2SAlan Cox 		/* the API states this is given in secs */
455b7e04f8cSWim Van Sebroeck 		new_timeout /= 60;
456b7e04f8cSWim Van Sebroeck 		if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
457b7e04f8cSWim Van Sebroeck 			return -EINVAL;
458b7e04f8cSWim Van Sebroeck 		timeout = new_timeout;
459b7e04f8cSWim Van Sebroeck 		pc87413_refresh();
460aee334c2SAlan Cox 		/* fall through and return the new timeout... */
461b7e04f8cSWim Van Sebroeck 	case WDIOC_GETTIMEOUT:
462b7e04f8cSWim Van Sebroeck 		new_timeout = timeout * 60;
463b7e04f8cSWim Van Sebroeck 		return put_user(new_timeout, uarg.i);
464aee334c2SAlan Cox 	default:
465aee334c2SAlan Cox 		return -ENOTTY;
466b7e04f8cSWim Van Sebroeck 	}
467b7e04f8cSWim Van Sebroeck }
468b7e04f8cSWim Van Sebroeck 
469b7e04f8cSWim Van Sebroeck /* -- Notifier funtions -----------------------------------------*/
470b7e04f8cSWim Van Sebroeck 
471b7e04f8cSWim Van Sebroeck /**
472b7e04f8cSWim Van Sebroeck  *	notify_sys:
473b7e04f8cSWim Van Sebroeck  *	@this: our notifier block
474b7e04f8cSWim Van Sebroeck  *	@code: the event being reported
475b7e04f8cSWim Van Sebroeck  *	@unused: unused
476b7e04f8cSWim Van Sebroeck  *
477b7e04f8cSWim Van Sebroeck  *	Our notifier is called on system shutdowns. We want to turn the card
478b7e04f8cSWim Van Sebroeck  *	off at reboot otherwise the machine will reboot again during memory
479b7e04f8cSWim Van Sebroeck  *	test or worse yet during the following fsck. This would suck, in fact
480b7e04f8cSWim Van Sebroeck  *	trust me - if it happens it does suck.
481b7e04f8cSWim Van Sebroeck  */
482b7e04f8cSWim Van Sebroeck 
483b7e04f8cSWim Van Sebroeck static int pc87413_notify_sys(struct notifier_block *this,
484b7e04f8cSWim Van Sebroeck 			      unsigned long code,
485b7e04f8cSWim Van Sebroeck 			      void *unused)
486b7e04f8cSWim Van Sebroeck {
487b7e04f8cSWim Van Sebroeck 	if (code == SYS_DOWN || code == SYS_HALT)
488b7e04f8cSWim Van Sebroeck 		/* Turn the card off */
489b7e04f8cSWim Van Sebroeck 		pc87413_disable();
490b7e04f8cSWim Van Sebroeck 	return NOTIFY_DONE;
491b7e04f8cSWim Van Sebroeck }
492b7e04f8cSWim Van Sebroeck 
493b7e04f8cSWim Van Sebroeck /* -- Module's structures ---------------------------------------*/
494b7e04f8cSWim Van Sebroeck 
495b7e04f8cSWim Van Sebroeck static const struct file_operations pc87413_fops = {
496b7e04f8cSWim Van Sebroeck 	.owner		= THIS_MODULE,
497b7e04f8cSWim Van Sebroeck 	.llseek		= no_llseek,
498b7e04f8cSWim Van Sebroeck 	.write		= pc87413_write,
499aee334c2SAlan Cox 	.unlocked_ioctl	= pc87413_ioctl,
500b7e04f8cSWim Van Sebroeck 	.open		= pc87413_open,
501b7e04f8cSWim Van Sebroeck 	.release	= pc87413_release,
502b7e04f8cSWim Van Sebroeck };
503b7e04f8cSWim Van Sebroeck 
504aee334c2SAlan Cox static struct notifier_block pc87413_notifier = {
505b7e04f8cSWim Van Sebroeck 	.notifier_call  = pc87413_notify_sys,
506b7e04f8cSWim Van Sebroeck };
507b7e04f8cSWim Van Sebroeck 
508aee334c2SAlan Cox static struct miscdevice pc87413_miscdev = {
509b7e04f8cSWim Van Sebroeck 	.minor          = WATCHDOG_MINOR,
510b7e04f8cSWim Van Sebroeck 	.name           = "watchdog",
5117944d3a5SWim Van Sebroeck 	.fops           = &pc87413_fops,
512b7e04f8cSWim Van Sebroeck };
513b7e04f8cSWim Van Sebroeck 
514b7e04f8cSWim Van Sebroeck /* -- Module init functions -------------------------------------*/
515b7e04f8cSWim Van Sebroeck 
516b7e04f8cSWim Van Sebroeck /**
517b7e04f8cSWim Van Sebroeck  *	pc87413_init: module's "constructor"
518b7e04f8cSWim Van Sebroeck  *
519b7e04f8cSWim Van Sebroeck  *	Set up the WDT watchdog board. All we have to do is grab the
520b7e04f8cSWim Van Sebroeck  *	resources we require and bitch if anyone beat us to them.
521b7e04f8cSWim Van Sebroeck  *	The open() function will actually kick the board off.
522b7e04f8cSWim Van Sebroeck  */
523b7e04f8cSWim Van Sebroeck 
524b7e04f8cSWim Van Sebroeck static int __init pc87413_init(void)
525b7e04f8cSWim Van Sebroeck {
526b7e04f8cSWim Van Sebroeck 	int ret;
527b7e04f8cSWim Van Sebroeck 
528aee334c2SAlan Cox 	printk(KERN_INFO PFX "Version " VERSION " at io 0x%X\n",
529aee334c2SAlan Cox 							WDT_INDEX_IO_PORT);
530b7e04f8cSWim Van Sebroeck 
531b7e04f8cSWim Van Sebroeck 	/* request_region(io, 2, "pc87413"); */
532b7e04f8cSWim Van Sebroeck 
533b7e04f8cSWim Van Sebroeck 	ret = register_reboot_notifier(&pc87413_notifier);
534b7e04f8cSWim Van Sebroeck 	if (ret != 0) {
535aee334c2SAlan Cox 		printk(KERN_ERR PFX
536aee334c2SAlan Cox 			"cannot register reboot notifier (err=%d)\n", ret);
537b7e04f8cSWim Van Sebroeck 	}
538b7e04f8cSWim Van Sebroeck 
539b7e04f8cSWim Van Sebroeck 	ret = misc_register(&pc87413_miscdev);
540b7e04f8cSWim Van Sebroeck 	if (ret != 0) {
541143a2e54SWim Van Sebroeck 		printk(KERN_ERR PFX
542143a2e54SWim Van Sebroeck 			"cannot register miscdev on minor=%d (err=%d)\n",
543b7e04f8cSWim Van Sebroeck 			WATCHDOG_MINOR, ret);
544b7e04f8cSWim Van Sebroeck 		unregister_reboot_notifier(&pc87413_notifier);
545b7e04f8cSWim Van Sebroeck 		return ret;
546b7e04f8cSWim Van Sebroeck 	}
547b7e04f8cSWim Van Sebroeck 	printk(KERN_INFO PFX "initialized. timeout=%d min \n", timeout);
548b7e04f8cSWim Van Sebroeck 	pc87413_enable();
549b7e04f8cSWim Van Sebroeck 	return 0;
550b7e04f8cSWim Van Sebroeck }
551b7e04f8cSWim Van Sebroeck 
552b7e04f8cSWim Van Sebroeck /**
553b7e04f8cSWim Van Sebroeck  *	pc87413_exit: module's "destructor"
554b7e04f8cSWim Van Sebroeck  *
555b7e04f8cSWim Van Sebroeck  *	Unload the watchdog. You cannot do this with any file handles open.
556b7e04f8cSWim Van Sebroeck  *	If your watchdog is set to continue ticking on close and you unload
557b7e04f8cSWim Van Sebroeck  *	it, well it keeps ticking. We won't get the interrupt but the board
558b7e04f8cSWim Van Sebroeck  *	will not touch PC memory so all is fine. You just have to load a new
559b7e04f8cSWim Van Sebroeck  *	module in 60 seconds or reboot.
560b7e04f8cSWim Van Sebroeck  */
561b7e04f8cSWim Van Sebroeck 
562b7e04f8cSWim Van Sebroeck static void __exit pc87413_exit(void)
563b7e04f8cSWim Van Sebroeck {
564b7e04f8cSWim Van Sebroeck 	/* Stop the timer before we leave */
565aee334c2SAlan Cox 	if (!nowayout) {
566b7e04f8cSWim Van Sebroeck 		pc87413_disable();
567b7e04f8cSWim Van Sebroeck 		printk(KERN_INFO MODNAME "Watchdog disabled.\n");
568b7e04f8cSWim Van Sebroeck 	}
569b7e04f8cSWim Van Sebroeck 
570b7e04f8cSWim Van Sebroeck 	misc_deregister(&pc87413_miscdev);
571b7e04f8cSWim Van Sebroeck 	unregister_reboot_notifier(&pc87413_notifier);
572b7e04f8cSWim Van Sebroeck 	/* release_region(io, 2); */
573b7e04f8cSWim Van Sebroeck 
5747944d3a5SWim Van Sebroeck 	printk(KERN_INFO MODNAME " watchdog component driver removed.\n");
575b7e04f8cSWim Van Sebroeck }
576b7e04f8cSWim Van Sebroeck 
577b7e04f8cSWim Van Sebroeck module_init(pc87413_init);
578b7e04f8cSWim Van Sebroeck module_exit(pc87413_exit);
579b7e04f8cSWim Van Sebroeck 
580143a2e54SWim Van Sebroeck MODULE_AUTHOR("Sven Anders <anders@anduras.de>, "
581143a2e54SWim Van Sebroeck 		"Marcus Junker <junker@anduras.de>,");
582b7e04f8cSWim Van Sebroeck MODULE_DESCRIPTION("PC87413 WDT driver");
583b7e04f8cSWim Van Sebroeck MODULE_LICENSE("GPL");
584b7e04f8cSWim Van Sebroeck 
585b7e04f8cSWim Van Sebroeck MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
586b7e04f8cSWim Van Sebroeck 
587b7e04f8cSWim Van Sebroeck module_param(io, int, 0);
58876550d32SRandy Dunlap MODULE_PARM_DESC(io, MODNAME " I/O port (default: "
58976550d32SRandy Dunlap 					__MODULE_STRING(IO_DEFAULT) ").");
590b7e04f8cSWim Van Sebroeck 
591b7e04f8cSWim Van Sebroeck module_param(timeout, int, 0);
592aee334c2SAlan Cox MODULE_PARM_DESC(timeout,
593aee334c2SAlan Cox 		"Watchdog timeout in minutes (default="
59476550d32SRandy Dunlap 				__MODULE_STRING(DEFAULT_TIMEOUT) ").");
595b7e04f8cSWim Van Sebroeck 
596b7e04f8cSWim Van Sebroeck module_param(nowayout, int, 0);
597aee334c2SAlan Cox MODULE_PARM_DESC(nowayout,
598aee334c2SAlan Cox 		"Watchdog cannot be stopped once started (default="
599aee334c2SAlan Cox 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
600b7e04f8cSWim Van Sebroeck 
601