2 * RDC321x watchdog driver
4 * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/miscdevice.h>
23 #include <linux/platform_device.h>
25 #include <linux/init.h>
26 #include <linux/ioport.h>
27 #include <linux/timer.h>
28 #include <linux/watchdog.h>
29 #include <linux/uaccess.h>
30 #include <linux/pci.h>
31 #include <linux/delay.h>
33 #include <asm/rdc321x_defs.h>
35 extern int rdc321x_pci_write(int reg, u32 val);
36 extern int rdc321x_pci_read(int reg, u32 *val);
38 #define RDC321X_WDT_REG 0x00000044
40 #define RDC_WDT_EN 0x00800000 /* Enable bit */
41 #define RDC_WDT_WDTIRQ 0x00400000 /* Create WDT IRQ before CPU reset */
42 #define RDC_WDT_NMIIRQ 0x00200000 /* Create NMI IRQ before CPU reset */
43 #define RDC_WDT_RST 0x00100000 /* Reset wdt */
44 #define RDC_WDT_NIF 0x00080000 /* NMI interrupt occured */
45 #define RDC_WDT_WIF 0x00040000 /* WDT interrupt occured */
46 #define RDC_WDT_IRT 0x00000700 /* IRQ Routing table */
47 #define RDC_WDT_CNT 0x0000007F /* WDT count */
49 /* default counter value (2.34 s) */
50 #define RDC_WDT_DFLT_CNT 0x00000040
52 #define RDC_WDT_SETUP (RDC_WDT_EN | RDC_WDT_NMIIRQ | RDC_WDT_RST | RDC_WDT_DFLT_CNT)
54 /* some device data */
56 struct timer_list timer;
64 static struct watchdog_info ident = {
65 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
66 .identity = "RDC321x WDT",
69 /* generic helper functions */
70 static void rdc321x_wdt_timer(unsigned long unused)
72 if (!rdc321x_wdt_dev.running) {
73 rdc321x_pci_write(RDC321X_WDT_REG, 0);
77 rdc321x_wdt_dev.seconds_left--;
79 if (rdc321x_wdt_dev.seconds_left < 1)
82 rdc321x_pci_write(RDC321X_WDT_REG, RDC_WDT_SETUP);
84 mod_timer(&rdc321x_wdt_dev.timer, HZ * 2 + jiffies);
87 static void rdc321x_wdt_reset(void)
89 rdc321x_wdt_dev.seconds_left = rdc321x_wdt_dev.total_seconds;
92 static void rdc321x_wdt_start(void)
94 if (rdc321x_wdt_dev.running)
97 rdc321x_wdt_dev.seconds_left = rdc321x_wdt_dev.total_seconds;
99 rdc321x_wdt_dev.running = true;
101 rdc321x_wdt_timer(0);
106 static int rdc321x_wdt_stop(void)
108 if (WATCHDOG_NOWAYOUT)
111 rdc321x_wdt_dev.running = false;
116 /* filesystem operations */
117 static int rdc321x_wdt_open(struct inode *inode, struct file *file)
119 if (xchg(&rdc321x_wdt_dev.inuse, true))
122 return nonseekable_open(inode, file);
125 static int rdc321x_wdt_release(struct inode *inode, struct file *file)
128 if (rdc321x_wdt_dev.close_expected) {
129 res = rdc321x_wdt_stop();
134 rdc321x_wdt_dev.inuse = false;
139 static long rdc321x_wdt_ioctl(struct file *file, unsigned int cmd,
142 void __user *argp = (void __user *)arg;
146 case WDIOC_KEEPALIVE:
149 case WDIOC_GETSUPPORT:
150 if (copy_to_user(argp, &ident, sizeof(ident)))
153 case WDIOC_SETTIMEOUT:
154 if (copy_from_user(&rdc321x_wdt_dev.total_seconds, argp, sizeof(int)))
156 rdc321x_wdt_dev.seconds_left = rdc321x_wdt_dev.total_seconds;
158 case WDIOC_GETTIMEOUT:
159 if (copy_to_user(argp, &rdc321x_wdt_dev.total_seconds, sizeof(int)))
162 case WDIOC_GETTIMELEFT:
163 if (copy_to_user(argp, &rdc321x_wdt_dev.seconds_left, sizeof(int)))
166 case WDIOC_SETOPTIONS:
167 if (copy_from_user(&value, argp, sizeof(int)))
170 case WDIOS_ENABLECARD:
173 case WDIOS_DISABLECARD:
174 return rdc321x_wdt_stop();
185 static ssize_t rdc321x_wdt_write(struct file *file, const char __user *buf,
186 size_t count, loff_t *ppos)
193 rdc321x_wdt_dev.close_expected = false;
195 for (i = 0; i != count; i++) {
198 if (get_user(c, buf + i))
202 rdc321x_wdt_dev.close_expected = true;
212 static const struct file_operations rdc321x_wdt_fops = {
214 .unlocked_ioctl = rdc321x_wdt_ioctl,
215 .open = rdc321x_wdt_open,
216 .write = rdc321x_wdt_write,
217 .release = rdc321x_wdt_release,
220 static struct miscdevice rdc321x_wdt_misc = {
221 .minor = WATCHDOG_MINOR,
223 .fops = &rdc321x_wdt_fops,
226 static int __init rdc321x_wdt_probe(struct platform_device *pdev)
230 err = rdc321x_pci_write(RDC321X_WDT_REG, 0);
234 rdc321x_wdt_dev.running = false;
235 rdc321x_wdt_dev.close_expected = false;
236 rdc321x_wdt_dev.inuse = 0;
237 setup_timer(&rdc321x_wdt_dev.timer, rdc321x_wdt_timer, 0);
238 rdc321x_wdt_dev.total_seconds = 100;
240 err = misc_register(&rdc321x_wdt_misc);
242 printk(KERN_ERR PFX "watchdog: misc_register failed\n");
246 panic_on_unrecovered_nmi = 1;
247 dev_info(&pdev->dev, "watchdog init success\n");
252 static int __devexit rdc321x_wdt_remove(struct platform_device *pdev)
254 if (rdc321x_wdt_dev.inuse)
255 rdc321x_wdt_dev.inuse = 0;
257 while (timer_pending(&rdc321x_wdt_dev.timer))
260 misc_deregister(&rdc321x_wdt_misc);
264 static struct platform_driver rdc321x_wdt_driver = {
265 .driver.name = "rdc321x-wdt",
266 .driver.owner = THIS_MODULE,
267 .probe = rdc321x_wdt_probe,
268 .remove = __devexit_p(rdc321x_wdt_remove),
271 static int __init rdc321x_wdt_init(void)
273 return platform_driver_register(&rdc321x_wdt_driver);
276 static void __exit rdc321x_wdt_exit(void)
278 platform_driver_unregister(&rdc321x_wdt_driver);
281 module_init(rdc321x_wdt_init);
282 module_exit(rdc321x_wdt_exit);
284 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
285 MODULE_DESCRIPTION("RDC321x Watchdog driver");
286 MODULE_LICENSE("GPL");
287 MODULE_ALIAS("platform:rdc321x-wdt");