This patch lets netconfig support the asus wl-500w. Signed-off-by: Ben Pfountz <netpr...
[librecmc/librecmc.git] / target / linux / ifxmips / files / drivers / char / watchdog / ifxmips_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 (C) 2006 infineon
17  *   Copyright (C) 2007 John Crispin <blogic@openwrt.org> 
18  *
19  */
20
21 #include <asm/uaccess.h>
22 #include <linux/errno.h>
23 #include <linux/proc_fs.h>
24 #include <linux/ioctl.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/watchdog.h>
28 #include <linux/miscdevice.h>
29 #include <asm-mips/ifxmips/ifxmips_wdt.h>
30 #include <asm-mips/ifxmips/ifxmips.h>
31
32 #define DRVNAME                 "ifxmips_wdt"
33
34 // TODO remove magic numbers and weirdo macros
35 extern unsigned int ifxmips_get_fpi_hz (void);
36
37 static int ifxmips_wdt_inuse = 0;
38 static int ifxmips_wdt_major = 0;
39
40 int
41 ifxmips_wdt_enable (unsigned int timeout)
42 {
43         unsigned int wdt_cr = 0;
44         unsigned int wdt_reload = 0;
45         unsigned int wdt_clkdiv, wdt_pwl, ffpi;
46         int retval = 0;
47
48         /* clock divider & prewarning limit */
49         wdt_clkdiv = 1 << (7 * IFXMIPS_BIU_WDT_CR_CLKDIV_GET(readl(IFXMIPS_BIU_WDT_CR)));
50         wdt_pwl = 0x8000 >> IFXMIPS_BIU_WDT_CR_PWL_GET(readl(IFXMIPS_BIU_WDT_CR));
51
52         //TODO
53         printk("WARNING FUNCTION CALL MISSING!!!");
54         //ffpi = cgu_get_io_region_clock();
55         printk("cpu clock = %d\n", ffpi);
56
57         /* caculate reload value */
58         wdt_reload = (timeout * (ffpi / wdt_clkdiv)) + wdt_pwl;
59
60         printk(KERN_WARNING DRVNAME ": wdt_pwl=0x%x, wdt_clkdiv=%d, ffpi=%d, wdt_reload = 0x%x\n",
61                 wdt_pwl, wdt_clkdiv, ffpi, wdt_reload);
62
63         if (wdt_reload > 0xFFFF)
64         {
65                 printk(KERN_WARNING DRVNAME ": timeout too large %d\n", timeout);
66                 retval = -EINVAL;
67                 goto out;
68         }
69
70         /* Write first part of password access */
71         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
72
73         wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
74         wdt_cr &= (!IFXMIPS_BIU_WDT_CR_PW_SET(0xff) &
75                    !IFXMIPS_BIU_WDT_CR_PWL_SET(0x3) &
76                    !IFXMIPS_BIU_WDT_CR_CLKDIV_SET(0x3) &
77                    !IFXMIPS_BIU_WDT_CR_RELOAD_SET(0xffff));
78
79         wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) |
80                    IFXMIPS_BIU_WDT_CR_PWL_SET(IFXMIPS_BIU_WDT_CR_PWL_GET(readl(IFXMIPS_BIU_WDT_CR))) |
81                    IFXMIPS_BIU_WDT_CR_CLKDIV_SET(IFXMIPS_BIU_WDT_CR_CLKDIV_GET(readl(IFXMIPS_BIU_WDT_CR))) |
82                    IFXMIPS_BIU_WDT_CR_RELOAD_SET(wdt_reload) |
83                    IFXMIPS_BIU_WDT_CR_GEN);
84
85         writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
86
87         printk("watchdog enabled\n");
88
89 out:
90         return retval;
91 }
92
93 void
94 ifxmips_wdt_disable (void)
95 {
96         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
97         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2), IFXMIPS_BIU_WDT_CR);
98
99         printk("watchdog disabled\n");
100 }
101
102 /* passed LPEN or DSEN */
103 void
104 ifxmips_wdt_enable_feature (int en, int type)
105 {
106         unsigned int wdt_cr = 0;
107
108         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
109
110         wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
111
112         if (en)
113         {
114                 wdt_cr &= (~IFXMIPS_BIU_WDT_CR_PW_SET(0xff));
115                 wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | type);
116         } else {
117                 wdt_cr &= (~IFXMIPS_BIU_WDT_CR_PW_SET(0xff) & ~type);
118                 wdt_cr |= IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2);
119         }
120
121         writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
122 }
123
124 void
125 ifxmips_wdt_prewarning_limit (int pwl)
126 {
127         unsigned int wdt_cr = 0;
128
129         wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
130         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
131
132         wdt_cr &= 0xf300ffff;
133         wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | IFXMIPS_BIU_WDT_CR_PWL_SET(pwl));
134
135         /* Set reload value in second password access */
136         writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
137 }
138
139 void
140 ifxmips_wdt_set_clkdiv (int clkdiv)
141 {
142         unsigned int wdt_cr = 0;
143
144         wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
145         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
146
147         wdt_cr &= 0xfc00ffff;
148         wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | IFXMIPS_BIU_WDT_CR_CLKDIV_SET(clkdiv));
149
150         /* Set reload value in second password access */
151         writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
152 }
153
154 static int
155 ifxmips_wdt_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
156            unsigned long arg)
157 {
158         int result = 0;
159         static int timeout = -1;
160         unsigned int user_arg;
161
162         if ((cmd != IFXMIPS_WDT_IOC_STOP) && (cmd != IFXMIPS_WDT_IOC_PING) && (cmd != IFXMIPS_WDT_IOC_GET_STATUS))
163         {
164                 if (copy_from_user((void *) &user_arg, (void *) arg, sizeof (int))){
165                         result = -EINVAL;
166                         goto out;
167                 }
168         }
169
170         switch (cmd)
171         {
172         case IFXMIPS_WDT_IOC_START:
173                 if ((result = ifxmips_wdt_enable(user_arg)) < 0)
174                         timeout = -1;
175                 else
176                         timeout = user_arg;
177                 break;
178
179         case IFXMIPS_WDT_IOC_STOP:
180                 printk(KERN_INFO DRVNAME ": disable watch dog timer\n");
181                 ifxmips_wdt_disable();
182                 break;
183
184         case IFXMIPS_WDT_IOC_PING:
185                 if (timeout < 0)
186                         result = -EIO;
187                 else
188                         result = ifxmips_wdt_enable(timeout);
189                 break;
190
191         case IFXMIPS_WDT_IOC_GET_STATUS:
192                 user_arg = readl(IFXMIPS_BIU_WDT_SR);
193                 copy_to_user((int*)arg, (int*)&user_arg, sizeof(int));
194                 break;
195
196         case IFXMIPS_WDT_IOC_SET_PWL:
197                 ifxmips_wdt_prewarning_limit(user_arg);
198                 break;
199
200         case IFXMIPS_WDT_IOC_SET_DSEN:
201                 ifxmips_wdt_enable_feature(user_arg, IFXMIPS_BIU_WDT_CR_DSEN);
202                 break;
203
204         case IFXMIPS_WDT_IOC_SET_LPEN:
205                 ifxmips_wdt_enable_feature(user_arg, IFXMIPS_BIU_WDT_CR_LPEN);
206                 break;
207
208         case IFXMIPS_WDT_IOC_SET_CLKDIV:
209                 ifxmips_wdt_set_clkdiv(user_arg);
210                 break;
211
212         default:
213                 printk(KERN_WARNING DRVNAME ": unknown watchdog iotcl\n");
214         }
215
216 out:
217         return result;
218 }
219
220 static int
221 ifxmips_wdt_open (struct inode *inode, struct file *file)
222 {
223         if (ifxmips_wdt_inuse)
224                 return -EBUSY;
225
226         ifxmips_wdt_inuse = 1;
227
228         return 0;
229 }
230
231 static int
232 ifxmips_wdt_release (struct inode *inode, struct file *file)
233 {
234         ifxmips_wdt_inuse = 0;
235
236         return 0;
237 }
238
239 int
240 ifxmips_wdt_register_proc_read (char *buf, char **start, off_t offset, int count,
241                         int *eof, void *data)
242 {
243         int len = 0;
244
245         len += sprintf (buf + len, "IFXMIPS_BIU_WDT_PROC_READ\n");
246         len += sprintf (buf + len, "IFXMIPS_BIU_WDT_CR(0x%08x)  : 0x%08x\n",
247                         (unsigned int)IFXMIPS_BIU_WDT_CR, readl(IFXMIPS_BIU_WDT_CR));
248         len += sprintf (buf + len, "IFXMIPS_BIU_WDT_SR(0x%08x)  : 0x%08x\n",
249                         (unsigned int)IFXMIPS_BIU_WDT_SR, readl(IFXMIPS_BIU_WDT_SR));
250
251         *eof = 1;
252
253         return len;
254 }
255
256 static const struct file_operations ifxmips_wdt_fops = {
257         .owner          = THIS_MODULE,
258         .llseek         = no_llseek,
259         .ioctl          = ifxmips_wdt_ioctl,
260         .open           = ifxmips_wdt_open,
261         .release        = ifxmips_wdt_release,
262 //      .write          = at91_wdt_write,
263 };
264
265 static struct miscdevice ifxmips_wdt_miscdev = {
266         .minor          = WATCHDOG_MINOR,
267         .name           = "ifxmips_wdt",
268         .fops           = &ifxmips_wdt_fops,
269 };
270
271
272 static int
273 ifxmips_wdt_probe (struct platform_device *pdev)
274 {
275         int ret = misc_register(&ifxmips_wdt_miscdev);
276         if (ret)
277                 return ret;
278
279         create_proc_read_entry(DRVNAME, 0, NULL, ifxmips_wdt_register_proc_read, NULL);
280
281         printk(KERN_INFO DRVNAME ": ifxmips watchdog loaded\n");
282
283         return 0;
284 }
285
286 static int
287 ifxmips_wdt_remove (struct platform_device *pdev)
288 {
289         misc_deregister(&ifxmips_wdt_miscdev);
290         remove_proc_entry(DRVNAME, NULL);
291         return 0;
292 }
293
294 static struct
295 platform_driver ifxmips_wdt_driver = {
296         .probe = ifxmips_wdt_probe,
297         .remove = ifxmips_wdt_remove,
298         .driver = {
299                 .name = DRVNAME,
300                 .owner = THIS_MODULE,
301         },
302 };
303
304 int __init
305 ifxmips_wdt_init_module (void)
306 {
307         int ret = platform_driver_register(&ifxmips_wdt_driver);
308         if (ret)
309                 printk(KERN_INFO DRVNAME ": Error registering platfom driver!");
310         return ret;
311 }
312
313 void
314 ifxmips_wdt_cleanup_module (void)
315 {
316         platform_driver_unregister(&ifxmips_wdt_driver);
317 }
318
319 module_init(ifxmips_wdt_init_module);
320 module_exit(ifxmips_wdt_cleanup_module);
321
322 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
323 MODULE_DESCRIPTION("Watchdog driver for infineon ifxmips family");
324 MODULE_LICENSE("GPL");
325 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);