initial merge of danube wdt code
[openwrt.git] / target / linux / danube / 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 <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/timer.h>
28 #include <linux/fs.h>
29 #include <linux/errno.h>
30 #include <linux/proc_fs.h>
31 #include <linux/stat.h>
32 #include <linux/tty.h>
33 #include <linux/selection.h>
34 #include <linux/kmod.h>
35 #include <linux/vmalloc.h>
36 #include <linux/kdev_t.h>
37 #include <linux/ioctl.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm-mips/danube/danube_wdt.h>
41 #include <asm-mips/danube/danube.h>
42
43 extern unsigned int danube_get_fpi_hz (void);
44
45 static int danube_wdt_inuse = 0;
46 static int danube_wdt_major = 0;
47
48 int
49 danube_wdt_enable (unsigned int timeout)
50 {
51 unsigned int wdt_cr = 0;
52 unsigned int wdt_reload = 0;
53 unsigned int wdt_clkdiv, clkdiv, wdt_pwl, pwl, ffpi;
54
55 /* clock divider & prewarning limit */
56 switch (clkdiv = DANUBE_BIU_WDT_CR_CLKDIV_GET(readl(DANUBE_BIU_WDT_CR)))
57 {
58 case 0:
59 wdt_clkdiv = 1;
60 break;
61 case 1:
62 wdt_clkdiv = 64;
63 break;
64 case 2:
65 wdt_clkdiv = 4096;
66 break;
67 case 3:
68 wdt_clkdiv = 262144;
69 break;
70 }
71
72 switch (pwl = DANUBE_BIU_WDT_CR_PWL_GET (readl(DANUBE_BIU_WDT_CR)))
73 {
74 case 0:
75 wdt_pwl = 0x8000;
76 break;
77 case 1:
78 wdt_pwl = 0x4000;
79 break;
80 case 2:
81 wdt_pwl = 0x2000;
82 break;
83 case 3:
84 wdt_pwl = 0x1000;
85 break;
86 }
87
88 //TODO
89 printk("WARNING FUNCTION CALL MISSING!!!");
90 //ffpi = cgu_get_io_region_clock();
91 printk("cpu clock = %d\n", ffpi);
92
93 /* caculate reload value */
94 wdt_reload = (timeout * (ffpi / wdt_clkdiv)) + wdt_pwl;
95
96 printk("wdt_pwl=0x%x, wdt_clkdiv=%d, ffpi=%d, wdt_reload = 0x%x\n",
97 wdt_pwl, wdt_clkdiv, ffpi, wdt_reload);
98
99 if (wdt_reload > 0xFFFF) {
100 printk ("timeout too large %d\n", timeout);
101 return -EINVAL;
102 }
103
104 /* Write first part of password access */
105 *DANUBE_BIU_WDT_CR = DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW1);
106
107 wdt_cr = *DANUBE_BIU_WDT_CR;
108 wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET (0xff) &
109 !DANUBE_BIU_WDT_CR_PWL_SET (0x3) &
110 !DANUBE_BIU_WDT_CR_CLKDIV_SET (0x3) &
111 !DANUBE_BIU_WDT_CR_RELOAD_SET (0xffff));
112
113 wdt_cr |= (DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
114 DANUBE_BIU_WDT_CR_PWL_SET (pwl) |
115 DANUBE_BIU_WDT_CR_CLKDIV_SET (clkdiv) |
116 DANUBE_BIU_WDT_CR_RELOAD_SET (wdt_reload) |
117 DANUBE_BIU_WDT_CR_GEN);
118
119 /* Set reload value in second password access */
120 *DANUBE_BIU_WDT_CR = wdt_cr;
121 printk ("enabled\n");
122 return 0;
123 }
124
125 void
126 danube_wdt_disable (void)
127 {
128 /* Write first part of password access */
129 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
130
131 /* Disable the watchdog in second password access (GEN=0) */
132 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2), DANUBE_BIU_WDT_CR);
133 }
134
135 void
136 danube_wdt_low_power (int en)
137 {
138 unsigned int wdt_cr = 0;
139
140 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
141
142 wdt_cr = readl(DANUBE_BIU_WDT_CR);
143
144 if (en)
145 {
146 wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET(0xff));
147 wdt_cr |=
148 (DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
149 DANUBE_BIU_WDT_CR_LPEN);
150 }
151 else {
152 wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET (0xff) &
153 !DANUBE_BIU_WDT_CR_LPEN);
154 wdt_cr |= DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2);
155 }
156
157 /* Set reload value in second password access */
158 writel(wdt_cr, DANUBE_BIU_WDT_CR);
159 }
160
161 void
162 danube_wdt_debug_suspend (int en)
163 {
164 unsigned int wdt_cr = 0;
165
166 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
167
168 wdt_cr = readl(DANUBE_BIU_WDT_CR);
169 if (en)
170 {
171 wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET (0xff));
172 wdt_cr |=
173 (DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
174 DANUBE_BIU_WDT_CR_DSEN);
175 }
176 else {
177 wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET (0xff) &
178 !DANUBE_BIU_WDT_CR_DSEN);
179 wdt_cr |= DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2);
180 }
181
182 /* Set reload value in second password access */
183 writel(wdt_cr, DANUBE_BIU_WDT_CR);
184 }
185
186 void
187 danube_wdt_prewarning_limit (int pwl)
188 {
189 unsigned int wdt_cr = 0;
190
191 wdt_cr = readl(DANUBE_BIU_WDT_CR);
192 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
193
194 wdt_cr &= 0xff00ffff; //(!DANUBE_BIU_WDT_CR_PW_SET(0xff));
195 wdt_cr &= 0xf3ffffff; //(!DANUBE_BIU_WDT_CR_PWL_SET(3));
196 wdt_cr |= (DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
197 DANUBE_BIU_WDT_CR_PWL_SET (pwl));
198
199 /* Set reload value in second password access */
200 writel(wdt_cr, DANUBE_BIU_WDT_CR);
201 }
202
203 void
204 danube_wdt_set_clkdiv (int clkdiv)
205 {
206 unsigned int wdt_cr = 0;
207
208 wdt_cr = readl(DANUBE_BIU_WDT_CR);
209 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
210
211 wdt_cr &= 0xff00ffff; //(!DANUBE_BIU_WDT_CR_PW_SET(0xff));
212 wdt_cr &= 0xfcffffff; //(!DANUBE_BIU_WDT_CR_CLKDIV_SET(3));
213 wdt_cr |= (DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
214 DANUBE_BIU_WDT_CR_CLKDIV_SET (clkdiv));
215
216 /* Set reload value in second password access */
217 writel(wdt_cr, DANUBE_BIU_WDT_CR);
218 }
219
220 static int
221 danube_wdt_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
222 unsigned long arg)
223 {
224 int result = 0;
225 int en = 0;
226 int istatus;
227 int pwl, clkdiv;
228 static int timeout = -1;
229
230 switch (cmd)
231 {
232 case DANUBE_WDT_IOC_START:
233 printk("enable watch dog timer!\n");
234 if (copy_from_user((void *) &timeout, (void *) arg, sizeof (int)))
235 {
236 printk ("invalid argument\n");
237 result = -EINVAL;
238 } else {
239 if ((result = danube_wdt_enable (timeout)) < 0)
240 timeout = -1;
241 }
242 break;
243
244 case DANUBE_WDT_IOC_STOP:
245 printk("disable watch dog timer\n");
246 timeout = -1;
247 danube_wdt_disable();
248 break;
249
250 case DANUBE_WDT_IOC_PING:
251 if (timeout < 0)
252 result = -EIO;
253 else
254 result = danube_wdt_enable(timeout);
255 break;
256
257 case DANUBE_WDT_IOC_SET_PWL:
258 if (copy_from_user((void *) &pwl, (void *) arg, sizeof (int)))
259 result = -EINVAL;
260 danube_wdt_prewarning_limit(pwl);
261 break;
262
263 case DANUBE_WDT_IOC_SET_DSEN:
264 if (copy_from_user((void *) &en, (void *) arg, sizeof (int)))
265 result = -EINVAL;
266 danube_wdt_debug_suspend (en);
267 break;
268
269 case DANUBE_WDT_IOC_SET_LPEN:
270 if (copy_from_user((void *) &en, (void *) arg, sizeof (int)))
271 result = -EINVAL;
272 danube_wdt_low_power(en);
273 break;
274
275 case DANUBE_WDT_IOC_SET_CLKDIV:
276 if (copy_from_user((void *) &clkdiv, (void *) arg, sizeof (int)))
277 result = -EINVAL;
278 danube_wdt_set_clkdiv (clkdiv);
279 break;
280
281 case DANUBE_WDT_IOC_GET_STATUS:
282 istatus = readl(DANUBE_BIU_WDT_SR);
283 copy_to_user((int *) arg, (int *) &istatus, sizeof (int));
284 break;
285 }
286
287 return result;
288 }
289
290 static int
291 danube_wdt_open (struct inode *inode, struct file *file)
292 {
293 if (danube_wdt_inuse)
294 return -EBUSY;
295 danube_wdt_inuse = 1;
296
297 return 0;
298 }
299
300 static int
301 danube_wdt_release (struct inode *inode, struct file *file)
302 {
303 danube_wdt_inuse = 0;
304
305 return 0;
306 }
307
308 int
309 danube_wdt_register_proc_read (char *buf, char **start, off_t offset, int count,
310 int *eof, void *data)
311 {
312 int len = 0;
313 len += sprintf (buf + len, "DANUBE_BIU_WDT_PROC_READ\n");
314
315 len += sprintf (buf + len, "DANUBE_BIU_WDT_CR(0x%08x) : 0x%08x\n",
316 (unsigned int)DANUBE_BIU_WDT_CR,
317 readl(DANUBE_BIU_WDT_CR));
318 len += sprintf (buf + len, "DANUBE_BIU_WDT_SR(0x%08x) : 0x%08x\n",
319 (unsigned int)DANUBE_BIU_WDT_SR,
320 readl(DANUBE_BIU_WDT_SR));
321
322 *eof = 1;
323
324 return len;
325 }
326
327 static struct file_operations wdt_fops = {
328 .owner = THIS_MODULE,
329 .ioctl = danube_wdt_ioctl,
330 .open = danube_wdt_open,
331 .release = danube_wdt_release,
332 };
333
334 int __init
335 danube_wdt_init_module (void)
336 {
337 danube_wdt_major = register_chrdev(0, "wdt", &wdt_fops);
338
339 if (danube_wdt_major < 0)
340 {
341 printk("cannot register watchdog device\n");
342
343 return -EINVAL;
344 }
345
346 create_proc_read_entry("danube_wdt", 0, NULL, danube_wdt_register_proc_read, NULL);
347
348 printk("danube watchdog loaded\n");
349
350 return 0;
351 }
352
353 void
354 danube_wdt_cleanup_module (void)
355 {
356 unregister_chrdev(danube_wdt_major, "wdt");
357 remove_proc_entry("danube_wdt", NULL);
358 }
359
360 module_init(danube_wdt_init_module);
361 module_exit(danube_wdt_cleanup_module);
This page took 0.081204 seconds and 5 git commands to generate.