]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* linux/drivers/char/watchdog/s3c2410_wdt.c |
2 | * | |
3 | * Copyright (c) 2004 Simtec Electronics | |
4 | * Ben Dooks <ben@simtec.co.uk> | |
5 | * | |
6 | * S3C2410 Watchdog Timer Support | |
7 | * | |
8 | * Based on, softdog.c by Alan Cox, | |
9 | * (c) Copyright 1996 Alan Cox <alan@redhat.com> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 | * | |
25 | * Changelog: | |
26 | * 05-Oct-2004 BJD Added semaphore init to stop crashes on open | |
27 | * Fixed tmr_count / wdt_count confusion | |
28 | * Added configurable debug | |
29 | * | |
af4bb822 BD |
30 | * 11-Jan-2005 BJD Fixed divide-by-2 in timeout code |
31 | * | |
32 | * 25-Jan-2005 DA Added suspend/resume support | |
94f1e9f3 | 33 | * Replaced reboot notifier with .shutdown method |
1da177e4 LT |
34 | * |
35 | * 10-Mar-2005 LCVR Changed S3C2410_VA to S3C24XX_VA | |
36 | */ | |
37 | ||
38 | #include <linux/module.h> | |
39 | #include <linux/moduleparam.h> | |
40 | #include <linux/config.h> | |
41 | #include <linux/types.h> | |
42 | #include <linux/timer.h> | |
43 | #include <linux/miscdevice.h> | |
44 | #include <linux/watchdog.h> | |
45 | #include <linux/fs.h> | |
1da177e4 LT |
46 | #include <linux/init.h> |
47 | #include <linux/device.h> | |
48 | #include <linux/interrupt.h> | |
49 | ||
50 | #include <asm/uaccess.h> | |
51 | #include <asm/io.h> | |
52 | ||
53 | #include <asm/arch/map.h> | |
54 | #include <asm/hardware/clock.h> | |
55 | ||
56 | #undef S3C24XX_VA_WATCHDOG | |
57 | #define S3C24XX_VA_WATCHDOG (0) | |
58 | ||
59 | #include <asm/arch/regs-watchdog.h> | |
60 | ||
61 | #define PFX "s3c2410-wdt: " | |
62 | ||
63 | #define CONFIG_S3C2410_WATCHDOG_ATBOOT (0) | |
64 | #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15) | |
65 | ||
4bfdf378 | 66 | static int nowayout = WATCHDOG_NOWAYOUT; |
1da177e4 LT |
67 | static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME; |
68 | static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT; | |
69 | static int soft_noboot = 0; | |
70 | static int debug = 0; | |
71 | ||
72 | module_param(tmr_margin, int, 0); | |
73 | module_param(tmr_atboot, int, 0); | |
74 | module_param(nowayout, int, 0); | |
75 | module_param(soft_noboot, int, 0); | |
76 | module_param(debug, int, 0); | |
77 | ||
78 | MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. default=" __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")"); | |
79 | ||
80 | MODULE_PARM_DESC(tmr_atboot, "Watchdog is started at boot time if set to 1, default=" __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT)); | |
81 | ||
82 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)"); | |
83 | ||
84 | MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default depends on ONLY_TESTING)"); | |
85 | ||
86 | MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug, (default 0)"); | |
87 | ||
88 | ||
89 | typedef enum close_state { | |
90 | CLOSE_STATE_NOT, | |
91 | CLOSE_STATE_ALLOW=0x4021 | |
92 | } close_state_t; | |
93 | ||
94 | static DECLARE_MUTEX(open_lock); | |
95 | ||
96 | static struct resource *wdt_mem; | |
97 | static struct resource *wdt_irq; | |
98 | static struct clk *wdt_clock; | |
99 | static void __iomem *wdt_base; | |
100 | static unsigned int wdt_count; | |
101 | static close_state_t allow_close; | |
102 | ||
103 | /* watchdog control routines */ | |
104 | ||
105 | #define DBG(msg...) do { \ | |
106 | if (debug) \ | |
107 | printk(KERN_INFO msg); \ | |
108 | } while(0) | |
109 | ||
110 | /* functions */ | |
111 | ||
112 | static int s3c2410wdt_keepalive(void) | |
113 | { | |
114 | writel(wdt_count, wdt_base + S3C2410_WTCNT); | |
115 | return 0; | |
116 | } | |
117 | ||
118 | static int s3c2410wdt_stop(void) | |
119 | { | |
120 | unsigned long wtcon; | |
121 | ||
122 | wtcon = readl(wdt_base + S3C2410_WTCON); | |
123 | wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN); | |
124 | writel(wtcon, wdt_base + S3C2410_WTCON); | |
125 | ||
126 | return 0; | |
127 | } | |
128 | ||
129 | static int s3c2410wdt_start(void) | |
130 | { | |
131 | unsigned long wtcon; | |
132 | ||
133 | s3c2410wdt_stop(); | |
134 | ||
135 | wtcon = readl(wdt_base + S3C2410_WTCON); | |
136 | wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128; | |
137 | ||
138 | if (soft_noboot) { | |
139 | wtcon |= S3C2410_WTCON_INTEN; | |
140 | wtcon &= ~S3C2410_WTCON_RSTEN; | |
141 | } else { | |
142 | wtcon &= ~S3C2410_WTCON_INTEN; | |
143 | wtcon |= S3C2410_WTCON_RSTEN; | |
144 | } | |
145 | ||
146 | DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n", | |
147 | __FUNCTION__, wdt_count, wtcon); | |
148 | ||
149 | writel(wdt_count, wdt_base + S3C2410_WTDAT); | |
150 | writel(wdt_count, wdt_base + S3C2410_WTCNT); | |
151 | writel(wtcon, wdt_base + S3C2410_WTCON); | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
156 | static int s3c2410wdt_set_heartbeat(int timeout) | |
157 | { | |
158 | unsigned int freq = clk_get_rate(wdt_clock); | |
159 | unsigned int count; | |
160 | unsigned int divisor = 1; | |
161 | unsigned long wtcon; | |
162 | ||
163 | if (timeout < 1) | |
164 | return -EINVAL; | |
165 | ||
166 | freq /= 128; | |
167 | count = timeout * freq; | |
168 | ||
169 | DBG("%s: count=%d, timeout=%d, freq=%d\n", | |
170 | __FUNCTION__, count, timeout, freq); | |
171 | ||
172 | /* if the count is bigger than the watchdog register, | |
173 | then work out what we need to do (and if) we can | |
174 | actually make this value | |
175 | */ | |
176 | ||
177 | if (count >= 0x10000) { | |
178 | for (divisor = 1; divisor <= 0x100; divisor++) { | |
179 | if ((count / divisor) < 0x10000) | |
180 | break; | |
181 | } | |
182 | ||
183 | if ((count / divisor) >= 0x10000) { | |
184 | printk(KERN_ERR PFX "timeout %d too big\n", timeout); | |
185 | return -EINVAL; | |
186 | } | |
187 | } | |
188 | ||
189 | tmr_margin = timeout; | |
190 | ||
191 | DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n", | |
192 | __FUNCTION__, timeout, divisor, count, count/divisor); | |
193 | ||
194 | count /= divisor; | |
195 | wdt_count = count; | |
196 | ||
197 | /* update the pre-scaler */ | |
198 | wtcon = readl(wdt_base + S3C2410_WTCON); | |
199 | wtcon &= ~S3C2410_WTCON_PRESCALE_MASK; | |
200 | wtcon |= S3C2410_WTCON_PRESCALE(divisor-1); | |
201 | ||
202 | writel(count, wdt_base + S3C2410_WTDAT); | |
203 | writel(wtcon, wdt_base + S3C2410_WTCON); | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
208 | /* | |
209 | * /dev/watchdog handling | |
210 | */ | |
211 | ||
212 | static int s3c2410wdt_open(struct inode *inode, struct file *file) | |
213 | { | |
214 | if(down_trylock(&open_lock)) | |
215 | return -EBUSY; | |
216 | ||
217 | if (nowayout) { | |
218 | __module_get(THIS_MODULE); | |
219 | } else { | |
220 | allow_close = CLOSE_STATE_ALLOW; | |
221 | } | |
222 | ||
223 | /* start the timer */ | |
224 | s3c2410wdt_start(); | |
225 | return nonseekable_open(inode, file); | |
226 | } | |
227 | ||
228 | static int s3c2410wdt_release(struct inode *inode, struct file *file) | |
229 | { | |
230 | /* | |
231 | * Shut off the timer. | |
232 | * Lock it in if it's a module and we set nowayout | |
233 | */ | |
234 | if (allow_close == CLOSE_STATE_ALLOW) { | |
235 | s3c2410wdt_stop(); | |
236 | } else { | |
237 | printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n"); | |
238 | s3c2410wdt_keepalive(); | |
239 | } | |
240 | ||
241 | allow_close = CLOSE_STATE_NOT; | |
242 | up(&open_lock); | |
243 | return 0; | |
244 | } | |
245 | ||
246 | static ssize_t s3c2410wdt_write(struct file *file, const char __user *data, | |
247 | size_t len, loff_t *ppos) | |
248 | { | |
249 | /* | |
250 | * Refresh the timer. | |
251 | */ | |
252 | if(len) { | |
253 | if (!nowayout) { | |
254 | size_t i; | |
255 | ||
256 | /* In case it was set long ago */ | |
257 | allow_close = CLOSE_STATE_NOT; | |
258 | ||
259 | for (i = 0; i != len; i++) { | |
260 | char c; | |
261 | ||
262 | if (get_user(c, data + i)) | |
263 | return -EFAULT; | |
264 | if (c == 'V') | |
265 | allow_close = CLOSE_STATE_ALLOW; | |
266 | } | |
267 | } | |
268 | ||
269 | s3c2410wdt_keepalive(); | |
270 | } | |
271 | return len; | |
272 | } | |
273 | ||
274 | #define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | |
275 | ||
276 | static struct watchdog_info s3c2410_wdt_ident = { | |
277 | .options = OPTIONS, | |
278 | .firmware_version = 0, | |
279 | .identity = "S3C2410 Watchdog", | |
280 | }; | |
281 | ||
282 | ||
283 | static int s3c2410wdt_ioctl(struct inode *inode, struct file *file, | |
284 | unsigned int cmd, unsigned long arg) | |
285 | { | |
286 | void __user *argp = (void __user *)arg; | |
287 | int __user *p = argp; | |
288 | int new_margin; | |
289 | ||
290 | switch (cmd) { | |
291 | default: | |
292 | return -ENOIOCTLCMD; | |
293 | ||
294 | case WDIOC_GETSUPPORT: | |
295 | return copy_to_user(argp, &s3c2410_wdt_ident, | |
296 | sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0; | |
297 | ||
298 | case WDIOC_GETSTATUS: | |
299 | case WDIOC_GETBOOTSTATUS: | |
300 | return put_user(0, p); | |
301 | ||
302 | case WDIOC_KEEPALIVE: | |
303 | s3c2410wdt_keepalive(); | |
304 | return 0; | |
305 | ||
306 | case WDIOC_SETTIMEOUT: | |
307 | if (get_user(new_margin, p)) | |
308 | return -EFAULT; | |
309 | ||
310 | if (s3c2410wdt_set_heartbeat(new_margin)) | |
311 | return -EINVAL; | |
312 | ||
313 | s3c2410wdt_keepalive(); | |
314 | return put_user(tmr_margin, p); | |
315 | ||
316 | case WDIOC_GETTIMEOUT: | |
317 | return put_user(tmr_margin, p); | |
318 | } | |
319 | } | |
320 | ||
1da177e4 LT |
321 | /* kernel interface */ |
322 | ||
323 | static struct file_operations s3c2410wdt_fops = { | |
324 | .owner = THIS_MODULE, | |
325 | .llseek = no_llseek, | |
326 | .write = s3c2410wdt_write, | |
327 | .ioctl = s3c2410wdt_ioctl, | |
328 | .open = s3c2410wdt_open, | |
329 | .release = s3c2410wdt_release, | |
330 | }; | |
331 | ||
332 | static struct miscdevice s3c2410wdt_miscdev = { | |
333 | .minor = WATCHDOG_MINOR, | |
334 | .name = "watchdog", | |
335 | .fops = &s3c2410wdt_fops, | |
336 | }; | |
337 | ||
1da177e4 LT |
338 | /* interrupt handler code */ |
339 | ||
340 | static irqreturn_t s3c2410wdt_irq(int irqno, void *param, | |
341 | struct pt_regs *regs) | |
342 | { | |
343 | printk(KERN_INFO PFX "Watchdog timer expired!\n"); | |
344 | ||
345 | s3c2410wdt_keepalive(); | |
346 | return IRQ_HANDLED; | |
347 | } | |
348 | /* device interface */ | |
349 | ||
350 | static int s3c2410wdt_probe(struct device *dev) | |
351 | { | |
352 | struct platform_device *pdev = to_platform_device(dev); | |
353 | struct resource *res; | |
354 | int started = 0; | |
355 | int ret; | |
356 | int size; | |
357 | ||
358 | DBG("%s: probe=%p, device=%p\n", __FUNCTION__, pdev, dev); | |
359 | ||
360 | /* get the memory region for the watchdog timer */ | |
361 | ||
362 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
363 | if (res == NULL) { | |
364 | printk(KERN_INFO PFX "failed to get memory region resouce\n"); | |
365 | return -ENOENT; | |
366 | } | |
367 | ||
368 | size = (res->end-res->start)+1; | |
369 | wdt_mem = request_mem_region(res->start, size, pdev->name); | |
370 | if (wdt_mem == NULL) { | |
371 | printk(KERN_INFO PFX "failed to get memory region\n"); | |
372 | return -ENOENT; | |
373 | } | |
374 | ||
375 | wdt_base = ioremap(res->start, size); | |
376 | if (wdt_base == 0) { | |
377 | printk(KERN_INFO PFX "failed to ioremap() region\n"); | |
378 | return -EINVAL; | |
379 | } | |
380 | ||
381 | DBG("probe: mapped wdt_base=%p\n", wdt_base); | |
382 | ||
383 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | |
384 | if (res == NULL) { | |
385 | printk(KERN_INFO PFX "failed to get irq resource\n"); | |
386 | return -ENOENT; | |
387 | } | |
388 | ||
389 | ret = request_irq(res->start, s3c2410wdt_irq, 0, pdev->name, dev); | |
390 | if (ret != 0) { | |
391 | printk(KERN_INFO PFX "failed to install irq (%d)\n", ret); | |
392 | return ret; | |
393 | } | |
394 | ||
395 | wdt_clock = clk_get(dev, "watchdog"); | |
396 | if (wdt_clock == NULL) { | |
397 | printk(KERN_INFO PFX "failed to find watchdog clock source\n"); | |
398 | return -ENOENT; | |
399 | } | |
400 | ||
401 | clk_use(wdt_clock); | |
402 | clk_enable(wdt_clock); | |
403 | ||
404 | /* see if we can actually set the requested timer margin, and if | |
405 | * not, try the default value */ | |
406 | ||
407 | if (s3c2410wdt_set_heartbeat(tmr_margin)) { | |
408 | started = s3c2410wdt_set_heartbeat(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME); | |
409 | ||
410 | if (started == 0) { | |
411 | printk(KERN_INFO PFX "tmr_margin value out of range, default %d used\n", | |
412 | CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME); | |
413 | } else { | |
414 | printk(KERN_INFO PFX "default timer value is out of range, cannot start\n"); | |
415 | } | |
416 | } | |
417 | ||
1da177e4 LT |
418 | ret = misc_register(&s3c2410wdt_miscdev); |
419 | if (ret) { | |
420 | printk (KERN_ERR PFX "cannot register miscdev on minor=%d (%d)\n", | |
421 | WATCHDOG_MINOR, ret); | |
1da177e4 LT |
422 | return ret; |
423 | } | |
424 | ||
425 | if (tmr_atboot && started == 0) { | |
426 | printk(KERN_INFO PFX "Starting Watchdog Timer\n"); | |
427 | s3c2410wdt_start(); | |
428 | } | |
429 | ||
430 | return 0; | |
431 | } | |
432 | ||
433 | static int s3c2410wdt_remove(struct device *dev) | |
434 | { | |
435 | if (wdt_mem != NULL) { | |
436 | release_resource(wdt_mem); | |
437 | kfree(wdt_mem); | |
438 | wdt_mem = NULL; | |
439 | } | |
440 | ||
441 | if (wdt_irq != NULL) { | |
442 | free_irq(wdt_irq->start, dev); | |
443 | wdt_irq = NULL; | |
444 | } | |
445 | ||
446 | if (wdt_clock != NULL) { | |
447 | clk_disable(wdt_clock); | |
448 | clk_unuse(wdt_clock); | |
449 | clk_put(wdt_clock); | |
450 | wdt_clock = NULL; | |
451 | } | |
452 | ||
453 | misc_deregister(&s3c2410wdt_miscdev); | |
454 | return 0; | |
455 | } | |
456 | ||
94f1e9f3 BD |
457 | static void s3c2410wdt_shutdown(struct device *dev) |
458 | { | |
459 | s3c2410wdt_stop(); | |
460 | } | |
461 | ||
af4bb822 BD |
462 | #ifdef CONFIG_PM |
463 | ||
464 | static unsigned long wtcon_save; | |
465 | static unsigned long wtdat_save; | |
466 | ||
9480e307 | 467 | static int s3c2410wdt_suspend(struct device *dev, pm_message_t state) |
af4bb822 | 468 | { |
9480e307 RK |
469 | /* Save watchdog state, and turn it off. */ |
470 | wtcon_save = readl(wdt_base + S3C2410_WTCON); | |
471 | wtdat_save = readl(wdt_base + S3C2410_WTDAT); | |
af4bb822 | 472 | |
9480e307 RK |
473 | /* Note that WTCNT doesn't need to be saved. */ |
474 | s3c2410wdt_stop(); | |
af4bb822 BD |
475 | |
476 | return 0; | |
477 | } | |
478 | ||
9480e307 | 479 | static int s3c2410wdt_resume(struct device *dev) |
af4bb822 | 480 | { |
9480e307 | 481 | /* Restore watchdog state. */ |
af4bb822 | 482 | |
9480e307 RK |
483 | writel(wtdat_save, wdt_base + S3C2410_WTDAT); |
484 | writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */ | |
485 | writel(wtcon_save, wdt_base + S3C2410_WTCON); | |
af4bb822 | 486 | |
9480e307 RK |
487 | printk(KERN_INFO PFX "watchdog %sabled\n", |
488 | (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis"); | |
af4bb822 BD |
489 | |
490 | return 0; | |
491 | } | |
492 | ||
493 | #else | |
494 | #define s3c2410wdt_suspend NULL | |
495 | #define s3c2410wdt_resume NULL | |
496 | #endif /* CONFIG_PM */ | |
497 | ||
498 | ||
1da177e4 LT |
499 | static struct device_driver s3c2410wdt_driver = { |
500 | .name = "s3c2410-wdt", | |
501 | .bus = &platform_bus_type, | |
502 | .probe = s3c2410wdt_probe, | |
503 | .remove = s3c2410wdt_remove, | |
94f1e9f3 | 504 | .shutdown = s3c2410wdt_shutdown, |
af4bb822 BD |
505 | .suspend = s3c2410wdt_suspend, |
506 | .resume = s3c2410wdt_resume, | |
1da177e4 LT |
507 | }; |
508 | ||
509 | ||
1da177e4 LT |
510 | static char banner[] __initdata = KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n"; |
511 | ||
512 | static int __init watchdog_init(void) | |
513 | { | |
514 | printk(banner); | |
515 | return driver_register(&s3c2410wdt_driver); | |
516 | } | |
517 | ||
518 | static void __exit watchdog_exit(void) | |
519 | { | |
520 | driver_unregister(&s3c2410wdt_driver); | |
1da177e4 LT |
521 | } |
522 | ||
523 | module_init(watchdog_init); | |
524 | module_exit(watchdog_exit); | |
525 | ||
af4bb822 BD |
526 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, " |
527 | "Dimitry Andric <dimitry.andric@tomtom.com>"); | |
1da177e4 LT |
528 | MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver"); |
529 | MODULE_LICENSE("GPL"); | |
530 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |