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>
32 #include <asm/rdc321x_defs.h>
34 extern int rdc321x_pci_write(int reg
, u32 val
);
35 extern int rdc321x_pci_read(int reg
, u32
*val
);
37 #define RDC321X_WDT_REG 0x00000044
39 #define RDC_WDT_EN 0x00800000 /* Enable bit */
40 #define RDC_WDT_WDTIRQ 0x00400000 /* Create WDT IRQ before CPU reset */
41 #define RDC_WDT_NMIIRQ 0x00200000 /* Create NMI IRQ before CPU reset */
42 #define RDC_WDT_RST 0x00100000 /* Reset wdt */
43 #define RDC_WDT_NIF 0x00080000 /* NMI interrupt occured */
44 #define RDC_WDT_WIF 0x00040000 /* WDT interrupt occured */
45 #define RDC_WDT_IRT 0x00000700 /* IRQ Routing table */
46 #define RDC_WDT_CNT 0x0000007F /* WDT count */
48 /* default counter value (2.34 s) */
49 #define RDC_WDT_DFLT_CNT 0x00000040
51 #define RDC_WDT_SETUP (RDC_WDT_EN | RDC_WDT_NMIIRQ | RDC_WDT_RST | RDC_WDT_DFLT_CNT)
53 /* some device data */
55 struct timer_list timer
;
63 static struct watchdog_info ident
= {
64 .options
= WDIOF_SETTIMEOUT
| WDIOF_MAGICCLOSE
,
65 .identity
= "RDC321x WDT",
68 /* generic helper functions */
69 static void rdc321x_wdt_timer(unsigned long unused
)
71 if (!rdc321x_wdt_dev
.running
) {
72 rdc321x_pci_write(RDC321X_WDT_REG
, 0);
76 rdc321x_wdt_dev
.seconds_left
--;
78 if (rdc321x_wdt_dev
.seconds_left
< 1)
81 rdc321x_pci_write(RDC321X_WDT_REG
, RDC_WDT_SETUP
);
83 mod_timer(&rdc321x_wdt_dev
.timer
, HZ
* 2 + jiffies
);
86 static void rdc321x_wdt_reset(void)
88 rdc321x_wdt_dev
.seconds_left
= rdc321x_wdt_dev
.total_seconds
;
91 static void rdc321x_wdt_start(void)
93 if (rdc321x_wdt_dev
.running
)
96 rdc321x_wdt_dev
.seconds_left
= rdc321x_wdt_dev
.total_seconds
;
98 rdc321x_wdt_dev
.running
= true;
100 rdc321x_wdt_timer(0);
105 static int rdc321x_wdt_stop(void)
107 if (WATCHDOG_NOWAYOUT
)
110 rdc321x_wdt_dev
.running
= false;
115 /* filesystem operations */
116 static int rdc321x_wdt_open(struct inode
*inode
, struct file
*file
)
118 if (xchg(&rdc321x_wdt_dev
.inuse
, true))
121 return nonseekable_open(inode
, file
);
124 static int rdc321x_wdt_release(struct inode
*inode
, struct file
*file
)
126 if (rdc321x_wdt_dev
.close_expected
)
129 rdc321x_wdt_dev
.inuse
= false;
134 static long rdc321x_wdt_ioctl(struct file
*file
, unsigned int cmd
,
137 void __user
*argp
= (void __user
*)arg
;
141 case WDIOC_KEEPALIVE
:
144 case WDIOC_GETSUPPORT
:
145 if (copy_to_user(argp
, &ident
, sizeof(ident
)))
148 case WDIOC_SETTIMEOUT
:
149 if (copy_from_user(&rdc321x_wdt_dev
.total_seconds
, argp
, sizeof(int)))
151 rdc321x_wdt_dev
.seconds_left
= rdc321x_wdt_dev
.total_seconds
;
153 case WDIOC_GETTIMEOUT
:
154 if (copy_to_user(argp
, &rdc321x_wdt_dev
.total_seconds
, sizeof(int)))
157 case WDIOC_GETTIMELEFT
:
158 if (copy_to_user(argp
, &rdc321x_wdt_dev
.seconds_left
, sizeof(int)))
161 case WDIOC_SETOPTIONS
:
162 if (copy_from_user(&value
, argp
, sizeof(int)))
165 case WDIOS_ENABLECARD
:
168 case WDIOS_DISABLECARD
:
169 return rdc321x_wdt_stop();
180 static ssize_t
rdc321x_wdt_write(struct file
*file
, const char __user
*buf
,
181 size_t count
, loff_t
*ppos
)
188 rdc321x_wdt_dev
.close_expected
= false;
190 for (i
= 0; i
!= count
; i
++) {
193 if (get_user(c
, buf
+ i
))
197 rdc321x_wdt_dev
.close_expected
= true;
207 static const struct file_operations rdc321x_wdt_fops
= {
209 .unlocked_ioctl
= rdc321x_wdt_ioctl
,
210 .open
= rdc321x_wdt_open
,
211 .write
= rdc321x_wdt_write
,
212 .release
= rdc321x_wdt_release
,
215 static struct miscdevice rdc321x_wdt_misc
= {
216 .minor
= WATCHDOG_MINOR
,
218 .fops
= &rdc321x_wdt_fops
,
221 static int __init
rdc321x_wdt_probe(struct platform_device
*pdev
)
225 err
= rdc321x_pci_write(RDC321X_WDT_REG
, 0);
229 rdc321x_wdt_dev
.running
= false;
230 rdc321x_wdt_dev
.close_expected
= false;
231 rdc321x_wdt_dev
.inuse
= 0;
232 setup_timer(&rdc321x_wdt_dev
.timer
, rdc321x_wdt_timer
, 0);
233 rdc321x_wdt_dev
.total_seconds
= 100;
235 err
= misc_register(&rdc321x_wdt_misc
);
237 printk(KERN_ERR PFX
"watchdog: misc_register failed\n");
241 panic_on_unrecovered_nmi
= 1;
242 dev_info(&pdev
->dev
, "watchdog inig success\n");
247 static int __devexit
rdc321x_wdt_remove(struct platform_device
*pdev
)
249 if (rdc321x_wdt_dev
.inuse
)
250 rdc321x_wdt_dev
.inuse
= 0;
251 misc_deregister(&rdc321x_wdt_misc
);
255 static struct platform_driver rdc321x_wdt_driver
= {
256 .driver
.name
= "rdc321x-wdt",
257 .driver
.owner
= THIS_MODULE
,
258 .probe
= rdc321x_wdt_probe
,
259 .remove
= __devexit_p(rdc321x_wdt_remove
),
262 static int __init
rdc321x_wdt_init(void)
264 return platform_driver_register(&rdc321x_wdt_driver
);
267 static void __exit
rdc321x_wdt_exit(void)
269 platform_driver_unregister(&rdc321x_wdt_driver
);
272 module_init(rdc321x_wdt_init
);
273 module_exit(rdc321x_wdt_exit
);
275 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
276 MODULE_DESCRIPTION("RDC321x Watchdog driver");
277 MODULE_LICENSE("GPL");
278 MODULE_ALIAS("platform:rdc321x-wdt");