13b69f68ab8ebe66ccded10323bb770715b41583
[openwrt.git] / target / linux / rdc / files-2.6.24 / arch / x86 / mach-rdc / wdt.c
1 /*
2  * RDC321x watchdog driver
3  *
4  * Copyright (C) 2007 Florian Fainelli <florian@openwrt.org>
5  *
6  * This driver is highly inspired from the cpu5_wdt driver
7  *
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.
12  *
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.
17  *
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.
21  *
22  */
23
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
38 #include <asm/io.h>
39 #include <asm/uaccess.h>
40
41 #include <asm/mach-rdc/rdc321x_defs.h>
42
43 #define RDC_WDT_MASK            0x80000000      /* Mask */
44 #define RDC_WDT_EN              0x00800000      /* Enable bit */
45 #define RDC_WDT_WTI             0x00200000      /* Generate a CPU reset/NMI/WDT irq when WDT timeout is reached */
46 #define RDC_WDT_RST             0x00100000      /* Reset bit */
47 #define RDC_WDT_WIF             0x00040000      /* WDT IRQ Flag */
48 #define RDC_WDT_IRT             0x00000100      /* IRQ Routing table */
49 #define RDC_WDT_CNT             0x00000001      /* WDT count */
50
51 #define RDC_CLS_TMR             0x80003844      /* Clear timer */
52
53 #define RDC_WDT_INTERVAL        (HZ/10+1)
54
55 int nowayout = WATCHDOG_NOWAYOUT;
56 module_param(nowayout, int, 0);
57 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
58
59 static int ticks = 1000;
60
61 /* some device data */
62
63 static struct {
64         struct completion stop;
65         volatile int running;
66         struct timer_list timer;
67         volatile int queue;
68         int default_ticks;
69         unsigned long inuse;
70 } rdc321x_wdt_device;
71
72 /* generic helper functions */
73
74 static void rdc321x_wdt_trigger(unsigned long unused)
75 {
76         if( rdc321x_wdt_device.running )
77                 ticks--;
78
79         /* keep watchdog alive */
80         outl(RDC_WDT_EN|inl(RDC3210_CFGREG_DATA), RDC3210_CFGREG_DATA); 
81         
82         /* requeue?? */
83         if (rdc321x_wdt_device.queue && ticks)
84                 mod_timer(&rdc321x_wdt_device.timer, jiffies + RDC_WDT_INTERVAL);
85         else {
86                 /* ticks doesn't matter anyway */
87                 complete(&rdc321x_wdt_device.stop);
88         }
89
90 }
91
92 static void rdc321x_wdt_reset(void)
93 {
94         ticks = rdc321x_wdt_device.default_ticks;
95 }
96
97 static void rdc321x_wdt_start(void)
98 {
99         if (!rdc321x_wdt_device.queue) {
100                 rdc321x_wdt_device.queue = 1;
101
102                 /* Clear the timer */
103                 outl(RDC_CLS_TMR, RDC3210_CFGREG_ADDR);
104                 
105                 /* Enable watchdog and set the timeout to 81.92 us */
106                 outl(RDC_WDT_EN|RDC_WDT_CNT, RDC3210_CFGREG_DATA);
107
108                 mod_timer(&rdc321x_wdt_device.timer, jiffies + RDC_WDT_INTERVAL);
109         }
110
111         /* if process dies, counter is not decremented */
112         rdc321x_wdt_device.running++;
113 }
114
115 static int rdc321x_wdt_stop(void)
116 {
117         if (rdc321x_wdt_device.running)
118                 rdc321x_wdt_device.running = 0;
119
120         ticks = rdc321x_wdt_device.default_ticks;
121
122         return -EIO;
123 }
124
125 /* filesystem operations */
126
127 static int rdc321x_wdt_open(struct inode *inode, struct file *file)
128 {
129         if (test_and_set_bit(0, &rdc321x_wdt_device.inuse))
130                 return -EBUSY;
131
132         return nonseekable_open(inode, file);
133 }
134
135 static int rdc321x_wdt_release(struct inode *inode, struct file *file)
136 {
137         clear_bit(0, &rdc321x_wdt_device.inuse);
138         return 0;
139 }
140
141 static int rdc321x_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
142 {
143         void __user *argp = (void __user *)arg;
144         unsigned int value;
145         static struct watchdog_info ident =
146         {
147                 .options = WDIOF_CARDRESET,
148                 .identity = "RDC321x WDT",
149         };
150
151         switch(cmd) {
152                 case WDIOC_KEEPALIVE:
153                         rdc321x_wdt_reset();
154                         break;
155                 case WDIOC_GETSTATUS:
156                         /* Read the value from the DATA register */
157                         value = inl(RDC3210_CFGREG_DATA);
158                         if ( copy_to_user(argp, &value, sizeof(int)) )
159                                 return -EFAULT;
160                         break;
161                 case WDIOC_GETSUPPORT:
162                         if ( copy_to_user(argp, &ident, sizeof(ident)) )
163                                 return -EFAULT;
164                         break;
165                 case WDIOC_SETOPTIONS:
166                         if ( copy_from_user(&value, argp, sizeof(int)) )
167                                 return -EFAULT;
168                         switch(value) {
169                                 case WDIOS_ENABLECARD:
170                                         rdc321x_wdt_start();
171                                         break;
172                                 case WDIOS_DISABLECARD:
173                                         return rdc321x_wdt_stop();
174                                 default:
175                                         return -EINVAL;
176                         }
177                         break;
178                 default:
179                         return -ENOTTY;
180         }
181         return 0;
182 }
183
184 static ssize_t rdc321x_wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
185 {
186         if ( !count )
187                 return -EIO;
188
189         rdc321x_wdt_reset();
190
191         return count;
192 }
193
194 static const struct file_operations rdc321x_wdt_fops = {
195         .owner          = THIS_MODULE,
196         .llseek         = no_llseek,
197         .ioctl          = rdc321x_wdt_ioctl,
198         .open           = rdc321x_wdt_open,
199         .write          = rdc321x_wdt_write,
200         .release        = rdc321x_wdt_release,
201 };
202
203 static struct miscdevice rdc321x_wdt_misc = {
204         .minor  = WATCHDOG_MINOR,
205         .name   = "watchdog",
206         .fops   = &rdc321x_wdt_fops,
207 };
208
209 static int __devinit rdc321x_wdt_probe(struct platform_device *pdev)
210 {
211         int err;
212
213         if ( (err = misc_register(&rdc321x_wdt_misc)) < 0 ) {
214                 printk(KERN_ERR PFX "misc_register failed\n");
215                 return err;
216         }
217
218         /* Reset the watchdog */
219         outl(RDC_WDT_RST, RDC3210_CFGREG_DATA);
220
221         init_completion(&rdc321x_wdt_device.stop);
222         rdc321x_wdt_device.queue = 0;
223
224         clear_bit(0, &rdc321x_wdt_device.inuse);
225
226         setup_timer(&rdc321x_wdt_device.timer, rdc321x_wdt_trigger, 0);
227
228         rdc321x_wdt_device.default_ticks = ticks;
229
230         printk(KERN_INFO PFX "init success\n");
231
232         return 0;
233 }
234
235 static int rdc321x_wdt_remove(struct platform_device *pdev)
236 {
237         if (rdc321x_wdt_device.queue) {
238                 rdc321x_wdt_device.queue = 0;
239                 wait_for_completion(&rdc321x_wdt_device.stop);
240         }
241
242         misc_deregister(&rdc321x_wdt_misc);
243
244         return 0;
245 }
246
247 static struct platform_driver rdc321x_wdt_driver = {
248         .probe = rdc321x_wdt_probe,
249         .remove = rdc321x_wdt_remove,
250         .driver = {
251                 .owner = THIS_MODULE,
252                 .name = "rdc321x-wdt",
253         },
254 };
255
256 static int __init rdc321x_wdt_init(void)
257 {
258         return platform_driver_register(&rdc321x_wdt_driver);
259 }
260
261 static void __exit rdc321x_wdt_exit(void)
262 {
263         platform_driver_unregister(&rdc321x_wdt_driver);
264 }
265
266 module_init(rdc321x_wdt_init);
267 module_exit(rdc321x_wdt_exit);
268
269 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
270 MODULE_DESCRIPTION("RDC321x watchdog driver");
271 MODULE_LICENSE("GPL");
272 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);