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.
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.
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.
16 * Copyright (C) 2006 infineon
17 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
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>
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>
43 extern unsigned int danube_get_fpi_hz (void);
45 static int danube_wdt_inuse = 0;
46 static int danube_wdt_major = 0;
49 danube_wdt_enable (unsigned int timeout)
51 unsigned int wdt_cr = 0;
52 unsigned int wdt_reload = 0;
53 unsigned int wdt_clkdiv, clkdiv, wdt_pwl, pwl, ffpi;
55 /* clock divider & prewarning limit */
56 switch (clkdiv = DANUBE_BIU_WDT_CR_CLKDIV_GET(readl(DANUBE_BIU_WDT_CR)))
72 switch (pwl = DANUBE_BIU_WDT_CR_PWL_GET (readl(DANUBE_BIU_WDT_CR)))
89 printk("WARNING FUNCTION CALL MISSING!!!");
90 //ffpi = cgu_get_io_region_clock();
91 printk("cpu clock = %d\n", ffpi);
93 /* caculate reload value */
94 wdt_reload = (timeout * (ffpi / wdt_clkdiv)) + wdt_pwl;
96 printk("wdt_pwl=0x%x, wdt_clkdiv=%d, ffpi=%d, wdt_reload = 0x%x\n",
97 wdt_pwl, wdt_clkdiv, ffpi, wdt_reload);
99 if (wdt_reload > 0xFFFF) {
100 printk ("timeout too large %d\n", timeout);
104 /* Write first part of password access */
105 *DANUBE_BIU_WDT_CR = DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW1);
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));
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);
119 /* Set reload value in second password access */
120 *DANUBE_BIU_WDT_CR = wdt_cr;
121 printk ("enabled\n");
126 danube_wdt_disable (void)
128 /* Write first part of password access */
129 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
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);
136 danube_wdt_low_power (int en)
138 unsigned int wdt_cr = 0;
140 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
142 wdt_cr = readl(DANUBE_BIU_WDT_CR);
146 wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET(0xff));
148 (DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
149 DANUBE_BIU_WDT_CR_LPEN);
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);
157 /* Set reload value in second password access */
158 writel(wdt_cr, DANUBE_BIU_WDT_CR);
162 danube_wdt_debug_suspend (int en)
164 unsigned int wdt_cr = 0;
166 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
168 wdt_cr = readl(DANUBE_BIU_WDT_CR);
171 wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET (0xff));
173 (DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
174 DANUBE_BIU_WDT_CR_DSEN);
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);
182 /* Set reload value in second password access */
183 writel(wdt_cr, DANUBE_BIU_WDT_CR);
187 danube_wdt_prewarning_limit (int pwl)
189 unsigned int wdt_cr = 0;
191 wdt_cr = readl(DANUBE_BIU_WDT_CR);
192 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
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));
199 /* Set reload value in second password access */
200 writel(wdt_cr, DANUBE_BIU_WDT_CR);
204 danube_wdt_set_clkdiv (int clkdiv)
206 unsigned int wdt_cr = 0;
208 wdt_cr = readl(DANUBE_BIU_WDT_CR);
209 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);
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));
216 /* Set reload value in second password access */
217 writel(wdt_cr, DANUBE_BIU_WDT_CR);
221 danube_wdt_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
228 static int timeout = -1;
232 case DANUBE_WDT_IOC_START:
233 printk("enable watch dog timer!\n");
234 if (copy_from_user((void *) &timeout, (void *) arg, sizeof (int)))
236 printk ("invalid argument\n");
239 if ((result = danube_wdt_enable (timeout)) < 0)
244 case DANUBE_WDT_IOC_STOP:
245 printk("disable watch dog timer\n");
247 danube_wdt_disable();
250 case DANUBE_WDT_IOC_PING:
254 result = danube_wdt_enable(timeout);
257 case DANUBE_WDT_IOC_SET_PWL:
258 if (copy_from_user((void *) &pwl, (void *) arg, sizeof (int)))
260 danube_wdt_prewarning_limit(pwl);
263 case DANUBE_WDT_IOC_SET_DSEN:
264 if (copy_from_user((void *) &en, (void *) arg, sizeof (int)))
266 danube_wdt_debug_suspend (en);
269 case DANUBE_WDT_IOC_SET_LPEN:
270 if (copy_from_user((void *) &en, (void *) arg, sizeof (int)))
272 danube_wdt_low_power(en);
275 case DANUBE_WDT_IOC_SET_CLKDIV:
276 if (copy_from_user((void *) &clkdiv, (void *) arg, sizeof (int)))
278 danube_wdt_set_clkdiv (clkdiv);
281 case DANUBE_WDT_IOC_GET_STATUS:
282 istatus = readl(DANUBE_BIU_WDT_SR);
283 copy_to_user((int *) arg, (int *) &istatus, sizeof (int));
291 danube_wdt_open (struct inode *inode, struct file *file)
293 if (danube_wdt_inuse)
295 danube_wdt_inuse = 1;
301 danube_wdt_release (struct inode *inode, struct file *file)
303 danube_wdt_inuse = 0;
309 danube_wdt_register_proc_read (char *buf, char **start, off_t offset, int count,
310 int *eof, void *data)
313 len += sprintf (buf + len, "DANUBE_BIU_WDT_PROC_READ\n");
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));
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,
335 danube_wdt_init_module (void)
337 danube_wdt_major = register_chrdev(0, "wdt", &wdt_fops);
339 if (danube_wdt_major < 0)
341 printk("cannot register watchdog device\n");
346 create_proc_read_entry("danube_wdt", 0, NULL, danube_wdt_register_proc_read, NULL);
348 printk("danube watchdog loaded\n");
354 danube_wdt_cleanup_module (void)
356 unregister_chrdev(danube_wdt_major, "wdt");
357 remove_proc_entry("danube_wdt", NULL);
360 module_init(danube_wdt_init_module);
361 module_exit(danube_wdt_cleanup_module);