[WATCHDOG] ib700wdt.c - convert to platform_device
[deliverable/linux.git] / drivers / char / watchdog / ib700wdt.c
CommitLineData
1da177e4
LT
1/*
2 * IB700 Single Board Computer WDT driver
3 *
4 * (c) Copyright 2001 Charles Howes <chowes@vsol.net>
5 *
c9d7710e
WVS
6 * Based on advantechwdt.c which is based on acquirewdt.c which
7 * is based on wdt.c.
1da177e4
LT
8 *
9 * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
10 *
11 * Based on acquirewdt.c which is based on wdt.c.
12 * Original copyright messages:
13 *
14 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
15 * http://www.redhat.com
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
21 *
22 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
23 * warranty for any of this software. This material is provided
24 * "AS-IS" and at no charge.
25 *
26 * (c) Copyright 1995 Alan Cox <alan@redhat.com>
27 *
c9d7710e
WVS
28 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
29 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
30 * Added timeout module option to override default
1da177e4
LT
31 *
32 */
33
1da177e4
LT
34#include <linux/module.h>
35#include <linux/types.h>
36#include <linux/miscdevice.h>
37#include <linux/watchdog.h>
38#include <linux/ioport.h>
39#include <linux/notifier.h>
40#include <linux/fs.h>
41#include <linux/reboot.h>
42#include <linux/init.h>
43#include <linux/spinlock.h>
44#include <linux/moduleparam.h>
745ac1ea 45#include <linux/platform_device.h>
1da177e4
LT
46
47#include <asm/io.h>
48#include <asm/uaccess.h>
49#include <asm/system.h>
50
745ac1ea 51static struct platform_device *ibwdt_platform_device;
1da177e4
LT
52static unsigned long ibwdt_is_open;
53static spinlock_t ibwdt_lock;
54static char expect_close;
55
745ac1ea
WVS
56/* Module information */
57#define DRV_NAME "ib700wdt"
58#define PFX DRV_NAME ": "
1da177e4
LT
59
60/*
61 *
62 * Watchdog Timer Configuration
63 *
64 * The function of the watchdog timer is to reset the system
65 * automatically and is defined at I/O port 0443H. To enable the
66 * watchdog timer and allow the system to reset, write I/O port 0443H.
67 * To disable the timer, write I/O port 0441H for the system to stop the
68 * watchdog function. The timer has a tolerance of 20% for its
69 * intervals.
70 *
71 * The following describes how the timer should be programmed.
72 *
73 * Enabling Watchdog:
74 * MOV AX,000FH (Choose the values from 0 to F)
75 * MOV DX,0443H
76 * OUT DX,AX
77 *
78 * Disabling Watchdog:
79 * MOV AX,000FH (Any value is fine.)
80 * MOV DX,0441H
81 * OUT DX,AX
82 *
83 * Watchdog timer control table:
84 * Level Value Time/sec | Level Value Time/sec
85 * 1 F 0 | 9 7 16
86 * 2 E 2 | 10 6 18
87 * 3 D 4 | 11 5 20
88 * 4 C 6 | 12 4 22
89 * 5 B 8 | 13 3 24
90 * 6 A 10 | 14 2 26
91 * 7 9 12 | 15 1 28
92 * 8 8 14 | 16 0 30
93 *
94 */
95
96static int wd_times[] = {
97 30, /* 0x0 */
98 28, /* 0x1 */
99 26, /* 0x2 */
100 24, /* 0x3 */
101 22, /* 0x4 */
102 20, /* 0x5 */
103 18, /* 0x6 */
104 16, /* 0x7 */
105 14, /* 0x8 */
106 12, /* 0x9 */
107 10, /* 0xA */
108 8, /* 0xB */
109 6, /* 0xC */
110 4, /* 0xD */
111 2, /* 0xE */
112 0, /* 0xF */
113};
114
115#define WDT_STOP 0x441
116#define WDT_START 0x443
117
118/* Default timeout */
119#define WD_TIMO 0 /* 30 seconds +/- 20%, from table */
120
121static int wd_margin = WD_TIMO;
122
4bfdf378 123static int nowayout = WATCHDOG_NOWAYOUT;
1da177e4 124module_param(nowayout, int, 0);
bffda5c8 125MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
1da177e4
LT
126
127
128/*
35d55c94 129 * Watchdog Operations
1da177e4
LT
130 */
131
132static void
133ibwdt_ping(void)
134{
e42162a4
WVS
135 spin_lock(&ibwdt_lock);
136
1da177e4
LT
137 /* Write a watchdog value */
138 outb_p(wd_margin, WDT_START);
e42162a4
WVS
139
140 spin_unlock(&ibwdt_lock);
1da177e4
LT
141}
142
35d55c94
WVS
143static void
144ibwdt_disable(void)
145{
e42162a4 146 spin_lock(&ibwdt_lock);
35d55c94 147 outb_p(0, WDT_STOP);
e42162a4 148 spin_unlock(&ibwdt_lock);
35d55c94
WVS
149}
150
151static int
152ibwdt_set_heartbeat(int t)
153{
154 int i;
155
156 if ((t < 0) || (t > 30))
157 return -EINVAL;
158
159 for (i = 0x0F; i > -1; i--)
160 if (wd_times[i] > t)
161 break;
162 wd_margin = i;
163 return 0;
164}
165
166/*
167 * /dev/watchdog handling
168 */
169
1da177e4
LT
170static ssize_t
171ibwdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
172{
173 if (count) {
174 if (!nowayout) {
175 size_t i;
176
177 /* In case it was set long ago */
178 expect_close = 0;
179
180 for (i = 0; i != count; i++) {
181 char c;
182 if (get_user(c, buf + i))
183 return -EFAULT;
184 if (c == 'V')
185 expect_close = 42;
186 }
187 }
188 ibwdt_ping();
189 }
190 return count;
191}
192
193static int
194ibwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
195 unsigned long arg)
196{
35d55c94 197 int new_margin;
1da177e4
LT
198 void __user *argp = (void __user *)arg;
199 int __user *p = argp;
200
201 static struct watchdog_info ident = {
202 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
203 .firmware_version = 1,
204 .identity = "IB700 WDT",
205 };
206
207 switch (cmd) {
208 case WDIOC_GETSUPPORT:
209 if (copy_to_user(argp, &ident, sizeof(ident)))
210 return -EFAULT;
211 break;
212
213 case WDIOC_GETSTATUS:
c9d7710e 214 case WDIOC_GETBOOTSTATUS:
1da177e4
LT
215 return put_user(0, p);
216
217 case WDIOC_KEEPALIVE:
218 ibwdt_ping();
219 break;
220
221 case WDIOC_SETTIMEOUT:
222 if (get_user(new_margin, p))
223 return -EFAULT;
35d55c94 224 if (ibwdt_set_heartbeat(new_margin))
1da177e4 225 return -EINVAL;
1da177e4
LT
226 ibwdt_ping();
227 /* Fall */
228
229 case WDIOC_GETTIMEOUT:
230 return put_user(wd_times[wd_margin], p);
e42162a4
WVS
231
232 case WDIOC_SETOPTIONS:
233 {
234 int options, retval = -EINVAL;
235
236 if (get_user(options, p))
237 return -EFAULT;
238
239 if (options & WDIOS_DISABLECARD) {
240 ibwdt_disable();
241 retval = 0;
242 }
243
244 if (options & WDIOS_ENABLECARD) {
245 ibwdt_ping();
246 retval = 0;
247 }
248
249 return retval;
250 }
1da177e4
LT
251
252 default:
795b89d2 253 return -ENOTTY;
1da177e4
LT
254 }
255 return 0;
256}
257
258static int
259ibwdt_open(struct inode *inode, struct file *file)
260{
1da177e4 261 if (test_and_set_bit(0, &ibwdt_is_open)) {
1da177e4
LT
262 return -EBUSY;
263 }
264 if (nowayout)
265 __module_get(THIS_MODULE);
266
267 /* Activate */
268 ibwdt_ping();
1da177e4
LT
269 return nonseekable_open(inode, file);
270}
271
272static int
273ibwdt_close(struct inode *inode, struct file *file)
274{
c9d7710e 275 if (expect_close == 42) {
35d55c94 276 ibwdt_disable();
c9d7710e 277 } else {
1da177e4 278 printk(KERN_CRIT PFX "WDT device closed unexpectedly. WDT will not stop!\n");
c9d7710e
WVS
279 ibwdt_ping();
280 }
1da177e4
LT
281 clear_bit(0, &ibwdt_is_open);
282 expect_close = 0;
1da177e4
LT
283 return 0;
284}
285
286/*
287 * Notifier for system down
288 */
289
290static int
291ibwdt_notify_sys(struct notifier_block *this, unsigned long code,
292 void *unused)
293{
294 if (code == SYS_DOWN || code == SYS_HALT) {
295 /* Turn the WDT off */
35d55c94 296 ibwdt_disable();
1da177e4
LT
297 }
298 return NOTIFY_DONE;
299}
300
301/*
302 * Kernel Interfaces
303 */
304
62322d25 305static const struct file_operations ibwdt_fops = {
1da177e4
LT
306 .owner = THIS_MODULE,
307 .llseek = no_llseek,
308 .write = ibwdt_write,
309 .ioctl = ibwdt_ioctl,
310 .open = ibwdt_open,
311 .release = ibwdt_close,
312};
313
314static struct miscdevice ibwdt_miscdev = {
315 .minor = WATCHDOG_MINOR,
316 .name = "watchdog",
317 .fops = &ibwdt_fops,
318};
319
320/*
321 * The WDT needs to learn about soft shutdowns in order to
322 * turn the timebomb registers off.
323 */
324
325static struct notifier_block ibwdt_notifier = {
326 .notifier_call = ibwdt_notify_sys,
327};
328
f6e48039
WVS
329/*
330 * Init & exit routines
331 */
332
745ac1ea 333static int __devinit ibwdt_probe(struct platform_device *dev)
1da177e4
LT
334{
335 int res;
336
1da177e4 337 spin_lock_init(&ibwdt_lock);
1da177e4
LT
338
339#if WDT_START != WDT_STOP
340 if (!request_region(WDT_STOP, 1, "IB700 WDT")) {
341 printk (KERN_ERR PFX "STOP method I/O %X is not available.\n", WDT_STOP);
342 res = -EIO;
343 goto out_nostopreg;
344 }
345#endif
346
347 if (!request_region(WDT_START, 1, "IB700 WDT")) {
348 printk (KERN_ERR PFX "START method I/O %X is not available.\n", WDT_START);
349 res = -EIO;
350 goto out_nostartreg;
351 }
f6e48039 352
1da177e4
LT
353 res = register_reboot_notifier(&ibwdt_notifier);
354 if (res) {
355 printk (KERN_ERR PFX "Failed to register reboot notifier.\n");
356 goto out_noreboot;
357 }
f6e48039
WVS
358
359 res = misc_register(&ibwdt_miscdev);
360 if (res) {
361 printk (KERN_ERR PFX "failed to register misc device\n");
362 goto out_nomisc;
363 }
1da177e4
LT
364 return 0;
365
f6e48039
WVS
366out_nomisc:
367 unregister_reboot_notifier(&ibwdt_notifier);
1da177e4
LT
368out_noreboot:
369 release_region(WDT_START, 1);
370out_nostartreg:
371#if WDT_START != WDT_STOP
372 release_region(WDT_STOP, 1);
373#endif
374out_nostopreg:
1da177e4
LT
375 return res;
376}
377
745ac1ea 378static int __devexit ibwdt_remove(struct platform_device *dev)
1da177e4
LT
379{
380 misc_deregister(&ibwdt_miscdev);
381 unregister_reboot_notifier(&ibwdt_notifier);
f6e48039 382 release_region(WDT_START,1);
1da177e4
LT
383#if WDT_START != WDT_STOP
384 release_region(WDT_STOP,1);
385#endif
745ac1ea
WVS
386 return 0;
387}
388
389static struct platform_driver ibwdt_driver = {
390 .probe = ibwdt_probe,
391 .remove = __devexit_p(ibwdt_remove),
392 .driver = {
393 .owner = THIS_MODULE,
394 .name = DRV_NAME,
395 },
396};
397
398static int __init ibwdt_init(void)
399{
400 int err;
401
402 printk(KERN_INFO PFX "WDT driver for IB700 single board computer initialising.\n");
403
404 err = platform_driver_register(&ibwdt_driver);
405 if (err)
406 return err;
407
408 ibwdt_platform_device = platform_device_register_simple(DRV_NAME, -1, NULL, 0);
409 if (IS_ERR(ibwdt_platform_device)) {
410 err = PTR_ERR(ibwdt_platform_device);
411 goto unreg_platform_driver;
412 }
413
414 return 0;
415
416unreg_platform_driver:
417 platform_driver_unregister(&ibwdt_driver);
418 return err;
419}
420
421static void __exit ibwdt_exit(void)
422{
423 platform_device_unregister(ibwdt_platform_device);
424 platform_driver_unregister(&ibwdt_driver);
425 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
1da177e4
LT
426}
427
428module_init(ibwdt_init);
429module_exit(ibwdt_exit);
430
431MODULE_AUTHOR("Charles Howes <chowes@vsol.net>");
432MODULE_DESCRIPTION("IB700 SBC watchdog driver");
433MODULE_LICENSE("GPL");
434MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
435
436/* end of ib700wdt.c */
This page took 0.190882 seconds and 5 git commands to generate.