Blame view

drivers/watchdog/pc87413_wdt.c 14.7 KB
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
1
2
3
  /*
   *      NS pc87413-wdt Watchdog Timer driver for Linux 2.6.x.x
   *
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
4
   *      This code is based on wdt.c with original copyright.
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
5
   *
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
6
7
   *      (C) Copyright 2006 Sven Anders, <anders@anduras.de>
   *                     and Marcus Junker, <junker@anduras.de>
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
8
9
10
11
12
13
   *
   *      This program is free software; you can redistribute it and/or
   *      modify it under the terms of the GNU General Public License
   *      as published by the Free Software Foundation; either version
   *      2 of the License, or (at your option) any later version.
   *
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
14
   *      Neither Sven Anders, Marcus Junker nor ANDURAS AG
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
15
16
17
   *      admit liability nor provide warranty for any of this software.
   *      This material is provided "AS-IS" and at no charge.
   *
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
18
   *      Release 1.1
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
19
   */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
20
21
22
23
24
25
26
27
28
29
30
31
  #include <linux/module.h>
  #include <linux/types.h>
  #include <linux/miscdevice.h>
  #include <linux/watchdog.h>
  #include <linux/ioport.h>
  #include <linux/delay.h>
  #include <linux/notifier.h>
  #include <linux/fs.h>
  #include <linux/reboot.h>
  #include <linux/init.h>
  #include <linux/spinlock.h>
  #include <linux/moduleparam.h>
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
32
33
  #include <linux/io.h>
  #include <linux/uaccess.h>
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
34

789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
35
  #include <asm/system.h>
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
36
  /* #define DEBUG 1 */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
37

7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
38
  #define DEFAULT_TIMEOUT     1		/* 1 minute */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
39
  #define MAX_TIMEOUT         255
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
40

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
41
42
43
44
  #define VERSION             "1.1"
  #define MODNAME             "pc87413 WDT"
  #define PFX                 MODNAME ": "
  #define DPFX                MODNAME " - DEBUG: "
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
45

7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
46
  #define WDT_INDEX_IO_PORT   (io+0)	/* I/O port base (index register) */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
47
48
  #define WDT_DATA_IO_PORT    (WDT_INDEX_IO_PORT+1)
  #define SWC_LDN             0x04
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
49
50
51
52
  #define SIOCFG2             0x22	/* Serial IO register */
  #define WDCTL               0x10	/* Watchdog-Timer-Controll-Register */
  #define WDTO                0x11	/* Watchdog timeout register */
  #define WDCFG               0x12	/* Watchdog config register */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
53

76550d329   Randy Dunlap   watchdog: fix sev...
54
55
56
  #define IO_DEFAULT	0x2E		/* Address used on Portwell Boards */
  
  static int io = IO_DEFAULT;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
57

7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
58
  static int timeout = DEFAULT_TIMEOUT;	/* timeout value */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
59
  static unsigned long timer_enabled;	/* is the timer enabled? */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
60

aee334c23   Alan Cox   [WATCHDOG 31/57] ...
61
  static char expect_close;		/* is the close expected? */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
62

aee334c23   Alan Cox   [WATCHDOG 31/57] ...
63
  static DEFINE_SPINLOCK(io_lock);	/* to guard us from io races */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
64

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
65
  static int nowayout = WATCHDOG_NOWAYOUT;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
66

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
67
  /* -- Low level function ----------------------------------------*/
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
68

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
69
  /* Select pins for Watchdog output */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
70

