blob: 131193a7acdfd0bb07660095e0ff57c500eed6cb [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.
Alan Cox103a1d52008-08-04 17:56:28 +010018 * 20020221 Zwane Mwaikambo Cleanups as suggested by Jeff Garzik
19 * and Alan Cox.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * 20020222 Zwane Mwaikambo Added probing.
21 * 20020225 Zwane Mwaikambo Added ISAPNP support.
22 * 20020412 Rob Radez Broke out start/stop functions
Alan Cox103a1d52008-08-04 17:56:28 +010023 * <rob@osinvestor.com> Return proper status instead of
24 * temperature warning
25 * Add WDIOC_GETBOOTSTATUS and
26 * WDIOC_SETOPTIONS ioctls
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * Fix CONFIG_WATCHDOG_NOWAYOUT
Alan Cox103a1d52008-08-04 17:56:28 +010028 * 20020530 Joel Becker Add Matt Domsch's nowayout module
29 * option
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * 20030116 Adam Belay Updated to the latest pnp code
31 *
32 */
33
Joe Perches27c766a2012-02-15 15:06:19 -080034#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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>
46#include <linux/fs.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040047#include <linux/semaphore.h>
Alan Cox103a1d52008-08-04 17:56:28 +010048#include <linux/io.h>
49#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define SC1200_MODULE_VER "build 20020303"
52#define SC1200_MODULE_NAME "sc1200wdt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define MAX_TIMEOUT 255 /* 255 minutes */
55#define PMIR (io) /* Power Management Index Register */
56#define PMDR (io+1) /* Power Management Data Register */
57
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 */
67
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 */
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int timeout = 1;
76static int io = -1;
77static int io_len = 2; /* for non plug and play */
Alan Cox103a1d52008-08-04 17:56:28 +010078static unsigned long open_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static char expect_close;
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -070080static DEFINE_SPINLOCK(sc1200wdt_lock); /* io port access serialisation */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82#if defined CONFIG_PNP
83static int isapnp = 1;
84static struct pnp_dev *wdt_dev;
85
86module_param(isapnp, int, 0);
Alan Cox103a1d52008-08-04 17:56:28 +010087MODULE_PARM_DESC(isapnp,
88 "When set to 0 driver ISA PnP support will be disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#endif
90
91module_param(io, int, 0);
92MODULE_PARM_DESC(io, "io port");
93module_param(timeout, int, 0);
94MODULE_PARM_DESC(timeout, "range is 0-255 minutes, default is 1");
95
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010096static bool nowayout = WATCHDOG_NOWAYOUT;
97module_param(nowayout, bool, 0);
Alan Cox103a1d52008-08-04 17:56:28 +010098MODULE_PARM_DESC(nowayout,
99 "Watchdog cannot be stopped once started (default="
100 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102
103
104/* Read from Data Register */
Alan Cox103a1d52008-08-04 17:56:28 +0100105static inline void __sc1200wdt_read_data(unsigned char index,
106 unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 outb_p(index, PMIR);
109 *data = inb(PMDR);
Alan Cox103a1d52008-08-04 17:56:28 +0100110}
111
112static void sc1200wdt_read_data(unsigned char index, unsigned char *data)
113{
114 spin_lock(&sc1200wdt_lock);
115 __sc1200wdt_read_data(index, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 spin_unlock(&sc1200wdt_lock);
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Write to Data Register */
Alan Cox103a1d52008-08-04 17:56:28 +0100120static inline void __sc1200wdt_write_data(unsigned char index,
121 unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 outb_p(index, PMIR);
124 outb(data, PMDR);
Alan Cox103a1d52008-08-04 17:56:28 +0100125}
126
127static inline void sc1200wdt_write_data(unsigned char index,
128 unsigned char data)
129{
130 spin_lock(&sc1200wdt_lock);
131 __sc1200wdt_write_data(index, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 spin_unlock(&sc1200wdt_lock);
133}
134
135
136static void sc1200wdt_start(void)
137{
138 unsigned char reg;
Alan Cox103a1d52008-08-04 17:56:28 +0100139 spin_lock(&sc1200wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Alan Cox103a1d52008-08-04 17:56:28 +0100141 __sc1200wdt_read_data(WDCF, &reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* assert WDO when any of the following interrupts are triggered too */
143 reg |= (KBC_IRQ | MSE_IRQ | UART1_IRQ | UART2_IRQ);
Alan Cox103a1d52008-08-04 17:56:28 +0100144 __sc1200wdt_write_data(WDCF, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* set the timeout and get the ball rolling */
Alan Cox103a1d52008-08-04 17:56:28 +0100146 __sc1200wdt_write_data(WDTO, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Alan Cox103a1d52008-08-04 17:56:28 +0100148 spin_unlock(&sc1200wdt_lock);
149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151static void sc1200wdt_stop(void)
152{
153 sc1200wdt_write_data(WDTO, 0);
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* This returns the status of the WDO signal, inactive high. */
157static inline int sc1200wdt_status(void)
158{
159 unsigned char ret;
160
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
165 */
Alan Cox103a1d52008-08-04 17:56:28 +0100166 return (ret & 0x01) ? 0 : WDIOF_KEEPALIVEPING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static int sc1200wdt_open(struct inode *inode, struct file *file)
170{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* allow one at a time */
Alan Cox103a1d52008-08-04 17:56:28 +0100172 if (test_and_set_bit(0, &open_flag))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -EBUSY;
174
175 if (timeout > MAX_TIMEOUT)
176 timeout = MAX_TIMEOUT;
177
178 sc1200wdt_start();
Joe Perches27c766a2012-02-15 15:06:19 -0800179 pr_info("Watchdog enabled, timeout = %d min(s)", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Wim Van Sebroeck6abe78b2007-07-24 21:38:37 +0000181 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184
Alan Cox103a1d52008-08-04 17:56:28 +0100185static long sc1200wdt_ioctl(struct file *file, unsigned int cmd,
186 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int new_timeout;
189 void __user *argp = (void __user *)arg;
190 int __user *p = argp;
Alan Cox103a1d52008-08-04 17:56:28 +0100191 static const struct watchdog_info ident = {
192 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
193 WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .firmware_version = 0,
195 .identity = "PC87307/PC97307",
196 };
197
198 switch (cmd) {
Alan Cox103a1d52008-08-04 17:56:28 +0100199 case WDIOC_GETSUPPORT:
Wim Van Sebroecke04ab952009-09-02 09:10:07 +0000200 if (copy_to_user(argp, &ident, sizeof(ident)))
Alan Cox103a1d52008-08-04 17:56:28 +0100201 return -EFAULT;
202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Alan Cox103a1d52008-08-04 17:56:28 +0100204 case WDIOC_GETSTATUS:
205 return put_user(sc1200wdt_status(), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Alan Cox103a1d52008-08-04 17:56:28 +0100207 case WDIOC_GETBOOTSTATUS:
208 return put_user(0, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Alan Cox103a1d52008-08-04 17:56:28 +0100210 case WDIOC_SETOPTIONS:
211 {
212 int options, retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Alan Cox103a1d52008-08-04 17:56:28 +0100214 if (get_user(options, p))
215 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Alan Cox103a1d52008-08-04 17:56:28 +0100217 if (options & WDIOS_DISABLECARD) {
218 sc1200wdt_stop();
219 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Alan Cox103a1d52008-08-04 17:56:28 +0100221
222 if (options & WDIOS_ENABLECARD) {
223 sc1200wdt_start();
224 retval = 0;
225 }
226
227 return retval;
228 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000229 case WDIOC_KEEPALIVE:
230 sc1200wdt_write_data(WDTO, timeout);
231 return 0;
232
233 case WDIOC_SETTIMEOUT:
234 if (get_user(new_timeout, p))
235 return -EFAULT;
236 /* the API states this is given in secs */
237 new_timeout /= 60;
238 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
239 return -EINVAL;
240 timeout = new_timeout;
241 sc1200wdt_write_data(WDTO, timeout);
242 /* fall through and return the new timeout */
243
244 case WDIOC_GETTIMEOUT:
245 return put_user(timeout * 60, p);
246
Alan Cox103a1d52008-08-04 17:56:28 +0100247 default:
248 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250}
251
252
253static int sc1200wdt_release(struct inode *inode, struct file *file)
254{
255 if (expect_close == 42) {
256 sc1200wdt_stop();
Joe Perches27c766a2012-02-15 15:06:19 -0800257 pr_info("Watchdog disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 } else {
259 sc1200wdt_write_data(WDTO, timeout);
Joe Perches27c766a2012-02-15 15:06:19 -0800260 pr_crit("Unexpected close!, timeout = %d min(s)\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Alan Cox103a1d52008-08-04 17:56:28 +0100262 clear_bit(0, &open_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 expect_close = 0;
264
265 return 0;
266}
267
268
Alan Cox103a1d52008-08-04 17:56:28 +0100269static ssize_t sc1200wdt_write(struct file *file, const char __user *data,
270 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 if (len) {
273 if (!nowayout) {
274 size_t i;
275
276 expect_close = 0;
277
278 for (i = 0; i != len; i++) {
279 char c;
280
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000281 if (get_user(c, data + i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -EFAULT;
283 if (c == 'V')
284 expect_close = 42;
285 }
286 }
287
288 sc1200wdt_write_data(WDTO, timeout);
289 return len;
290 }
291
292 return 0;
293}
294
295
Alan Cox103a1d52008-08-04 17:56:28 +0100296static int sc1200wdt_notify_sys(struct notifier_block *this,
297 unsigned long code, void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 if (code == SYS_DOWN || code == SYS_HALT)
300 sc1200wdt_stop();
301
302 return NOTIFY_DONE;
303}
304
305
Alan Cox103a1d52008-08-04 17:56:28 +0100306static struct notifier_block sc1200wdt_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 .notifier_call = sc1200wdt_notify_sys,
308};
309
Alan Cox103a1d52008-08-04 17:56:28 +0100310static const struct file_operations sc1200wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .owner = THIS_MODULE,
312 .llseek = no_llseek,
313 .write = sc1200wdt_write,
Alan Cox103a1d52008-08-04 17:56:28 +0100314 .unlocked_ioctl = sc1200wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 .open = sc1200wdt_open,
316 .release = sc1200wdt_release,
317};
318
Alan Cox103a1d52008-08-04 17:56:28 +0100319static struct miscdevice sc1200wdt_miscdev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 .minor = WATCHDOG_MINOR,
321 .name = "watchdog",
322 .fops = &sc1200wdt_fops,
323};
324
325
326static int __init sc1200wdt_probe(void)
327{
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.
Alan Cox103a1d52008-08-04 17:56:28 +0100331 * NB. This could be done with accuracy by reading the SID registers,
332 * but we don't have access to those io regions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
334
335 unsigned char reg;
336
337 sc1200wdt_read_data(PMC3, &reg);
Alan Cox103a1d52008-08-04 17:56:28 +0100338 reg &= 0x0f; /* we don't want the UART busy bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return (reg == 0x0e) ? 0 : -ENODEV;
340}
341
342
343#if defined CONFIG_PNP
344
345static struct pnp_device_id scl200wdt_pnp_devices[] = {
346 /* National Semiconductor PC87307/PC97307 watchdog component */
347 {.id = "NSC0800", .driver_data = 0},
348 {.id = ""},
349};
350
Alan Cox103a1d52008-08-04 17:56:28 +0100351static int scl200wdt_pnp_probe(struct pnp_dev *dev,
352 const struct pnp_device_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 /* this driver only supports one card at a time */
355 if (wdt_dev || !isapnp)
356 return -EBUSY;
357
358 wdt_dev = dev;
359 io = pnp_port_start(wdt_dev, 0);
360 io_len = pnp_port_len(wdt_dev, 0);
361
362 if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800363 pr_err("Unable to register IO port %#x\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return -EBUSY;
365 }
366
Joe Perches27c766a2012-02-15 15:06:19 -0800367 pr_info("PnP device found at io port %#x/%d\n", io, io_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
370
Alan Cox103a1d52008-08-04 17:56:28 +0100371static void scl200wdt_pnp_remove(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Alan Cox103a1d52008-08-04 17:56:28 +0100373 if (wdt_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 release_region(io, io_len);
375 wdt_dev = NULL;
376 }
377}
378
379static struct pnp_driver scl200wdt_pnp_driver = {
380 .name = "scl200wdt",
381 .id_table = scl200wdt_pnp_devices,
382 .probe = scl200wdt_pnp_probe,
383 .remove = scl200wdt_pnp_remove,
384};
385
386#endif /* CONFIG_PNP */
387
388
389static int __init sc1200wdt_init(void)
390{
391 int ret;
392
Joe Perches27c766a2012-02-15 15:06:19 -0800393 pr_info("%s\n", SC1200_MODULE_VER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#if defined CONFIG_PNP
396 if (isapnp) {
397 ret = pnp_register_driver(&scl200wdt_pnp_driver);
398 if (ret)
399 goto out_clean;
400 }
401#endif
402
403 if (io == -1) {
Joe Perches27c766a2012-02-15 15:06:19 -0800404 pr_err("io parameter must be specified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ret = -EINVAL;
Akinobu Mita150ed8e2006-10-29 03:43:19 +0900406 goto out_pnp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
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 */
Alandace8bb2013-12-04 15:31:52 +0000412 if (isapnp)
413 pnp_unregister_driver(&scl200wdt_pnp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 isapnp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#endif
416
417 if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800418 pr_err("Unable to register IO port %#x\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 ret = -EBUSY;
Akinobu Mita150ed8e2006-10-29 03:43:19 +0900420 goto out_pnp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
423 ret = sc1200wdt_probe();
424 if (ret)
425 goto out_io;
426
427 ret = register_reboot_notifier(&sc1200wdt_notifier);
428 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800429 pr_err("Unable to register reboot notifier err = %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 goto out_io;
431 }
432
433 ret = misc_register(&sc1200wdt_miscdev);
434 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800435 pr_err("Unable to register miscdev on minor %d\n",
436 WATCHDOG_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 goto out_rbt;
438 }
439
440 /* ret = 0 */
441
442out_clean:
443 return ret;
444
445out_rbt:
446 unregister_reboot_notifier(&sc1200wdt_notifier);
447
448out_io:
449 release_region(io, io_len);
450
Akinobu Mita150ed8e2006-10-29 03:43:19 +0900451out_pnp:
452#if defined CONFIG_PNP
453 if (isapnp)
454 pnp_unregister_driver(&scl200wdt_pnp_driver);
455#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 goto out_clean;
457}
458
459
460static void __exit sc1200wdt_exit(void)
461{
462 misc_deregister(&sc1200wdt_miscdev);
463 unregister_reboot_notifier(&sc1200wdt_notifier);
464
465#if defined CONFIG_PNP
Alan Cox103a1d52008-08-04 17:56:28 +0100466 if (isapnp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 pnp_unregister_driver(&scl200wdt_pnp_driver);
468 else
469#endif
470 release_region(io, io_len);
471}
472
473module_init(sc1200wdt_init);
474module_exit(sc1200wdt_exit);
475
476MODULE_AUTHOR("Zwane Mwaikambo <zwane@commfireservices.com>");
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000477MODULE_DESCRIPTION(
478 "Driver for National Semiconductor PC87307/PC97307 watchdog component");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479MODULE_LICENSE("GPL");