blob: 24401e84729e7a5eec5216ca87cf26659073e7af [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * National Semiconductor PC87307/PC97307 (ala SC1200) WDT driver
3 * (c) Copyright 2002 Zwane Mwaikambo <zwane@commfireservices.com>,
4 * All Rights Reserved.
5 * Based on wdt.c and wdt977.c by Alan Cox and Woody Suwalski respectively.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * The author(s) of this software shall not be held liable for damages
13 * of any nature resulting due to the use of this software. This
14 * software is provided AS-IS with no warranties.
15 *
16 * Changelog:
17 * 20020220 Zwane Mwaikambo Code based on datasheet, no hardware.
18 * 20020221 Zwane Mwaikambo Cleanups as suggested by Jeff Garzik and Alan Cox.
19 * 20020222 Zwane Mwaikambo Added probing.
20 * 20020225 Zwane Mwaikambo Added ISAPNP support.
21 * 20020412 Rob Radez Broke out start/stop functions
22 * <rob@osinvestor.com> Return proper status instead of temperature warning
23 * Add WDIOC_GETBOOTSTATUS and WDIOC_SETOPTIONS ioctls
24 * Fix CONFIG_WATCHDOG_NOWAYOUT
25 * 20020530 Joel Becker Add Matt Domsch's nowayout module option
26 * 20030116 Adam Belay Updated to the latest pnp code
27 *
28 */
29
30#include <linux/config.h>
31#include <linux/module.h>
32#include <linux/moduleparam.h>
33#include <linux/miscdevice.h>
34#include <linux/watchdog.h>
35#include <linux/ioport.h>
36#include <linux/spinlock.h>
37#include <linux/notifier.h>
38#include <linux/reboot.h>
39#include <linux/init.h>
40#include <linux/pnp.h>
41#include <linux/fs.h>
42#include <linux/pci.h>
43
44#include <asm/semaphore.h>
45#include <asm/io.h>
46#include <asm/uaccess.h>
47
48#define SC1200_MODULE_VER "build 20020303"
49#define SC1200_MODULE_NAME "sc1200wdt"
50#define PFX SC1200_MODULE_NAME ": "
51
52#define MAX_TIMEOUT 255 /* 255 minutes */
53#define PMIR (io) /* Power Management Index Register */
54#define PMDR (io+1) /* Power Management Data Register */
55
56/* Data Register indexes */
57#define FER1 0x00 /* Function enable register 1 */
58#define FER2 0x01 /* Function enable register 2 */
59#define PMC1 0x02 /* Power Management Ctrl 1 */
60#define PMC2 0x03 /* Power Management Ctrl 2 */
61#define PMC3 0x04 /* Power Management Ctrl 3 */
62#define WDTO 0x05 /* Watchdog timeout register */
63#define WDCF 0x06 /* Watchdog config register */
64#define WDST 0x07 /* Watchdog status register */
65
66/* WDCF bitfields - which devices assert WDO */
67#define KBC_IRQ 0x01 /* Keyboard Controller */
68#define MSE_IRQ 0x02 /* Mouse */
69#define UART1_IRQ 0x03 /* Serial0 */
70#define UART2_IRQ 0x04 /* Serial1 */
71/* 5 -7 are reserved */
72
73static char banner[] __initdata = KERN_INFO PFX SC1200_MODULE_VER;
74static int timeout = 1;
75static int io = -1;
76static int io_len = 2; /* for non plug and play */
77static struct semaphore open_sem;
78static char expect_close;
79static spinlock_t sc1200wdt_lock; /* io port access serialisation */
80
81#if defined CONFIG_PNP
82static int isapnp = 1;
83static struct pnp_dev *wdt_dev;
84
85module_param(isapnp, int, 0);
86MODULE_PARM_DESC(isapnp, "When set to 0 driver ISA PnP support will be disabled");
87#endif
88
89module_param(io, int, 0);
90MODULE_PARM_DESC(io, "io port");
91module_param(timeout, int, 0);
92MODULE_PARM_DESC(timeout, "range is 0-255 minutes, default is 1");
93
94#ifdef CONFIG_WATCHDOG_NOWAYOUT
95static int nowayout = 1;
96#else
97static int nowayout = 0;
98#endif
99
100module_param(nowayout, int, 0);
101MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
102
103
104
105/* Read from Data Register */
106static inline void sc1200wdt_read_data(unsigned char index, unsigned char *data)
107{
108 spin_lock(&sc1200wdt_lock);
109 outb_p(index, PMIR);
110 *data = inb(PMDR);
111 spin_unlock(&sc1200wdt_lock);
112}
113
114
115/* Write to Data Register */
116static inline void sc1200wdt_write_data(unsigned char index, unsigned char data)
117{
118 spin_lock(&sc1200wdt_lock);
119 outb_p(index, PMIR);
120 outb(data, PMDR);
121 spin_unlock(&sc1200wdt_lock);
122}
123
124
125static void sc1200wdt_start(void)
126{
127 unsigned char reg;
128
129 sc1200wdt_read_data(WDCF, &reg);
130 /* assert WDO when any of the following interrupts are triggered too */
131 reg |= (KBC_IRQ | MSE_IRQ | UART1_IRQ | UART2_IRQ);
132 sc1200wdt_write_data(WDCF, reg);
133 /* set the timeout and get the ball rolling */
134 sc1200wdt_write_data(WDTO, timeout);
135}
136
137
138static void sc1200wdt_stop(void)
139{
140 sc1200wdt_write_data(WDTO, 0);
141}
142
143
144/* This returns the status of the WDO signal, inactive high. */
145static inline int sc1200wdt_status(void)
146{
147 unsigned char ret;
148
149 sc1200wdt_read_data(WDST, &ret);
150 /* If the bit is inactive, the watchdog is enabled, so return
151 * KEEPALIVEPING which is a bit of a kludge because there's nothing
152 * else for enabled/disabled status
153 */
154 return (ret & 0x01) ? 0 : WDIOF_KEEPALIVEPING; /* bits 1 - 7 are undefined */
155}
156
157
158static int sc1200wdt_open(struct inode *inode, struct file *file)
159{
160 nonseekable_open(inode, file);
161
162 /* allow one at a time */
163 if (down_trylock(&open_sem))
164 return -EBUSY;
165
166 if (timeout > MAX_TIMEOUT)
167 timeout = MAX_TIMEOUT;
168
169 sc1200wdt_start();
170 printk(KERN_INFO PFX "Watchdog enabled, timeout = %d min(s)", timeout);
171
172 return 0;
173}
174
175
176static int sc1200wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
177{
178 int new_timeout;
179 void __user *argp = (void __user *)arg;
180 int __user *p = argp;
181 static struct watchdog_info ident = {
182 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
183 .firmware_version = 0,
184 .identity = "PC87307/PC97307",
185 };
186
187 switch (cmd) {
188 default:
189 return -ENOIOCTLCMD; /* Keep Pavel Machek amused ;) */
190
191 case WDIOC_GETSUPPORT:
192 if (copy_to_user(argp, &ident, sizeof ident))
193 return -EFAULT;
194 return 0;
195
196 case WDIOC_GETSTATUS:
197 return put_user(sc1200wdt_status(), p);
198
199 case WDIOC_GETBOOTSTATUS:
200 return put_user(0, p);
201
202 case WDIOC_KEEPALIVE:
203 sc1200wdt_write_data(WDTO, timeout);
204 return 0;
205
206 case WDIOC_SETTIMEOUT:
207 if (get_user(new_timeout, p))
208 return -EFAULT;
209
210 /* the API states this is given in secs */
211 new_timeout /= 60;
212 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
213 return -EINVAL;
214
215 timeout = new_timeout;
216 sc1200wdt_write_data(WDTO, timeout);
217 /* fall through and return the new timeout */
218
219 case WDIOC_GETTIMEOUT:
220 return put_user(timeout * 60, p);
221
222 case WDIOC_SETOPTIONS:
223 {
224 int options, retval = -EINVAL;
225
226 if (get_user(options, p))
227 return -EFAULT;
228
229 if (options & WDIOS_DISABLECARD) {
230 sc1200wdt_stop();
231 retval = 0;
232 }
233
234 if (options & WDIOS_ENABLECARD) {
235 sc1200wdt_start();
236 retval = 0;
237 }
238
239 return retval;
240 }
241 }
242}
243
244
245static int sc1200wdt_release(struct inode *inode, struct file *file)
246{
247 if (expect_close == 42) {
248 sc1200wdt_stop();
249 printk(KERN_INFO PFX "Watchdog disabled\n");
250 } else {
251 sc1200wdt_write_data(WDTO, timeout);
252 printk(KERN_CRIT PFX "Unexpected close!, timeout = %d min(s)\n", timeout);
253 }
254 up(&open_sem);
255 expect_close = 0;
256
257 return 0;
258}
259
260
261static ssize_t sc1200wdt_write(struct file *file, const char __user *data, size_t len, loff_t *ppos)
262{
263 if (len) {
264 if (!nowayout) {
265 size_t i;
266
267 expect_close = 0;
268
269 for (i = 0; i != len; i++) {
270 char c;
271
272 if (get_user(c, data+i))
273 return -EFAULT;
274 if (c == 'V')
275 expect_close = 42;
276 }
277 }
278
279 sc1200wdt_write_data(WDTO, timeout);
280 return len;
281 }
282
283 return 0;
284}
285
286
287static int sc1200wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
288{
289 if (code == SYS_DOWN || code == SYS_HALT)
290 sc1200wdt_stop();
291
292 return NOTIFY_DONE;
293}
294
295
296static struct notifier_block sc1200wdt_notifier =
297{
298 .notifier_call = sc1200wdt_notify_sys,
299};
300
301static struct file_operations sc1200wdt_fops =
302{
303 .owner = THIS_MODULE,
304 .llseek = no_llseek,
305 .write = sc1200wdt_write,
306 .ioctl = sc1200wdt_ioctl,
307 .open = sc1200wdt_open,
308 .release = sc1200wdt_release,
309};
310
311static struct miscdevice sc1200wdt_miscdev =
312{
313 .minor = WATCHDOG_MINOR,
314 .name = "watchdog",
315 .fops = &sc1200wdt_fops,
316};
317
318
319static int __init sc1200wdt_probe(void)
320{
321 /* The probe works by reading the PMC3 register's default value of 0x0e
322 * there is one caveat, if the device disables the parallel port or any
323 * of the UARTs we won't be able to detect it.
324 * Nb. This could be done with accuracy by reading the SID registers, but
325 * we don't have access to those io regions.
326 */
327
328 unsigned char reg;
329
330 sc1200wdt_read_data(PMC3, &reg);
331 reg &= 0x0f; /* we don't want the UART busy bits */
332 return (reg == 0x0e) ? 0 : -ENODEV;
333}
334
335
336#if defined CONFIG_PNP
337
338static struct pnp_device_id scl200wdt_pnp_devices[] = {
339 /* National Semiconductor PC87307/PC97307 watchdog component */
340 {.id = "NSC0800", .driver_data = 0},
341 {.id = ""},
342};
343
344static int scl200wdt_pnp_probe(struct pnp_dev * dev, const struct pnp_device_id *dev_id)
345{
346 /* this driver only supports one card at a time */
347 if (wdt_dev || !isapnp)
348 return -EBUSY;
349
350 wdt_dev = dev;
351 io = pnp_port_start(wdt_dev, 0);
352 io_len = pnp_port_len(wdt_dev, 0);
353
354 if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
355 printk(KERN_ERR PFX "Unable to register IO port %#x\n", io);
356 return -EBUSY;
357 }
358
359 printk(KERN_INFO "scl200wdt: PnP device found at io port %#x/%d\n", io, io_len);
360 return 0;
361}
362
363static void scl200wdt_pnp_remove(struct pnp_dev * dev)
364{
365 if (wdt_dev){
366 release_region(io, io_len);
367 wdt_dev = NULL;
368 }
369}
370
371static struct pnp_driver scl200wdt_pnp_driver = {
372 .name = "scl200wdt",
373 .id_table = scl200wdt_pnp_devices,
374 .probe = scl200wdt_pnp_probe,
375 .remove = scl200wdt_pnp_remove,
376};
377
378#endif /* CONFIG_PNP */
379
380
381static int __init sc1200wdt_init(void)
382{
383 int ret;
384
385 printk(banner);
386
387 spin_lock_init(&sc1200wdt_lock);
388 sema_init(&open_sem, 1);
389
390#if defined CONFIG_PNP
391 if (isapnp) {
392 ret = pnp_register_driver(&scl200wdt_pnp_driver);
393 if (ret)
394 goto out_clean;
395 }
396#endif
397
398 if (io == -1) {
399 printk(KERN_ERR PFX "io parameter must be specified\n");
400 ret = -EINVAL;
401 goto out_clean;
402 }
403
404#if defined CONFIG_PNP
405 /* now that the user has specified an IO port and we haven't detected
406 * any devices, disable pnp support */
407 isapnp = 0;
408 pnp_unregister_driver(&scl200wdt_pnp_driver);
409#endif
410
411 if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
412 printk(KERN_ERR PFX "Unable to register IO port %#x\n", io);
413 ret = -EBUSY;
414 goto out_clean;
415 }
416
417 ret = sc1200wdt_probe();
418 if (ret)
419 goto out_io;
420
421 ret = register_reboot_notifier(&sc1200wdt_notifier);
422 if (ret) {
423 printk(KERN_ERR PFX "Unable to register reboot notifier err = %d\n", ret);
424 goto out_io;
425 }
426
427 ret = misc_register(&sc1200wdt_miscdev);
428 if (ret) {
429 printk(KERN_ERR PFX "Unable to register miscdev on minor %d\n", WATCHDOG_MINOR);
430 goto out_rbt;
431 }
432
433 /* ret = 0 */
434
435out_clean:
436 return ret;
437
438out_rbt:
439 unregister_reboot_notifier(&sc1200wdt_notifier);
440
441out_io:
442 release_region(io, io_len);
443
444 goto out_clean;
445}
446
447
448static void __exit sc1200wdt_exit(void)
449{
450 misc_deregister(&sc1200wdt_miscdev);
451 unregister_reboot_notifier(&sc1200wdt_notifier);
452
453#if defined CONFIG_PNP
454 if(isapnp)
455 pnp_unregister_driver(&scl200wdt_pnp_driver);
456 else
457#endif
458 release_region(io, io_len);
459}
460
461module_init(sc1200wdt_init);
462module_exit(sc1200wdt_exit);
463
464MODULE_AUTHOR("Zwane Mwaikambo <zwane@commfireservices.com>");
465MODULE_DESCRIPTION("Driver for National Semiconductor PC87307/PC97307 watchdog component");
466MODULE_LICENSE("GPL");
467MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);