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 EXYNOS850_CLUSTER0_NONCPU_OUT 0x1220
60 #define EXYNOS850_CLUSTER0_NONCPU_INT_EN 0x1244
61 #define EXYNOS850_CLUSTER1_NONCPU_OUT 0x1620
62 #define EXYNOS850_CLUSTER1_NONCPU_INT_EN 0x1644
64 #define EXYNOS850_CLUSTER0_WDTRESET_BIT 24
65 #define EXYNOS850_CLUSTER1_WDTRESET_BIT 23
68 * DOC: Quirk flags for different Samsung watchdog IP-cores
70 * This driver supports multiple Samsung SoCs, each of which might have
71 * different set of registers and features supported. As watchdog block
72 * sometimes requires modifying PMU registers for proper functioning, register
73 * differences in both watchdog and PMU IP-cores should be accounted for. Quirk
74 * flags described below serve the purpose of telling the driver about mentioned
75 * SoC traits, and can be specified in driver data for each particular supported
78 * %QUIRK_HAS_WTCLRINT_REG: Watchdog block has WTCLRINT register. It's used to
79 * clear the interrupt once the interrupt service routine is complete. It's
80 * write-only, writing any values to this register clears the interrupt, but
81 * reading is not permitted.
83 * %QUIRK_HAS_PMU_MASK_RESET: PMU block has the register for disabling/enabling
84 * WDT reset request. On old SoCs it's usually called MASK_WDT_RESET_REQUEST,
85 * new SoCs have CLUSTERx_NONCPU_INT_EN register, which 'mask_bit' value is
86 * inverted compared to the former one.
88 * %QUIRK_HAS_PMU_RST_STAT: PMU block has RST_STAT (reset status) register,
89 * which contains bits indicating the reason for most recent CPU reset. If
90 * present, driver will use this register to check if previous reboot was due to
91 * watchdog timer reset.
93 * %QUIRK_HAS_PMU_AUTO_DISABLE: PMU block has AUTOMATIC_WDT_RESET_DISABLE
94 * register. If 'mask_bit' bit is set, PMU will disable WDT reset when
95 * corresponding processor is in reset state.
97 * %QUIRK_HAS_PMU_CNT_EN: PMU block has some register (e.g. CLUSTERx_NONCPU_OUT)
98 * with "watchdog counter enable" bit. That bit should be set to make watchdog
101 #define QUIRK_HAS_WTCLRINT_REG (1 << 0)
102 #define QUIRK_HAS_PMU_MASK_RESET (1 << 1)
103 #define QUIRK_HAS_PMU_RST_STAT (1 << 2)
104 #define QUIRK_HAS_PMU_AUTO_DISABLE (1 << 3)
105 #define QUIRK_HAS_PMU_CNT_EN (1 << 4)
107 /* These quirks require that we have a PMU register map */
108 #define QUIRKS_HAVE_PMUREG \
109 (QUIRK_HAS_PMU_MASK_RESET | QUIRK_HAS_PMU_RST_STAT | \
110 QUIRK_HAS_PMU_AUTO_DISABLE | QUIRK_HAS_PMU_CNT_EN)
112 static bool nowayout = WATCHDOG_NOWAYOUT;
113 static int tmr_margin;
114 static int tmr_atboot = S3C2410_WATCHDOG_ATBOOT;
115 static int soft_noboot;
117 module_param(tmr_margin, int, 0);
118 module_param(tmr_atboot, int, 0);
119 module_param(nowayout, bool, 0);
120 module_param(soft_noboot, int, 0);
122 MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
123 __MODULE_STRING(S3C2410_WATCHDOG_DEFAULT_TIME) ")");
124 MODULE_PARM_DESC(tmr_atboot,
125 "Watchdog is started at boot time if set to 1, default="
126 __MODULE_STRING(S3C2410_WATCHDOG_ATBOOT));
127 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
128 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
129 MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default 0)");
132 * struct s3c2410_wdt_variant - Per-variant config data
134 * @disable_reg: Offset in pmureg for the register that disables the watchdog
135 * timer reset functionality.
136 * @mask_reset_reg: Offset in pmureg for the register that masks the watchdog
137 * timer reset functionality.
138 * @mask_reset_inv: If set, mask_reset_reg value will have inverted meaning.
139 * @mask_bit: Bit number for the watchdog timer in the disable register and the
140 * mask reset register.
141 * @rst_stat_reg: Offset in pmureg for the register that has the reset status.
142 * @rst_stat_bit: Bit number in the rst_stat register indicating a watchdog
144 * @cnt_en_reg: Offset in pmureg for the register that enables WDT counter.
145 * @cnt_en_bit: Bit number for "watchdog counter enable" in cnt_en register.
146 * @quirks: A bitfield of quirks.
149 struct s3c2410_wdt_variant {
163 struct clk *bus_clk; /* for register interface (PCLK) */
164 struct clk *src_clk; /* for WDT counter */
165 void __iomem *reg_base;
168 unsigned long wtcon_save;
169 unsigned long wtdat_save;
170 struct watchdog_device wdt_device;
171 struct notifier_block freq_transition;
172 const struct s3c2410_wdt_variant *drv_data;
173 struct regmap *pmureg;
176 static const struct s3c2410_wdt_variant drv_data_s3c2410 = {
181 static const struct s3c2410_wdt_variant drv_data_s3c6410 = {
182 .quirks = QUIRK_HAS_WTCLRINT_REG,
185 static const struct s3c2410_wdt_variant drv_data_exynos5250 = {
186 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
187 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
189 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
191 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
192 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
195 static const struct s3c2410_wdt_variant drv_data_exynos5420 = {
196 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
197 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
199 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
201 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
202 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
205 static const struct s3c2410_wdt_variant drv_data_exynos7 = {
206 .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
207 .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
209 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
210 .rst_stat_bit = 23, /* A57 WDTRESET */
211 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
212 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
215 static const struct s3c2410_wdt_variant drv_data_exynos850_cl0 = {
216 .mask_reset_reg = EXYNOS850_CLUSTER0_NONCPU_INT_EN,
218 .mask_reset_inv = true,
219 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
220 .rst_stat_bit = EXYNOS850_CLUSTER0_WDTRESET_BIT,
221 .cnt_en_reg = EXYNOS850_CLUSTER0_NONCPU_OUT,
223 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
224 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN,
227 static const struct s3c2410_wdt_variant drv_data_exynos850_cl1 = {
228 .mask_reset_reg = EXYNOS850_CLUSTER1_NONCPU_INT_EN,
230 .mask_reset_inv = true,
231 .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
232 .rst_stat_bit = EXYNOS850_CLUSTER1_WDTRESET_BIT,
233 .cnt_en_reg = EXYNOS850_CLUSTER1_NONCPU_OUT,
235 .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
236 QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN,
239 static const struct of_device_id s3c2410_wdt_match[] = {
240 { .compatible = "samsung,s3c2410-wdt",
241 .data = &drv_data_s3c2410 },
242 { .compatible = "samsung,s3c6410-wdt",
243 .data = &drv_data_s3c6410 },
244 { .compatible = "samsung,exynos5250-wdt",
245 .data = &drv_data_exynos5250 },
246 { .compatible = "samsung,exynos5420-wdt",
247 .data = &drv_data_exynos5420 },
248 { .compatible = "samsung,exynos7-wdt",
249 .data = &drv_data_exynos7 },
250 { .compatible = "samsung,exynos850-wdt",
251 .data = &drv_data_exynos850_cl0 },
254 MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
257 static const struct platform_device_id s3c2410_wdt_ids[] = {
259 .name = "s3c2410-wdt",
260 .driver_data = (unsigned long)&drv_data_s3c2410,
264 MODULE_DEVICE_TABLE(platform, s3c2410_wdt_ids);
268 static inline unsigned long s3c2410wdt_get_freq(struct s3c2410_wdt *wdt)
270 return clk_get_rate(wdt->src_clk ? wdt->src_clk : wdt->bus_clk);
273 static inline unsigned int s3c2410wdt_max_timeout(struct s3c2410_wdt *wdt)
275 const unsigned long freq = s3c2410wdt_get_freq(wdt);
277 return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1)
278 / S3C2410_WTCON_MAXDIV);
281 static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
283 return container_of(nb, struct s3c2410_wdt, freq_transition);
286 static int s3c2410wdt_disable_wdt_reset(struct s3c2410_wdt *wdt, bool mask)
288 const u32 mask_val = BIT(wdt->drv_data->mask_bit);
289 const u32 val = mask ? mask_val : 0;
292 ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->disable_reg,
295 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
300 static int s3c2410wdt_mask_wdt_reset(struct s3c2410_wdt *wdt, bool mask)
302 const u32 mask_val = BIT(wdt->drv_data->mask_bit);
303 const bool val_inv = wdt->drv_data->mask_reset_inv;
304 const u32 val = (mask ^ val_inv) ? mask_val : 0;
307 ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->mask_reset_reg,
310 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
315 static int s3c2410wdt_enable_counter(struct s3c2410_wdt *wdt, bool en)
317 const u32 mask_val = BIT(wdt->drv_data->cnt_en_bit);
318 const u32 val = en ? mask_val : 0;
321 ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->cnt_en_reg,
324 dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
329 static int s3c2410wdt_enable(struct s3c2410_wdt *wdt, bool en)
333 if (wdt->drv_data->quirks & QUIRK_HAS_PMU_AUTO_DISABLE) {
334 ret = s3c2410wdt_disable_wdt_reset(wdt, !en);
339 if (wdt->drv_data->quirks & QUIRK_HAS_PMU_MASK_RESET) {
340 ret = s3c2410wdt_mask_wdt_reset(wdt, !en);
345 if (wdt->drv_data->quirks & QUIRK_HAS_PMU_CNT_EN) {
346 ret = s3c2410wdt_enable_counter(wdt, en);
354 static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
356 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
358 spin_lock(&wdt->lock);
359 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
360 spin_unlock(&wdt->lock);
365 static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
369 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
370 wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
371 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
374 static int s3c2410wdt_stop(struct watchdog_device *wdd)
376 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
378 spin_lock(&wdt->lock);
379 __s3c2410wdt_stop(wdt);
380 spin_unlock(&wdt->lock);
385 static int s3c2410wdt_start(struct watchdog_device *wdd)
388 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
390 spin_lock(&wdt->lock);
392 __s3c2410wdt_stop(wdt);
394 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
395 wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
398 wtcon |= S3C2410_WTCON_INTEN;
399 wtcon &= ~S3C2410_WTCON_RSTEN;
401 wtcon &= ~S3C2410_WTCON_INTEN;
402 wtcon |= S3C2410_WTCON_RSTEN;
405 dev_dbg(wdt->dev, "Starting watchdog: count=0x%08x, wtcon=%08lx\n",
408 writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
409 writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
410 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
411 spin_unlock(&wdt->lock);
416 static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
418 return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
421 static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd,
422 unsigned int timeout)
424 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
425 unsigned long freq = s3c2410wdt_get_freq(wdt);
427 unsigned int divisor = 1;
433 freq = DIV_ROUND_UP(freq, 128);
434 count = timeout * freq;
436 dev_dbg(wdt->dev, "Heartbeat: count=%d, timeout=%d, freq=%lu\n",
437 count, timeout, freq);
439 /* if the count is bigger than the watchdog register,
440 then work out what we need to do (and if) we can
441 actually make this value
444 if (count >= 0x10000) {
445 divisor = DIV_ROUND_UP(count, 0xffff);
447 if (divisor > 0x100) {
448 dev_err(wdt->dev, "timeout %d too big\n", timeout);
453 dev_dbg(wdt->dev, "Heartbeat: timeout=%d, divisor=%d, count=%d (%08x)\n",
454 timeout, divisor, count, DIV_ROUND_UP(count, divisor));
456 count = DIV_ROUND_UP(count, divisor);
459 /* update the pre-scaler */
460 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
461 wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
462 wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
464 writel(count, wdt->reg_base + S3C2410_WTDAT);
465 writel(wtcon, wdt->reg_base + S3C2410_WTCON);
467 wdd->timeout = (count * divisor) / freq;
472 static int s3c2410wdt_restart(struct watchdog_device *wdd, unsigned long action,
475 struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
476 void __iomem *wdt_base = wdt->reg_base;
478 /* disable watchdog, to be safe */
479 writel(0, wdt_base + S3C2410_WTCON);
481 /* put initial values into count and data */
482 writel(0x80, wdt_base + S3C2410_WTCNT);
483 writel(0x80, wdt_base + S3C2410_WTDAT);
485 /* set the watchdog to go and reset... */
486 writel(S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV16 |
487 S3C2410_WTCON_RSTEN | S3C2410_WTCON_PRESCALE(0x20),
488 wdt_base + S3C2410_WTCON);
490 /* wait for reset to assert... */
496 #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
498 static const struct watchdog_info s3c2410_wdt_ident = {
500 .firmware_version = 0,
501 .identity = "S3C2410 Watchdog",
504 static const struct watchdog_ops s3c2410wdt_ops = {
505 .owner = THIS_MODULE,
506 .start = s3c2410wdt_start,
507 .stop = s3c2410wdt_stop,
508 .ping = s3c2410wdt_keepalive,
509 .set_timeout = s3c2410wdt_set_heartbeat,
510 .restart = s3c2410wdt_restart,
513 static const struct watchdog_device s3c2410_wdd = {
514 .info = &s3c2410_wdt_ident,
515 .ops = &s3c2410wdt_ops,
516 .timeout = S3C2410_WATCHDOG_DEFAULT_TIME,
519 /* interrupt handler code */
521 static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
523 struct s3c2410_wdt *wdt = platform_get_drvdata(param);
525 dev_info(wdt->dev, "watchdog timer expired (irq)\n");
527 s3c2410wdt_keepalive(&wdt->wdt_device);
529 if (wdt->drv_data->quirks & QUIRK_HAS_WTCLRINT_REG)
530 writel(0x1, wdt->reg_base + S3C2410_WTCLRINT);
535 #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
537 static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
538 unsigned long val, void *data)
541 struct s3c2410_wdt *wdt = freq_to_wdt(nb);
543 if (!s3c2410wdt_is_running(wdt))
546 if (val == CPUFREQ_PRECHANGE) {
547 /* To ensure that over the change we don't cause the
548 * watchdog to trigger, we perform an keep-alive if
549 * the watchdog is running.
552 s3c2410wdt_keepalive(&wdt->wdt_device);
553 } else if (val == CPUFREQ_POSTCHANGE) {
554 s3c2410wdt_stop(&wdt->wdt_device);
556 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
557 wdt->wdt_device.timeout);
560 s3c2410wdt_start(&wdt->wdt_device);
569 dev_err(wdt->dev, "cannot set new value for timeout %d\n",
570 wdt->wdt_device.timeout);
574 static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
576 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
578 return cpufreq_register_notifier(&wdt->freq_transition,
579 CPUFREQ_TRANSITION_NOTIFIER);
582 static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
584 wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
586 cpufreq_unregister_notifier(&wdt->freq_transition,
587 CPUFREQ_TRANSITION_NOTIFIER);
592 static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
597 static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
602 static inline unsigned int s3c2410wdt_get_bootstatus(struct s3c2410_wdt *wdt)
604 unsigned int rst_stat;
607 if (!(wdt->drv_data->quirks & QUIRK_HAS_PMU_RST_STAT))
610 ret = regmap_read(wdt->pmureg, wdt->drv_data->rst_stat_reg, &rst_stat);
612 dev_warn(wdt->dev, "Couldn't get RST_STAT register\n");
613 else if (rst_stat & BIT(wdt->drv_data->rst_stat_bit))
614 return WDIOF_CARDRESET;
619 static inline const struct s3c2410_wdt_variant *
620 s3c2410_get_wdt_drv_data(struct platform_device *pdev)
622 const struct s3c2410_wdt_variant *variant;
623 struct device *dev = &pdev->dev;
625 variant = of_device_get_match_data(dev);
627 /* Device matched by platform_device_id */
628 variant = (struct s3c2410_wdt_variant *)
629 platform_get_device_id(pdev)->driver_data;
633 /* Choose Exynos850 driver data w.r.t. cluster index */
634 if (variant == &drv_data_exynos850_cl0) {
638 err = of_property_read_u32(dev->of_node,
639 "samsung,cluster-index", &index);
641 dev_err(dev, "failed to get cluster index\n");
647 return &drv_data_exynos850_cl0;
649 return &drv_data_exynos850_cl1;
651 dev_err(dev, "wrong cluster index: %u\n", index);
660 static int s3c2410wdt_probe(struct platform_device *pdev)
662 struct device *dev = &pdev->dev;
663 struct s3c2410_wdt *wdt;
668 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
673 spin_lock_init(&wdt->lock);
674 wdt->wdt_device = s3c2410_wdd;
676 wdt->drv_data = s3c2410_get_wdt_drv_data(pdev);
680 if (wdt->drv_data->quirks & QUIRKS_HAVE_PMUREG) {
681 wdt->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
682 "samsung,syscon-phandle");
683 if (IS_ERR(wdt->pmureg)) {
684 dev_err(dev, "syscon regmap lookup failed.\n");
685 return PTR_ERR(wdt->pmureg);
689 wdt_irq = platform_get_irq(pdev, 0);
693 /* get the memory region for the watchdog timer */
694 wdt->reg_base = devm_platform_ioremap_resource(pdev, 0);
695 if (IS_ERR(wdt->reg_base))
696 return PTR_ERR(wdt->reg_base);
698 wdt->bus_clk = devm_clk_get(dev, "watchdog");
699 if (IS_ERR(wdt->bus_clk)) {
700 dev_err(dev, "failed to find bus clock\n");
701 return PTR_ERR(wdt->bus_clk);
704 ret = clk_prepare_enable(wdt->bus_clk);
706 dev_err(dev, "failed to enable bus clock\n");
711 * "watchdog_src" clock is optional; if it's not present -- just skip it
712 * and use "watchdog" clock as both bus and source clock.
714 wdt->src_clk = devm_clk_get_optional(dev, "watchdog_src");
715 if (IS_ERR(wdt->src_clk)) {
716 dev_err_probe(dev, PTR_ERR(wdt->src_clk),
717 "failed to get source clock\n");
718 ret = PTR_ERR(wdt->src_clk);
722 ret = clk_prepare_enable(wdt->src_clk);
724 dev_err(dev, "failed to enable source clock\n");
728 wdt->wdt_device.min_timeout = 1;
729 wdt->wdt_device.max_timeout = s3c2410wdt_max_timeout(wdt);
731 ret = s3c2410wdt_cpufreq_register(wdt);
733 dev_err(dev, "failed to register cpufreq\n");
737 watchdog_set_drvdata(&wdt->wdt_device, wdt);
739 /* see if we can actually set the requested timer margin, and if
740 * not, try the default value */
742 watchdog_init_timeout(&wdt->wdt_device, tmr_margin, dev);
743 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
744 wdt->wdt_device.timeout);
746 ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
747 S3C2410_WATCHDOG_DEFAULT_TIME);
749 dev_warn(dev, "tmr_margin value out of range, default %d used\n",
750 S3C2410_WATCHDOG_DEFAULT_TIME);
752 dev_err(dev, "failed to use default timeout\n");
757 ret = devm_request_irq(dev, wdt_irq, s3c2410wdt_irq, 0,
760 dev_err(dev, "failed to install irq (%d)\n", ret);
764 watchdog_set_nowayout(&wdt->wdt_device, nowayout);
765 watchdog_set_restart_priority(&wdt->wdt_device, 128);
767 wdt->wdt_device.bootstatus = s3c2410wdt_get_bootstatus(wdt);
768 wdt->wdt_device.parent = dev;
771 * If "tmr_atboot" param is non-zero, start the watchdog right now. Also
772 * set WDOG_HW_RUNNING bit, so that watchdog core can kick the watchdog.
774 * If we're not enabling the watchdog, then ensure it is disabled if it
775 * has been left running from the bootloader or other source.
778 dev_info(dev, "starting watchdog timer\n");
779 s3c2410wdt_start(&wdt->wdt_device);
780 set_bit(WDOG_HW_RUNNING, &wdt->wdt_device.status);
782 s3c2410wdt_stop(&wdt->wdt_device);
785 ret = watchdog_register_device(&wdt->wdt_device);
789 ret = s3c2410wdt_enable(wdt, true);
793 platform_set_drvdata(pdev, wdt);
795 /* print out a statement of readiness */
797 wtcon = readl(wdt->reg_base + S3C2410_WTCON);
799 dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
800 (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
801 (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
802 (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
807 watchdog_unregister_device(&wdt->wdt_device);
810 s3c2410wdt_cpufreq_deregister(wdt);
813 clk_disable_unprepare(wdt->src_clk);
816 clk_disable_unprepare(wdt->bus_clk);
821 static int s3c2410wdt_remove(struct platform_device *dev)
824 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
826 ret = s3c2410wdt_enable(wdt, false);
830 watchdog_unregister_device(&wdt->wdt_device);
832 s3c2410wdt_cpufreq_deregister(wdt);
834 clk_disable_unprepare(wdt->src_clk);
835 clk_disable_unprepare(wdt->bus_clk);
840 static void s3c2410wdt_shutdown(struct platform_device *dev)
842 struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
844 s3c2410wdt_enable(wdt, false);
845 s3c2410wdt_stop(&wdt->wdt_device);
848 #ifdef CONFIG_PM_SLEEP
850 static int s3c2410wdt_suspend(struct device *dev)
853 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
855 /* Save watchdog state, and turn it off. */
856 wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
857 wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
859 ret = s3c2410wdt_enable(wdt, false);
863 /* Note that WTCNT doesn't need to be saved. */
864 s3c2410wdt_stop(&wdt->wdt_device);
869 static int s3c2410wdt_resume(struct device *dev)
872 struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
874 /* Restore watchdog state. */
875 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
876 writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
877 writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
879 ret = s3c2410wdt_enable(wdt, true);
883 dev_info(dev, "watchdog %sabled\n",
884 (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
890 static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
893 static struct platform_driver s3c2410wdt_driver = {
894 .probe = s3c2410wdt_probe,
895 .remove = s3c2410wdt_remove,
896 .shutdown = s3c2410wdt_shutdown,
897 .id_table = s3c2410_wdt_ids,
899 .name = "s3c2410-wdt",
900 .pm = &s3c2410wdt_pm_ops,
901 .of_match_table = of_match_ptr(s3c2410_wdt_match),
905 module_platform_driver(s3c2410wdt_driver);
907 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Dimitry Andric <dimitry.andric@tomtom.com>");
908 MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
909 MODULE_LICENSE("GPL");