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/types.h>
41 #include <linux/timer.h>
42 #include <linux/miscdevice.h>
43 #include <linux/watchdog.h>
45 #include <linux/init.h>
46 #include <linux/platform_device.h>
47 #include <linux/interrupt.h>
48 #include <linux/clk.h>
49 #include <linux/uaccess.h>
54 #undef S3C_VA_WATCHDOG
55 #define S3C_VA_WATCHDOG (0)
57 #include <asm/plat-s3c/regs-watchdog.h>
59 #define PFX "s3c2410-wdt: "
61 #define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
62 #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
64 static int nowayout = WATCHDOG_NOWAYOUT;
65 static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
66 static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
67 static int soft_noboot;
70 module_param(tmr_margin, int, 0);
71 module_param(tmr_atboot, int, 0);
72 module_param(nowayout, int, 0);
73 module_param(soft_noboot, int, 0);
74 module_param(debug, int, 0);
76 MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. default="
77 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
78 MODULE_PARM_DESC(tmr_atboot,
79 "Watchdog is started at boot time if set to 1, default="
80 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
81 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
82 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
83 MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default depends on ONLY_TESTING)");
84 MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug, (default 0)");
87 typedef enum close_state {
89 CLOSE_STATE_ALLOW = 0x4021
92 static unsigned long open_lock;
93 static struct device *wdt_dev; /* platform device attached to */
94 static struct resource *wdt_mem;
95 static struct resource *wdt_irq;
96 static struct clk *wdt_clock;
97 static void __iomem *wdt_base;
98 static unsigned int wdt_count;
99 static close_state_t allow_close;
100 static DEFINE_SPINLOCK(wdt_lock);
102 /* watchdog control routines */
104 #define DBG(msg...) do { \
106 printk(KERN_INFO msg); \
111 static void s3c2410wdt_keepalive(void)
113 spin_lock(&wdt_lock);
114 writel(wdt_count, wdt_base + S3C2410_WTCNT);
115 spin_unlock(&wdt_lock);
118 static void __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);
127 static void s3c2410wdt_stop(void)
129 spin_lock(&wdt_lock);
131 spin_unlock(&wdt_lock);
134 static void s3c2410wdt_start(void)
138 spin_lock(&wdt_lock);
142 wtcon = readl(wdt_base + S3C2410_WTCON);
143 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
146 wtcon |= S3C2410_WTCON_INTEN;
147 wtcon &= ~S3C2410_WTCON_RSTEN;
149 wtcon &= ~S3C2410_WTCON_INTEN;
150 wtcon |= S3C2410_WTCON_RSTEN;
153 DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
154 __func__, wdt_count, wtcon);
156 writel(wdt_count, wdt_base + S3C2410_WTDAT);
157 writel(wdt_count, wdt_base + S3C2410_WTCNT);
158 writel(wtcon, wdt_base + S3C2410_WTCON);
159 spin_unlock(&wdt_lock);
164 static int s3c2410wdt_set_heartbeat(int timeout)
166 unsigned int freq = clk_get_rate(wdt_clock);
168 unsigned int divisor = 1;
175 count = timeout * freq;
177 DBG("%s: count=%d, timeout=%d, freq=%d\n",
178 __func__, count, timeout, freq);
180 /* if the count is bigger than the watchdog register,
181 then work out what we need to do (and if) we can
182 actually make this value
185 if (count >= 0x10000) {
186 for (divisor = 1; divisor <= 0x100; divisor++) {
187 if ((count / divisor) < 0x10000)
191 if ((count / divisor) >= 0x10000) {
192 dev_err(wdt_dev, "timeout %d too big\n", timeout);
197 tmr_margin = timeout;
199 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
200 __func__, timeout, divisor, count, count/divisor);
205 /* update the pre-scaler */
206 wtcon = readl(wdt_base + S3C2410_WTCON);
207 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
208 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
210 writel(count, wdt_base + S3C2410_WTDAT);
211 writel(wtcon, wdt_base + S3C2410_WTCON);
217 * /dev/watchdog handling
220 static int s3c2410wdt_open(struct inode *inode, struct file *file)
222 if (test_and_set_bit(0, &open_lock))
226 __module_get(THIS_MODULE);
228 allow_close = CLOSE_STATE_NOT;
230 /* start the timer */
232 return nonseekable_open(inode, file);
235 static int s3c2410wdt_release(struct inode *inode, struct file *file)
238 * Shut off the timer.
239 * Lock it in if it's a module and we set nowayout
242 if (allow_close == CLOSE_STATE_ALLOW)
245 dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");
246 s3c2410wdt_keepalive();
248 allow_close = CLOSE_STATE_NOT;
249 clear_bit(0, &open_lock);
253 static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
254 size_t len, loff_t *ppos)
263 /* In case it was set long ago */
264 allow_close = CLOSE_STATE_NOT;
266 for (i = 0; i != len; i++) {
269 if (get_user(c, data + i))
272 allow_close = CLOSE_STATE_ALLOW;
275 s3c2410wdt_keepalive();
280 #define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE
282 static const struct watchdog_info s3c2410_wdt_ident = {
284 .firmware_version = 0,
285 .identity = "S3C2410 Watchdog",
289 static long s3c2410wdt_ioctl(struct file *file, unsigned int cmd,
292 void __user *argp = (void __user *)arg;
293 int __user *p = argp;
297 case WDIOC_GETSUPPORT:
298 return copy_to_user(argp, &s3c2410_wdt_ident,
299 sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0;
300 case WDIOC_GETSTATUS:
301 case WDIOC_GETBOOTSTATUS:
302 return put_user(0, p);
303 case WDIOC_KEEPALIVE:
304 s3c2410wdt_keepalive();
306 case WDIOC_SETTIMEOUT:
307 if (get_user(new_margin, p))
309 if (s3c2410wdt_set_heartbeat(new_margin))
311 s3c2410wdt_keepalive();
312 return put_user(tmr_margin, p);
313 case WDIOC_GETTIMEOUT:
314 return put_user(tmr_margin, p);
320 /* kernel interface */
322 static const struct file_operations s3c2410wdt_fops = {
323 .owner = THIS_MODULE,
325 .write = s3c2410wdt_write,
326 .unlocked_ioctl = s3c2410wdt_ioctl,
327 .open = s3c2410wdt_open,
328 .release = s3c2410wdt_release,
331 static struct miscdevice s3c2410wdt_miscdev = {
332 .minor = WATCHDOG_MINOR,
334 .fops = &s3c2410wdt_fops,
337 /* interrupt handler code */
339 static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
341 dev_info(wdt_dev, "watchdog timer expired (irq)\n");
343 s3c2410wdt_keepalive();
346 /* device interface */
348 static int s3c2410wdt_probe(struct platform_device *pdev)
350 struct resource *res;
357 DBG("%s: probe=%p\n", __func__, pdev);
360 wdt_dev = &pdev->dev;
362 /* get the memory region for the watchdog timer */
364 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
366 dev_err(dev, "no memory resource specified\n");
370 size = (res->end-res->start)+1;
371 wdt_mem = request_mem_region(res->start, size, pdev->name);
372 if (wdt_mem == NULL) {
373 dev_err(dev, "failed to get memory region\n");
378 wdt_base = ioremap(res->start, size);
380 dev_err(dev, "failed to ioremap() region\n");
385 DBG("probe: mapped wdt_base=%p\n", wdt_base);
387 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
388 if (wdt_irq == NULL) {
389 dev_err(dev, "no irq resource specified\n");
394 ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
396 dev_err(dev, "failed to install irq (%d)\n", ret);
400 wdt_clock = clk_get(&pdev->dev, "watchdog");
401 if (IS_ERR(wdt_clock)) {
402 dev_err(dev, "failed to find watchdog clock source\n");
403 ret = PTR_ERR(wdt_clock);
407 clk_enable(wdt_clock);
409 /* see if we can actually set the requested timer margin, and if
410 * not, try the default value */
412 if (s3c2410wdt_set_heartbeat(tmr_margin)) {
413 started = s3c2410wdt_set_heartbeat(
414 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
418 "tmr_margin value out of range, default %d used\n",
419 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
421 dev_info(dev, "default timer value is out of range, cannot start\n");
424 ret = misc_register(&s3c2410wdt_miscdev);
426 dev_err(dev, "cannot register miscdev on minor=%d (%d)\n",
427 WATCHDOG_MINOR, ret);
431 if (tmr_atboot && started == 0) {
432 dev_info(dev, "starting watchdog timer\n");
434 } else if (!tmr_atboot) {
435 /* if we're not enabling the watchdog, then ensure it is
436 * disabled if it has been left running from the bootloader
442 /* print out a statement of readiness */
444 wtcon = readl(wdt_base + S3C2410_WTCON);
446 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
447 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
448 (wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",
449 (wtcon & S3C2410_WTCON_INTEN) ? "" : "en");
454 clk_disable(wdt_clock);
458 free_irq(wdt_irq->start, pdev);
464 release_resource(wdt_mem);
470 static int s3c2410wdt_remove(struct platform_device *dev)
472 release_resource(wdt_mem);
476 free_irq(wdt_irq->start, dev);
479 clk_disable(wdt_clock);
484 misc_deregister(&s3c2410wdt_miscdev);
488 static void s3c2410wdt_shutdown(struct platform_device *dev)
495 static unsigned long wtcon_save;
496 static unsigned long wtdat_save;
498 static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
500 /* Save watchdog state, and turn it off. */
501 wtcon_save = readl(wdt_base + S3C2410_WTCON);
502 wtdat_save = readl(wdt_base + S3C2410_WTDAT);
504 /* Note that WTCNT doesn't need to be saved. */
510 static int s3c2410wdt_resume(struct platform_device *dev)
512 /* Restore watchdog state. */
514 writel(wtdat_save, wdt_base + S3C2410_WTDAT);
515 writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
516 writel(wtcon_save, wdt_base + S3C2410_WTCON);
518 printk(KERN_INFO PFX "watchdog %sabled\n",
519 (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
525 #define s3c2410wdt_suspend NULL
526 #define s3c2410wdt_resume NULL
527 #endif /* CONFIG_PM */
530 static struct platform_driver s3c2410wdt_driver = {
531 .probe = s3c2410wdt_probe,
532 .remove = s3c2410wdt_remove,
533 .shutdown = s3c2410wdt_shutdown,
534 .suspend = s3c2410wdt_suspend,
535 .resume = s3c2410wdt_resume,
537 .owner = THIS_MODULE,
538 .name = "s3c2410-wdt",
543 static char banner[] __initdata =
544 KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
546 static int __init watchdog_init(void)
549 return platform_driver_register(&s3c2410wdt_driver);
552 static void __exit watchdog_exit(void)
554 platform_driver_unregister(&s3c2410wdt_driver);
557 module_init(watchdog_init);
558 module_exit(watchdog_exit);
560 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
561 "Dimitry Andric <dimitry.andric@tomtom.com>");
562 MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
563 MODULE_LICENSE("GPL");
564 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
565 MODULE_ALIAS("platform:s3c2410-wdt");