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_enable_feature (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 static int timeout = -1;
172 unsigned int user_arg;
173
174 if ((cmd != DANUBE_WDT_IOC_STOP) && (cmd != DANUBE_WDT_IOC_PING) && (cmd != DANUBE_WDT_IOC_GET_STATUS))
175 {
176 if (copy_from_user((void *) &user_arg, (void *) arg, sizeof (int)))
177 result = -EINVAL;
178 }
179
180 switch (cmd)
181 {
182 case DANUBE_WDT_IOC_START:
183 printk("enable watch dog timer!\n");
184 if ((result = danube_wdt_enable(user_arg)) < 0)
185 timeout = -1;
186 else
187 timeout = user_arg;
188 break;
189
190 case DANUBE_WDT_IOC_STOP:
191 printk("disable watch dog timer\n");
192 danube_wdt_disable();
193 break;
194
195 case DANUBE_WDT_IOC_PING:
196 if (timeout < 0)
197 result = -EIO;
198 else
199 result = danube_wdt_enable(timeout);
200 break;
201
202 case DANUBE_WDT_IOC_GET_STATUS:
203 user_arg = readl(DANUBE_BIU_WDT_SR);
204 copy_to_user((int*)arg, (int*)&user_arg, sizeof(int));
205 break;
206
207 case DANUBE_WDT_IOC_SET_PWL:
208 danube_wdt_prewarning_limit(user_arg);
209 break;
210
211 case DANUBE_WDT_IOC_SET_DSEN:
212 danube_wdt_enable_feature(user_arg, DANUBE_BIU_WDT_CR_DSEN);
213 break;
214
215 case DANUBE_WDT_IOC_SET_LPEN:
216 danube_wdt_enable_feature(user_arg, DANUBE_BIU_WDT_CR_LPEN);
217 break;
218
219 case DANUBE_WDT_IOC_SET_CLKDIV:
220 danube_wdt_set_clkdiv(user_arg);
221 break;
222 }
223
224 return result;
225 }
226
227 static int
228 danube_wdt_open (struct inode *inode, struct file *file)
229 {
230 if (danube_wdt_inuse)
231 return -EBUSY;
232
233 danube_wdt_inuse = 1;
234
235 return 0;
236 }
237
238 static int
239 danube_wdt_release (struct inode *inode, struct file *file)
240 {
241 danube_wdt_inuse = 0;
242
243 return 0;
244 }
245
246 int
247 danube_wdt_register_proc_read (char *buf, char **start, off_t offset, int count,
248 int *eof, void *data)
249 {
250 int len = 0;
251
252 len += sprintf (buf + len, "DANUBE_BIU_WDT_PROC_READ\n");
253 len += sprintf (buf + len, "DANUBE_BIU_WDT_CR(0x%08x) : 0x%08x\n",
254 (unsigned int)DANUBE_BIU_WDT_CR, readl(DANUBE_BIU_WDT_CR));
255 len += sprintf (buf + len, "DANUBE_BIU_WDT_SR(0x%08x) : 0x%08x\n",
256 (unsigned int)DANUBE_BIU_WDT_SR, readl(DANUBE_BIU_WDT_SR));
257
258 *eof = 1;
259
260 return len;
261 }
262
263 static struct file_operations wdt_fops = {
264 .owner = THIS_MODULE,
265 .ioctl = danube_wdt_ioctl,
266 .open = danube_wdt_open,
267 .release = danube_wdt_release,
268 };
269
270 int __init
271 danube_wdt_init_module (void)
272 {
273 danube_wdt_major = register_chrdev(0, "wdt", &wdt_fops);
274
275 if (danube_wdt_major < 0)
276 {
277 printk("cannot register watchdog device\n");
278
279 return -EINVAL;
280 }
281
282 create_proc_read_entry("danube_wdt", 0, NULL, danube_wdt_register_proc_read, NULL);
283
284 printk("danube watchdog loaded\n");
285
286 return 0;
287 }
288
289 void
290 danube_wdt_cleanup_module (void)
291 {
292 unregister_chrdev(danube_wdt_major, "wdt");
293 remove_proc_entry("danube_wdt", NULL);
294 }
295
296 module_init(danube_wdt_init_module);
297 module_exit(danube_wdt_cleanup_module);
This page took 0.077656 seconds and 5 git commands to generate.