Blame view

drivers/watchdog/sbc8360.c 9.59 KB
d01732789   Guenter Roeck   watchdog: convert...
1
  // SPDX-License-Identifier: GPL-2.0+
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
2
3
4
5
6
7
  /*
   *	SBC8360 Watchdog driver
   *
   *	(c) Copyright 2005 Webcon, Inc.
   *
   *	Based on ib700wdt.c, which is based on advantechwdt.c which is based
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
8
   *	on acquirewdt.c which is based on wdt.c.
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
9
10
11
   *
   *	(c) Copyright 2001 Charles Howes <chowes@vsol.net>
   *
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
12
13
   *	Based on advantechwdt.c which is based on acquirewdt.c which
   *	is based on wdt.c.
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
14
15
16
17
18
19
   *
   *	(c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
   *
   *	Based on acquirewdt.c which is based on wdt.c.
   *	Original copyright messages:
   *
29fa0586d   Alan Cox   [PATCH] Switch al...
20
21
   *	(c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
   *						All Rights Reserved.
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
22
   *
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
23
24
25
26
   *	Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
   *	warranty for any of this software. This material is provided
   *	"AS-IS" and at no charge.
   *
29fa0586d   Alan Cox   [PATCH] Switch al...
27
   *	(c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
28
   *
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
29
30
31
   *	14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
   *	     Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
   *	     Added timeout module option to override default
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
32
33
   *
   */
27c766aaa   Joe Perches   watchdog: Use pr_...
34
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
35
36
37
38
39
40
41
42
43
44
45
46
  #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>
9f53c8de1   Alan Cox   [WATCHDOG 39/57] ...
47
48
  #include <linux/io.h>
  #include <linux/uaccess.h>
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
49

