6e1eb799a3a118dedcb51176a259df352f4fd557
[librecmc/librecmc.git] / target / linux / amazon / files / drivers / watchdog / amazon_wdt.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  * 
16  * Copyright 2004 Wu Qi Ming <gokimi@msn.com>
17  * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
18  */
19
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/sched.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/timer.h>
27 #include <linux/fs.h>
28 #include <linux/errno.h>
29 #include <linux/proc_fs.h>
30 #include <linux/stat.h>
31 #include <linux/tty.h>
32 #include <linux/selection.h>
33 #include <linux/kmod.h>
34 #include <linux/vmalloc.h>
35 #include <linux/kdev_t.h>
36 #include <linux/ioctl.h>
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
39 #include <linux/platform_device.h>
40 #include <asm/amazon/amazon.h>
41 #include <asm/amazon/amazon_wdt.h>
42
43 #define DRV_NAME "AMAZON WDT:"
44
45 #undef AMAZON_WDT_DEBUG 
46
47 extern unsigned int amazon_get_fpi_hz(void);
48 static int amazon_wdt_isopen = 0;
49
50 #ifdef AMAZON_WDT_DEBUG
51 static struct proc_dir_entry* amazon_wdt_dir;
52 #endif
53
54 int wdt_enable(int timeout)
55 {
56         u32 hard_psw, ffpi;
57         int reload_value, divider = 1;
58   
59         ffpi = amazon_get_fpi_hz();
60
61         reload_value = 65536 - timeout * ffpi / 256;
62         
63         if (reload_value < 0) {
64                 divider = 0;
65                 reload_value = 65536 - timeout * ffpi / 16384;
66         }
67
68         if (reload_value < 0){
69                 printk(KERN_INFO DRV_NAME "timeout too large %d\n", timeout);
70                 return -EINVAL;
71         }
72         
73         printk(KERN_INFO DRV_NAME "timeout:%d reload_value: %8x\n", timeout, reload_value);
74         
75         hard_psw = (amazon_readl(AMAZON_WDT_CON0) & 0xffffff01) +
76                                 (amazon_readl(AMAZON_WDT_CON1) & 0xc) + 0xf0;  
77         amazon_writel(hard_psw, AMAZON_WDT_CON0);
78         wmb();
79         
80         amazon_writel((hard_psw & 0xff00) + (reload_value << 16) + 0xf2, AMAZON_WDT_CON0);
81         wmb();
82
83         amazon_writel(divider << 2, AMAZON_WDT_CON1);  
84         wmb();
85         
86         hard_psw = (amazon_readl(AMAZON_WDT_CON0) & 0xffffff01) +
87                                 (amazon_readl(AMAZON_WDT_CON1) & 0xc) + 0xf0;
88         amazon_writel(hard_psw, AMAZON_WDT_CON0);
89         wmb();
90         
91         amazon_writel_masked(AMAZON_WDT_CON0, 0xff, 0xf3);
92         wmb();
93         return 0;
94 }
95
96 void wdt_disable(void)
97 {
98         u32 hard_psw = 0;
99
100         hard_psw = (amazon_readl(AMAZON_WDT_CON0) & 0xffffff01) + 
101                         (amazon_readl(AMAZON_WDT_CON1) & 0xc) + 0xf0;  
102         amazon_writel(hard_psw, AMAZON_WDT_CON0);
103         wmb();
104
105         amazon_writel_masked(AMAZON_WDT_CON0, 0xff, 0xf2);
106         wmb();
107
108         amazon_writel_masked(AMAZON_WDT_CON1, 0x8, 0x8);  
109         wmb();
110
111         hard_psw=(amazon_readl(AMAZON_WDT_CON0) & 0xffffff01) +
112                         (amazon_readl(AMAZON_WDT_CON1) & 0xc) + 0xf0;  
113         amazon_writel(hard_psw, AMAZON_WDT_CON0);
114         wmb();
115
116         amazon_writel_masked(AMAZON_WDT_CON0, 0xff, 0xf3);
117         wmb();
118
119         return;
120 }
121
122 static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
123 {
124         int result=0;
125         static int timeout=-1;
126
127         switch(cmd){
128                 case AMAZON_WDT_IOC_START:
129                         printk(KERN_INFO DRV_NAME "enable watch dog timer!\n");
130                         if (copy_from_user((void*)&timeout, (void*)arg, sizeof (int))) {
131                                 printk(KERN_INFO DRV_NAME "invalid argument\n");
132                                 result=-EINVAL;
133                         } else if ((result = wdt_enable(timeout)) < 0) {
134                                 timeout = -1;
135                         }
136                         break;
137                 
138                 case AMAZON_WDT_IOC_STOP:
139                         printk(KERN_INFO DRV_NAME "disable watch dog timer\n");
140                         timeout = -1;
141                         wdt_disable();
142                         break;
143                 
144                 case AMAZON_WDT_IOC_PING:
145                         if (timeout < 0) {
146                                 result = -EIO;
147                         } else {
148                                 result = wdt_enable(timeout); 
149                         }               
150                         break;
151                 
152                 default:
153                         result=-EINVAL;
154                         break;
155         }
156         return result;
157 }
158
159 static ssize_t wdt_read(struct file *file, char *buf, size_t count, loff_t *offset)
160 {
161         return 0;
162 }
163
164 static ssize_t wdt_write(struct file *file, const char *buf, size_t count, loff_t *offset)
165 {       
166         return count;
167 }
168
169 static int wdt_open(struct inode *inode, struct file *file)
170 {
171         if (amazon_wdt_isopen == 1)
172                 return -EBUSY;
173
174         amazon_wdt_isopen = 1;
175         printk(KERN_INFO DRV_NAME "opened\n");  
176         return 0;
177 }
178
179 static int wdt_release(struct inode *inode, struct file *file)
180 {
181         amazon_wdt_isopen = 0;
182         printk(KERN_INFO DRV_NAME "closed\n");  
183         return 0;
184 }
185
186 #ifdef AMAZON_WDT_DEBUG
187 int wdt_register_proc_read(char *buf, char **start, off_t offset,
188                          int count, int *eof, void *data)
189 {
190         int len=0;
191         len+=sprintf(buf+len,"NMISR:    0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_NMISR));
192         len+=sprintf(buf+len,"RST_REQ:  0x%08x\n",AMAZON_WDT_REG32(AMAZON_RST_REQ));
193         len+=sprintf(buf+len,"RST_SR:   0x%08x\n",AMAZON_WDT_REG32(AMAZON_RST_SR));
194         len+=sprintf(buf+len,"WDT_CON0: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_CON0));
195         len+=sprintf(buf+len,"WDT_CON1: 0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_CON1));
196         len+=sprintf(buf+len,"WDT_SR:   0x%08x\n",AMAZON_WDT_REG32(AMAZON_WDT_SR));
197         *eof = 1;
198         return len;
199 }
200 #endif
201
202 static struct file_operations wdt_fops = {
203         read:           wdt_read,
204         write:          wdt_write,
205         ioctl:          wdt_ioctl,
206         open:           wdt_open,
207         release:        wdt_release,    
208 };
209
210 static int __init amazon_wdt_probe(struct platform_device *dev)
211 {
212         int result = result = register_chrdev(0, "watchdog", &wdt_fops);
213         
214         if (result < 0) {
215         printk(KERN_INFO DRV_NAME "cannot register device\n");
216         return result;
217         }
218
219 #ifdef AMAZON_WDT_DEBUG
220         amazon_wdt_dir=proc_mkdir("amazon_wdt",NULL);
221         create_proc_read_entry("wdt_register", 0, amazon_wdt_dir,
222                                 wdt_register_proc_read, NULL);  
223 #endif
224
225         amazon_wdt_isopen=0;
226         printk(KERN_INFO DRV_NAME "driver loaded but inactive\n");
227         return 0;
228 }
229
230 static int amazon_wdt_remove(struct platform_device *dev)
231 {
232         unregister_chrdev(0, "watchdog");
233 #ifdef AMAZON_WDT_DEBUG
234         remove_proc_entry("wdt_register", amazon_wdt_dir);
235         remove_proc_entry("amazon_wdt", NULL);
236 #endif
237         printk(KERN_INFO DRV_NAME "unregistered\n");
238         return 0;
239 }
240
241 static struct platform_driver amazon_wdt_driver = {
242         .probe = amazon_wdt_probe,
243         .remove = amazon_wdt_remove,
244         .driver = {
245                 .name = "amazon_wdt",
246                 .owner = THIS_MODULE,
247         },
248 };
249
250 static int __init amazon_wdt_init(void)
251 {
252         int ret = platform_driver_register(&amazon_wdt_driver);
253         if (ret)
254                 printk(KERN_WARNING "amazon_wdt: error registering platfom driver!\n");
255         return ret;
256 }
257
258 static void __exit amazon_wdt_exit(void)
259 {
260         platform_driver_unregister(&amazon_wdt_driver);
261 }
262
263 module_init(amazon_wdt_init);
264 module_exit(amazon_wdt_exit);
265
266 MODULE_LICENSE ("GPL");
267 MODULE_AUTHOR("Infineon / John Crispin <blogic@openwrt.org>");
268 MODULE_DESCRIPTION("AMAZON WDT driver");
269