2 * National Semiconductor PC87307/PC97307 (ala SC1200) WDT driver
3 * (c) Copyright 2002 Zwane Mwaikambo <zwane@commfireservices.com>,
5 * Based on wdt.c and wdt977.c by Alan Cox and Woody Suwalski respectively.
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.
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.
17 * 20020220 Zwane Mwaikambo Code based on datasheet, no hardware.
18 * 20020221 Zwane Mwaikambo Cleanups as suggested by Jeff Garzik
20 * 20020222 Zwane Mwaikambo Added probing.
21 * 20020225 Zwane Mwaikambo Added ISAPNP support.
22 * 20020412 Rob Radez Broke out start/stop functions
23 * <rob@osinvestor.com> Return proper status instead of
25 * Add WDIOC_GETBOOTSTATUS and
26 * WDIOC_SETOPTIONS ioctls
27 * Fix CONFIG_WATCHDOG_NOWAYOUT
28 * 20020530 Joel Becker Add Matt Domsch's nowayout module
30 * 20030116 Adam Belay Updated to the latest pnp code
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/miscdevice.h>
39 #include <linux/watchdog.h>
40 #include <linux/ioport.h>
41 #include <linux/spinlock.h>
42 #include <linux/notifier.h>
43 #include <linux/reboot.h>
44 #include <linux/init.h>
45 #include <linux/pnp.h>
47 #include <linux/semaphore.h>
49 #include <linux/uaccess.h>
51 #define SC1200_MODULE_VER "build 20020303"
52 #define SC1200_MODULE_NAME "sc1200wdt"
54 #define MAX_TIMEOUT 255 /* 255 minutes */
55 #define PMIR (io) /* Power Management Index Register */
56 #define PMDR (io+1) /* Power Management Data Register */
58 /* Data Register indexes */
59 #define FER1 0x00 /* Function enable register 1 */
60 #define FER2 0x01 /* Function enable register 2 */
61 #define PMC1 0x02 /* Power Management Ctrl 1 */
62 #define PMC2 0x03 /* Power Management Ctrl 2 */
63 #define PMC3 0x04 /* Power Management Ctrl 3 */
64 #define WDTO 0x05 /* Watchdog timeout register */
65 #define WDCF 0x06 /* Watchdog config register */
66 #define WDST 0x07 /* Watchdog status register */
68 /* WDCF bitfields - which devices assert WDO */
69 #define KBC_IRQ 0x01 /* Keyboard Controller */
70 #define MSE_IRQ 0x02 /* Mouse */
71 #define UART1_IRQ 0x03 /* Serial0 */
72 #define UART2_IRQ 0x04 /* Serial1 */
73 /* 5 -7 are reserved */
75 static int timeout = 1;
77 static int io_len = 2; /* for non plug and play */
78 static unsigned long open_flag;
79 static char expect_close;
80 static DEFINE_SPINLOCK(sc1200wdt_lock); /* io port access serialisation */
82 #if defined CONFIG_PNP
83 static int isapnp = 1;
84 static struct pnp_dev *wdt_dev;
86 module_param(isapnp, int, 0);
87 MODULE_PARM_DESC(isapnp,
88 "When set to 0 driver ISA PnP support will be disabled");
91 module_param(io, int, 0);
92 MODULE_PARM_DESC(io, "io port");
93 module_param(timeout, int, 0);
94 MODULE_PARM_DESC(timeout, "range is 0-255 minutes, default is 1");
96 static bool nowayout = WATCHDOG_NOWAYOUT;
97 module_param(nowayout, bool, 0);
98 MODULE_PARM_DESC(nowayout,
99 "Watchdog cannot be stopped once started (default="
100 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
104 /* Read from Data Register */
105 static inline void __sc1200wdt_read_data(unsigned char index,
112 static void sc1200wdt_read_data(unsigned char index, unsigned char *data)
114 spin_lock(&sc1200wdt_lock);
115 __sc1200wdt_read_data(index, data);
116 spin_unlock(&sc1200wdt_lock);
119 /* Write to Data Register */
120 static inline void __sc1200wdt_write_data(unsigned char index,
127 static inline void sc1200wdt_write_data(unsigned char index,
130 spin_lock(&sc1200wdt_lock);
131 __sc1200wdt_write_data(index, data);
132 spin_unlock(&sc1200wdt_lock);
136 static void sc1200wdt_start(void)
139 spin_lock(&sc1200wdt_lock);
141 __sc1200wdt_read_data(WDCF, ®);
142 /* assert WDO when any of the following interrupts are triggered too */
143 reg |= (KBC_IRQ | MSE_IRQ | UART1_IRQ | UART2_IRQ);
144 __sc1200wdt_write_data(WDCF, reg);
145 /* set the timeout and get the ball rolling */
146 __sc1200wdt_write_data(WDTO, timeout);
148 spin_unlock(&sc1200wdt_lock);
151 static void sc1200wdt_stop(void)
153 sc1200wdt_write_data(WDTO, 0);
156 /* This returns the status of the WDO signal, inactive high. */
157 static inline int sc1200wdt_status(void)
161 sc1200wdt_read_data(WDST, &ret);
162 /* If the bit is inactive, the watchdog is enabled, so return
163 * KEEPALIVEPING which is a bit of a kludge because there's nothing
164 * else for enabled/disabled status
166 return (ret & 0x01) ? 0 : WDIOF_KEEPALIVEPING;
169 static int sc1200wdt_open(struct inode *inode, struct file *file)
171 /* allow one at a time */
172 if (test_and_set_bit(0, &open_flag))
175 if (timeout > MAX_TIMEOUT)
176 timeout = MAX_TIMEOUT;
179 pr_info("Watchdog enabled, timeout = %d min(s)", timeout);
181 return nonseekable_open(inode, file);
185 static long sc1200wdt_ioctl(struct file *file, unsigned int cmd,
189 void __user *argp = (void __user *)arg;
190 int __user *p = argp;
191 static const struct watchdog_info ident = {
192 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
194 .firmware_version = 0,
195 .identity = "PC87307/PC97307",
199 case WDIOC_GETSUPPORT:
200 if (copy_to_user(argp, &ident, sizeof(ident)))
204 case WDIOC_GETSTATUS:
205 return put_user(sc1200wdt_status(), p);
207 case WDIOC_GETBOOTSTATUS:
208 return put_user(0, p);
210 case WDIOC_SETOPTIONS:
212 int options, retval = -EINVAL;
214 if (get_user(options, p))
217 if (options & WDIOS_DISABLECARD) {
222 if (options & WDIOS_ENABLECARD) {
229 case WDIOC_KEEPALIVE:
230 sc1200wdt_write_data(WDTO, timeout);
233 case WDIOC_SETTIMEOUT:
234 if (get_user(new_timeout, p))
236 /* the API states this is given in secs */
238 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
240 timeout = new_timeout;
241 sc1200wdt_write_data(WDTO, timeout);
242 /* fall through and return the new timeout */
244 case WDIOC_GETTIMEOUT:
245 return put_user(timeout * 60, p);
253 static int sc1200wdt_release(struct inode *inode, struct file *file)
255 if (expect_close == 42) {
257 pr_info("Watchdog disabled\n");
259 sc1200wdt_write_data(WDTO, timeout);
260 pr_crit("Unexpected close!, timeout = %d min(s)\n", timeout);
262 clear_bit(0, &open_flag);
269 static ssize_t sc1200wdt_write(struct file *file, const char __user *data,
270 size_t len, loff_t *ppos)
278 for (i = 0; i != len; i++) {
281 if (get_user(c, data + i))
288 sc1200wdt_write_data(WDTO, timeout);
296 static int sc1200wdt_notify_sys(struct notifier_block *this,
297 unsigned long code, void *unused)
299 if (code == SYS_DOWN || code == SYS_HALT)
306 static struct notifier_block sc1200wdt_notifier = {
307 .notifier_call = sc1200wdt_notify_sys,
310 static const struct file_operations sc1200wdt_fops = {
311 .owner = THIS_MODULE,
313 .write = sc1200wdt_write,
314 .unlocked_ioctl = sc1200wdt_ioctl,
315 .open = sc1200wdt_open,
316 .release = sc1200wdt_release,
319 static struct miscdevice sc1200wdt_miscdev = {
320 .minor = WATCHDOG_MINOR,
322 .fops = &sc1200wdt_fops,
326 static int __init sc1200wdt_probe(void)
328 /* The probe works by reading the PMC3 register's default value of 0x0e
329 * there is one caveat, if the device disables the parallel port or any
330 * of the UARTs we won't be able to detect it.
331 * NB. This could be done with accuracy by reading the SID registers,
332 * but we don't have access to those io regions.
337 sc1200wdt_read_data(PMC3, ®);
338 reg &= 0x0f; /* we don't want the UART busy bits */
339 return (reg == 0x0e) ? 0 : -ENODEV;
343 #if defined CONFIG_PNP
345 static struct pnp_device_id scl200wdt_pnp_devices[] = {
346 /* National Semiconductor PC87307/PC97307 watchdog component */
347 {.id = "NSC0800", .driver_data = 0},
351 static int scl200wdt_pnp_probe(struct pnp_dev *dev,
352 const struct pnp_device_id *dev_id)
354 /* this driver only supports one card at a time */
355 if (wdt_dev || !isapnp)
359 io = pnp_port_start(wdt_dev, 0);
360 io_len = pnp_port_len(wdt_dev, 0);
362 if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
363 pr_err("Unable to register IO port %#x\n", io);
367 pr_info("PnP device found at io port %#x/%d\n", io, io_len);
371 static void scl200wdt_pnp_remove(struct pnp_dev *dev)
374 release_region(io, io_len);
379 static struct pnp_driver scl200wdt_pnp_driver = {
381 .id_table = scl200wdt_pnp_devices,
382 .probe = scl200wdt_pnp_probe,
383 .remove = scl200wdt_pnp_remove,
386 #endif /* CONFIG_PNP */
389 static int __init sc1200wdt_init(void)
393 pr_info("%s\n", SC1200_MODULE_VER);
395 #if defined CONFIG_PNP
397 ret = pnp_register_driver(&scl200wdt_pnp_driver);
404 pr_err("io parameter must be specified\n");
409 #if defined CONFIG_PNP
410 /* now that the user has specified an IO port and we haven't detected
411 * any devices, disable pnp support */
413 pnp_unregister_driver(&scl200wdt_pnp_driver);
417 if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
418 pr_err("Unable to register IO port %#x\n", io);
423 ret = sc1200wdt_probe();
427 ret = register_reboot_notifier(&sc1200wdt_notifier);
429 pr_err("Unable to register reboot notifier err = %d\n", ret);
433 ret = misc_register(&sc1200wdt_miscdev);
435 pr_err("Unable to register miscdev on minor %d\n",
446 unregister_reboot_notifier(&sc1200wdt_notifier);
449 release_region(io, io_len);
452 #if defined CONFIG_PNP
454 pnp_unregister_driver(&scl200wdt_pnp_driver);
460 static void __exit sc1200wdt_exit(void)
462 misc_deregister(&sc1200wdt_miscdev);
463 unregister_reboot_notifier(&sc1200wdt_notifier);
465 #if defined CONFIG_PNP
467 pnp_unregister_driver(&scl200wdt_pnp_driver);
470 release_region(io, io_len);
473 module_init(sc1200wdt_init);
474 module_exit(sc1200wdt_exit);
476 MODULE_AUTHOR("Zwane Mwaikambo <zwane@commfireservices.com>");
478 "Driver for National Semiconductor PC87307/PC97307 watchdog component");
479 MODULE_LICENSE("GPL");