Blame view

drivers/watchdog/pc87413_wdt.c 13.8 KB
d01732789   Guenter Roeck   watchdog: convert...
1
  // SPDX-License-Identifier: GPL-2.0+
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
2
3
4
  /*
   *      NS pc87413-wdt Watchdog Timer driver for Linux 2.6.x.x
   *
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
5
   *      This code is based on wdt.c with original copyright.
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
6
   *
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
7
8
   *      (C) Copyright 2006 Sven Anders, <anders@anduras.de>
   *                     and Marcus Junker, <junker@anduras.de>
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
9
   *
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
10
   *      Neither Sven Anders, Marcus Junker nor ANDURAS AG
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
11
12
13
   *      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...
14
   *      Release 1.1
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
15
   */
27c766aaa   Joe Perches   watchdog: Use pr_...
16
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
17
18
19
20
21
22
23
24
25
26
27
28
  #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] ...
29
30
  #include <linux/io.h>
  #include <linux/uaccess.h>
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
31

789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
32

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
33
  /* #define DEBUG 1 */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
34

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

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
38
39
  #define VERSION             "1.1"
  #define MODNAME             "pc87413 WDT"
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
40
  #define DPFX                MODNAME " - DEBUG: "
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
41

7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
42
  #define WDT_INDEX_IO_PORT   (io+0)	/* I/O port base (index register) */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
43
44
  #define WDT_DATA_IO_PORT    (WDT_INDEX_IO_PORT+1)
  #define SWC_LDN             0x04
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
45
  #define SIOCFG2             0x22	/* Serial IO register */
25985edce   Lucas De Marchi   Fix common misspe...
46
  #define WDCTL               0x10	/* Watchdog-Timer-Control-Register */
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
47
48
  #define WDTO                0x11	/* Watchdog timeout register */
  #define WDCFG               0x12	/* Watchdog config register */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
49

76550d329   Randy Dunlap   watchdog: fix sev...
50
51
52
  #define IO_DEFAULT	0x2E		/* Address used on Portwell Boards */
  
  static int io = IO_DEFAULT;
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
53
  static int swc_base_addr = -1;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
54

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

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

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

86a1e1896   Wim Van Sebroeck   watchdog: nowayou...
62
  static bool nowayout = WATCHDOG_NOWAYOUT;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
63

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

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

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

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
72
  	/* Step 1: Select multiple pin,pin55,as WDT output */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
73
74
  
  	outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
75
  	cr_data = inb(WDT_DATA_IO_PORT);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
76
77
78
79
80
  
  	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...
81
  #ifdef DEBUG
27c766aaa   Joe Perches   watchdog: Use pr_...
82
  	pr_info(DPFX
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
83
84
85
  		"Select multiple pin,pin55,as WDT output: Bit7 to 1: %d
  ",
  								cr_data);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
86
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
87
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
88
  /* Enable SWC functions */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
89

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

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
103
  #ifdef DEBUG