3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
50
51
  
  static unsigned long sbc8360_is_open;
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
52
  static char expect_close;
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  /*
   *
   * Watchdog Timer Configuration
   *
   * The function of the watchdog timer is to reset the system automatically
   * and is defined at I/O port 0120H and 0121H.  To enable the watchdog timer
   * and allow the system to reset, write appropriate values from the table
   * below to I/O port 0120H and 0121H.  To disable the timer, write a zero
   * value to I/O port 0121H for the system to stop the watchdog function.
   *
   * The following describes how the timer should be programmed (according to
   * the vendor documentation)
   *
   * Enabling Watchdog:
   * MOV AX,000AH (enable, phase I)
   * MOV DX,0120H
   * OUT DX,AX
   * MOV AX,000BH (enable, phase II)
   * MOV DX,0120H
   * OUT DX,AX
   * MOV AX,000nH (set multiplier n, from 1-4)
   * MOV DX,0120H
   * OUT DX,AX
   * MOV AX,000mH (set base timer m, from 0-F)
   * MOV DX,0121H
   * OUT DX,AX
   *
   * Reset timer:
   * MOV AX,000mH (same as set base timer, above)
   * MOV DX,0121H
   * OUT DX,AX
   *
   * Disabling Watchdog:
   * MOV AX,0000H (a zero value)
   * MOV DX,0120H
   * OUT DX,AX
   *
   * Watchdog timeout configuration values:
   *		N
   *	M |	1	2	3	4
   *	--|----------------------------------
   *	0 |	0.5s	5s	50s	100s
   *	1 |	1s	10s	100s	200s
   *	2 |	1.5s	15s	150s	300s
   *	3 |	2s	20s	200s	400s
   *	4 |	2.5s	25s	250s	500s
   *	5 |	3s	30s	300s	600s
   *	6 |	3.5s	35s	350s	700s
   *	7 |	4s	40s	400s	800s
   *	8 |	4.5s	45s	450s	900s
   *	9 |	5s	50s	500s	1000s
   *	A |	5.5s	55s	550s	1100s
   *	B |	6s	60s	600s	1200s
   *	C |	6.5s	65s	650s	1300s
   *	D |	7s	70s	700s	1400s
   *	E |	7.5s	75s	750s	1500s
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
109
   *	F |	8s	80s	800s	1600s
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
   *
   * Another way to say the same things is:
   *  For N=1, Timeout = (M+1) * 0.5s
   *  For N=2, Timeout = (M+1) * 5s
   *  For N=3, Timeout = (M+1) * 50s
   *  For N=4, Timeout = (M+1) * 100s
   *
   */
  
  static int wd_times[64][2] = {
  	{0, 1},			/* 0  = 0.5s */
  	{1, 1},			/* 1  = 1s   */
  	{2, 1},			/* 2  = 1.5s */
  	{3, 1},			/* 3  = 2s   */
  	{4, 1},			/* 4  = 2.5s */
  	{5, 1},			/* 5  = 3s   */
  	{6, 1},			/* 6  = 3.5s */
  	{7, 1},			/* 7  = 4s   */
  	{8, 1},			/* 8  = 4.5s */
  	{9, 1},			/* 9  = 5s   */
  	{0xA, 1},		/* 10 = 5.5s */
  	{0xB, 1},		/* 11 = 6s   */
  	{0xC, 1},		/* 12 = 6.5s */
  	{0xD, 1},		/* 13 = 7s   */
  	{0xE, 1},		/* 14 = 7.5s */
  	{0xF, 1},		/* 15 = 8s   */
  	{0, 2},			/* 16 = 5s  */
  	{1, 2},			/* 17 = 10s */
  	{2, 2},			/* 18 = 15s */
  	{3, 2},			/* 19 = 20s */
  	{4, 2},			/* 20 = 25s */
  	{5, 2},			/* 21 = 30s */
  	{6, 2},			/* 22 = 35s */
  	{7, 2},			/* 23 = 40s */
  	{8, 2},			/* 24 = 45s */
  	{9, 2},			/* 25 = 50s */
  	{0xA, 2},		/* 26 = 55s */
  	{0xB, 2},		/* 27 = 60s */
  	{0xC, 2},		/* 28 = 65s */
  	{0xD, 2},		/* 29 = 70s */
  	{0xE, 2},		/* 30 = 75s */
  	{0xF, 2},		/* 31 = 80s */
  	{0, 3},			/* 32 = 50s  */
  	{1, 3},			/* 33 = 100s */
  	{2, 3},			/* 34 = 150s */
  	{3, 3},			/* 35 = 200s */
  	{4, 3},			/* 36 = 250s */
  	{5, 3},			/* 37 = 300s */
  	{6, 3},			/* 38 = 350s */
  	{7, 3},			/* 39 = 400s */
  	{8, 3},			/* 40 = 450s */
  	{9, 3},			/* 41 = 500s */
  	{0xA, 3},		/* 42 = 550s */
  	{0xB, 3},		/* 43 = 600s */
  	{0xC, 3},		/* 44 = 650s */
  	{0xD, 3},		/* 45 = 700s */
  	{0xE, 3},		/* 46 = 750s */
  	{0xF, 3},		/* 47 = 800s */
  	{0, 4},			/* 48 = 100s */
  	{1, 4},			/* 49 = 200s */
  	{2, 4},			/* 50 = 300s */
  	{3, 4},			/* 51 = 400s */
  	{4, 4},			/* 52 = 500s */
  	{5, 4},			/* 53 = 600s */
  	{6, 4},			/* 54 = 700s */
  	{7, 4},			/* 55 = 800s */
  	{8, 4},			/* 56 = 900s */
  	{9, 4},			/* 57 = 1000s */
  	{0xA, 4},		/* 58 = 1100s */
  	{0xB, 4},		/* 59 = 1200s */
  	{0xC, 4},		/* 60 = 1300s */
  	{0xD, 4},		/* 61 = 1400s */
  	{0xE, 4},		/* 62 = 1500s */
  	{0xF, 4}		/* 63 = 1600s */
  };
  
  #define SBC8360_ENABLE 0x120
  #define SBC8360_BASETIME 0x121
  
  static int timeout = 27;
  static int wd_margin = 0xB;
  static int wd_multiplier = 2;
86a1e1896   Wim Van Sebroeck   watchdog: nowayou...
192
  static bool nowayout = WATCHDOG_NOWAYOUT;
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
193

290995fc3   Ian E. Morgan   [PATCH] SBC8360: ...
194
  module_param(timeout, int, 0);
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
195
  MODULE_PARM_DESC(timeout, "Index into timeout table (0-63) (default=27 (60s))");
