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_enable_feature (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
,
171 static int timeout
= -1;
172 unsigned int user_arg
;
174 if ((cmd
!= DANUBE_WDT_IOC_STOP
) && (cmd
!= DANUBE_WDT_IOC_PING
) && (cmd
!= DANUBE_WDT_IOC_GET_STATUS
))
176 if (copy_from_user((void *) &user_arg
, (void *) arg
, sizeof (int)))
182 case DANUBE_WDT_IOC_START
:
183 printk("enable watch dog timer!\n");
184 if ((result
= danube_wdt_enable(user_arg
)) < 0)
190 case DANUBE_WDT_IOC_STOP
:
191 printk("disable watch dog timer\n");
192 danube_wdt_disable();
195 case DANUBE_WDT_IOC_PING
:
199 result
= danube_wdt_enable(timeout
);
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));
207 case DANUBE_WDT_IOC_SET_PWL
:
208 danube_wdt_prewarning_limit(user_arg
);
211 case DANUBE_WDT_IOC_SET_DSEN
:
212 danube_wdt_enable_feature(user_arg
, DANUBE_BIU_WDT_CR_DSEN
);
215 case DANUBE_WDT_IOC_SET_LPEN
:
216 danube_wdt_enable_feature(user_arg
, DANUBE_BIU_WDT_CR_LPEN
);
219 case DANUBE_WDT_IOC_SET_CLKDIV
:
220 danube_wdt_set_clkdiv(user_arg
);
228 danube_wdt_open (struct inode
*inode
, struct file
*file
)
230 if (danube_wdt_inuse
)
233 danube_wdt_inuse
= 1;
239 danube_wdt_release (struct inode
*inode
, struct file
*file
)
241 danube_wdt_inuse
= 0;
247 danube_wdt_register_proc_read (char *buf
, char **start
, off_t offset
, int count
,
248 int *eof
, void *data
)
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
));
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
,
271 danube_wdt_init_module (void)
273 danube_wdt_major
= register_chrdev(0, "wdt", &wdt_fops
);
275 if (danube_wdt_major
< 0)
277 printk("cannot register watchdog device\n");
282 create_proc_read_entry("danube_wdt", 0, NULL
, danube_wdt_register_proc_read
, NULL
);
284 printk("danube watchdog loaded\n");
290 danube_wdt_cleanup_module (void)
292 unregister_chrdev(danube_wdt_major
, "wdt");
293 remove_proc_entry("danube_wdt", NULL
);
296 module_init(danube_wdt_init_module
);
297 module_exit(danube_wdt_cleanup_module
);