27c766aaa   Joe Perches   watchdog: Use pr_...
104
105
  	pr_info(DPFX "pc87413 - Enable SWC functions
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
106
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
107
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
108
  /* Read SWC I/O base address */
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
109
  static void pc87413_get_swc_base_addr(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
110
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
111
  	unsigned char addr_l, addr_h = 0;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
112
113
  
  	/* Step 3: Read SWC I/O Base Address */
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
114
  	outb_p(0x60, WDT_INDEX_IO_PORT);	/* Read Index 0x60 */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
115
  	addr_h = inb(WDT_DATA_IO_PORT);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
116

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

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
119
  	addr_l = inb(WDT_DATA_IO_PORT);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
120
121
  
  	swc_base_addr = (addr_h << 8) + addr_l;
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
122
  #ifdef DEBUG
27c766aaa   Joe Perches   watchdog: Use pr_...
123
  	pr_info(DPFX
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
124
125
126
  		"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...
127
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
128
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
129
  /* Select Bank 3 of SWC */
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
130
  static inline void pc87413_swc_bank3(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
131
132
  {
  	/* Step 4: Select Bank3 of SWC */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
133
  	outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
134
  #ifdef DEBUG
27c766aaa   Joe Perches   watchdog: Use pr_...
135
136
  	pr_info(DPFX "Select Bank3 of SWC
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
137
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
138
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
139
  /* Set watchdog timeout to x minutes */
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
140
  static inline void pc87413_programm_wdto(char pc87413_time)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
141
142
  {
  	/* Step 5: Programm WDTO, Twd. */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
143
  	outb_p(pc87413_time, swc_base_addr + WDTO);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
144
  #ifdef DEBUG
27c766aaa   Joe Perches   watchdog: Use pr_...
145
146
  	pr_info(DPFX "Set WDTO to %d minutes
  ", pc87413_time);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
147
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
148
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
149
  /* Enable WDEN */
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
150
  static inline void pc87413_enable_wden(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
151
152
  {
  	/* Step 6: Enable WDEN */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
153
  	outb_p(inb(swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
154
  #ifdef DEBUG
27c766aaa   Joe Perches   watchdog: Use pr_...
155
156
  	pr_info(DPFX "Enable WDEN
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
157
  #endif
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
158
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
159
  /* Enable SW_WD_TREN */
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
160
  static inline void pc87413_enable_sw_wd_tren(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
161
162
  {
  	/* Enable SW_WD_TREN */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
163
  	outb_p(inb(swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
164
  #ifdef DEBUG
27c766aaa   Joe Perches   watchdog: Use pr_...
165
166
  	pr_info(DPFX "Enable SW_WD_TREN
  ");
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
  /* Disable SW_WD_TREN */
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
170
  static inline void pc87413_disable_sw_wd_tren(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
171
172
  {
  	/* Disable SW_WD_TREN */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
173
  	outb_p(inb(swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
174
  #ifdef DEBUG
27c766aaa   Joe Perches   watchdog: Use pr_...
175
176
  	pr_info(DPFX "pc87413 - Disable 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
  /* Enable SW_WD_TRG */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
180

7ccdb9467   Jonathan McDowell   watchdog: pc87413...
181
  static inline void pc87413_enable_sw_wd_trg(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
182
  {
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
183
  	/* Enable SW_WD_TRG */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
184
  	outb_p(inb(swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
185
  #ifdef DEBUG
27c766aaa   Joe Perches   watchdog: Use pr_...
186
187
  	pr_info(DPFX "pc87413 - Enable SW_WD_TRG
  ");
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
  /* Disable SW_WD_TRG */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
191

7ccdb9467   Jonathan McDowell   watchdog: pc87413...
192
  static inline void pc87413_disable_sw_wd_trg(void)
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
193
  {
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
194
  	/* Disable SW_WD_TRG */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
195
  	outb_p(inb(swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
196
  #ifdef DEBUG
27c766aaa   Joe Perches   watchdog: Use pr_...
197
198
  	pr_info(DPFX "Disable 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
  /* -- Higher level functions ------------------------------------*/
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
202

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

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
205
  static void pc87413_enable(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
206
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
207
  	spin_lock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
208

7ccdb9467   Jonathan McDowell   watchdog: pc87413...
209
210
211
212
213
  	pc87413_swc_bank3();
  	pc87413_programm_wdto(timeout);
  	pc87413_enable_wden();
  	pc87413_enable_sw_wd_tren();
  	pc87413_enable_sw_wd_trg();
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
214
215
  
  	spin_unlock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
216
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
217
  /* Disable the watchdog */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
218

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
219
  static void pc87413_disable(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
220
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
221
  	spin_lock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
222

7ccdb9467   Jonathan McDowell   watchdog: pc87413...
223
224
225
226
  	pc87413_swc_bank3();
  	pc87413_disable_sw_wd_tren();
  	pc87413_disable_sw_wd_trg();
  	pc87413_programm_wdto(0);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
227
228
  
  	spin_unlock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
229
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
230
  /* Refresh the watchdog */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
231

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
232
  static void pc87413_refresh(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
233
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
234
  	spin_lock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
235

7ccdb9467   Jonathan McDowell   watchdog: pc87413...
236
237
238
239
240
241
242
  	pc87413_swc_bank3();
  	pc87413_disable_sw_wd_tren();
  	pc87413_disable_sw_wd_trg();
  	pc87413_programm_wdto(timeout);
  	pc87413_enable_wden();
  	pc87413_enable_sw_wd_tren();
  	pc87413_enable_sw_wd_trg();
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
243

00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
244
  	spin_unlock(&io_lock);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
245
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  /* -- 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...
264

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

27c766aaa   Joe Perches   watchdog: Use pr_...
268
269
  	pr_info("Watchdog enabled. Timeout set to %d minute(s).
  ", timeout);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
270

c5bf68fe0   Kirill Smelkov   *: convert stream...
271
  	return stream_open(inode, file);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
272
  }
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
273
274
  
  /**
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
275
276
277
   *	pc87413_release:
   *	@inode: inode to board
   *	@file: file handle to board
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
278
   *
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
279
280
281
282
283
   *	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...
284
   */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
285
  static int pc87413_release(struct inode *inode, struct file *file)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
286
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
287
288
289
290
  	/* Shut off the timer. */
  
  	if (expect_close == 42) {
  		pc87413_disable();
27c766aaa   Joe Perches   watchdog: Use pr_...
291
292
  		pr_info("Watchdog disabled, sleeping again...
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
293
  	} else {
27c766aaa   Joe Perches   watchdog: Use pr_...
294
295
  		pr_crit("Unexpected close, not stopping watchdog!
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
296
297
  		pc87413_refresh();
  	}
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
298
299
  	clear_bit(0, &timer_enabled);
  	expect_close = 0;
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
300
  	return 0;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
301
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
302
303
304
305
306
307
308
309
  /**
   *	pc87413_status:
   *
   *      return, if the watchdog is enabled (timeout is set...)
   */
  
  
  static int pc87413_status(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
310
  {
0835caa2b   Wim Van Sebroeck   [WATCHDOG] NS pc8...
311
  	  return 0; /* currently not supported */
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
312
313
314
315
316
  }
  
  /**
   *	pc87413_write:
   *	@file: file handle to the watchdog
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
317
318
   *	@data: data buffer to write
   *	@len: length in bytes
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
319
320
321
322
323
   *	@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...
324
325
  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...
326
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
327
328
329
330
331
332
333
  	/* 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] ...
334
335
  			/* scan to see whether or not we got the
  			   magic character */
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
336
337
  			for (i = 0; i != len; i++) {
  				char c;
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
338
  				if (get_user(c, data + i))
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
339
340
341
342
343
344
345
346
  					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...
347
  	}
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
348
  	return len;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
349
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
350
  /**
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
351
   *	pc87413_ioctl:
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
352
353
354
355
356
357
358
359
   *	@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] ...
360
361
  static long pc87413_ioctl(struct file *file, unsigned int cmd,
  						unsigned long arg)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
362
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
363
364
365
366
367
368
  	int new_timeout;
  
  	union {
  		struct watchdog_info __user *ident;
  		int __user *i;
  	} uarg;
42747d712   Wim Van Sebroeck   [WATCHDOG] watchd...
369
  	static const struct watchdog_info ident = {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
370
  		.options          = WDIOF_KEEPALIVEPING |
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
371
372
  				    WDIOF_SETTIMEOUT |
  				    WDIOF_MAGICCLOSE,
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
373
  		.firmware_version = 1,
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
374
  		.identity         = "PC87413(HF/F) watchdog",
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
375
  	};
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
376
  	uarg.i = (int __user *)arg;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
377

aee334c23   Alan Cox   [WATCHDOG 31/57] ...
378
379
380
381
382
383
384
385
  	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...
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
  	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] ...
401
402
  	case WDIOC_KEEPALIVE:
  		pc87413_refresh();
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
403
  #ifdef DEBUG
27c766aaa   Joe Perches   watchdog: Use pr_...
404
405
  		pr_info(DPFX "keepalive
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
406
  #endif
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
407
408
409
410
411
412
413
414
415
416
  		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();
bd490f822   Gustavo A. R. Silva   watchdog: Use fal...
417
  		fallthrough;	/* and return the new timeout */
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
418
419
420
  	case WDIOC_GETTIMEOUT:
  		new_timeout = timeout * 60;
  		return put_user(new_timeout, uarg.i);
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
421
422
  	default:
  		return -ENOTTY;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
423
  	}
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
424
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
425
  /* -- Notifier funtions -----------------------------------------*/
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
426
427
428
429
430
431
432
433
434
435
436
  /**
   *	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...
437
438
439
  static int pc87413_notify_sys(struct notifier_block *this,
  			      unsigned long code,
  			      void *unused)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
440
  {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
441
  	if (code == SYS_DOWN || code == SYS_HALT)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
442
443
  		/* Turn the card off */
  		pc87413_disable();
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
444
445
  	return NOTIFY_DONE;
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
446
  /* -- Module's structures ---------------------------------------*/
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
447

2b8693c06   Arjan van de Ven   [PATCH] mark stru...
448
  static const struct file_operations pc87413_fops = {
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
449
  	.owner		= THIS_MODULE,
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
450
  	.llseek		= no_llseek,
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
451
  	.write		= pc87413_write,
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
452
  	.unlocked_ioctl	= pc87413_ioctl,
b6dfb2477   Arnd Bergmann   compat_ioctl: mov...
453
  	.compat_ioctl	= compat_ptr_ioctl,
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
454
455
456
  	.open		= pc87413_open,
  	.release	= pc87413_release,
  };
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
457
  static struct notifier_block pc87413_notifier = {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
458
  	.notifier_call  = pc87413_notify_sys,
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
459
  };
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
460
  static struct miscdevice pc87413_miscdev = {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
461
462
  	.minor          = WATCHDOG_MINOR,
  	.name           = "watchdog",
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
463
  	.fops           = &pc87413_fops,
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
464
  };
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
465
  /* -- Module init functions -------------------------------------*/
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
466
467
  
  /**
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
468
   *	pc87413_init: module's "constructor"
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
469
470
471
472
473
   *
   *	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...
474
  static int __init pc87413_init(void)
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
475
476
  {
  	int ret;
27c766aaa   Joe Perches   watchdog: Use pr_...
477
478
  	pr_info("Version " VERSION " at io 0x%X
  ",
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
479
  							WDT_INDEX_IO_PORT);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
480

7ccdb9467   Jonathan McDowell   watchdog: pc87413...
481
482
  	if (!request_muxed_region(io, 2, MODNAME))
  		return -EBUSY;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
483

789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
484
  	ret = register_reboot_notifier(&pc87413_notifier);
5f5e19093   Jingoo Han   watchdog: fix che...
485
  	if (ret != 0)
27c766aaa   Joe Perches   watchdog: Use pr_...
486
487
  		pr_err("cannot register reboot notifier (err=%d)
  ", ret);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
488

789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
489
  	ret = misc_register(&pc87413_miscdev);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
490
  	if (ret != 0) {
27c766aaa   Joe Perches   watchdog: Use pr_...
491
492
493
  		pr_err("cannot register miscdev on minor=%d (err=%d)
  ",
  		       WATCHDOG_MINOR, ret);
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
494
  		goto reboot_unreg;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
495
  	}
27c766aaa   Joe Perches   watchdog: Use pr_...
496
497
  	pr_info("initialized. timeout=%d min
  ", timeout);
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
498
499
500
501
502
503
  
  	pc87413_select_wdt_out();
  	pc87413_enable_swc();
  	pc87413_get_swc_base_addr();
  
  	if (!request_region(swc_base_addr, 0x20, MODNAME)) {
27c766aaa   Joe Perches   watchdog: Use pr_...
504
505
  		pr_err("cannot request SWC region at 0x%x
  ", swc_base_addr);
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
506
507
508
  		ret = -EBUSY;
  		goto misc_unreg;
  	}
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
509
  	pc87413_enable();
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
510
511
  
  	release_region(io, 2);
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
512
  	return 0;
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
513
514
515
516
517
518
519
  
  misc_unreg:
  	misc_deregister(&pc87413_miscdev);
  reboot_unreg:
  	unregister_reboot_notifier(&pc87413_notifier);
  	release_region(io, 2);
  	return ret;
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
520
  }
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
521
522
523
524
525
526
527
528
529
530
531
532
533
  /**
   *	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] ...
534
  	if (!nowayout) {
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
535
  		pc87413_disable();
27c766aaa   Joe Perches   watchdog: Use pr_...
536
537
  		pr_info("Watchdog disabled
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
538
539
540
541
  	}
  
  	misc_deregister(&pc87413_miscdev);
  	unregister_reboot_notifier(&pc87413_notifier);
7ccdb9467   Jonathan McDowell   watchdog: pc87413...
542
  	release_region(swc_base_addr, 0x20);
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
543

27c766aaa   Joe Perches   watchdog: Use pr_...
544
545
  	pr_info("watchdog component driver removed
  ");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
546
  }
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
547
548
549
  
  module_init(pc87413_init);
  module_exit(pc87413_exit);
5f5e19093   Jingoo Han   watchdog: fix che...
550
551
  MODULE_AUTHOR("Sven Anders <anders@anduras.de>");
  MODULE_AUTHOR("Marcus Junker <junker@anduras.de>");
789fc0adb   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
552
  MODULE_DESCRIPTION("PC87413 WDT driver");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
553
  MODULE_LICENSE("GPL");
5d1c93ce2   David Howells   Annotate hardware...
554
  module_param_hw(io, int, ioport, 0);
76550d329   Randy Dunlap   watchdog: fix sev...
555
556
  MODULE_PARM_DESC(io, MODNAME " I/O port (default: "
  					__MODULE_STRING(IO_DEFAULT) ").");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
557
558
  
  module_param(timeout, int, 0);
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
559
560
  MODULE_PARM_DESC(timeout,
  		"Watchdog timeout in minutes (default="
76550d329   Randy Dunlap   watchdog: fix sev...
561
  				__MODULE_STRING(DEFAULT_TIMEOUT) ").");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
562

86a1e1896   Wim Van Sebroeck   watchdog: nowayou...
563
  module_param(nowayout, bool, 0);
aee334c23   Alan Cox   [WATCHDOG 31/57] ...
564
565
566
  MODULE_PARM_DESC(nowayout,
  		"Watchdog cannot be stopped once started (default="
  				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
00b3b3e66   Sven Anders & Marcus Junker   [WATCHDOG] NS pc8...
567