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