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
, wdt_pwl
, ffpi
;
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
));
61 printk("WARNING FUNCTION CALL MISSING!!!");
62 //ffpi = cgu_get_io_region_clock();
63 printk("cpu clock = %d\n", ffpi
);
65 /* caculate reload value */
66 wdt_reload
= (timeout
* (ffpi
/ wdt_clkdiv
)) + wdt_pwl
;
68 printk("wdt_pwl=0x%x, wdt_clkdiv=%d, ffpi=%d, wdt_reload = 0x%x\n",
69 wdt_pwl
, wdt_clkdiv
, ffpi
, wdt_reload
);
71 if (wdt_reload
> 0xFFFF)
73 printk ("timeout too large %d\n", timeout
);
78 /* Write first part of password access */
79 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1
), DANUBE_BIU_WDT_CR
);
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));
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
);
93 /* Set reload value in second password access */
94 writel(wdt_cr
, DANUBE_BIU_WDT_CR
);
95 printk("watchdog enabled\n");
102 danube_wdt_disable (void)
104 /* Write first part of password access */
105 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1
), DANUBE_BIU_WDT_CR
);
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
);
111 /* passed LPEN or DSEN */
113 danube_wdt_low_power_or_debug (int en
, int type
)
115 unsigned int wdt_cr
= 0;
117 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1
), DANUBE_BIU_WDT_CR
);
119 wdt_cr
= readl(DANUBE_BIU_WDT_CR
);
123 wdt_cr
&= (~DANUBE_BIU_WDT_CR_PW_SET(0xff));
124 wdt_cr
|= (DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2
) | type
);
126 wdt_cr
&= (~DANUBE_BIU_WDT_CR_PW_SET(0xff) & ~type
);
127 wdt_cr
|= DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2
);
130 /* Set reload value in second password access */
131 writel(wdt_cr
, DANUBE_BIU_WDT_CR
);
135 danube_wdt_prewarning_limit (int pwl
)
137 unsigned int wdt_cr
= 0;
139 wdt_cr
= readl(DANUBE_BIU_WDT_CR
);
140 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1
), DANUBE_BIU_WDT_CR
);
142 wdt_cr
&= 0xf300ffff;
143 wdt_cr
|= (DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2
) |
144 DANUBE_BIU_WDT_CR_PWL_SET(pwl
));
146 /* Set reload value in second password access */
147 writel(wdt_cr
, DANUBE_BIU_WDT_CR
);
151 danube_wdt_set_clkdiv (int clkdiv
)
153 unsigned int wdt_cr
= 0;
155 wdt_cr
= readl(DANUBE_BIU_WDT_CR
);
156 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1
), DANUBE_BIU_WDT_CR
);
158 wdt_cr
&= 0xfc00ffff;
159 wdt_cr
|= (DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2
) |
160 DANUBE_BIU_WDT_CR_CLKDIV_SET(clkdiv
));
162 /* Set reload value in second password access */
163 writel(wdt_cr
, DANUBE_BIU_WDT_CR
);
167 danube_wdt_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
,
174 static int timeout
= -1;
178 case DANUBE_WDT_IOC_START
:
179 printk("enable watch dog timer!\n");
180 if (copy_from_user((void *) &timeout
, (void *) arg
, sizeof (int)))
182 printk ("invalid argument\n");
185 if ((result
= danube_wdt_enable (timeout
)) < 0)
190 case DANUBE_WDT_IOC_STOP
:
191 printk("disable watch dog timer\n");
193 danube_wdt_disable();
196 case DANUBE_WDT_IOC_PING
:
200 result
= danube_wdt_enable(timeout
);
203 case DANUBE_WDT_IOC_GET_STATUS
:
204 istatus
= readl(DANUBE_BIU_WDT_SR
);
205 copy_to_user((int *) arg
, (int *) &istatus
, sizeof (int));
208 case DANUBE_WDT_IOC_SET_PWL
:
209 if (copy_from_user((void *) &pwl
, (void *) arg
, sizeof (int)))
211 danube_wdt_prewarning_limit(pwl
);
214 case DANUBE_WDT_IOC_SET_DSEN
:
215 if (copy_from_user((void *) &en
, (void *) arg
, sizeof (int)))
217 danube_wdt_low_power_or_debug(en
, DANUBE_BIU_WDT_CR_DSEN
);
220 case DANUBE_WDT_IOC_SET_LPEN
:
221 if (copy_from_user((void *) &en
, (void *) arg
, sizeof (int)))
223 danube_wdt_low_power_or_debug(en
, DANUBE_BIU_WDT_CR_LPEN
);
226 case DANUBE_WDT_IOC_SET_CLKDIV
:
227 if (copy_from_user((void *) &clkdiv
, (void *) arg
, sizeof (int)))
229 danube_wdt_set_clkdiv(clkdiv
);
237 danube_wdt_open (struct inode
*inode
, struct file
*file
)
239 if (danube_wdt_inuse
)
242 danube_wdt_inuse
= 1;
248 danube_wdt_release (struct inode
*inode
, struct file
*file
)
250 danube_wdt_inuse
= 0;
256 danube_wdt_register_proc_read (char *buf
, char **start
, off_t offset
, int count
,
257 int *eof
, void *data
)
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
));
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
,
280 danube_wdt_init_module (void)
282 danube_wdt_major
= register_chrdev(0, "wdt", &wdt_fops
);
284 if (danube_wdt_major
< 0)
286 printk("cannot register watchdog device\n");
291 create_proc_read_entry("danube_wdt", 0, NULL
, danube_wdt_register_proc_read
, NULL
);
293 printk("danube watchdog loaded\n");
299 danube_wdt_cleanup_module (void)
301 unregister_chrdev(danube_wdt_major
, "wdt");
302 remove_proc_entry("danube_wdt", NULL
);
305 module_init(danube_wdt_init_module
);
306 module_exit(danube_wdt_cleanup_module
);