Ajoute le support easygate
[librecmc/librecmc.git] / target / linux / easygate-2.6 / files / arch / mips / bcm963xx / wdt.c
1 /*
2  * Watchdog driver for the BCM963xx devices
3  * 
4  * Copyright (C) 2007 OpenWrt.org
5  *                      Florian Fainelli <florian@openwrt.org>
6  * 
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  */
12
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/miscdevice.h>
17 #include <linux/fs.h>
18 #include <linux/init.h>
19 #include <linux/notifier.h>
20 #include <linux/watchdog.h>
21 #include <linux/timer.h>
22 #include <linux/jiffies.h>
23 #include <linux/completion.h>
24 #include <linux/ioport.h>
25
26 typedef struct bcm963xx_timer {
27         unsigned short unused0;
28         unsigned char  timer_mask;
29 #define TIMER0EN        0x01
30 #define TIMER1EN        0x02
31 #define TIMER2EN        0x04
32         unsigned char  timer_ints;
33 #define TIMER0          0x01
34 #define TIMER1          0x02
35 #define TIMER2          0x04
36 #define WATCHDOG        0x08
37         unsigned long   timer_ctl0;
38         unsigned long   timer_ctl1;
39         unsigned long   timer_ctl2;
40 #define TIMERENABLE     0x80000000
41 #define RSTCNTCLR       0x40000000      
42         unsigned long   timer_cnt0;
43         unsigned long   timer_cnt1;
44         unsigned long   timer_cnt2;
45         unsigned long   wdt_def_count;
46
47         /* Write 0xff00 0x00ff to Start timer
48         * Write 0xee00 0x00ee to Stop and re-load default count
49         * Read from this register returns current watch dog count
50         */
51         unsigned long   wdt_ctl;
52
53         /* Number of 40-MHz ticks for WD Reset pulse to last */
54         unsigned long   wdt_rst_count;
55 } bcm963xx_timer;
56
57 static struct bcm963xx_wdt_device {
58         struct completion stop;
59         volatile int running;
60         struct timer_list timer;
61         volatile int queue;
62         int default_ticks;
63         unsigned long inuse;
64 } bcm963xx_wdt_device;
65
66 static int ticks = 1000;
67
68 #define WDT_BASE        0xfffe0200
69 #define WDT             ((volatile bcm963xx_timer * const) WDT_BASE)
70
71 #define BCM963XX_INTERVAL        (HZ/10+1)
72
73 static void bcm963xx_wdt_trigger(unsigned long unused)
74 {
75         if (bcm963xx_wdt_device.running)
76                 ticks--;
77
78         /* Load the default ticking value into the reset counter register */    
79         WDT->wdt_rst_count = bcm963xx_wdt_device.default_ticks;
80         
81         if (bcm963xx_wdt_device.queue && ticks) {
82                 bcm963xx_wdt_device.timer.expires = jiffies + BCM963XX_INTERVAL;
83                 add_timer(&bcm963xx_wdt_device.timer);
84         }
85         else {
86                 complete(&bcm963xx_wdt_device.stop);
87         }
88 }
89
90 static void bcm963xx_wdt_reset(void)
91 {
92         ticks = bcm963xx_wdt_device.default_ticks;
93 }
94
95 static void bcm963xx_wdt_start(void)
96 {
97         if (!bcm963xx_wdt_device.queue) {
98                 bcm963xx_wdt_device.queue;
99                 /* Enable the watchdog by writing 0xff00 ,then 0x00ff to the control register */
100                 WDT->wdt_ctl = 0xff00;
101                 WDT->wdt_ctl = 0x00ff;
102                 bcm963xx_wdt_device.timer.expires = jiffies + BCM963XX_INTERVAL;
103                 add_timer(&bcm963xx_wdt_device.timer);
104         }
105         bcm963xx_wdt_device.running++;
106 }
107
108 static int bcm963xx_wdt_stop(void)
109 {
110         if (bcm963xx_wdt_device.running)
111                 bcm963xx_wdt_device.running = 0;
112         
113         ticks = bcm963xx_wdt_device.default_ticks;
114
115         /* Stop the watchdog by writing 0xee00 then 0x00ee to the control register */
116         WDT->wdt_ctl = 0xee00;
117         WDT->wdt_ctl = 0x00ee;
118
119         return -EIO;
120 }
121
122 static int bcm963xx_wdt_open(struct inode *inode, struct file *file)
123 {
124         if (test_and_set_bit(0, &bcm963xx_wdt_device.inuse))
125                 return -EBUSY;
126         return nonseekable_open(inode, file);
127 }
128
129 static int bcm963xx_wdt_release(struct inode *inode, struct file *file)
130 {
131         clear_bit(0, &bcm963xx_wdt_device.inuse);
132         return 0;
133 }
134
135 static int bcm963xx_wdt_ioctl(struct inode *inode, struct file *file,
136                                 unsigned int cmd, unsigned long arg)
137 {
138         void __user *argp = (void __user *)arg;
139         unsigned int value;
140
141         static struct watchdog_info ident = {
142                 .options = WDIOF_CARDRESET,
143                 .identity = "BCM963xx WDT",
144         };
145
146         switch (cmd) {
147                 case WDIOC_KEEPALIVE:
148                         bcm963xx_wdt_reset();
149                         break;
150                 case WDIOC_GETSTATUS:
151                         /* Reading from the control register will return the current value */
152                         value = WDT->wdt_ctl;
153                         if ( copy_to_user(argp, &value, sizeof(int)) )
154                                 return -EFAULT;
155                         break;
156                 case WDIOC_GETSUPPORT:
157                         if ( copy_to_user(argp, &ident, sizeof(ident)) )
158                                 return -EFAULT;
159                         break;
160                 case WDIOC_SETOPTIONS:
161                         if ( copy_from_user(&value, argp, sizeof(int)) )
162                                 return -EFAULT;
163                         switch(value) {
164                                 case WDIOS_ENABLECARD:
165                                         bcm963xx_wdt_start();
166                                         break;
167                                 case WDIOS_DISABLECARD:
168                                         bcm963xx_wdt_stop();
169                                         break;
170                                 default:
171                                         return -EINVAL;
172                         }
173                         break;
174                 default:
175                         return -ENOTTY;
176         }
177         return 0;               
178 }
179
180 static int bcm963xx_wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
181 {
182         if (!count)
183                 return -EIO;
184         bcm963xx_wdt_reset();
185         return count;
186 }
187
188 static const struct file_operations bcm963xx_wdt_fops = {
189         .owner          = THIS_MODULE,
190         .llseek         = no_llseek,
191         .write          = bcm963xx_wdt_write,
192         .ioctl          = bcm963xx_wdt_ioctl,
193         .open           = bcm963xx_wdt_open,
194         .release        = bcm963xx_wdt_release,
195 };      
196
197 static struct miscdevice bcm963xx_wdt_miscdev = {
198         .minor  = WATCHDOG_MINOR,
199         .name   = "watchdog",
200         .fops   = &bcm963xx_wdt_fops,
201 };
202
203 static void __exit bcm963xx_wdt_exit(void)
204 {
205         if (bcm963xx_wdt_device.queue ){
206                 bcm963xx_wdt_device.queue = 0;
207                 wait_for_completion(&bcm963xx_wdt_device.stop);
208         }       
209         misc_deregister(&bcm963xx_wdt_miscdev);
210 }
211
212 static int __init bcm963xx_wdt_init(void)
213 {
214         int ret = 0;
215         
216         printk("Broadcom BCM963xx Watchdog timer\n");
217
218         ret = misc_register(&bcm963xx_wdt_miscdev);
219         if (ret) {
220                 printk(KERN_CRIT "Cannot register miscdev on minor=%d (err=%d)\n", WATCHDOG_MINOR, ret);
221                 return ret;
222         }
223         init_completion(&bcm963xx_wdt_device.stop);
224         bcm963xx_wdt_device.queue = 0;
225         
226         clear_bit(0, &bcm963xx_wdt_device.inuse);
227         
228         init_timer(&bcm963xx_wdt_device.timer);
229         bcm963xx_wdt_device.timer.function = bcm963xx_wdt_trigger;
230         bcm963xx_wdt_device.timer.data = 0;
231
232         bcm963xx_wdt_device.default_ticks = ticks;      
233         return ret;
234 }
235
236         
237 module_init(bcm963xx_wdt_init);
238 module_exit(bcm963xx_wdt_exit);
239
240 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
241 MODULE_DESCRIPTION("Broadcom BCM963xx Watchdog driver");
242 MODULE_LICENSE("GPL");