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