brcm63xx cleanups; add a watchdog driver, indent serial console driver, add support...
[openwrt.git] / target / linux / brcm63xx-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         /* Also reload default count */
94         WDT->wdt_def_count = ticks;
95         WDT->wdt_ctl = 0xee00;
96         WDT->wdt_ctl = 0x00ee;
97 }
98
99 static void bcm963xx_wdt_start(void)
100 {
101         if (!bcm963xx_wdt_device.queue) {
102                 bcm963xx_wdt_device.queue;
103                 /* Enable the watchdog by writing 0xff00 ,then 0x00ff to the control register */
104                 WDT->wdt_ctl = 0xff00;
105                 WDT->wdt_ctl = 0x00ff;
106                 bcm963xx_wdt_device.timer.expires = jiffies + BCM963XX_INTERVAL;
107                 add_timer(&bcm963xx_wdt_device.timer);
108         }
109         bcm963xx_wdt_device.running++;
110 }
111
112 static int bcm963xx_wdt_stop(void)
113 {
114         if (bcm963xx_wdt_device.running)
115                 bcm963xx_wdt_device.running = 0;
116         
117         ticks = bcm963xx_wdt_device.default_ticks;
118
119         /* Stop the watchdog by writing 0xee00 then 0x00ee to the control register */
120         WDT->wdt_ctl = 0xee00;
121         WDT->wdt_ctl = 0x00ee;
122
123         return -EIO;
124 }
125
126 static int bcm963xx_wdt_open(struct inode *inode, struct file *file)
127 {
128         if (test_and_set_bit(0, &bcm963xx_wdt_device.inuse))
129                 return -EBUSY;
130         return nonseekable_open(inode, file);
131 }
132
133 static int bcm963xx_wdt_release(struct inode *inode, struct file *file)
134 {
135         clear_bit(0, &bcm963xx_wdt_device.inuse);
136         return 0;
137 }
138
139 static int bcm963xx_wdt_ioctl(struct inode *inode, struct file *file,
140                                 unsigned int cmd, unsigned long arg)
141 {
142         void __user *argp = (void __user *)arg;
143         unsigned int value;
144
145         static struct watchdog_info ident = {
146                 .options = WDIOF_CARDRESET,
147                 .identity = "BCM963xx WDT",
148         };
149
150         switch (cmd) {
151                 case WDIOC_KEEPALIVE:
152                         bcm963xx_wdt_reset();
153                         break;
154                 case WDIOC_GETSTATUS:
155                         /* Reading from the control register will return the current value */
156                         value = WDT->wdt_ctl;
157                         if ( copy_to_user(argp, &value, sizeof(int)) )
158                                 return -EFAULT;
159                         break;
160                 case WDIOC_GETSUPPORT:
161                         if ( copy_to_user(argp, &ident, sizeof(ident)) )
162                                 return -EFAULT;
163                         break;
164                 case WDIOC_SETOPTIONS:
165                         if ( copy_from_user(&value, argp, sizeof(int)) )
166                                 return -EFAULT;
167                         switch(value) {
168                                 case WDIOS_ENABLECARD:
169                                         bcm963xx_wdt_start();
170                                         break;
171                                 case WDIOS_DISABLECARD:
172                                         bcm963xx_wdt_stop();
173                                         break;
174                                 default:
175                                         return -EINVAL;
176                         }
177                         break;
178                 default:
179                         return -ENOTTY;
180         }
181         return 0;               
182 }
183
184 static int bcm963xx_wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
185 {
186         if (!count)
187                 return -EIO;
188         bcm963xx_wdt_reset();
189         return count;
190 }
191
192 static const struct file_operations bcm963xx_wdt_fops = {
193         .owner          = THIS_MODULE,
194         .llseek         = no_llseek,
195         .write          = bcm963xx_wdt_write,
196         .ioctl          = bcm963xx_wdt_ioctl,
197         .open           = bcm963xx_wdt_open,
198         .release        = bcm963xx_wdt_release,
199 };      
200
201 static struct miscdevice bcm963xx_wdt_miscdev = {
202         .minor  = WATCHDOG_MINOR,
203         .name   = "watchdog",
204         .fops   = &bcm963xx_wdt_fops,
205 };
206
207 static void __exit bcm963xx_wdt_exit(void)
208 {
209         if (bcm963xx_wdt_device.queue ){
210                 bcm963xx_wdt_device.queue = 0;
211                 wait_for_completion(&bcm963xx_wdt_device.stop);
212         }       
213         misc_deregister(&bcm963xx_wdt_miscdev);
214 }
215
216 static int __init bcm963xx_wdt_init(void)
217 {
218         int ret = 0;
219         
220         printk("Broadcom BCM963xx Watchdog timer\n");
221
222         ret = misc_register(&bcm963xx_wdt_miscdev);
223         if (ret) {
224                 printk(KERN_CRIT "Cannot register miscdev on minor=%d (err=%d)\n", WATCHDOG_MINOR, ret);
225                 return ret;
226         }
227         init_completion(&bcm963xx_wdt_device.stop);
228         bcm963xx_wdt_device.queue = 0;
229         
230         clear_bit(0, &bcm963xx_wdt_device.inuse);
231         
232         init_timer(&bcm963xx_wdt_device.timer);
233         bcm963xx_wdt_device.timer.function = bcm963xx_wdt_trigger;
234         bcm963xx_wdt_device.timer.data = 0;
235
236         bcm963xx_wdt_device.default_ticks = ticks;      
237         return ret;
238 }
239
240         
241 module_init(bcm963xx_wdt_init);
242 module_exit(bcm963xx_wdt_exit);
243
244 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
245 MODULE_DESCRIPTION("Broadcom BCM963xx Watchdog driver");
246 MODULE_LICENSE("GPL");