danube to ifxmips transition
[openwrt.git] / target / linux / ifxmips / files / drivers / char / watchdog / danube_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 <asm-mips/ifxmips/ifxmips_wdt.h>
27 #include <asm-mips/ifxmips/ifxmips.h>
28
29
30 // TODO remove magic numbers and weirdo macros
31
32 extern unsigned int ifxmips_get_fpi_hz (void);
33
34 static int ifxmips_wdt_inuse = 0;
35 static int ifxmips_wdt_major = 0;
36
37 int
38 ifxmips_wdt_enable (unsigned int timeout)
39 {
40         unsigned int wdt_cr = 0;
41         unsigned int wdt_reload = 0;
42         unsigned int wdt_clkdiv, wdt_pwl, ffpi;
43         int retval = 0;
44
45         /* clock divider & prewarning limit */
46         wdt_clkdiv = 1 << (7 * IFXMIPS_BIU_WDT_CR_CLKDIV_GET(readl(IFXMIPS_BIU_WDT_CR)));
47         wdt_pwl = 0x8000 >> IFXMIPS_BIU_WDT_CR_PWL_GET(readl(IFXMIPS_BIU_WDT_CR));
48
49         //TODO
50         printk("WARNING FUNCTION CALL MISSING!!!");
51         //ffpi = cgu_get_io_region_clock();
52         printk("cpu clock = %d\n", ffpi);
53
54         /* caculate reload value */
55         wdt_reload = (timeout * (ffpi / wdt_clkdiv)) + wdt_pwl;
56
57         printk("wdt_pwl=0x%x, wdt_clkdiv=%d, ffpi=%d, wdt_reload = 0x%x\n",
58                 wdt_pwl, wdt_clkdiv, ffpi, wdt_reload);
59
60         if (wdt_reload > 0xFFFF)
61         {
62                 printk ("timeout too large %d\n", timeout);
63                 retval = -EINVAL;
64                 goto out;
65         }
66
67         /* Write first part of password access */
68         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
69
70         wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
71         wdt_cr &= (!IFXMIPS_BIU_WDT_CR_PW_SET(0xff) &
72                    !IFXMIPS_BIU_WDT_CR_PWL_SET(0x3) &
73                    !IFXMIPS_BIU_WDT_CR_CLKDIV_SET(0x3) &
74                    !IFXMIPS_BIU_WDT_CR_RELOAD_SET(0xffff));
75
76         wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) |
77                    IFXMIPS_BIU_WDT_CR_PWL_SET(IFXMIPS_BIU_WDT_CR_PWL_GET(readl(IFXMIPS_BIU_WDT_CR))) |
78                    IFXMIPS_BIU_WDT_CR_CLKDIV_SET(IFXMIPS_BIU_WDT_CR_CLKDIV_GET(readl(IFXMIPS_BIU_WDT_CR))) |
79                    IFXMIPS_BIU_WDT_CR_RELOAD_SET(wdt_reload) |
80                    IFXMIPS_BIU_WDT_CR_GEN);
81
82         writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
83
84         printk("watchdog enabled\n");
85
86 out:
87         return retval;
88 }
89
90 void
91 ifxmips_wdt_disable (void)
92 {
93         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
94         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2), IFXMIPS_BIU_WDT_CR);
95
96         printk("watchdog disabled\n");
97 }
98
99 /* passed LPEN or DSEN */
100 void
101 ifxmips_wdt_enable_feature (int en, int type)
102 {
103         unsigned int wdt_cr = 0;
104
105         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
106
107         wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
108
109         if (en)
110         {
111                 wdt_cr &= (~IFXMIPS_BIU_WDT_CR_PW_SET(0xff));
112                 wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | type);
113         } else {
114                 wdt_cr &= (~IFXMIPS_BIU_WDT_CR_PW_SET(0xff) & ~type);
115                 wdt_cr |= IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2);
116         }
117
118         writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
119 }
120
121 void
122 ifxmips_wdt_prewarning_limit (int pwl)
123 {
124         unsigned int wdt_cr = 0;
125
126         wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
127         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
128
129         wdt_cr &= 0xf300ffff;
130         wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | IFXMIPS_BIU_WDT_CR_PWL_SET(pwl));
131
132         /* Set reload value in second password access */
133         writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
134 }
135
136 void
137 ifxmips_wdt_set_clkdiv (int clkdiv)
138 {
139         unsigned int wdt_cr = 0;
140
141         wdt_cr = readl(IFXMIPS_BIU_WDT_CR);
142         writel(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
143
144         wdt_cr &= 0xfc00ffff;
145         wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | IFXMIPS_BIU_WDT_CR_CLKDIV_SET(clkdiv));
146
147         /* Set reload value in second password access */
148         writel(wdt_cr, IFXMIPS_BIU_WDT_CR);
149 }
150
151 static int
152 ifxmips_wdt_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
153            unsigned long arg)
154 {
155         int result = 0;
156         static int timeout = -1;
157         unsigned int user_arg;
158
159         if ((cmd != IFXMIPS_WDT_IOC_STOP) && (cmd != IFXMIPS_WDT_IOC_PING) && (cmd != IFXMIPS_WDT_IOC_GET_STATUS))
160         {
161                 if (copy_from_user((void *) &user_arg, (void *) arg, sizeof (int))){
162                         result = -EINVAL;
163                         goto out;
164                 }
165         }
166
167         switch (cmd)
168         {
169         case IFXMIPS_WDT_IOC_START:
170                 if ((result = ifxmips_wdt_enable(user_arg)) < 0)
171                         timeout = -1;
172                 else
173                         timeout = user_arg;
174                 break;
175
176         case IFXMIPS_WDT_IOC_STOP:
177                 printk("disable watch dog timer\n");
178                 ifxmips_wdt_disable();
179                 break;
180
181         case IFXMIPS_WDT_IOC_PING:
182                 if (timeout < 0)
183                         result = -EIO;
184                 else
185                         result = ifxmips_wdt_enable(timeout);
186                 break;
187
188         case IFXMIPS_WDT_IOC_GET_STATUS:
189                 user_arg = readl(IFXMIPS_BIU_WDT_SR);
190                 copy_to_user((int*)arg, (int*)&user_arg, sizeof(int));
191                 break;
192
193         case IFXMIPS_WDT_IOC_SET_PWL:
194                 ifxmips_wdt_prewarning_limit(user_arg);
195                 break;
196
197         case IFXMIPS_WDT_IOC_SET_DSEN:
198                 ifxmips_wdt_enable_feature(user_arg, IFXMIPS_BIU_WDT_CR_DSEN);
199                 break;
200
201         case IFXMIPS_WDT_IOC_SET_LPEN:
202                 ifxmips_wdt_enable_feature(user_arg, IFXMIPS_BIU_WDT_CR_LPEN);
203                 break;
204
205         case IFXMIPS_WDT_IOC_SET_CLKDIV:
206                 ifxmips_wdt_set_clkdiv(user_arg);
207                 break;
208
209         default:
210                 printk("unknown watchdog iotcl\n");
211         }
212
213 out:
214         return result;
215 }
216
217 static int
218 ifxmips_wdt_open (struct inode *inode, struct file *file)
219 {
220         if (ifxmips_wdt_inuse)
221                 return -EBUSY;
222
223         ifxmips_wdt_inuse = 1;
224
225         return 0;
226 }
227
228 static int
229 ifxmips_wdt_release (struct inode *inode, struct file *file)
230 {
231         ifxmips_wdt_inuse = 0;
232
233         return 0;
234 }
235
236 int
237 ifxmips_wdt_register_proc_read (char *buf, char **start, off_t offset, int count,
238                         int *eof, void *data)
239 {
240         int len = 0;
241
242         len += sprintf (buf + len, "IFXMIPS_BIU_WDT_PROC_READ\n");
243         len += sprintf (buf + len, "IFXMIPS_BIU_WDT_CR(0x%08x)  : 0x%08x\n",
244                         (unsigned int)IFXMIPS_BIU_WDT_CR, readl(IFXMIPS_BIU_WDT_CR));
245         len += sprintf (buf + len, "IFXMIPS_BIU_WDT_SR(0x%08x)  : 0x%08x\n",
246                         (unsigned int)IFXMIPS_BIU_WDT_SR, readl(IFXMIPS_BIU_WDT_SR));
247
248         *eof = 1;
249
250         return len;
251 }
252
253 static struct file_operations wdt_fops = {
254       .owner = THIS_MODULE,
255       .ioctl = ifxmips_wdt_ioctl,
256       .open = ifxmips_wdt_open,
257       .release = ifxmips_wdt_release,
258 };
259
260 int __init
261 ifxmips_wdt_init_module (void)
262 {
263         ifxmips_wdt_major = register_chrdev(0, "wdt", &wdt_fops);
264
265         if (ifxmips_wdt_major < 0)
266         {
267                 printk("cannot register watchdog device\n");
268
269                 return -EINVAL;
270         }
271
272         create_proc_read_entry("ifxmips_wdt", 0, NULL, ifxmips_wdt_register_proc_read, NULL);
273
274         printk("ifxmips watchdog loaded\n");
275
276         return 0;
277 }
278
279 void
280 ifxmips_wdt_cleanup_module (void)
281 {
282         unregister_chrdev(ifxmips_wdt_major, "wdt");
283         remove_proc_entry("ifxmips_wdt", NULL);
284 }
285
286 module_init(ifxmips_wdt_init_module);
287 module_exit(ifxmips_wdt_cleanup_module);