1 /* linux/drivers/char/watchdog/s3c2410_wdt.c
3 * Copyright (c) 2004 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
6 * S3C2410 Watchdog Timer Support
8 * Based on, softdog.c by Alan Cox,
9 * (c) Copyright 1996 Alan Cox <alan@redhat.com>
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.
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.
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
26 * 05-Oct-2004 BJD Added semaphore init to stop crashes on open
27 * Fixed tmr_count / wdt_count confusion
28 * Added configurable debug
30 * 11-Jan-2005 BJD Fixed divide-by-2 in timeout code
32 * 25-Jan-2005 DA Added suspend/resume support
33 * Replaced reboot notifier with .shutdown method
35 * 10-Mar-2005 LCVR Changed S3C2410_VA to S3C24XX_VA
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>
46 #include <linux/init.h>
47 #include <linux/platform_device.h>
48 #include <linux/interrupt.h>
50 #include <asm/uaccess.h>
53 #include <asm/arch/map.h>
54 #include <asm/hardware/clock.h>
56 #undef S3C24XX_VA_WATCHDOG
57 #define S3C24XX_VA_WATCHDOG (0)
59 #include <asm/arch/regs-watchdog.h>
61 #define PFX "s3c2410-wdt: "
63 #define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
64 #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
66 static int nowayout
= WATCHDOG_NOWAYOUT
;
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;
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);
78 MODULE_PARM_DESC(tmr_margin
, "Watchdog tmr_margin in seconds. default=" __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME
) ")");
80 MODULE_PARM_DESC(tmr_atboot
, "Watchdog is started at boot time if set to 1, default=" __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT
));
82 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
84 MODULE_PARM_DESC(soft_noboot
, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default depends on ONLY_TESTING)");
86 MODULE_PARM_DESC(debug
, "Watchdog debug, set to >1 for debug, (default 0)");
89 typedef enum close_state
{
91 CLOSE_STATE_ALLOW
=0x4021
94 static DECLARE_MUTEX(open_lock
);
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
;
103 /* watchdog control routines */
105 #define DBG(msg...) do { \
107 printk(KERN_INFO msg); \
112 static int s3c2410wdt_keepalive(void)
114 writel(wdt_count
, wdt_base
+ S3C2410_WTCNT
);
118 static int s3c2410wdt_stop(void)
122 wtcon
= readl(wdt_base
+ S3C2410_WTCON
);
123 wtcon
&= ~(S3C2410_WTCON_ENABLE
| S3C2410_WTCON_RSTEN
);
124 writel(wtcon
, wdt_base
+ S3C2410_WTCON
);
129 static int s3c2410wdt_start(void)
135 wtcon
= readl(wdt_base
+ S3C2410_WTCON
);
136 wtcon
|= S3C2410_WTCON_ENABLE
| S3C2410_WTCON_DIV128
;
139 wtcon
|= S3C2410_WTCON_INTEN
;
140 wtcon
&= ~S3C2410_WTCON_RSTEN
;
142 wtcon
&= ~S3C2410_WTCON_INTEN
;
143 wtcon
|= S3C2410_WTCON_RSTEN
;
146 DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
147 __FUNCTION__
, wdt_count
, wtcon
);
149 writel(wdt_count
, wdt_base
+ S3C2410_WTDAT
);
150 writel(wdt_count
, wdt_base
+ S3C2410_WTCNT
);
151 writel(wtcon
, wdt_base
+ S3C2410_WTCON
);
156 static int s3c2410wdt_set_heartbeat(int timeout
)
158 unsigned int freq
= clk_get_rate(wdt_clock
);
160 unsigned int divisor
= 1;
167 count
= timeout
* freq
;
169 DBG("%s: count=%d, timeout=%d, freq=%d\n",
170 __FUNCTION__
, count
, timeout
, freq
);
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
177 if (count
>= 0x10000) {
178 for (divisor
= 1; divisor
<= 0x100; divisor
++) {
179 if ((count
/ divisor
) < 0x10000)
183 if ((count
/ divisor
) >= 0x10000) {
184 printk(KERN_ERR PFX
"timeout %d too big\n", timeout
);
189 tmr_margin
= timeout
;
191 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
192 __FUNCTION__
, timeout
, divisor
, count
, count
/divisor
);
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);
202 writel(count
, wdt_base
+ S3C2410_WTDAT
);
203 writel(wtcon
, wdt_base
+ S3C2410_WTCON
);
209 * /dev/watchdog handling
212 static int s3c2410wdt_open(struct inode
*inode
, struct file
*file
)
214 if(down_trylock(&open_lock
))
218 __module_get(THIS_MODULE
);
220 allow_close
= CLOSE_STATE_ALLOW
;
223 /* start the timer */
225 return nonseekable_open(inode
, file
);
228 static int s3c2410wdt_release(struct inode
*inode
, struct file
*file
)
231 * Shut off the timer.
232 * Lock it in if it's a module and we set nowayout
234 if (allow_close
== CLOSE_STATE_ALLOW
) {
237 printk(KERN_CRIT PFX
"Unexpected close, not stopping watchdog!\n");
238 s3c2410wdt_keepalive();
241 allow_close
= CLOSE_STATE_NOT
;
246 static ssize_t
s3c2410wdt_write(struct file
*file
, const char __user
*data
,
247 size_t len
, loff_t
*ppos
)
256 /* In case it was set long ago */
257 allow_close
= CLOSE_STATE_NOT
;
259 for (i
= 0; i
!= len
; i
++) {
262 if (get_user(c
, data
+ i
))
265 allow_close
= CLOSE_STATE_ALLOW
;
269 s3c2410wdt_keepalive();
274 #define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE
276 static struct watchdog_info s3c2410_wdt_ident
= {
278 .firmware_version
= 0,
279 .identity
= "S3C2410 Watchdog",
283 static int s3c2410wdt_ioctl(struct inode
*inode
, struct file
*file
,
284 unsigned int cmd
, unsigned long arg
)
286 void __user
*argp
= (void __user
*)arg
;
287 int __user
*p
= argp
;
294 case WDIOC_GETSUPPORT
:
295 return copy_to_user(argp
, &s3c2410_wdt_ident
,
296 sizeof(s3c2410_wdt_ident
)) ? -EFAULT
: 0;
298 case WDIOC_GETSTATUS
:
299 case WDIOC_GETBOOTSTATUS
:
300 return put_user(0, p
);
302 case WDIOC_KEEPALIVE
:
303 s3c2410wdt_keepalive();
306 case WDIOC_SETTIMEOUT
:
307 if (get_user(new_margin
, p
))
310 if (s3c2410wdt_set_heartbeat(new_margin
))
313 s3c2410wdt_keepalive();
314 return put_user(tmr_margin
, p
);
316 case WDIOC_GETTIMEOUT
:
317 return put_user(tmr_margin
, p
);
321 /* kernel interface */
323 static struct file_operations s3c2410wdt_fops
= {
324 .owner
= THIS_MODULE
,
326 .write
= s3c2410wdt_write
,
327 .ioctl
= s3c2410wdt_ioctl
,
328 .open
= s3c2410wdt_open
,
329 .release
= s3c2410wdt_release
,
332 static struct miscdevice s3c2410wdt_miscdev
= {
333 .minor
= WATCHDOG_MINOR
,
335 .fops
= &s3c2410wdt_fops
,
338 /* interrupt handler code */
340 static irqreturn_t
s3c2410wdt_irq(int irqno
, void *param
,
341 struct pt_regs
*regs
)
343 printk(KERN_INFO PFX
"Watchdog timer expired!\n");
345 s3c2410wdt_keepalive();
348 /* device interface */
350 static int s3c2410wdt_probe(struct platform_device
*pdev
)
352 struct resource
*res
;
357 DBG("%s: probe=%p\n", __FUNCTION__
, pdev
);
359 /* get the memory region for the watchdog timer */
361 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
363 printk(KERN_INFO PFX
"failed to get memory region resouce\n");
367 size
= (res
->end
-res
->start
)+1;
368 wdt_mem
= request_mem_region(res
->start
, size
, pdev
->name
);
369 if (wdt_mem
== NULL
) {
370 printk(KERN_INFO PFX
"failed to get memory region\n");
374 wdt_base
= ioremap(res
->start
, size
);
376 printk(KERN_INFO PFX
"failed to ioremap() region\n");
380 DBG("probe: mapped wdt_base=%p\n", wdt_base
);
382 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
384 printk(KERN_INFO PFX
"failed to get irq resource\n");
388 ret
= request_irq(res
->start
, s3c2410wdt_irq
, 0, pdev
->name
, pdev
);
390 printk(KERN_INFO PFX
"failed to install irq (%d)\n", ret
);
394 wdt_clock
= clk_get(&pdev
->dev
, "watchdog");
395 if (wdt_clock
== NULL
) {
396 printk(KERN_INFO PFX
"failed to find watchdog clock source\n");
401 clk_enable(wdt_clock
);
403 /* see if we can actually set the requested timer margin, and if
404 * not, try the default value */
406 if (s3c2410wdt_set_heartbeat(tmr_margin
)) {
407 started
= s3c2410wdt_set_heartbeat(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME
);
410 printk(KERN_INFO PFX
"tmr_margin value out of range, default %d used\n",
411 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME
);
413 printk(KERN_INFO PFX
"default timer value is out of range, cannot start\n");
417 ret
= misc_register(&s3c2410wdt_miscdev
);
419 printk (KERN_ERR PFX
"cannot register miscdev on minor=%d (%d)\n",
420 WATCHDOG_MINOR
, ret
);
424 if (tmr_atboot
&& started
== 0) {
425 printk(KERN_INFO PFX
"Starting Watchdog Timer\n");
432 static int s3c2410wdt_remove(struct platform_device
*dev
)
434 if (wdt_mem
!= NULL
) {
435 release_resource(wdt_mem
);
440 if (wdt_irq
!= NULL
) {
441 free_irq(wdt_irq
->start
, dev
);
445 if (wdt_clock
!= NULL
) {
446 clk_disable(wdt_clock
);
447 clk_unuse(wdt_clock
);
452 misc_deregister(&s3c2410wdt_miscdev
);
456 static void s3c2410wdt_shutdown(struct platform_device
*dev
)
463 static unsigned long wtcon_save
;
464 static unsigned long wtdat_save
;
466 static int s3c2410wdt_suspend(struct platform_device
*dev
, pm_message_t state
)
468 /* Save watchdog state, and turn it off. */
469 wtcon_save
= readl(wdt_base
+ S3C2410_WTCON
);
470 wtdat_save
= readl(wdt_base
+ S3C2410_WTDAT
);
472 /* Note that WTCNT doesn't need to be saved. */
478 static int s3c2410wdt_resume(struct platform_device
*dev
)
480 /* Restore watchdog state. */
482 writel(wtdat_save
, wdt_base
+ S3C2410_WTDAT
);
483 writel(wtdat_save
, wdt_base
+ S3C2410_WTCNT
); /* Reset count */
484 writel(wtcon_save
, wdt_base
+ S3C2410_WTCON
);
486 printk(KERN_INFO PFX
"watchdog %sabled\n",
487 (wtcon_save
& S3C2410_WTCON_ENABLE
) ? "en" : "dis");
493 #define s3c2410wdt_suspend NULL
494 #define s3c2410wdt_resume NULL
495 #endif /* CONFIG_PM */
498 static struct platform_driver s3c2410wdt_driver
= {
499 .probe
= s3c2410wdt_probe
,
500 .remove
= s3c2410wdt_remove
,
501 .shutdown
= s3c2410wdt_shutdown
,
502 .suspend
= s3c2410wdt_suspend
,
503 .resume
= s3c2410wdt_resume
,
505 .owner
= THIS_MODULE
,
506 .name
= "s3c2410-wdt",
511 static char banner
[] __initdata
= KERN_INFO
"S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
513 static int __init
watchdog_init(void)
516 return platform_driver_register(&s3c2410wdt_driver
);
519 static void __exit
watchdog_exit(void)
521 platform_driver_unregister(&s3c2410wdt_driver
);
524 module_init(watchdog_init
);
525 module_exit(watchdog_exit
);
527 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
528 "Dimitry Andric <dimitry.andric@tomtom.com>");
529 MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
530 MODULE_LICENSE("GPL");
531 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);