adds timer unit to ifxmips tree
[openwrt.git] / target / linux / ifxmips / files / drivers / watchdog / ifxmips_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 <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 <linux/platform_device.h>
27 #include <linux/watchdog.h>
28 #include <linux/miscdevice.h>
29 #include <asm-mips/ifxmips/ifxmips_wdt.h>
30 #include <asm-mips/ifxmips/ifxmips_cgu.h>
31 #include <asm-mips/ifxmips/ifxmips.h>
32
33 #define DRVNAME "ifxmips_wdt"
34
35 // TODO remove magic numbers and weirdo macros
36 extern unsigned int ifxmips_get_fpi_hz (void);
37
38 static int ifxmips_wdt_inuse = 0;
39
40 int
41 ifxmips_wdt_enable (unsigned int timeout)
42 {
43 unsigned int wdt_cr = 0;
44 unsigned int wdt_reload = 0;
45 unsigned int wdt_clkdiv, wdt_pwl, ffpi;
46 int retval = 0;
47
48 /* clock divider & prewarning limit */
49 wdt_clkdiv = 1 << (7 * IFXMIPS_BIU_WDT_CR_CLKDIV_GET(ifxmips_r32(IFXMIPS_BIU_WDT_CR)));
50 wdt_pwl = 0x8000 >> IFXMIPS_BIU_WDT_CR_PWL_GET(ifxmips_r32(IFXMIPS_BIU_WDT_CR));
51
52 ffpi = cgu_get_io_region_clock();
53 printk("cpu clock = %d\n", ffpi);
54
55 /* caculate reload value */
56 wdt_reload = (timeout * (ffpi / wdt_clkdiv)) + wdt_pwl;
57
58 printk(KERN_WARNING DRVNAME ": wdt_pwl=0x%x, wdt_clkdiv=%d, ffpi=%d, wdt_reload = 0x%x\n",
59 wdt_pwl, wdt_clkdiv, ffpi, wdt_reload);
60
61 if (wdt_reload > 0xFFFF)
62 {
63 printk(KERN_WARNING DRVNAME ": timeout too large %d\n", timeout);
64 retval = -EINVAL;
65 goto out;
66 }
67
68 /* Write first part of password access */
69 ifxmips_w32(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
70
71 wdt_cr = ifxmips_r32(IFXMIPS_BIU_WDT_CR);
72 wdt_cr &= (!IFXMIPS_BIU_WDT_CR_PW_SET(0xff) &
73 !IFXMIPS_BIU_WDT_CR_PWL_SET(0x3) &
74 !IFXMIPS_BIU_WDT_CR_CLKDIV_SET(0x3) &
75 !IFXMIPS_BIU_WDT_CR_RELOAD_SET(0xffff));
76
77 wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) |
78 IFXMIPS_BIU_WDT_CR_PWL_SET(IFXMIPS_BIU_WDT_CR_PWL_GET(ifxmips_r32(IFXMIPS_BIU_WDT_CR))) |
79 IFXMIPS_BIU_WDT_CR_CLKDIV_SET(IFXMIPS_BIU_WDT_CR_CLKDIV_GET(ifxmips_r32(IFXMIPS_BIU_WDT_CR))) |
80 IFXMIPS_BIU_WDT_CR_RELOAD_SET(wdt_reload) |
81 IFXMIPS_BIU_WDT_CR_GEN);
82
83 ifxmips_w32(wdt_cr, IFXMIPS_BIU_WDT_CR);
84
85 printk("watchdog enabled\n");
86
87 out:
88 return retval;
89 }
90
91 void
92 ifxmips_wdt_disable (void)
93 {
94 ifxmips_w32(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
95 ifxmips_w32(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2), IFXMIPS_BIU_WDT_CR);
96
97 printk("watchdog disabled\n");
98 }
99
100 /* passed LPEN or DSEN */
101 void
102 ifxmips_wdt_enable_feature (int en, int type)
103 {
104 unsigned int wdt_cr = 0;
105
106 ifxmips_w32(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
107
108 wdt_cr = ifxmips_r32(IFXMIPS_BIU_WDT_CR);
109
110 if (en)
111 {
112 wdt_cr &= (~IFXMIPS_BIU_WDT_CR_PW_SET(0xff));
113 wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | type);
114 } else {
115 wdt_cr &= (~IFXMIPS_BIU_WDT_CR_PW_SET(0xff) & ~type);
116 wdt_cr |= IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2);
117 }
118
119 ifxmips_w32(wdt_cr, IFXMIPS_BIU_WDT_CR);
120 }
121
122 void
123 ifxmips_wdt_prewarning_limit (int pwl)
124 {
125 unsigned int wdt_cr = 0;
126
127 wdt_cr = ifxmips_r32(IFXMIPS_BIU_WDT_CR);
128 ifxmips_w32(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
129
130 wdt_cr &= 0xf300ffff;
131 wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | IFXMIPS_BIU_WDT_CR_PWL_SET(pwl));
132
133 /* Set reload value in second password access */
134 ifxmips_w32(wdt_cr, IFXMIPS_BIU_WDT_CR);
135 }
136
137 void
138 ifxmips_wdt_set_clkdiv (int clkdiv)
139 {
140 unsigned int wdt_cr = 0;
141
142 wdt_cr = ifxmips_r32(IFXMIPS_BIU_WDT_CR);
143 ifxmips_w32(IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW1), IFXMIPS_BIU_WDT_CR);
144
145 wdt_cr &= 0xfc00ffff;
146 wdt_cr |= (IFXMIPS_BIU_WDT_CR_PW_SET(IFXMIPS_WDT_PW2) | IFXMIPS_BIU_WDT_CR_CLKDIV_SET(clkdiv));
147
148 /* Set reload value in second password access */
149 ifxmips_w32(wdt_cr, IFXMIPS_BIU_WDT_CR);
150 }
151
152 static int
153 ifxmips_wdt_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
154 unsigned long arg)
155 {
156 int result = 0;
157 static int timeout = -1;
158 unsigned int user_arg;
159
160 if ((cmd != IFXMIPS_WDT_IOC_STOP) && (cmd != IFXMIPS_WDT_IOC_PING) && (cmd != IFXMIPS_WDT_IOC_GET_STATUS))
161 {
162 if (copy_from_user((void *) &user_arg, (void *) arg, sizeof (int))){
163 result = -EINVAL;
164 goto out;
165 }
166 }
167
168 switch (cmd)
169 {
170 case IFXMIPS_WDT_IOC_START:
171 if ((result = ifxmips_wdt_enable(user_arg)) < 0)
172 timeout = -1;
173 else
174 timeout = user_arg;
175 break;
176
177 case IFXMIPS_WDT_IOC_STOP:
178 printk(KERN_INFO DRVNAME ": disable watch dog timer\n");
179 ifxmips_wdt_disable();
180 break;
181
182 case IFXMIPS_WDT_IOC_PING:
183 if (timeout < 0)
184 result = -EIO;
185 else
186 result = ifxmips_wdt_enable(timeout);
187 break;
188
189 case IFXMIPS_WDT_IOC_GET_STATUS:
190 user_arg = ifxmips_r32(IFXMIPS_BIU_WDT_SR);
191 copy_to_user((int*)arg, (int*)&user_arg, sizeof(int));
192 break;
193
194 case IFXMIPS_WDT_IOC_SET_PWL:
195 ifxmips_wdt_prewarning_limit(user_arg);
196 break;
197
198 case IFXMIPS_WDT_IOC_SET_DSEN:
199 ifxmips_wdt_enable_feature(user_arg, IFXMIPS_BIU_WDT_CR_DSEN);
200 break;
201
202 case IFXMIPS_WDT_IOC_SET_LPEN:
203 ifxmips_wdt_enable_feature(user_arg, IFXMIPS_BIU_WDT_CR_LPEN);
204 break;
205
206 case IFXMIPS_WDT_IOC_SET_CLKDIV:
207 ifxmips_wdt_set_clkdiv(user_arg);
208 break;
209
210 default:
211 printk(KERN_WARNING DRVNAME ": unknown watchdog iotcl\n");
212 }
213
214 out:
215 return result;
216 }
217
218 static int
219 ifxmips_wdt_open (struct inode *inode, struct file *file)
220 {
221 if (ifxmips_wdt_inuse)
222 return -EBUSY;
223
224 ifxmips_wdt_inuse = 1;
225
226 return 0;
227 }
228
229 static int
230 ifxmips_wdt_release (struct inode *inode, struct file *file)
231 {
232 ifxmips_wdt_inuse = 0;
233
234 return 0;
235 }
236
237 int
238 ifxmips_wdt_register_proc_read (char *buf, char **start, off_t offset, int count,
239 int *eof, void *data)
240 {
241 int len = 0;
242
243 len += sprintf (buf + len, "IFXMIPS_BIU_WDT_PROC_READ\n");
244 len += sprintf (buf + len, "IFXMIPS_BIU_WDT_CR(0x%08x) : 0x%08x\n",
245 (unsigned int)IFXMIPS_BIU_WDT_CR, ifxmips_r32(IFXMIPS_BIU_WDT_CR));
246 len += sprintf (buf + len, "IFXMIPS_BIU_WDT_SR(0x%08x) : 0x%08x\n",
247 (unsigned int)IFXMIPS_BIU_WDT_SR, ifxmips_r32(IFXMIPS_BIU_WDT_SR));
248
249 *eof = 1;
250
251 return len;
252 }
253
254 static const struct file_operations ifxmips_wdt_fops = {
255 .owner = THIS_MODULE,
256 .llseek = no_llseek,
257 .ioctl = ifxmips_wdt_ioctl,
258 .open = ifxmips_wdt_open,
259 .release = ifxmips_wdt_release,
260 // .write = at91_wdt_write,
261 };
262
263 static struct miscdevice ifxmips_wdt_miscdev = {
264 .minor = WATCHDOG_MINOR,
265 .name = "ifxmips_wdt",
266 .fops = &ifxmips_wdt_fops,
267 };
268
269
270 static int
271 ifxmips_wdt_probe (struct platform_device *pdev)
272 {
273 int ret = misc_register(&ifxmips_wdt_miscdev);
274 if (ret)
275 return ret;
276
277 create_proc_read_entry(DRVNAME, 0, NULL, ifxmips_wdt_register_proc_read, NULL);
278
279 printk(KERN_INFO DRVNAME ": ifxmips watchdog loaded\n");
280
281 return 0;
282 }
283
284 static int
285 ifxmips_wdt_remove (struct platform_device *pdev)
286 {
287 misc_deregister(&ifxmips_wdt_miscdev);
288 remove_proc_entry(DRVNAME, NULL);
289 return 0;
290 }
291
292 static struct
293 platform_driver ifxmips_wdt_driver = {
294 .probe = ifxmips_wdt_probe,
295 .remove = ifxmips_wdt_remove,
296 .driver = {
297 .name = DRVNAME,
298 .owner = THIS_MODULE,
299 },
300 };
301
302 int __init
303 ifxmips_wdt_init_module (void)
304 {
305 int ret = platform_driver_register(&ifxmips_wdt_driver);
306 if (ret)
307 printk(KERN_INFO DRVNAME ": Error registering platfom driver!");
308 return ret;
309 }
310
311 void
312 ifxmips_wdt_cleanup_module (void)
313 {
314 platform_driver_unregister(&ifxmips_wdt_driver);
315 }
316
317 module_init(ifxmips_wdt_init_module);
318 module_exit(ifxmips_wdt_cleanup_module);
319
320 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
321 MODULE_DESCRIPTION("Watchdog driver for infineon ifxmips family");
322 MODULE_LICENSE("GPL");
323 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
This page took 0.065571 seconds and 5 git commands to generate.