mm-only debug patch...
[mmotm.git] / drivers / watchdog / rdc321x_wdt.c
blob4976bfd1fce6cc66476c21069bd89ae3d5312169
1 /*
2 * RDC321x watchdog driver
4 * Copyright (C) 2007 Florian Fainelli <florian@openwrt.org>
6 * This driver is highly inspired from the cpu5_wdt driver
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/types.h>
27 #include <linux/errno.h>
28 #include <linux/miscdevice.h>
29 #include <linux/fs.h>
30 #include <linux/init.h>
31 #include <linux/ioport.h>
32 #include <linux/timer.h>
33 #include <linux/completion.h>
34 #include <linux/jiffies.h>
35 #include <linux/platform_device.h>
36 #include <linux/watchdog.h>
37 #include <linux/io.h>
38 #include <linux/uaccess.h>
40 #include <asm/rdc321x_defs.h>
42 #define RDC_WDT_MASK 0x80000000 /* Mask */
43 #define RDC_WDT_EN 0x00800000 /* Enable bit */
44 #define RDC_WDT_WTI 0x00200000 /* Generate CPU reset/NMI/WDT on timeout */
45 #define RDC_WDT_RST 0x00100000 /* Reset bit */
46 #define RDC_WDT_WIF 0x00040000 /* WDT IRQ Flag */
47 #define RDC_WDT_IRT 0x00000100 /* IRQ Routing table */
48 #define RDC_WDT_CNT 0x00000001 /* WDT count */
50 #define RDC_CLS_TMR 0x80003844 /* Clear timer */
52 #define RDC_WDT_INTERVAL (HZ/10+1)
54 static int ticks = 1000;
56 /* some device data */
58 static struct {
59 struct completion stop;
60 int running;
61 struct timer_list timer;
62 int queue;
63 int default_ticks;
64 unsigned long inuse;
65 spinlock_t lock;
66 } rdc321x_wdt_device;
68 /* generic helper functions */
70 static void rdc321x_wdt_trigger(unsigned long unused)
72 unsigned long flags;
74 if (rdc321x_wdt_device.running)
75 ticks--;
77 /* keep watchdog alive */
78 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
79 outl(RDC_WDT_EN | inl(RDC3210_CFGREG_DATA),
80 RDC3210_CFGREG_DATA);
81 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
83 /* requeue?? */
84 if (rdc321x_wdt_device.queue && ticks)
85 mod_timer(&rdc321x_wdt_device.timer,
86 jiffies + RDC_WDT_INTERVAL);
87 else {
88 /* ticks doesn't matter anyway */
89 complete(&rdc321x_wdt_device.stop);
94 static void rdc321x_wdt_reset(void)
96 ticks = rdc321x_wdt_device.default_ticks;
99 static void rdc321x_wdt_start(void)
101 unsigned long flags;
103 if (!rdc321x_wdt_device.queue) {
104 rdc321x_wdt_device.queue = 1;
106 /* Clear the timer */
107 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
108 outl(RDC_CLS_TMR, RDC3210_CFGREG_ADDR);
110 /* Enable watchdog and set the timeout to 81.92 us */
111 outl(RDC_WDT_EN | RDC_WDT_CNT, RDC3210_CFGREG_DATA);
112 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
114 mod_timer(&rdc321x_wdt_device.timer,
115 jiffies + RDC_WDT_INTERVAL);
118 /* if process dies, counter is not decremented */
119 rdc321x_wdt_device.running++;
122 static int rdc321x_wdt_stop(void)
124 if (rdc321x_wdt_device.running)
125 rdc321x_wdt_device.running = 0;
127 ticks = rdc321x_wdt_device.default_ticks;
129 return -EIO;
132 /* filesystem operations */
133 static int rdc321x_wdt_open(struct inode *inode, struct file *file)
135 if (test_and_set_bit(0, &rdc321x_wdt_device.inuse))
136 return -EBUSY;
138 return nonseekable_open(inode, file);
141 static int rdc321x_wdt_release(struct inode *inode, struct file *file)
143 clear_bit(0, &rdc321x_wdt_device.inuse);
144 return 0;
147 static long rdc321x_wdt_ioctl(struct file *file, unsigned int cmd,
148 unsigned long arg)
150 void __user *argp = (void __user *)arg;
151 unsigned int value;
152 static struct watchdog_info ident = {
153 .options = WDIOF_CARDRESET,
154 .identity = "RDC321x WDT",
156 unsigned long flags;
158 switch (cmd) {
159 case WDIOC_KEEPALIVE:
160 rdc321x_wdt_reset();
161 break;
162 case WDIOC_GETSTATUS:
163 /* Read the value from the DATA register */
164 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
165 value = inl(RDC3210_CFGREG_DATA);
166 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
167 if (copy_to_user(argp, &value, sizeof(int)))
168 return -EFAULT;
169 break;
170 case WDIOC_GETSUPPORT:
171 if (copy_to_user(argp, &ident, sizeof(ident)))
172 return -EFAULT;
173 break;
174 case WDIOC_SETOPTIONS:
175 if (copy_from_user(&value, argp, sizeof(int)))
176 return -EFAULT;
177 switch (value) {
178 case WDIOS_ENABLECARD:
179 rdc321x_wdt_start();
180 break;
181 case WDIOS_DISABLECARD:
182 return rdc321x_wdt_stop();
183 default:
184 return -EINVAL;
186 break;
187 default:
188 return -ENOTTY;
190 return 0;
193 static ssize_t rdc321x_wdt_write(struct file *file, const char __user *buf,
194 size_t count, loff_t *ppos)
196 if (!count)
197 return -EIO;
199 rdc321x_wdt_reset();
201 return count;
204 static const struct file_operations rdc321x_wdt_fops = {
205 .owner = THIS_MODULE,
206 .llseek = no_llseek,
207 .unlocked_ioctl = rdc321x_wdt_ioctl,
208 .open = rdc321x_wdt_open,
209 .write = rdc321x_wdt_write,
210 .release = rdc321x_wdt_release,
213 static struct miscdevice rdc321x_wdt_misc = {
214 .minor = WATCHDOG_MINOR,
215 .name = "watchdog",
216 .fops = &rdc321x_wdt_fops,
219 static int __devinit rdc321x_wdt_probe(struct platform_device *pdev)
221 int err;
223 err = misc_register(&rdc321x_wdt_misc);
224 if (err < 0) {
225 printk(KERN_ERR PFX "watchdog misc_register failed\n");
226 return err;
229 spin_lock_init(&rdc321x_wdt_device.lock);
231 /* Reset the watchdog */
232 outl(RDC_WDT_RST, RDC3210_CFGREG_DATA);
234 init_completion(&rdc321x_wdt_device.stop);
235 rdc321x_wdt_device.queue = 0;
237 clear_bit(0, &rdc321x_wdt_device.inuse);
239 setup_timer(&rdc321x_wdt_device.timer, rdc321x_wdt_trigger, 0);
241 rdc321x_wdt_device.default_ticks = ticks;
243 printk(KERN_INFO PFX "watchdog init success\n");
245 return 0;
248 static int __devexit rdc321x_wdt_remove(struct platform_device *pdev)
250 if (rdc321x_wdt_device.queue) {
251 rdc321x_wdt_device.queue = 0;
252 wait_for_completion(&rdc321x_wdt_device.stop);
255 misc_deregister(&rdc321x_wdt_misc);
257 return 0;
260 static struct platform_driver rdc321x_wdt_driver = {
261 .probe = rdc321x_wdt_probe,
262 .remove = __devexit_p(rdc321x_wdt_remove),
263 .driver = {
264 .owner = THIS_MODULE,
265 .name = "rdc321x-wdt",
269 static int __init rdc321x_wdt_init(void)
271 return platform_driver_register(&rdc321x_wdt_driver);
274 static void __exit rdc321x_wdt_exit(void)
276 platform_driver_unregister(&rdc321x_wdt_driver);
279 module_init(rdc321x_wdt_init);
280 module_exit(rdc321x_wdt_exit);
282 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
283 MODULE_DESCRIPTION("RDC321x watchdog driver");
284 MODULE_LICENSE("GPL");
285 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);