cleanup danube watchdog timer
[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, wdt_pwl, ffpi;
54 int retval = 0;
55
56 /* clock divider & prewarning limit */
57 wdt_clkdiv = 1 << (7 * DANUBE_BIU_WDT_CR_CLKDIV_GET(readl(DANUBE_BIU_WDT_CR)));
58 wdt_pwl = 0x8000 >> DANUBE_BIU_WDT_CR_PWL_GET(readl(DANUBE_BIU_WDT_CR));
59
60 //TODO
61 printk("WARNING FUNCTION CALL MISSING!!!");
62 //ffpi = cgu_get_io_region_clock();
63 printk("cpu clock = %d\n", ffpi);
64
65 /* caculate reload value */
66 wdt_reload = (timeout * (ffpi / wdt_clkdiv)) + wdt_pwl;
67
68 printk("wdt_pwl=0x%x, wdt_clkdiv=%d, ffpi=%d, wdt_reload = 0x%x\n",
69 wdt_pwl, wdt_clkdiv, ffpi, wdt_reload);
70
71 if (wdt_reload > 0xFFFF)
72 {
73 printk ("timeout too large %d\n", timeout);
74 retval = -EINVAL;
75 goto out;
76 }
77
78 /* Write first part of password access */
79 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
80
81 wdt_cr = readl(DANUBE_BIU_WDT_CR);
82 wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET(0xff) &
83 !DANUBE_BIU_WDT_CR_PWL_SET(0x3) &
84 !DANUBE_BIU_WDT_CR_CLKDIV_SET(0x3) &
85 !DANUBE_BIU_WDT_CR_RELOAD_SET(0xffff));
86
87 wdt_cr |= (DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2) |
88 DANUBE_BIU_WDT_CR_PWL_SET(DANUBE_BIU_WDT_CR_PWL_GET(readl(DANUBE_BIU_WDT_CR))) |
89 DANUBE_BIU_WDT_CR_CLKDIV_SET(DANUBE_BIU_WDT_CR_CLKDIV_GET(readl(DANUBE_BIU_WDT_CR))) |
90 DANUBE_BIU_WDT_CR_RELOAD_SET(wdt_reload) |
91 DANUBE_BIU_WDT_CR_GEN);
92
93 /* Set reload value in second password access */
94 writel(wdt_cr, DANUBE_BIU_WDT_CR);
95 printk("watchdog enabled\n");
96
97 out:
98 return retval;
99 }
100
101 void
102 danube_wdt_disable (void)
103 {
104 /* Write first part of password access */
105 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
106
107 /* Disable the watchdog in second password access (GEN=0) */
108 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2), DANUBE_BIU_WDT_CR);
109 }
110
111 /* passed LPEN or DSEN */
112 void
113 danube_wdt_low_power_or_debug (int en, int type)
114 {
115 unsigned int wdt_cr = 0;
116
117 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
118
119 wdt_cr = readl(DANUBE_BIU_WDT_CR);
120
121 if (en)
122 {
123 wdt_cr &= (~DANUBE_BIU_WDT_CR_PW_SET(0xff));
124 wdt_cr |= (DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2) | type);
125 } else {
126 wdt_cr &= (~DANUBE_BIU_WDT_CR_PW_SET(0xff) & ~type);
127 wdt_cr |= DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2);
128 }
129
130 /* Set reload value in second password access */
131 writel(wdt_cr, DANUBE_BIU_WDT_CR);
132 }
133
134 void
135 danube_wdt_prewarning_limit (int pwl)
136 {
137 unsigned int wdt_cr = 0;
138
139 wdt_cr = readl(DANUBE_BIU_WDT_CR);
140 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
141
142 wdt_cr &= 0xf300ffff;
143 wdt_cr |= (DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2) |
144 DANUBE_BIU_WDT_CR_PWL_SET(pwl));
145
146 /* Set reload value in second password access */
147 writel(wdt_cr, DANUBE_BIU_WDT_CR);
148 }
149
150 void
151 danube_wdt_set_clkdiv (int clkdiv)
152 {
153 unsigned int wdt_cr = 0;
154
155 wdt_cr = readl(DANUBE_BIU_WDT_CR);
156 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
157
158 wdt_cr &= 0xfc00ffff;
159 wdt_cr |= (DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2) |
160 DANUBE_BIU_WDT_CR_CLKDIV_SET(clkdiv));
161
162 /* Set reload value in second password access */
163 writel(wdt_cr, DANUBE_BIU_WDT_CR);
164 }
165
166 static int
167 danube_wdt_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
168 unsigned long arg)
169 {
170 int result = 0;
171 int en = 0;
172 int istatus;
173 int pwl, clkdiv;
174 static int timeout = -1;
175
176 switch (cmd)
177 {
178 case DANUBE_WDT_IOC_START:
179 printk("enable watch dog timer!\n");
180 if (copy_from_user((void *) &timeout, (void *) arg, sizeof (int)))
181 {
182 printk ("invalid argument\n");
183 result = -EINVAL;
184 } else {
185 if ((result = danube_wdt_enable (timeout)) < 0)
186 timeout = -1;
187 }
188 break;
189
190 case DANUBE_WDT_IOC_STOP:
191 printk("disable watch dog timer\n");
192 timeout = -1;
193 danube_wdt_disable();
194 break;
195
196 case DANUBE_WDT_IOC_PING:
197 if (timeout < 0)
198 result = -EIO;
199 else
200 result = danube_wdt_enable(timeout);
201 break;
202
203 case DANUBE_WDT_IOC_GET_STATUS:
204 istatus = readl(DANUBE_BIU_WDT_SR);
205 copy_to_user((int *) arg, (int *) &istatus, sizeof (int));
206 break;
207
208 case DANUBE_WDT_IOC_SET_PWL:
209 if (copy_from_user((void *) &pwl, (void *) arg, sizeof (int)))
210 result = -EINVAL;
211 danube_wdt_prewarning_limit(pwl);
212 break;
213
214 case DANUBE_WDT_IOC_SET_DSEN:
215 if (copy_from_user((void *) &en, (void *) arg, sizeof (int)))
216 result = -EINVAL;
217 danube_wdt_low_power_or_debug(en, DANUBE_BIU_WDT_CR_DSEN);
218 break;
219
220 case DANUBE_WDT_IOC_SET_LPEN:
221 if (copy_from_user((void *) &en, (void *) arg, sizeof (int)))
222 result = -EINVAL;
223 danube_wdt_low_power_or_debug(en, DANUBE_BIU_WDT_CR_LPEN);
224 break;
225
226 case DANUBE_WDT_IOC_SET_CLKDIV:
227 if (copy_from_user((void *) &clkdiv, (void *) arg, sizeof (int)))
228 result = -EINVAL;
229 danube_wdt_set_clkdiv(clkdiv);
230 break;
231 }
232
233 return result;
234 }
235
236 static int
237 danube_wdt_open (struct inode *inode, struct file *file)
238 {
239 if (danube_wdt_inuse)
240 return -EBUSY;
241
242 danube_wdt_inuse = 1;
243
244 return 0;
245 }
246
247 static int
248 danube_wdt_release (struct inode *inode, struct file *file)
249 {
250 danube_wdt_inuse = 0;
251
252 return 0;
253 }
254
255 int
256 danube_wdt_register_proc_read (char *buf, char **start, off_t offset, int count,
257 int *eof, void *data)
258 {
259 int len = 0;
260
261 len += sprintf (buf + len, "DANUBE_BIU_WDT_PROC_READ\n");
262 len += sprintf (buf + len, "DANUBE_BIU_WDT_CR(0x%08x) : 0x%08x\n",
263 (unsigned int)DANUBE_BIU_WDT_CR, readl(DANUBE_BIU_WDT_CR));
264 len += sprintf (buf + len, "DANUBE_BIU_WDT_SR(0x%08x) : 0x%08x\n",
265 (unsigned int)DANUBE_BIU_WDT_SR, readl(DANUBE_BIU_WDT_SR));
266
267 *eof = 1;
268
269 return len;
270 }
271
272 static struct file_operations wdt_fops = {
273 .owner = THIS_MODULE,
274 .ioctl = danube_wdt_ioctl,
275 .open = danube_wdt_open,
276 .release = danube_wdt_release,
277 };
278
279 int __init
280 danube_wdt_init_module (void)
281 {
282 danube_wdt_major = register_chrdev(0, "wdt", &wdt_fops);
283
284 if (danube_wdt_major < 0)
285 {
286 printk("cannot register watchdog device\n");
287
288 return -EINVAL;
289 }
290
291 create_proc_read_entry("danube_wdt", 0, NULL, danube_wdt_register_proc_read, NULL);
292
293 printk("danube watchdog loaded\n");
294
295 return 0;
296 }
297
298 void
299 danube_wdt_cleanup_module (void)
300 {
301 unregister_chrdev(danube_wdt_major, "wdt");
302 remove_proc_entry("danube_wdt", NULL);
303 }
304
305 module_init(danube_wdt_init_module);
306 module_exit(danube_wdt_cleanup_module);
This page took 0.06439 seconds and 5 git commands to generate.