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 <asm/uaccess.h>
22 #include <linux/errno.h>
23 #include <linux/proc_fs.h>
24 #include <linux/ioctl.h>
25 #include <linux/module.h>
26 #include <asm-mips/danube/danube_wdt.h>
27 #include <asm-mips/danube/danube.h>
30 // TODO remove magic numbers and weirdo macros
32 extern unsigned int danube_get_fpi_hz (void);
34 static int danube_wdt_inuse
= 0;
35 static int danube_wdt_major
= 0;
38 danube_wdt_enable (unsigned int timeout
)
40 unsigned int wdt_cr
= 0;
41 unsigned int wdt_reload
= 0;
42 unsigned int wdt_clkdiv
, wdt_pwl
, ffpi
;
45 /* clock divider & prewarning limit */
46 wdt_clkdiv
= 1 << (7 * DANUBE_BIU_WDT_CR_CLKDIV_GET(readl(DANUBE_BIU_WDT_CR
)));
47 wdt_pwl
= 0x8000 >> DANUBE_BIU_WDT_CR_PWL_GET(readl(DANUBE_BIU_WDT_CR
));
50 printk("WARNING FUNCTION CALL MISSING!!!");
51 //ffpi = cgu_get_io_region_clock();
52 printk("cpu clock = %d\n", ffpi
);
54 /* caculate reload value */
55 wdt_reload
= (timeout
* (ffpi
/ wdt_clkdiv
)) + wdt_pwl
;
57 printk("wdt_pwl=0x%x, wdt_clkdiv=%d, ffpi=%d, wdt_reload = 0x%x\n",
58 wdt_pwl
, wdt_clkdiv
, ffpi
, wdt_reload
);
60 if (wdt_reload
> 0xFFFF)
62 printk ("timeout too large %d\n", timeout
);
67 /* Write first part of password access */
68 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1
), DANUBE_BIU_WDT_CR
);
70 wdt_cr
= readl(DANUBE_BIU_WDT_CR
);
71 wdt_cr
&= (!DANUBE_BIU_WDT_CR_PW_SET(0xff) &
72 !DANUBE_BIU_WDT_CR_PWL_SET(0x3) &
73 !DANUBE_BIU_WDT_CR_CLKDIV_SET(0x3) &
74 !DANUBE_BIU_WDT_CR_RELOAD_SET(0xffff));
76 wdt_cr
|= (DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2
) |
77 DANUBE_BIU_WDT_CR_PWL_SET(DANUBE_BIU_WDT_CR_PWL_GET(readl(DANUBE_BIU_WDT_CR
))) |
78 DANUBE_BIU_WDT_CR_CLKDIV_SET(DANUBE_BIU_WDT_CR_CLKDIV_GET(readl(DANUBE_BIU_WDT_CR
))) |
79 DANUBE_BIU_WDT_CR_RELOAD_SET(wdt_reload
) |
80 DANUBE_BIU_WDT_CR_GEN
);
82 writel(wdt_cr
, DANUBE_BIU_WDT_CR
);
84 printk("watchdog enabled\n");
91 danube_wdt_disable (void)
93 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1
), DANUBE_BIU_WDT_CR
);
94 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2
), DANUBE_BIU_WDT_CR
);
96 printk("watchdog disabled\n");
99 /* passed LPEN or DSEN */
101 danube_wdt_enable_feature (int en
, int type
)
103 unsigned int wdt_cr
= 0;
105 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1
), DANUBE_BIU_WDT_CR
);
107 wdt_cr
= readl(DANUBE_BIU_WDT_CR
);
111 wdt_cr
&= (~DANUBE_BIU_WDT_CR_PW_SET(0xff));
112 wdt_cr
|= (DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2
) | type
);
114 wdt_cr
&= (~DANUBE_BIU_WDT_CR_PW_SET(0xff) & ~type
);
115 wdt_cr
|= DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2
);
118 writel(wdt_cr
, DANUBE_BIU_WDT_CR
);
122 danube_wdt_prewarning_limit (int pwl
)
124 unsigned int wdt_cr
= 0;
126 wdt_cr
= readl(DANUBE_BIU_WDT_CR
);
127 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1
), DANUBE_BIU_WDT_CR
);
129 wdt_cr
&= 0xf300ffff;
130 wdt_cr
|= (DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2
) | DANUBE_BIU_WDT_CR_PWL_SET(pwl
));
132 /* Set reload value in second password access */
133 writel(wdt_cr
, DANUBE_BIU_WDT_CR
);
137 danube_wdt_set_clkdiv (int clkdiv
)
139 unsigned int wdt_cr
= 0;
141 wdt_cr
= readl(DANUBE_BIU_WDT_CR
);
142 writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1
), DANUBE_BIU_WDT_CR
);
144 wdt_cr
&= 0xfc00ffff;
145 wdt_cr
|= (DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2
) | DANUBE_BIU_WDT_CR_CLKDIV_SET(clkdiv
));
147 /* Set reload value in second password access */
148 writel(wdt_cr
, DANUBE_BIU_WDT_CR
);
152 danube_wdt_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
,
156 static int timeout
= -1;
157 unsigned int user_arg
;
159 if ((cmd
!= DANUBE_WDT_IOC_STOP
) && (cmd
!= DANUBE_WDT_IOC_PING
) && (cmd
!= DANUBE_WDT_IOC_GET_STATUS
))
161 if (copy_from_user((void *) &user_arg
, (void *) arg
, sizeof (int))){
169 case DANUBE_WDT_IOC_START
:
170 if ((result
= danube_wdt_enable(user_arg
)) < 0)
176 case DANUBE_WDT_IOC_STOP
:
177 printk("disable watch dog timer\n");
178 danube_wdt_disable();
181 case DANUBE_WDT_IOC_PING
:
185 result
= danube_wdt_enable(timeout
);
188 case DANUBE_WDT_IOC_GET_STATUS
:
189 user_arg
= readl(DANUBE_BIU_WDT_SR
);
190 copy_to_user((int*)arg
, (int*)&user_arg
, sizeof(int));
193 case DANUBE_WDT_IOC_SET_PWL
:
194 danube_wdt_prewarning_limit(user_arg
);
197 case DANUBE_WDT_IOC_SET_DSEN
:
198 danube_wdt_enable_feature(user_arg
, DANUBE_BIU_WDT_CR_DSEN
);
201 case DANUBE_WDT_IOC_SET_LPEN
:
202 danube_wdt_enable_feature(user_arg
, DANUBE_BIU_WDT_CR_LPEN
);
205 case DANUBE_WDT_IOC_SET_CLKDIV
:
206 danube_wdt_set_clkdiv(user_arg
);
210 printk("unknown watchdog iotcl\n");
218 danube_wdt_open (struct inode
*inode
, struct file
*file
)
220 if (danube_wdt_inuse
)
223 danube_wdt_inuse
= 1;
229 danube_wdt_release (struct inode
*inode
, struct file
*file
)
231 danube_wdt_inuse
= 0;
237 danube_wdt_register_proc_read (char *buf
, char **start
, off_t offset
, int count
,
238 int *eof
, void *data
)
242 len
+= sprintf (buf
+ len
, "DANUBE_BIU_WDT_PROC_READ\n");
243 len
+= sprintf (buf
+ len
, "DANUBE_BIU_WDT_CR(0x%08x) : 0x%08x\n",
244 (unsigned int)DANUBE_BIU_WDT_CR
, readl(DANUBE_BIU_WDT_CR
));
245 len
+= sprintf (buf
+ len
, "DANUBE_BIU_WDT_SR(0x%08x) : 0x%08x\n",
246 (unsigned int)DANUBE_BIU_WDT_SR
, readl(DANUBE_BIU_WDT_SR
));
253 static struct file_operations wdt_fops
= {
254 .owner
= THIS_MODULE
,
255 .ioctl
= danube_wdt_ioctl
,
256 .open
= danube_wdt_open
,
257 .release
= danube_wdt_release
,
261 danube_wdt_init_module (void)
263 danube_wdt_major
= register_chrdev(0, "wdt", &wdt_fops
);
265 if (danube_wdt_major
< 0)
267 printk("cannot register watchdog device\n");
272 create_proc_read_entry("danube_wdt", 0, NULL
, danube_wdt_register_proc_read
, NULL
);
274 printk("danube watchdog loaded\n");
280 danube_wdt_cleanup_module (void)
282 unregister_chrdev(danube_wdt_major
, "wdt");
283 remove_proc_entry("danube_wdt", NULL
);
286 module_init(danube_wdt_init_module
);
287 module_exit(danube_wdt_cleanup_module
);