1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/types.h>
15 #include <linux/timer.h>
16 #include <linux/watchdog.h>
17 #include <linux/platform_device.h>
18 #include <linux/interrupt.h>
19 #include <linux/clk.h>
20 #include <linux/uaccess.h>
22 #include <linux/cpufreq.h>
23 #include <linux/slab.h>
24 #include <linux/err.h>
26 #include <linux/of_device.h>
27 #include <linux/mfd/syscon.h>
28 #include <linux/regmap.h>
29 #include <linux/delay.h>
31 #define S3C2410_WTCON 0x00
32 #define S3C2410_WTDAT 0x04
33 #define S3C2410_WTCNT 0x08
34 #define S3C2410_WTCLRINT 0x0c
36 #define S3C2410_WTCNT_MAXCNT 0xffff
38 #define S3C2410_WTCON_RSTEN (1 << 0)
39 #define S3C2410_WTCON_INTEN (1 << 2)
40 #define S3C2410_WTCON_ENABLE (1 << 5)
42 #define S3C2410_WTCON_DIV16 (0 << 3)
43 #define S3C2410_WTCON_DIV32 (1 << 3)
44 #define S3C2410_WTCON_DIV64 (2 << 3)
45 #define S3C2410_WTCON_DIV128 (3 << 3)
47 #define S3C2410_WTCON_MAXDIV 0x80
49 #define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
50 #define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
51 #define S3C2410_WTCON_PRESCALE_MAX 0xff
53 #define S3C2410_WATCHDOG_ATBOOT (0)
54 #define S3C2410_WATCHDOG_DEFAULT_TIME (15)
56 #define EXYNOS5_RST_STAT_REG_OFFSET 0x0404
57 #define EXYNOS5_WDT_DISABLE_REG_OFFSET 0x0408
58 #define EXYNOS5_WDT_MASK_RESET_REG_OFFSET 0x040c
59 #define QUIRK_HAS_PMU_CONFIG (1 << 0)
60 #define QUIRK_HAS_RST_STAT (1 << 1)
61 #define QUIRK_HAS_WTCLRINT_REG (1 << 2)
63 /* These quirks require that we have a PMU register map */
64 #define QUIRKS_HAVE_PMUREG (QUIRK_HAS_PMU_CONFIG | \
67 static bool nowayout = WATCHDOG_NOWAYOUT;
68 static int tmr_margin;
69 static int tmr_atboot = S3C2410_WATCHDOG_ATBOOT;
70 static int soft_noboot;
72 module_param(tmr_margin, int, 0);
73 module_param(tmr_atboot, int, 0);
74 module_param(nowayout, bool, 0);
75 module_param(soft_noboot, int, 0);
77 MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
78 __MODULE_STRING(S3C2410_WATCHDOG_DEFAULT_TIME) ")");
79 MODULE_PARM_DESC(tmr_atboot,
80 "Watchdog is started at boot time if set to 1, default="
81 __MODULE_STRING(S3C2410_WATCHDOG_ATBOOT));
82 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
83 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
84 MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default 0)");
87 * struct s3c2410_wdt_variant - Per-variant config data
89 * @disable_reg: Offset in pmureg for the register that disables the watchdog
90 * timer reset functionality.
91 * @mask_reset_reg: Offset in pmureg for the register that masks the watchdog
92 * timer reset functionality.
93 * @mask_bit: Bit number for the watchdog timer in the disable register and the
94 * mask reset register.
95 * @rst_stat_reg: Offset in pmureg for the register that has the reset status.
96 * @rst_stat_bit: Bit number in the rst_stat register indicating a watchdog
98 * @quirks: A bitfield of quirks.
101 struct s3c2410_wdt_variant {
113 void __iomem *reg_base;
116 unsigned long wtcon_save;
117 unsigned long wtdat_save;
118 struct watchdog_device wdt_device;
119 struct notifier_block freq_transition;
120 const struct s3c2410_wdt_variant *drv_data;
121 struct regmap *pmureg;
124 static const struct s3c2410_wdt_variant drv_data_s3c2410 = {
129 static const struct s3c2410_wdt_variant drv_data_s3c6410 = {
130 .quirks = QUIRK_HAS_WTCLRINT_REG,
133 static const struct s3c2410_wdt_variant drv_data_exynos5250 = {
134 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
135 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
137 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
139 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
140 | QUIRK_HAS_WTCLRINT_REG,
143 static const struct s3c2410_wdt_variant drv_data_exynos5420 = {
144 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
145 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
147 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
149 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
150 | QUIRK_HAS_WTCLRINT_REG,
153 static const struct s3c2410_wdt_variant drv_data_exynos7 = {
154 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
155 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
157 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
158 .rst_stat_bit = 23, /* A57 WDTRESET */
159 .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
160 | QUIRK_HAS_WTCLRINT_REG,
163 static const struct of_device_id s3c2410_wdt_match[] = {
164 { .compatible = "samsung,s3c2410-wdt",
165 .data = &drv_data_s3c2410 },
166 { .compatible = "samsung,s3c6410-wdt",
167 .data = &drv_data_s3c6410 },
168 { .compatible = "samsung,exynos5250-wdt",
169 .data = &drv_data_exynos5250 },
170 { .compatible = "samsung,exynos5420-wdt",
171 .data = &drv_data_exynos5420 },
172 { .compatible = "samsung,exynos7-wdt",
173 .data = &drv_data_exynos7 },
176 MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
179 static const struct platform_device_id s3c2410_wdt_ids[] = {
181 .name = "s3c2410-wdt",
182 .driver_data = (unsigned long)&drv_data_s3c2410,
186 MODULE_DEVICE_TABLE(platform, s3c2410_wdt_ids);
190 static inline unsigned int s3c2410wdt_max_timeout(struct clk *clock)
192 unsigned long freq = clk_get_rate(clock);
194 return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1)
195 / S3C2410_WTCON_MAXDIV);
198 static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
200 return container_of(nb, struct s3c2410_wdt, freq_transition);
203 static int s3c2410wdt_mask_and_disable_reset(struct s3c2410_wdt *wdt, bool mask)
206 u32 mask_val = 1 << wdt->drv_data->mask_bit;
209 /* No need to do anything if no PMU CONFIG needed */
210 if (!(wdt->drv_data->quirks & QUIRK_HAS_PMU_CONFIG))
216 ret = regmap_update_bits(wdt->pmureg,
217 wdt->drv_data->disable_reg,
222 ret = regmap_update_bits(wdt->pmureg,
223 wdt->drv_data->mask_reset_reg,
227 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
232 static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
234 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
236 spin_lock(&wdt->lock);
237 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
238 spin_unlock(&wdt->lock);
243 static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
247 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
248 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
249 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
252 static int s3c2410wdt_stop(struct watchdog_device *wdd)
254 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
256 spin_lock(&wdt->lock);
257 __s3c2410wdt_stop(wdt);
258 spin_unlock(&wdt->lock);
263 static int s3c2410wdt_start(struct watchdog_device *wdd)
266 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
268 spin_lock(&wdt->lock);
270 __s3c2410wdt_stop(wdt);
272 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
273 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
276 wtcon |= S3C2410_WTCON_INTEN;
277 wtcon &= ~S3C2410_WTCON_RSTEN;
279 wtcon &= ~S3C2410_WTCON_INTEN;
280 wtcon |= S3C2410_WTCON_RSTEN;
283 dev_dbg(wdt->dev, "Starting watchdog: count=0x%08x, wtcon=%08lx\n",
286 writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
287 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
288 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
289 spin_unlock(&wdt->lock);
294 static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
296 return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
299 static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd,
300 unsigned int timeout)
302 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
303 unsigned long freq = clk_get_rate(wdt->clock);
305 unsigned int divisor = 1;
311 freq = DIV_ROUND_UP(freq, 128);
312 count = timeout * freq;
314 dev_dbg(wdt->dev, "Heartbeat: count=%d, timeout=%d, freq=%lu\n",
315 count, timeout, freq);
317 /* if the count is bigger than the watchdog register,
318 then work out what we need to do (and if) we can
319 actually make this value
322 if (count >= 0x10000) {
323 divisor = DIV_ROUND_UP(count, 0xffff);
325 if (divisor > 0x100) {
326 dev_err(wdt->dev, "timeout %d too big\n", timeout);
331 dev_dbg(wdt->dev, "Heartbeat: timeout=%d, divisor=%d, count=%d (%08x)\n",
332 timeout, divisor, count, DIV_ROUND_UP(count, divisor));
334 count = DIV_ROUND_UP(count, divisor);
337 /* update the pre-scaler */
338 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
339 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
340 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
342 writel(count, wdt->reg_base + S3C2410_WTDAT);
343 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
345 wdd->timeout = (count * divisor) / freq;
350 static int s3c2410wdt_restart(struct watchdog_device *wdd, unsigned long action,
353 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
354 void __iomem *wdt_base = wdt->reg_base;
356 /* disable watchdog, to be safe */
357 writel(0, wdt_base + S3C2410_WTCON);
359 /* put initial values into count and data */
360 writel(0x80, wdt_base + S3C2410_WTCNT);
361 writel(0x80, wdt_base + S3C2410_WTDAT);
363 /* set the watchdog to go and reset... */
364 writel(S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV16 |
365 S3C2410_WTCON_RSTEN | S3C2410_WTCON_PRESCALE(0x20),
366 wdt_base + S3C2410_WTCON);
368 /* wait for reset to assert... */
374 #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
376 static const struct watchdog_info s3c2410_wdt_ident = {
378 .firmware_version = 0,
379 .identity = "S3C2410 Watchdog",
382 static const struct watchdog_ops s3c2410wdt_ops = {
383 .owner = THIS_MODULE,
384 .start = s3c2410wdt_start,
385 .stop = s3c2410wdt_stop,
386 .ping = s3c2410wdt_keepalive,
387 .set_timeout = s3c2410wdt_set_heartbeat,
388 .restart = s3c2410wdt_restart,
391 static const struct watchdog_device s3c2410_wdd = {
392 .info = &s3c2410_wdt_ident,
393 .ops = &s3c2410wdt_ops,
394 .timeout = S3C2410_WATCHDOG_DEFAULT_TIME,
397 /* interrupt handler code */
399 static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
401 struct s3c2410_wdt *wdt = platform_get_drvdata(param);
403 dev_info(wdt->dev, "watchdog timer expired (irq)\n");
405 s3c2410wdt_keepalive(&wdt->wdt_device);
407 if (wdt->drv_data->quirks & QUIRK_HAS_WTCLRINT_REG)
408 writel(0x1, wdt->reg_base + S3C2410_WTCLRINT);
413 #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
415 static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
416 unsigned long val, void *data)
419 struct s3c2410_wdt *wdt = freq_to_wdt(nb);
421 if (!s3c2410wdt_is_running(wdt))
424 if (val == CPUFREQ_PRECHANGE) {
425 /* To ensure that over the change we don't cause the
426 * watchdog to trigger, we perform an keep-alive if
427 * the watchdog is running.
430 s3c2410wdt_keepalive(&wdt->wdt_device);
431 } else if (val == CPUFREQ_POSTCHANGE) {
432 s3c2410wdt_stop(&wdt->wdt_device);
434 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
435 wdt->wdt_device.timeout);
438 s3c2410wdt_start(&wdt->wdt_device);
447 dev_err(wdt->dev, "cannot set new value for timeout %d\n",
448 wdt->wdt_device.timeout);
452 static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
454 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
456 return cpufreq_register_notifier(&wdt->freq_transition,
457 CPUFREQ_TRANSITION_NOTIFIER);
460 static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
462 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
464 cpufreq_unregister_notifier(&wdt->freq_transition,
465 CPUFREQ_TRANSITION_NOTIFIER);
470 static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
475 static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
480 static inline unsigned int s3c2410wdt_get_bootstatus(struct s3c2410_wdt *wdt)
482 unsigned int rst_stat;
485 if (!(wdt->drv_data->quirks & QUIRK_HAS_RST_STAT))
488 ret = regmap_read(wdt->pmureg, wdt->drv_data->rst_stat_reg, &rst_stat);
490 dev_warn(wdt->dev, "Couldn't get RST_STAT register\n");
491 else if (rst_stat & BIT(wdt->drv_data->rst_stat_bit))
492 return WDIOF_CARDRESET;
497 static inline const struct s3c2410_wdt_variant *
498 s3c2410_get_wdt_drv_data(struct platform_device *pdev)
500 const struct s3c2410_wdt_variant *variant;
502 variant = of_device_get_match_data(&pdev->dev);
504 /* Device matched by platform_device_id */
505 variant = (struct s3c2410_wdt_variant *)
506 platform_get_device_id(pdev)->driver_data;
512 static int s3c2410wdt_probe(struct platform_device *pdev)
514 struct device *dev = &pdev->dev;
515 struct s3c2410_wdt *wdt;
516 struct resource *wdt_irq;
521 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
526 spin_lock_init(&wdt->lock);
527 wdt->wdt_device = s3c2410_wdd;
529 wdt->drv_data = s3c2410_get_wdt_drv_data(pdev);
530 if (wdt->drv_data->quirks & QUIRKS_HAVE_PMUREG) {
531 wdt->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
532 "samsung,syscon-phandle");
533 if (IS_ERR(wdt->pmureg)) {
534 dev_err(dev, "syscon regmap lookup failed.\n");
535 return PTR_ERR(wdt->pmureg);
539 wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
540 if (wdt_irq == NULL) {
541 dev_err(dev, "no irq resource specified\n");
546 /* get the memory region for the watchdog timer */
547 wdt->reg_base = devm_platform_ioremap_resource(pdev, 0);
548 if (IS_ERR(wdt->reg_base)) {
549 ret = PTR_ERR(wdt->reg_base);
553 wdt->clock = devm_clk_get(dev, "watchdog");
554 if (IS_ERR(wdt->clock)) {
555 dev_err(dev, "failed to find watchdog clock source\n");
556 ret = PTR_ERR(wdt->clock);
560 ret = clk_prepare_enable(wdt->clock);
562 dev_err(dev, "failed to enable clock\n");
566 wdt->wdt_device.min_timeout = 1;
567 wdt->wdt_device.max_timeout = s3c2410wdt_max_timeout(wdt->clock);
569 ret = s3c2410wdt_cpufreq_register(wdt);
571 dev_err(dev, "failed to register cpufreq\n");
575 watchdog_set_drvdata(&wdt->wdt_device, wdt);
577 /* see if we can actually set the requested timer margin, and if
578 * not, try the default value */
580 watchdog_init_timeout(&wdt->wdt_device, tmr_margin, dev);
581 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
582 wdt->wdt_device.timeout);
584 started = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
585 S3C2410_WATCHDOG_DEFAULT_TIME);
589 "tmr_margin value out of range, default %d used\n",
590 S3C2410_WATCHDOG_DEFAULT_TIME);
592 dev_info(dev, "default timer value is out of range, cannot start\n");
595 ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
598 dev_err(dev, "failed to install irq (%d)\n", ret);
602 watchdog_set_nowayout(&wdt->wdt_device, nowayout);
603 watchdog_set_restart_priority(&wdt->wdt_device, 128);
605 wdt->wdt_device.bootstatus = s3c2410wdt_get_bootstatus(wdt);
606 wdt->wdt_device.parent = dev;
608 ret = watchdog_register_device(&wdt->wdt_device);
612 ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
616 if (tmr_atboot && started == 0) {
617 dev_info(dev, "starting watchdog timer\n");
618 s3c2410wdt_start(&wdt->wdt_device);
619 } else if (!tmr_atboot) {
620 /* if we're not enabling the watchdog, then ensure it is
621 * disabled if it has been left running from the bootloader
624 s3c2410wdt_stop(&wdt->wdt_device);
627 platform_set_drvdata(pdev, wdt);
629 /* print out a statement of readiness */
631 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
633 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
634 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
635 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
636 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
641 watchdog_unregister_device(&wdt->wdt_device);
644 s3c2410wdt_cpufreq_deregister(wdt);
647 clk_disable_unprepare(wdt->clock);
653 static int s3c2410wdt_remove(struct platform_device *dev)
656 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
658 ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
662 watchdog_unregister_device(&wdt->wdt_device);
664 s3c2410wdt_cpufreq_deregister(wdt);
666 clk_disable_unprepare(wdt->clock);
671 static void s3c2410wdt_shutdown(struct platform_device *dev)
673 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
675 s3c2410wdt_mask_and_disable_reset(wdt, true);
677 s3c2410wdt_stop(&wdt->wdt_device);
680 #ifdef CONFIG_PM_SLEEP
682 static int s3c2410wdt_suspend(struct device *dev)
685 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
687 /* Save watchdog state, and turn it off. */
688 wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
689 wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
691 ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
695 /* Note that WTCNT doesn't need to be saved. */
696 s3c2410wdt_stop(&wdt->wdt_device);
701 static int s3c2410wdt_resume(struct device *dev)
704 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
706 /* Restore watchdog state. */
707 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
708 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
709 writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
711 ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
715 dev_info(dev, "watchdog %sabled\n",
716 (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
722 static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
725 static struct platform_driver s3c2410wdt_driver = {
726 .probe = s3c2410wdt_probe,
727 .remove = s3c2410wdt_remove,
728 .shutdown = s3c2410wdt_shutdown,
729 .id_table = s3c2410_wdt_ids,
731 .name = "s3c2410-wdt",
732 .pm = &s3c2410wdt_pm_ops,
733 .of_match_table = of_match_ptr(s3c2410_wdt_match),
737 module_platform_driver(s3c2410wdt_driver);
739 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Dimitry Andric <dimitry.andric@tomtom.com>");
740 MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
741 MODULE_LICENSE("GPL");