1 /* drivers/char/watchdog/scx200_wdt.c
3 National Semiconductor SCx200 Watchdog support
5 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
8 National Semiconductor PC87307/PC97307 (ala SC1200) WDT driver
9 (c) Copyright 2002 Zwane Mwaikambo <zwane@commfireservices.com>
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
16 The author(s) of this software shall not be held liable for damages
17 of any nature resulting due to the use of this software. This
18 software is provided AS-IS with no warranties. */
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/init.h>
24 #include <linux/miscdevice.h>
25 #include <linux/watchdog.h>
26 #include <linux/notifier.h>
27 #include <linux/reboot.h>
28 #include <linux/pci.h>
29 #include <linux/scx200.h>
31 #include <asm/uaccess.h>
34 #define NAME "scx200_wdt"
36 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
37 MODULE_DESCRIPTION("NatSemi SCx200 Watchdog Driver");
38 MODULE_LICENSE("GPL");
39 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
41 #ifndef CONFIG_WATCHDOG_NOWAYOUT
42 #define CONFIG_WATCHDOG_NOWAYOUT 0
45 static int margin = 60; /* in seconds */
46 module_param(margin, int, 0);
47 MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
49 static int nowayout = CONFIG_WATCHDOG_NOWAYOUT;
50 module_param(nowayout, int, 0);
51 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
53 static u16 wdto_restart;
54 static struct semaphore open_semaphore;
55 static char expect_close;
57 /* Bits of the WDCNFG register */
58 #define W_ENABLE 0x00fa /* Enable watchdog */
59 #define W_DISABLE 0x0000 /* Disable watchdog */
61 /* The scaling factor for the timer, this depends on the value of W_ENABLE */
62 #define W_SCALE (32768/1024)
64 static void scx200_wdt_ping(void)
66 outw(wdto_restart, SCx200_CB_BASE + SCx200_WDT_WDTO);
69 static void scx200_wdt_update_margin(void)
71 printk(KERN_INFO NAME ": timer margin %d seconds\n", margin);
72 wdto_restart = margin * W_SCALE;
75 static void scx200_wdt_enable(void)
77 printk(KERN_DEBUG NAME ": enabling watchdog timer, wdto_restart = %d\n",
80 outw(0, SCx200_CB_BASE + SCx200_WDT_WDTO);
81 outb(SCx200_WDT_WDSTS_WDOVF, SCx200_CB_BASE + SCx200_WDT_WDSTS);
82 outw(W_ENABLE, SCx200_CB_BASE + SCx200_WDT_WDCNFG);
87 static void scx200_wdt_disable(void)
89 printk(KERN_DEBUG NAME ": disabling watchdog timer\n");
91 outw(0, SCx200_CB_BASE + SCx200_WDT_WDTO);
92 outb(SCx200_WDT_WDSTS_WDOVF, SCx200_CB_BASE + SCx200_WDT_WDSTS);
93 outw(W_DISABLE, SCx200_CB_BASE + SCx200_WDT_WDCNFG);
96 static int scx200_wdt_open(struct inode *inode, struct file *file)
98 /* only allow one at a time */
99 if (down_trylock(&open_semaphore))
106 static int scx200_wdt_release(struct inode *inode, struct file *file)
108 if (expect_close != 42) {
109 printk(KERN_WARNING NAME ": watchdog device closed unexpectedly, will not disable the watchdog timer\n");
110 } else if (!nowayout) {
111 scx200_wdt_disable();
119 static int scx200_wdt_notify_sys(struct notifier_block *this,
120 unsigned long code, void *unused)
122 if (code == SYS_HALT || code == SYS_POWER_OFF)
124 scx200_wdt_disable();
129 static struct notifier_block scx200_wdt_notifier =
131 .notifier_call = scx200_wdt_notify_sys,
134 static ssize_t scx200_wdt_write(struct file *file, const char *data,
135 size_t len, loff_t *ppos)
137 if (ppos != &file->f_pos)
140 /* check for a magic close character */
148 for (i = 0; i < len; ++i) {
150 if (get_user(c, data+i))
162 static int scx200_wdt_ioctl(struct inode *inode, struct file *file,
163 unsigned int cmd, unsigned long arg)
165 static struct watchdog_info ident = {
166 .identity = "NatSemi SCx200 Watchdog",
167 .firmware_version = 1,
168 .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING),
175 case WDIOC_GETSUPPORT:
176 if(copy_to_user((struct watchdog_info *)arg, &ident,
180 case WDIOC_GETSTATUS:
181 case WDIOC_GETBOOTSTATUS:
182 if (put_user(0, (int *)arg))
185 case WDIOC_KEEPALIVE:
188 case WDIOC_SETTIMEOUT:
189 if (get_user(new_margin, (int *)arg))
194 scx200_wdt_update_margin();
196 case WDIOC_GETTIMEOUT:
197 if (put_user(margin, (int *)arg))
203 static struct file_operations scx200_wdt_fops = {
204 .owner = THIS_MODULE,
205 .write = scx200_wdt_write,
206 .ioctl = scx200_wdt_ioctl,
207 .open = scx200_wdt_open,
208 .release = scx200_wdt_release,
211 static struct miscdevice scx200_wdt_miscdev = {
212 .minor = WATCHDOG_MINOR,
214 .fops = &scx200_wdt_fops,
217 static int __init scx200_wdt_init(void)
221 printk(KERN_DEBUG NAME ": NatSemi SCx200 Watchdog Driver\n");
223 /* First check that this really is a NatSemi SCx200 CPU */
224 if ((pci_find_device(PCI_VENDOR_ID_NS,
225 PCI_DEVICE_ID_NS_SCx200_BRIDGE,
229 /* More sanity checks, verify that the configuration block is there */
230 if (!scx200_cb_probe(SCx200_CB_BASE)) {
231 printk(KERN_WARNING NAME ": no configuration block found\n");
235 if (!request_region(SCx200_CB_BASE + SCx200_WDT_OFFSET,
237 "NatSemi SCx200 Watchdog")) {
238 printk(KERN_WARNING NAME ": watchdog I/O region busy\n");
242 scx200_wdt_update_margin();
243 scx200_wdt_disable();
245 sema_init(&open_semaphore, 1);
247 r = misc_register(&scx200_wdt_miscdev);
249 release_region(SCx200_CB_BASE + SCx200_WDT_OFFSET,
254 r = register_reboot_notifier(&scx200_wdt_notifier);
256 printk(KERN_ERR NAME ": unable to register reboot notifier");
257 misc_deregister(&scx200_wdt_miscdev);
258 release_region(SCx200_CB_BASE + SCx200_WDT_OFFSET,
266 static void __exit scx200_wdt_cleanup(void)
268 unregister_reboot_notifier(&scx200_wdt_notifier);
269 misc_deregister(&scx200_wdt_miscdev);
270 release_region(SCx200_CB_BASE + SCx200_WDT_OFFSET,
274 module_init(scx200_wdt_init);
275 module_exit(scx200_wdt_cleanup);
279 compile-command: "make -k -C ../.. SUBDIRS=drivers/char modules"