86a1e1896   Wim Van Sebroeck   watchdog: nowayou...
196
  module_param(nowayout, bool, 0);
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
197
  MODULE_PARM_DESC(nowayout,
9f53c8de1   Alan Cox   [WATCHDOG 39/57] ...
198
199
  		 "Watchdog cannot be stopped once started (default="
  				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  
  /*
   *	Kernel methods.
   */
  
  /* Activate and pre-configure watchdog */
  static void sbc8360_activate(void)
  {
  	/* Enable the watchdog */
  	outb(0x0A, SBC8360_ENABLE);
  	msleep_interruptible(100);
  	outb(0x0B, SBC8360_ENABLE);
  	msleep_interruptible(100);
  	/* Set timeout multiplier */
  	outb(wd_multiplier, SBC8360_ENABLE);
  	msleep_interruptible(100);
  	/* Nothing happens until first sbc8360_ping() */
  }
  
  /* Kernel pings watchdog */
  static void sbc8360_ping(void)
  {
  	/* Write the base timer register */
  	outb(wd_margin, SBC8360_BASETIME);
  }
12b7a1523   Wim Van Sebroeck   [WATCHDOG] sbc836...
225
226
227
228
229
230
  /* stop watchdog */
  static void sbc8360_stop(void)
  {
  	/* De-activate the watchdog */
  	outb(0, SBC8360_ENABLE);
  }
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
231
  /* Userspace pings kernel driver, or requests clean close */
9f53c8de1   Alan Cox   [WATCHDOG 39/57] ...
232
233
  static ssize_t sbc8360_write(struct file *file, const char __user *buf,
  			     size_t count, loff_t *ppos)
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  {
  	if (count) {
  		if (!nowayout) {
  			size_t i;
  
  			/* In case it was set long ago */
  			expect_close = 0;
  
  			for (i = 0; i != count; i++) {
  				char c;
  				if (get_user(c, buf + i))
  					return -EFAULT;
  				if (c == 'V')
  					expect_close = 42;
  			}
  		}
  		sbc8360_ping();
  	}
  	return count;
  }
  
  static int sbc8360_open(struct inode *inode, struct file *file)
  {
9f53c8de1   Alan Cox   [WATCHDOG 39/57] ...
257
  	if (test_and_set_bit(0, &sbc8360_is_open))
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
258
  		return -EBUSY;
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
259
260
261
262
  	if (nowayout)
  		__module_get(THIS_MODULE);
  
  	/* Activate and ping once to start the countdown */
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
263
264
  	sbc8360_activate();
  	sbc8360_ping();
c5bf68fe0   Kirill Smelkov   *: convert stream...
265
  	return stream_open(inode, file);
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
266
267
268
269
  }
  
  static int sbc8360_close(struct inode *inode, struct file *file)
  {
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
270
  	if (expect_close == 42)
12b7a1523   Wim Van Sebroeck   [WATCHDOG] sbc836...
271
  		sbc8360_stop();
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
272
  	else
27c766aaa   Joe Perches   watchdog: Use pr_...
273
274
  		pr_crit("SBC8360 device closed unexpectedly.  SBC8360 will not stop!
  ");
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
275
276
277
  
  	clear_bit(0, &sbc8360_is_open);
  	expect_close = 0;
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
278
279
280
281
282
283
284
285
286
287
  	return 0;
  }
  
  /*
   *	Notifier for system down
   */
  
  static int sbc8360_notify_sys(struct notifier_block *this, unsigned long code,
  			      void *unused)
  {
12b7a1523   Wim Van Sebroeck   [WATCHDOG] sbc836...
288
289
  	if (code == SYS_DOWN || code == SYS_HALT)
  		sbc8360_stop();	/* Disable the SBC8360 Watchdog */
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
290
291
292
293
294
295
  	return NOTIFY_DONE;
  }
  
  /*
   *	Kernel Interfaces
   */
62322d255   Arjan van de Ven   [PATCH] make more...
296
  static const struct file_operations sbc8360_fops = {
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  	.owner = THIS_MODULE,
  	.llseek = no_llseek,
  	.write = sbc8360_write,
  	.open = sbc8360_open,
  	.release = sbc8360_close,
  };
  
  static struct miscdevice sbc8360_miscdev = {
  	.minor = WATCHDOG_MINOR,
  	.name = "watchdog",
  	.fops = &sbc8360_fops,
  };
  
  /*
   *	The SBC8360 needs to learn about soft shutdowns in order to
   *	turn the timebomb registers off.
   */
  
  static struct notifier_block sbc8360_notifier = {
  	.notifier_call = sbc8360_notify_sys,
  };
  
  static int __init sbc8360_init(void)
  {
  	int res;
  	unsigned long int mseconds = 60000;
fb8f7ba07   Alexey Dobriyan   [WATCHDOG] Semi-t...
323
  	if (timeout < 0 || timeout > 63) {
27c766aaa   Joe Perches   watchdog: Use pr_...
324
325
  		pr_err("Invalid timeout index (must be 0-63)
  ");
fb8f7ba07   Alexey Dobriyan   [WATCHDOG] Semi-t...
326
327
  		res = -EINVAL;
  		goto out;
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
328
329
330
  	}
  
  	if (!request_region(SBC8360_ENABLE, 1, "SBC8360")) {
27c766aaa   Joe Perches   watchdog: Use pr_...
331
332
  		pr_err("ENABLE method I/O %X is not available
  ",
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
333
334
  		       SBC8360_ENABLE);
  		res = -EIO;
fb8f7ba07   Alexey Dobriyan   [WATCHDOG] Semi-t...
335
  		goto out;
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
336
337
  	}
  	if (!request_region(SBC8360_BASETIME, 1, "SBC8360")) {
27c766aaa   Joe Perches   watchdog: Use pr_...
338
339
  		pr_err("BASETIME method I/O %X is not available
  ",
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
340
341
342
343
344
345
346
  		       SBC8360_BASETIME);
  		res = -EIO;
  		goto out_nobasetimereg;
  	}
  
  	res = register_reboot_notifier(&sbc8360_notifier);
  	if (res) {
27c766aaa   Joe Perches   watchdog: Use pr_...
347
348
  		pr_err("Failed to register reboot notifier
  ");
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
349
350
  		goto out_noreboot;
  	}
fb8f7ba07   Alexey Dobriyan   [WATCHDOG] Semi-t...
351
352
  	res = misc_register(&sbc8360_miscdev);
  	if (res) {
27c766aaa   Joe Perches   watchdog: Use pr_...
353
354
  		pr_err("failed to register misc device
  ");
fb8f7ba07   Alexey Dobriyan   [WATCHDOG] Semi-t...
355
  		goto out_nomisc;
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  	}
  
  	wd_margin = wd_times[timeout][0];
  	wd_multiplier = wd_times[timeout][1];
  
  	if (wd_multiplier == 1)
  		mseconds = (wd_margin + 1) * 500;
  	else if (wd_multiplier == 2)
  		mseconds = (wd_margin + 1) * 5000;
  	else if (wd_multiplier == 3)
  		mseconds = (wd_margin + 1) * 50000;
  	else if (wd_multiplier == 4)
  		mseconds = (wd_margin + 1) * 100000;
  
  	/* My kingdom for the ability to print "0.5 seconds" in the kernel! */
27c766aaa   Joe Perches   watchdog: Use pr_...
371
372
  	pr_info("Timeout set at %ld ms
  ", mseconds);
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
373
374
  
  	return 0;
9f53c8de1   Alan Cox   [WATCHDOG 39/57] ...
375
  out_nomisc:
fb8f7ba07   Alexey Dobriyan   [WATCHDOG] Semi-t...
376
  	unregister_reboot_notifier(&sbc8360_notifier);
9f53c8de1   Alan Cox   [WATCHDOG 39/57] ...
377
  out_noreboot:
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
378
  	release_region(SBC8360_BASETIME, 1);
9f53c8de1   Alan Cox   [WATCHDOG 39/57] ...
379
  out_nobasetimereg:
fb8f7ba07   Alexey Dobriyan   [WATCHDOG] Semi-t...
380
  	release_region(SBC8360_ENABLE, 1);
9f53c8de1   Alan Cox   [WATCHDOG 39/57] ...
381
  out:
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  	return res;
  }
  
  static void __exit sbc8360_exit(void)
  {
  	misc_deregister(&sbc8360_miscdev);
  	unregister_reboot_notifier(&sbc8360_notifier);
  	release_region(SBC8360_ENABLE, 1);
  	release_region(SBC8360_BASETIME, 1);
  }
  
  module_init(sbc8360_init);
  module_exit(sbc8360_exit);
  
  MODULE_AUTHOR("Ian E. Morgan <imorgan@webcon.ca>");
  MODULE_DESCRIPTION("SBC8360 watchdog driver");
  MODULE_LICENSE("GPL");
290995fc3   Ian E. Morgan   [PATCH] SBC8360: ...
399
  MODULE_VERSION("1.01");
3809ad384   Ian E. Morgan   [WATCHDOG] New SB...
400
401
  
  /* end of sbc8360.c */