aee334c23   Alan Cox   [WATCHDOG 31/57] ...
71
  static inline void pc87413_select_wdt_out(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
72
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
73
  	unsigned int cr_data = 0;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
74

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
75
  	/* Step 1: Select multiple pin,pin55,as WDT output */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
76
77
  
  	outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
78
  	cr_data = inb(WDT_DATA_IO_PORT);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
79
80
81
82
83
  
  	cr_data |= 0x80; /* Set Bit7 to 1*/
  	outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
  
  	outb_p(cr_data, WDT_DATA_IO_PORT);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
84
  #ifdef DEBUG
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
85
86
87
88
  	printk(KERN_INFO DPFX
  		"Select multiple pin,pin55,as WDT output: Bit7 to 1: %d
  ",
  								cr_data);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
89
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
90
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
91
  /* Enable SWC functions */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
92

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
93
94
  static inline void pc87413_enable_swc(void)
  {
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
95
  	unsigned int cr_data = 0;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
96
97
  
  	/* Step 2: Enable SWC functions */
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
98
  	outb_p(0x07, WDT_INDEX_IO_PORT);	/* Point SWC_LDN (LDN=4) */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
99
  	outb_p(SWC_LDN, WDT_DATA_IO_PORT);
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
100
  	outb_p(0x30, WDT_INDEX_IO_PORT);	/* Read Index 0x30 First */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
101
  	cr_data = inb(WDT_DATA_IO_PORT);
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
102
  	cr_data |= 0x01;			/* Set Bit0 to 1 */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
103
  	outb_p(0x30, WDT_INDEX_IO_PORT);
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
104
  	outb_p(cr_data, WDT_DATA_IO_PORT);	/* Index0x30_bit0P1 */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
105

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
106
  #ifdef DEBUG
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
107
108
  	printk(KERN_INFO DPFX "pc87413 - Enable SWC functions
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
109
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
110
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
111
112
113
  /* Read SWC I/O base address */
  
  static inline unsigned int pc87413_get_swc_base(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
114
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
115
116
  	unsigned int  swc_base_addr = 0;
  	unsigned char addr_l, addr_h = 0;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
117
118
  
  	/* Step 3: Read SWC I/O Base Address */
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
119
  	outb_p(0x60, WDT_INDEX_IO_PORT);	/* Read Index 0x60 */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
120
  	addr_h = inb(WDT_DATA_IO_PORT);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
121

7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
122
  	outb_p(0x61, WDT_INDEX_IO_PORT);	/* Read Index 0x61 */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
123

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
124
  	addr_l = inb(WDT_DATA_IO_PORT);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
125
126
  
  	swc_base_addr = (addr_h << 8) + addr_l;
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
127
  #ifdef DEBUG
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
128
129
130
131
  	printk(KERN_INFO DPFX
  		"Read SWC I/O Base Address: low %d, high %d, res %d
  ",
  						addr_l, addr_h, swc_base_addr);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
132
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
133
134
  	return swc_base_addr;
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
135
136
137
  /* Select Bank 3 of SWC */
  
  static inline void pc87413_swc_bank3(unsigned int swc_base_addr)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
138
139
  {
  	/* Step 4: Select Bank3 of SWC */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
140
  	outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
141
  #ifdef DEBUG
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
142
143
  	printk(KERN_INFO DPFX "Select Bank3 of SWC
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
144
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
145
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
146
147
148
149
  /* Set watchdog timeout to x minutes */
  
  static inline void pc87413_programm_wdto(unsigned int swc_base_addr,
  					 char pc87413_time)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
150
151
  {
  	/* Step 5: Programm WDTO, Twd. */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
152
  	outb_p(pc87413_time, swc_base_addr + WDTO);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
153
  #ifdef DEBUG
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
154
155
  	printk(KERN_INFO DPFX "Set WDTO to %d minutes
  ", pc87413_time);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
156
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
157
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
158
159
160
  /* Enable WDEN */
  
  static inline void pc87413_enable_wden(unsigned int swc_base_addr)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
161
162
  {
  	/* Step 6: Enable WDEN */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
163
  	outb_p(inb(swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
164
  #ifdef DEBUG
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
165
166
  	printk(KERN_INFO DPFX "Enable WDEN
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
167
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
168
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
169
170
  /* Enable SW_WD_TREN */
  static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
171
172
  {
  	/* Enable SW_WD_TREN */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
173
  	outb_p(inb(swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
174
  #ifdef DEBUG
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
175
176
  	printk(KERN_INFO DPFX "Enable SW_WD_TREN
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
177
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
178
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
179
180
181
  /* Disable SW_WD_TREN */
  
  static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
182
183
  {
  	/* Disable SW_WD_TREN */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
184
  	outb_p(inb(swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
185
  #ifdef DEBUG
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
186
187
  	printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
188
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
189
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
190
  /* Enable SW_WD_TRG */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
191

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
192
  static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
193
  {
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
194
  	/* Enable SW_WD_TRG */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
195
  	outb_p(inb(swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
196
  #ifdef DEBUG
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
197
198
  	printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
199
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
200
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
201
  /* Disable SW_WD_TRG */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
202

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
203
204
  static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr)
  {
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
205
  	/* Disable SW_WD_TRG */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
206
  	outb_p(inb(swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
207
  #ifdef DEBUG
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
208
209
  	printk(KERN_INFO DPFX "Disable SW_WD_TRG
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
210
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
211
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
212
  /* -- Higher level functions ------------------------------------*/
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
213

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
214
  /* Enable the watchdog */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
215

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
216
  static void pc87413_enable(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
217
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
218
219
220
  	unsigned int swc_base_addr;
  
  	spin_lock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
221
222
223
224
225
  
  	pc87413_select_wdt_out();
  	pc87413_enable_swc();
  	swc_base_addr = pc87413_get_swc_base();
  	pc87413_swc_bank3(swc_base_addr);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
226
227
228
229
230
231
  	pc87413_programm_wdto(swc_base_addr, timeout);
  	pc87413_enable_wden(swc_base_addr);
  	pc87413_enable_sw_wd_tren(swc_base_addr);
  	pc87413_enable_sw_wd_trg(swc_base_addr);
  
  	spin_unlock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
232
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
233
  /* Disable the watchdog */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
234

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
235
  static void pc87413_disable(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
236
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
237
238
239
  	unsigned int swc_base_addr;
  
  	spin_lock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
240
241
242
243
244
245
246
  
  	pc87413_select_wdt_out();
  	pc87413_enable_swc();
  	swc_base_addr = pc87413_get_swc_base();
  	pc87413_swc_bank3(swc_base_addr);
  	pc87413_disable_sw_wd_tren(swc_base_addr);
  	pc87413_disable_sw_wd_trg(swc_base_addr);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
247
248
249
  	pc87413_programm_wdto(swc_base_addr, 0);
  
  	spin_unlock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
250
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
251
  /* Refresh the watchdog */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
252

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
253
  static void pc87413_refresh(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
254
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
255
256
257
  	unsigned int swc_base_addr;
  
  	spin_lock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
258
259
260
261
262
  
  	pc87413_select_wdt_out();
  	pc87413_enable_swc();
  	swc_base_addr = pc87413_get_swc_base();
  	pc87413_swc_bank3(swc_base_addr);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
263
264
265
  	pc87413_disable_sw_wd_tren(swc_base_addr);
  	pc87413_disable_sw_wd_trg(swc_base_addr);
  	pc87413_programm_wdto(swc_base_addr, timeout);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
266
267
268
  	pc87413_enable_wden(swc_base_addr);
  	pc87413_enable_sw_wd_tren(swc_base_addr);
  	pc87413_enable_sw_wd_trg(swc_base_addr);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
269
  	spin_unlock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
270
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  /* -- File operations -------------------------------------------*/
  
  /**
   *	pc87413_open:
   *	@inode: inode of device
   *	@file: file handle to device
   *
   */
  
  static int pc87413_open(struct inode *inode, struct file *file)
  {
  	/* /dev/watchdog can only be opened once */
  
  	if (test_and_set_bit(0, &timer_enabled))
  		return -EBUSY;
  
  	if (nowayout)
  		__module_get(THIS_MODULE);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
289

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
290
291
  	/* Reload and activate timer */
  	pc87413_refresh();
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
292

aee334c23   Alan Cox   [WATCHDOG 31/57] ...
293
294
295
  	printk(KERN_INFO MODNAME
  		"Watchdog enabled. Timeout set to %d minute(s).
  ", timeout);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
296
297
298
  
  	return nonseekable_open(inode, file);
  }
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
299
300
  
  /**
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
301
302
303
   *	pc87413_release:
   *	@inode: inode to board
   *	@file: file handle to board
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
304
   *
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
305
306
307
308
309
   *	The watchdog has a configurable API. There is a religious dispute
   *	between people who want their watchdog to be able to shut down and
   *	those who want to be sure if the watchdog manager dies the machine
   *	reboots. In the former case we disable the counters, in the latter
   *	case you have to open it again very soon.
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
310
   */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
311
  static int pc87413_release(struct inode *inode, struct file *file)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
312
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
313
314
315
316
  	/* Shut off the timer. */
  
  	if (expect_close == 42) {
  		pc87413_disable();
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
317
318
319
  		printk(KERN_INFO MODNAME
  				"Watchdog disabled, sleeping again...
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
320
  	} else {
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
321
322
323
  		printk(KERN_CRIT MODNAME
  				"Unexpected close, not stopping watchdog!
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
324
325
  		pc87413_refresh();
  	}
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
326
327
  	clear_bit(0, &timer_enabled);
  	expect_close = 0;
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
328
  	return 0;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
329
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
330
331
332
333
334
335
336
337
  /**
   *	pc87413_status:
   *
   *      return, if the watchdog is enabled (timeout is set...)
   */
  
  
  static int pc87413_status(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
338
  {
0835caa2b   Wim Van Sebroeck   [WATCHDOG] NS pc8...
339
  	  return 0; /* currently not supported */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
340
341
342
343
344
  }
  
  /**
   *	pc87413_write:
   *	@file: file handle to the watchdog
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
345
346
   *	@data: data buffer to write
   *	@len: length in bytes
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
347
348
349
350
351
   *	@ppos: pointer to the position to write. No seeks allowed
   *
   *	A write to a watchdog device is defined as a keepalive signal. Any
   *	write of data will do, as we we don't define content meaning.
   */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
352
353
  static ssize_t pc87413_write(struct file *file, const char __user *data,
  			     size_t len, loff_t *ppos)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
354
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
355
356
357
358
359
360
361
  	/* See if we got the magic character 'V' and reload the timer */
  	if (len) {
  		if (!nowayout) {
  			size_t i;
  
  			/* reset expect flag */
  			expect_close = 0;
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
362
363
  			/* scan to see whether or not we got the
  			   magic character */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
364
365
  			for (i = 0; i != len; i++) {
  				char c;
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
366
  				if (get_user(c, data + i))
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
367
368
369
370
371
372
373
374
  					return -EFAULT;
  				if (c == 'V')
  					expect_close = 42;
  			}
  		}
  
  		/* someone wrote to us, we should reload the timer */
  		pc87413_refresh();
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
375
  	}
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
376
  	return len;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
377
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
378
  /**
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
379
   *	pc87413_ioctl:
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
380
381
382
383
384
385
386
387
   *	@file: file handle to the device
   *	@cmd: watchdog command
   *	@arg: argument pointer
   *
   *	The watchdog API defines a common set of functions for all watchdogs
   *	according to their available features. We only actually usefully support
   *	querying capabilities and current status.
   */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
388
389
  static long pc87413_ioctl(struct file *file, unsigned int cmd,
  						unsigned long arg)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
390
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
391
392
393
394
395
396
  	int new_timeout;
  
  	union {
  		struct watchdog_info __user *ident;
  		int __user *i;
  	} uarg;
42747d712   Wim Van Sebroeck   [WATCHDOG] watchd...
397
  	static const struct watchdog_info ident = {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
398
  		.options          = WDIOF_KEEPALIVEPING |
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
399
400
  				    WDIOF_SETTIMEOUT |
  				    WDIOF_MAGICCLOSE,
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
401
  		.firmware_version = 1,
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
402
  		.identity         = "PC87413(HF/F) watchdog",
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
403
  	};
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
404
  	uarg.i = (int __user *)arg;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
405

aee334c23   Alan Cox   [WATCHDOG 31/57] ...
406
407
408
409
410
411
412
413
  	switch (cmd) {
  	case WDIOC_GETSUPPORT:
  		return copy_to_user(uarg.ident, &ident,
  					sizeof(ident)) ? -EFAULT : 0;
  	case WDIOC_GETSTATUS:
  		return put_user(pc87413_status(), uarg.i);
  	case WDIOC_GETBOOTSTATUS:
  		return put_user(0, uarg.i);
0c06090c9   Wim Van Sebroeck   [WATCHDOG] Coding...
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
  	case WDIOC_SETOPTIONS:
  	{
  		int options, retval = -EINVAL;
  		if (get_user(options, uarg.i))
  			return -EFAULT;
  		if (options & WDIOS_DISABLECARD) {
  			pc87413_disable();
  			retval = 0;
  		}
  		if (options & WDIOS_ENABLECARD) {
  			pc87413_enable();
  			retval = 0;
  		}
  		return retval;
  	}
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
429
430
  	case WDIOC_KEEPALIVE:
  		pc87413_refresh();
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
431
  #ifdef DEBUG
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
432
433
  		printk(KERN_INFO DPFX "keepalive
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
434
  #endif
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  		return 0;
  	case WDIOC_SETTIMEOUT:
  		if (get_user(new_timeout, uarg.i))
  			return -EFAULT;
  		/* the API states this is given in secs */
  		new_timeout /= 60;
  		if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
  			return -EINVAL;
  		timeout = new_timeout;
  		pc87413_refresh();
  		/* fall through and return the new timeout... */
  	case WDIOC_GETTIMEOUT:
  		new_timeout = timeout * 60;
  		return put_user(new_timeout, uarg.i);
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
449
450
  	default:
  		return -ENOTTY;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
451
  	}
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
452
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
453
  /* -- Notifier funtions -----------------------------------------*/
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
454
455
456
457
458
459
460
461
462
463
464
  /**
   *	notify_sys:
   *	@this: our notifier block
   *	@code: the event being reported
   *	@unused: unused
   *
   *	Our notifier is called on system shutdowns. We want to turn the card
   *	off at reboot otherwise the machine will reboot again during memory
   *	test or worse yet during the following fsck. This would suck, in fact
   *	trust me - if it happens it does suck.
   */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
465
466
467
  static int pc87413_notify_sys(struct notifier_block *this,
  			      unsigned long code,
  			      void *unused)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
468
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
469
  	if (code == SYS_DOWN || code == SYS_HALT)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
470
471
  		/* Turn the card off */
  		pc87413_disable();
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
472
473
  	return NOTIFY_DONE;
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
474
  /* -- Module's structures ---------------------------------------*/
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
475

2b8693c06   Arjan van de Ven   [PATCH] mark stru...
476
  static const struct file_operations pc87413_fops = {
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
477
  	.owner		= THIS_MODULE,
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
478
  	.llseek		= no_llseek,
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
479
  	.write		= pc87413_write,
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
480
  	.unlocked_ioctl	= pc87413_ioctl,
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
481
482
483
  	.open		= pc87413_open,
  	.release	= pc87413_release,
  };
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
484
  static struct notifier_block pc87413_notifier = {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
485
  	.notifier_call  = pc87413_notify_sys,
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
486
  };
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
487
  static struct miscdevice pc87413_miscdev = {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
488
489
  	.minor          = WATCHDOG_MINOR,
  	.name           = "watchdog",
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
490
  	.fops           = &pc87413_fops,
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
491
  };
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
492
  /* -- Module init functions -------------------------------------*/
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
493
494
  
  /**
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
495
   *	pc87413_init: module's "constructor"
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
496
497
498
499
500
   *
   *	Set up the WDT watchdog board. All we have to do is grab the
   *	resources we require and bitch if anyone beat us to them.
   *	The open() function will actually kick the board off.
   */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
501
  static int __init pc87413_init(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
502
503
  {
  	int ret;
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
504
505
506
  	printk(KERN_INFO PFX "Version " VERSION " at io 0x%X
  ",
  							WDT_INDEX_IO_PORT);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
507
508
  
  	/* request_region(io, 2, "pc87413"); */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
509
510
  	ret = register_reboot_notifier(&pc87413_notifier);
  	if (ret != 0) {
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
511
512
513
  		printk(KERN_ERR PFX
  			"cannot register reboot notifier (err=%d)
  ", ret);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
514
  	}
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
515
  	ret = misc_register(&pc87413_miscdev);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
516
  	if (ret != 0) {
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
517
518
519
  		printk(KERN_ERR PFX
  			"cannot register miscdev on minor=%d (err=%d)
  ",
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
520
  			WATCHDOG_MINOR, ret);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
521
522
523
  		unregister_reboot_notifier(&pc87413_notifier);
  		return ret;
  	}
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
524
525
  	printk(KERN_INFO PFX "initialized. timeout=%d min 
  ", timeout);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
526
  	pc87413_enable();
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
527
528
  	return 0;
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
529
530
531
532
533
534
535
536
537
538
539
540
541
  /**
   *	pc87413_exit: module's "destructor"
   *
   *	Unload the watchdog. You cannot do this with any file handles open.
   *	If your watchdog is set to continue ticking on close and you unload
   *	it, well it keeps ticking. We won't get the interrupt but the board
   *	will not touch PC memory so all is fine. You just have to load a new
   *	module in 60 seconds or reboot.
   */
  
  static void __exit pc87413_exit(void)
  {
  	/* Stop the timer before we leave */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
542
  	if (!nowayout) {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
543
544
545
546
547
548
549
  		pc87413_disable();
  		printk(KERN_INFO MODNAME "Watchdog disabled.
  ");
  	}
  
  	misc_deregister(&pc87413_miscdev);
  	unregister_reboot_notifier(&pc87413_notifier);
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
550
  	/* release_region(io, 2); */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
551

7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
552
553
  	printk(KERN_INFO MODNAME " watchdog component driver removed.
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
554
  }
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
555
556
557
  
  module_init(pc87413_init);
  module_exit(pc87413_exit);
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
558
559
  MODULE_AUTHOR("Sven Anders <anders@anduras.de>, "
  		"Marcus Junker <junker@anduras.de>,");
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
560
  MODULE_DESCRIPTION("PC87413 WDT driver");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
561
  MODULE_LICENSE("GPL");
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
562
  MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
563
  module_param(io, int, 0);
76550d329   Randy Dunlap   watchdog: fix sev...
564
565
  MODULE_PARM_DESC(io, MODNAME " I/O port (default: "
  					__MODULE_STRING(IO_DEFAULT) ").");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
566
567
  
  module_param(timeout, int, 0);
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
568
569
  MODULE_PARM_DESC(timeout,
  		"Watchdog timeout in minutes (default="
76550d329   Randy Dunlap   watchdog: fix sev...
570
  				__MODULE_STRING(DEFAULT_TIMEOUT) ").");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
571
572
  
  module_param(nowayout, int, 0);
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
573
574
575
  MODULE_PARM_DESC(nowayout,
  		"Watchdog cannot be stopped once started (default="
  				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
576