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@lxorguk.ukuu.org.uk>
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/types.h>
31 #include <linux/timer.h>
32 #include <linux/miscdevice.h> /* for MODULE_ALIAS_MISCDEV */
33 #include <linux/watchdog.h>
34 #include <linux/init.h>
35 #include <linux/platform_device.h>
36 #include <linux/interrupt.h>
37 #include <linux/clk.h>
38 #include <linux/uaccess.h>
40 #include <linux/cpufreq.h>
41 #include <linux/slab.h>
42 #include <linux/err.h>
45 #define S3C2410_WTCON 0x00
46 #define S3C2410_WTDAT 0x04
47 #define S3C2410_WTCNT 0x08
49 #define S3C2410_WTCON_RSTEN (1 << 0)
50 #define S3C2410_WTCON_INTEN (1 << 2)
51 #define S3C2410_WTCON_ENABLE (1 << 5)
53 #define S3C2410_WTCON_DIV16 (0 << 3)
54 #define S3C2410_WTCON_DIV32 (1 << 3)
55 #define S3C2410_WTCON_DIV64 (2 << 3)
56 #define S3C2410_WTCON_DIV128 (3 << 3)
58 #define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
59 #define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
61 #define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
62 #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
64 static bool nowayout = WATCHDOG_NOWAYOUT;
65 static int tmr_margin;
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, bool, 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, "
84 "0 to reboot (default 0)");
85 MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug (default 0)");
90 void __iomem *reg_base;
93 unsigned long wtcon_save;
94 unsigned long wtdat_save;
95 struct watchdog_device wdt_device;
96 struct notifier_block freq_transition;
99 /* watchdog control routines */
101 #define DBG(fmt, ...) \
104 pr_info(fmt, ##__VA_ARGS__); \
109 static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
111 return container_of(nb, struct s3c2410_wdt, freq_transition);
114 static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
116 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
118 spin_lock(&wdt->lock);
119 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
120 spin_unlock(&wdt->lock);
125 static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
129 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
130 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
131 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
134 static int s3c2410wdt_stop(struct watchdog_device *wdd)
136 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
138 spin_lock(&wdt->lock);
139 __s3c2410wdt_stop(wdt);
140 spin_unlock(&wdt->lock);
145 static int s3c2410wdt_start(struct watchdog_device *wdd)
148 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
150 spin_lock(&wdt->lock);
152 __s3c2410wdt_stop(wdt);
154 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
155 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
158 wtcon |= S3C2410_WTCON_INTEN;
159 wtcon &= ~S3C2410_WTCON_RSTEN;
161 wtcon &= ~S3C2410_WTCON_INTEN;
162 wtcon |= S3C2410_WTCON_RSTEN;
165 DBG("%s: count=0x%08x, wtcon=%08lx\n",
166 __func__, wdt->count, wtcon);
168 writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
169 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
170 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
171 spin_unlock(&wdt->lock);
176 static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
178 return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
181 static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeout)
183 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
184 unsigned long freq = clk_get_rate(wdt->clock);
186 unsigned int divisor = 1;
193 count = timeout * freq;
195 DBG("%s: count=%d, timeout=%d, freq=%lu\n",
196 __func__, count, timeout, freq);
198 /* if the count is bigger than the watchdog register,
199 then work out what we need to do (and if) we can
200 actually make this value
203 if (count >= 0x10000) {
204 for (divisor = 1; divisor <= 0x100; divisor++) {
205 if ((count / divisor) < 0x10000)
209 if ((count / divisor) >= 0x10000) {
210 dev_err(wdt->dev, "timeout %d too big\n", timeout);
215 DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
216 __func__, timeout, divisor, count, count/divisor);
221 /* update the pre-scaler */
222 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
223 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
224 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
226 writel(count, wdt->reg_base + S3C2410_WTDAT);
227 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
229 wdd->timeout = (count * divisor) / freq;
234 #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
236 static const struct watchdog_info s3c2410_wdt_ident = {
238 .firmware_version = 0,
239 .identity = "S3C2410 Watchdog",
242 static struct watchdog_ops s3c2410wdt_ops = {
243 .owner = THIS_MODULE,
244 .start = s3c2410wdt_start,
245 .stop = s3c2410wdt_stop,
246 .ping = s3c2410wdt_keepalive,
247 .set_timeout = s3c2410wdt_set_heartbeat,
250 static struct watchdog_device s3c2410_wdd = {
251 .info = &s3c2410_wdt_ident,
252 .ops = &s3c2410wdt_ops,
253 .timeout = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME,
256 /* interrupt handler code */
258 static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
260 struct s3c2410_wdt *wdt = platform_get_drvdata(param);
262 dev_info(wdt->dev, "watchdog timer expired (irq)\n");
264 s3c2410wdt_keepalive(&wdt->wdt_device);
268 #ifdef CONFIG_CPU_FREQ
270 static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
271 unsigned long val, void *data)
274 struct s3c2410_wdt *wdt = freq_to_wdt(nb);
276 if (!s3c2410wdt_is_running(wdt))
279 if (val == CPUFREQ_PRECHANGE) {
280 /* To ensure that over the change we don't cause the
281 * watchdog to trigger, we perform an keep-alive if
282 * the watchdog is running.
285 s3c2410wdt_keepalive(&wdt->wdt_device);
286 } else if (val == CPUFREQ_POSTCHANGE) {
287 s3c2410wdt_stop(&wdt->wdt_device);
289 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
290 wdt->wdt_device.timeout);
293 s3c2410wdt_start(&wdt->wdt_device);
302 dev_err(wdt->dev, "cannot set new value for timeout %d\n",
303 wdt->wdt_device.timeout);
307 static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
309 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
311 return cpufreq_register_notifier(&wdt->freq_transition,
312 CPUFREQ_TRANSITION_NOTIFIER);
315 static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
317 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
319 cpufreq_unregister_notifier(&wdt->freq_transition,
320 CPUFREQ_TRANSITION_NOTIFIER);
325 static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
330 static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
335 static int s3c2410wdt_probe(struct platform_device *pdev)
338 struct s3c2410_wdt *wdt;
339 struct resource *wdt_mem;
340 struct resource *wdt_irq;
345 DBG("%s: probe=%p\n", __func__, pdev);
349 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
353 wdt->dev = &pdev->dev;
354 spin_lock_init(&wdt->lock);
355 wdt->wdt_device = s3c2410_wdd;
356 wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
357 if (wdt_mem == NULL) {
358 dev_err(dev, "no memory resource specified\n");
362 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
363 if (wdt_irq == NULL) {
364 dev_err(dev, "no irq resource specified\n");
369 /* get the memory region for the watchdog timer */
370 wdt->reg_base = devm_ioremap_resource(dev, wdt_mem);
371 if (IS_ERR(wdt->reg_base)) {
372 ret = PTR_ERR(wdt->reg_base);
376 DBG("probe: mapped reg_base=%p\n", wdt->reg_base);
378 wdt->clock = devm_clk_get(dev, "watchdog");
379 if (IS_ERR(wdt->clock)) {
380 dev_err(dev, "failed to find watchdog clock source\n");
381 ret = PTR_ERR(wdt->clock);
385 clk_prepare_enable(wdt->clock);
387 ret = s3c2410wdt_cpufreq_register(wdt);
389 dev_err(dev, "failed to register cpufreq\n");
393 watchdog_set_drvdata(&wdt->wdt_device, wdt);
395 /* see if we can actually set the requested timer margin, and if
396 * not, try the default value */
398 watchdog_init_timeout(&wdt->wdt_device, tmr_margin, &pdev->dev);
399 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
400 wdt->wdt_device.timeout);
402 started = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
403 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
407 "tmr_margin value out of range, default %d used\n",
408 CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
410 dev_info(dev, "default timer value is out of range, "
414 ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
417 dev_err(dev, "failed to install irq (%d)\n", ret);
421 watchdog_set_nowayout(&wdt->wdt_device, nowayout);
423 ret = watchdog_register_device(&wdt->wdt_device);
425 dev_err(dev, "cannot register watchdog (%d)\n", ret);
429 if (tmr_atboot && started == 0) {
430 dev_info(dev, "starting watchdog timer\n");
431 s3c2410wdt_start(&wdt->wdt_device);
432 } else if (!tmr_atboot) {
433 /* if we're not enabling the watchdog, then ensure it is
434 * disabled if it has been left running from the bootloader
437 s3c2410wdt_stop(&wdt->wdt_device);
440 platform_set_drvdata(pdev, wdt);
442 /* print out a statement of readiness */
444 wtcon = readl(wdt->reg_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) ? "en" : "dis",
449 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
454 s3c2410wdt_cpufreq_deregister(wdt);
457 clk_disable_unprepare(wdt->clock);
464 static int s3c2410wdt_remove(struct platform_device *dev)
466 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
468 watchdog_unregister_device(&wdt->wdt_device);
470 s3c2410wdt_cpufreq_deregister(wdt);
472 clk_disable_unprepare(wdt->clock);
478 static void s3c2410wdt_shutdown(struct platform_device *dev)
480 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
482 s3c2410wdt_stop(&wdt->wdt_device);
485 #ifdef CONFIG_PM_SLEEP
487 static int s3c2410wdt_suspend(struct device *dev)
489 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
491 /* Save watchdog state, and turn it off. */
492 wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
493 wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
495 /* Note that WTCNT doesn't need to be saved. */
496 s3c2410wdt_stop(&wdt->wdt_device);
501 static int s3c2410wdt_resume(struct device *dev)
503 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
505 /* Restore watchdog state. */
506 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
507 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
508 writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
510 dev_info(dev, "watchdog %sabled\n",
511 (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
517 static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
521 static const struct of_device_id s3c2410_wdt_match[] = {
522 { .compatible = "samsung,s3c2410-wdt" },
525 MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
528 static struct platform_driver s3c2410wdt_driver = {
529 .probe = s3c2410wdt_probe,
530 .remove = s3c2410wdt_remove,
531 .shutdown = s3c2410wdt_shutdown,
533 .owner = THIS_MODULE,
534 .name = "s3c2410-wdt",
535 .pm = &s3c2410wdt_pm_ops,
536 .of_match_table = of_match_ptr(s3c2410_wdt_match),
540 module_platform_driver(s3c2410wdt_driver);
542 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
543 "Dimitry Andric <dimitry.andric@tomtom.com>");
544 MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
545 MODULE_LICENSE("GPL");
546 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
547 MODULE_ALIAS("platform:s3c2410-wdt");