1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2021 StarFive, Inc <samin.guo@starfivetech.com>
4 * Copyright 2022 StarFive, Inc <xingyu.wu@starfivetech.com>
6 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING
7 * CUSTOMERS WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER
8 * FOR THEM TO SAVE TIME. AS A RESULT, STARFIVE SHALL NOT BE HELD LIABLE
9 * FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY
10 * CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE
11 * BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONNECTION
12 * WITH THEIR PRODUCTS.
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/mfd/syscon.h>
22 #include <linux/iopoll.h>
23 #include <linux/interrupt.h>
25 #include <linux/of_device.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <linux/timer.h>
30 #include <linux/uaccess.h>
31 #include <linux/watchdog.h>
32 #include <linux/reset.h>
33 #include <linux/reset-controller.h>
34 #include <linux/pm_runtime.h>
36 #define WDT_INT_DIS BIT(0)
38 #define TIMEOUT_US 10000
40 /* JH7100 WatchDog register define */
41 #define JH7100_WDGINTSTAUS 0x000
42 #define JH7100_WDOGCONTROL 0x104 /* Watchdog Control Register R/W */
43 #define JH7100_WDOGLOAD 0x108 /* The initial value to be loaded */
44 /* into the counter and is also used */
45 /* as the reload value. R/W */
46 #define JH7100_WDOGEN 0x110 /* Watchdog enable Register */
47 #define JH7100_WDOGRELOAD 0x114 /* Write this register to reload preset */
48 /* value to counter. (Write 0 or 1 are both ok) */
49 #define JH7100_WDOGVALUE 0x118 /* Watchdog Value Register RO */
50 #define JH7100_WDOGINTCLR 0x120 /* Watchdog Clear Interrupt Register WO */
51 #define JH7100_WDOGINTMSK 0x124 /* Watchdog Interrupt Mask Register */
52 #define JH7100_WDOGLOCK 0x13c /* Watchdog Lock Register R/W */
54 #define JH7100_UNLOCK_KEY 0x378f0765
55 #define JH7100_RESEN_SHIFT 0
56 #define JH7100_EN_SHIFT 0
57 #define JH7100_INTCLR_AVA_SHIFT 1 /* Watchdog can clear interrupt when this bit is 0 */
59 /* JH7110 WatchDog register define */
60 #define JH7110_WDOGLOAD 0x000 /* RW: Watchdog load register */
61 #define JH7110_WDOGVALUE 0x004 /* RO: The current value for the watchdog counter */
62 #define JH7110_WDOGCONTROL 0x008 /* RW: [0]: reset enable; [1]: int enable/wdt enable/reload counter; [31:2]: res */
63 #define JH7110_WDOGINTCLR 0x00c /* WO: clear intterupt && reload the counter */
64 #define JH7110_WDOGRIS 0x010 /* RO: Raw interrupt status from the counter */
65 #define JH7110_WDOGIMS 0x014 /* RO: Enabled interrupt status from the counter */
66 #define JH7110_WDOGLOCK 0xc00 /* RO: Enable write access to all other registers by writing 0x1ACCE551 */
67 #define JH7110_WDOGITCR 0xf00 /* RW: When set HIGH, places the Watchdog into integraeion test mode */
68 #define JH7110_WDOGITOP 0xf04 /* WO: [0] Integration Test WDOGRES value Integration Test Mode
69 * Value output on WDOGRES when in Integration Test Mode
70 * [1] Integration Test WDOGINT value
71 * Value output on WDOGINT when in Integration Test Mode
74 #define JH7110_UNLOCK_KEY 0x1acce551
75 #define JH7110_RESEN_SHIFT 1
76 #define JH7110_EN_SHIFT 0
77 #define JH7110_INT_EN_SHIFT JH7110_EN_SHIFT
80 #define WDOG_INT_EN 0x0
81 #define WDOG_RESET_EN 0x1
84 #define WDOG_LOCKED BIT(0)
86 #define STARFIVE_WATCHDOG_INTCLR 0x1
87 #define STARFIVE_WATCHDOG_ENABLE 0x1
88 #define STARFIVE_WATCHDOG_ATBOOT 0x0
89 #define STARFIVE_WATCHDOG_MAXCNT 0xffffffff
91 #define STARFIVE_WATCHDOG_DEFAULT_TIME (15)
93 static bool nowayout = WATCHDOG_NOWAYOUT;
94 static int tmr_margin;
95 static int tmr_atboot = STARFIVE_WATCHDOG_ATBOOT;
96 static int soft_noboot;
98 module_param(tmr_margin, int, 0);
99 module_param(tmr_atboot, int, 0);
100 module_param(nowayout, bool, 0);
101 module_param(soft_noboot, int, 0);
103 MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
104 __MODULE_STRING(STARFIVE_WATCHDOG_DEFAULT_TIME) ")");
105 MODULE_PARM_DESC(tmr_atboot,
106 "Watchdog is started at boot time if set to 1, default="
107 __MODULE_STRING(STARFIVE_WATCHDOG_ATBOOT));
108 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
109 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
110 MODULE_PARM_DESC(soft_noboot,
111 "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default 0)");
113 struct starfive_wdt_variant_t {
121 struct starfive_wdt_variant {
130 u32 enter_irq_status;
131 struct starfive_wdt_variant_t *variant;
134 struct starfive_wdt {
137 struct watchdog_device wdt_device;
138 struct clk *core_clk;
140 struct reset_control *rsts;
141 const struct starfive_wdt_variant *drv_data;
142 u32 count; /*count of timeout*/
143 u32 reload; /*restore the count*/
151 static struct starfive_wdt_variant_t jh7100_variant = {
152 .unlock_key = JH7100_UNLOCK_KEY,
153 .enrst_shift = JH7100_RESEN_SHIFT,
154 .en_shift = JH7100_EN_SHIFT,
156 .intclr_ava_shift = JH7100_INTCLR_AVA_SHIFT,
159 static struct starfive_wdt_variant_t jh7110_variant = {
160 .unlock_key = JH7110_UNLOCK_KEY,
161 .enrst_shift = JH7110_RESEN_SHIFT,
162 .en_shift = JH7110_EN_SHIFT,
165 static const struct starfive_wdt_variant drv_data_jh7100 = {
166 .control = JH7100_WDOGCONTROL,
167 .load = JH7100_WDOGLOAD,
168 .enable = JH7100_WDOGEN,
169 .reload = JH7100_WDOGRELOAD,
170 .value = JH7100_WDOGVALUE,
171 .int_clr = JH7100_WDOGINTCLR,
172 .int_mask = JH7100_WDOGINTMSK,
173 .unlock = JH7100_WDOGLOCK,
174 .variant = &jh7100_variant,
177 static const struct starfive_wdt_variant drv_data_jh7110 = {
178 .control = JH7110_WDOGCONTROL,
179 .load = JH7110_WDOGLOAD,
180 .enable = JH7110_WDOGCONTROL,
181 .value = JH7110_WDOGVALUE,
182 .int_clr = JH7110_WDOGINTCLR,
183 .unlock = JH7110_WDOGLOCK,
184 .enter_irq_status = JH7110_WDOGIMS,
185 .variant = &jh7110_variant,
188 static const struct of_device_id starfive_wdt_match[] = {
189 { .compatible = "starfive,jh7100-wdt", .data = &drv_data_jh7100 },
190 { .compatible = "starfive,jh7110-wdt", .data = &drv_data_jh7110 },
193 MODULE_DEVICE_TABLE(of, starfive_wdt_match);
196 static const struct platform_device_id starfive_wdt_ids[] = {
198 .name = "starfive-wdt",
199 .driver_data = (unsigned long)&drv_data_jh7100,
202 .name = "starfive-dskit-wdt",
203 .driver_data = (unsigned long)&drv_data_jh7110,
207 MODULE_DEVICE_TABLE(platform, starfive_wdt_ids);
209 static int starfive_wdt_get_clock_rate(struct starfive_wdt *wdt)
214 /* Next we try to get clock-frequency from dts.*/
215 ret = of_property_read_u32(wdt->dev->of_node, "clock-frequency", &freq);
217 wdt->freq = (u64)freq;
220 dev_dbg(wdt->dev, "get rate failed, need clock-frequency define in dts.\n");
222 if (!IS_ERR(wdt->core_clk)) {
223 wdt->freq = clk_get_rate(wdt->core_clk);
226 dev_err(wdt->dev, "get clock-frequency failed\n");
230 static int starfive_wdt_enable_clock(struct starfive_wdt *wdt)
234 wdt->apb_clk = devm_clk_get(wdt->dev, "apb_clk");
235 if (!IS_ERR(wdt->apb_clk)) {
236 err = clk_prepare_enable(wdt->apb_clk);
238 dev_warn(wdt->dev, "enable core_clk error.\n");
241 wdt->core_clk = devm_clk_get(wdt->dev, "core_clk");
242 if (!IS_ERR(wdt->core_clk)) {
243 err = clk_prepare_enable(wdt->core_clk);
245 dev_warn(wdt->dev, "enable apb_clk error.\n");
251 static int starfive_wdt_reset_init(struct starfive_wdt *wdt)
255 wdt->rsts = devm_reset_control_array_get_exclusive(wdt->dev);
256 if (!IS_ERR(wdt->rsts)) {
257 err = reset_control_deassert(wdt->rsts);
259 dev_err(wdt->dev, "deassert rsts error.\n");
261 err = PTR_ERR(wdt->rsts);
266 static __maybe_unused
267 u32 starfive_wdt_sec_to_ticks(struct starfive_wdt *wdt, u32 sec)
269 return sec * wdt->freq;
272 static __maybe_unused
273 u32 starfive_wdt_ticks_to_sec(struct starfive_wdt *wdt, u32 ticks)
275 return DIV_ROUND_CLOSEST(ticks, wdt->freq);
279 * Write unlock-key to unlock. Write other value to lock. When lock bit is 1,
280 * external accesses to other watchdog registers are ignored.
282 static int starfive_wdt_is_locked(struct starfive_wdt *wdt)
286 val = readl(wdt->base + wdt->drv_data->unlock);
287 return !!(val & WDOG_LOCKED);
290 static void starfive_wdt_unlock(struct starfive_wdt *wdt)
292 if (starfive_wdt_is_locked(wdt))
293 writel(wdt->drv_data->variant->unlock_key,
294 wdt->base + wdt->drv_data->unlock);
297 static void starfive_wdt_lock(struct starfive_wdt *wdt)
299 if (!starfive_wdt_is_locked(wdt))
300 writel(~wdt->drv_data->variant->unlock_key,
301 wdt->base + wdt->drv_data->unlock);
304 static int __maybe_unused starfive_wdt_is_running(struct starfive_wdt *wdt)
308 starfive_wdt_unlock(wdt);
309 val = readl(wdt->base + wdt->drv_data->enable);
310 starfive_wdt_lock(wdt);
312 return !!(val & STARFIVE_WATCHDOG_ENABLE <<
313 wdt->drv_data->variant->en_shift);
316 static inline void starfive_wdt_int_enable(struct starfive_wdt *wdt)
320 if (wdt->drv_data->int_mask) {
321 val = readl(wdt->base + wdt->drv_data->int_mask);
323 writel(val, wdt->base + wdt->drv_data->int_mask);
327 static inline void starfive_wdt_int_disable(struct starfive_wdt *wdt)
331 if (wdt->drv_data->int_mask) {
332 val = readl(wdt->base + wdt->drv_data->int_mask);
334 writel(val, wdt->base + wdt->drv_data->int_mask);
338 static void starfive_wdt_enable_reset(struct starfive_wdt *wdt)
342 val = readl(wdt->base + wdt->drv_data->control);
343 val |= WDOG_RESET_EN << wdt->drv_data->variant->enrst_shift;
344 /* enable wdog interrupt to reset */
345 writel(val, wdt->base + wdt->drv_data->control);
348 static void starfive_wdt_disable_reset(struct starfive_wdt *wdt)
352 val = readl(wdt->base + wdt->drv_data->control);
353 val &= ~(WDOG_RESET_EN << wdt->drv_data->variant->enrst_shift);
354 /*disable wdog interrupt to reset*/
355 writel(val, wdt->base + wdt->drv_data->control);
358 static bool starfive_wdt_enter_irq_status(struct starfive_wdt *wdt)
360 /* interrupt status whether has entered from the counter */
361 return !!readl(wdt->base + wdt->drv_data->enter_irq_status);
364 static void starfive_wdt_int_clr(struct starfive_wdt *wdt)
372 addr = wdt->base + wdt->drv_data->int_clr;
373 clr_ava_shift = wdt->drv_data->variant->intclr_ava_shift;
374 clr_check = wdt->drv_data->variant->intclr_check;
376 /* waiting interrupt can be to clearing */
378 ret = readl_poll_timeout_atomic(addr, value,
379 !(value & BIT(clr_ava_shift)), DELAY_US, TIMEOUT_US);
383 writel(STARFIVE_WATCHDOG_INTCLR, addr);
385 if (starfive_wdt_enter_irq_status(wdt))
386 enable_irq(wdt->irq);
389 static inline void starfive_wdt_set_count(struct starfive_wdt *wdt, u32 val)
391 writel(val, wdt->base + wdt->drv_data->load);
394 static inline u32 starfive_wdt_get_count(struct starfive_wdt *wdt)
396 return readl(wdt->base + wdt->drv_data->value);
399 static inline void starfive_wdt_enable(struct starfive_wdt *wdt)
403 val = readl(wdt->base + wdt->drv_data->enable);
404 val |= STARFIVE_WATCHDOG_ENABLE << wdt->drv_data->variant->en_shift;
405 writel(val, wdt->base + wdt->drv_data->enable);
408 static inline void starfive_wdt_disable(struct starfive_wdt *wdt)
412 val = readl(wdt->base + wdt->drv_data->enable);
413 val &= ~(STARFIVE_WATCHDOG_ENABLE << wdt->drv_data->variant->en_shift);
414 writel(val, wdt->base + wdt->drv_data->enable);
418 starfive_wdt_set_relod_count(struct starfive_wdt *wdt, u32 count)
420 writel(count, wdt->base + wdt->drv_data->load);
421 if (wdt->drv_data->reload)
422 writel(0x1, wdt->base + wdt->drv_data->reload);
424 /* jh7110 need enable controller to reload counter */
425 starfive_wdt_enable(wdt);
428 static int starfive_wdt_mask_and_disable_reset(struct starfive_wdt *wdt, bool mask)
430 starfive_wdt_unlock(wdt);
433 starfive_wdt_disable_reset(wdt);
435 starfive_wdt_enable_reset(wdt);
437 starfive_wdt_lock(wdt);
442 static unsigned int starfive_wdt_max_timeout(struct starfive_wdt *wdt)
444 return DIV_ROUND_UP(STARFIVE_WATCHDOG_MAXCNT, wdt->freq) - 1;
447 static unsigned int starfive_wdt_get_timeleft(struct watchdog_device *wdd)
449 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd);
453 starfive_wdt_unlock(wdt);
455 * Because set half count value,
456 * timeleft value should add the count value before first timeout.
458 irq_status = starfive_wdt_enter_irq_status(wdt) ? 1 : 0;
459 count = starfive_wdt_get_count(wdt) + (1 - irq_status) * wdt->count;
460 starfive_wdt_lock(wdt);
462 return starfive_wdt_ticks_to_sec(wdt, count);
465 static int starfive_wdt_keepalive(struct watchdog_device *wdd)
467 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd);
469 spin_lock(&wdt->lock);
471 starfive_wdt_unlock(wdt);
472 starfive_wdt_int_clr(wdt);
473 starfive_wdt_set_relod_count(wdt, wdt->count);
474 starfive_wdt_lock(wdt);
476 spin_unlock(&wdt->lock);
481 static irqreturn_t starfive_wdt_interrupt_handler(int irq, void *data)
484 * We don't clear the IRQ status. It's supposed to be done by the
485 * following ping operations.
487 struct platform_device *pdev = data;
488 struct starfive_wdt *wdt = platform_get_drvdata(pdev);
490 /* Disable the IRQ and avoid re-entry interrupt. */
491 disable_irq_nosync(wdt->irq);
496 static int starfive_wdt_stop(struct watchdog_device *wdd)
498 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd);
500 spin_lock(&wdt->lock);
502 starfive_wdt_unlock(wdt);
503 starfive_wdt_int_disable(wdt);
504 starfive_wdt_int_clr(wdt);
505 starfive_wdt_disable(wdt);
506 starfive_wdt_lock(wdt);
508 spin_unlock(&wdt->lock);
513 static int starfive_wdt_pm_stop(struct watchdog_device *wdd)
515 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd);
517 starfive_wdt_stop(wdd);
518 pm_runtime_put(wdt->dev);
523 static int starfive_wdt_start(struct watchdog_device *wdd)
525 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd);
527 pm_runtime_get_sync(wdt->dev);
529 spin_lock(&wdt->lock);
531 starfive_wdt_unlock(wdt);
534 starfive_wdt_disable_reset(wdt);
536 starfive_wdt_enable_reset(wdt);
538 starfive_wdt_int_clr(wdt);
539 starfive_wdt_set_count(wdt, wdt->count);
540 starfive_wdt_int_enable(wdt);
541 starfive_wdt_enable(wdt);
543 starfive_wdt_lock(wdt);
545 spin_unlock(&wdt->lock);
550 static int starfive_wdt_restart(struct watchdog_device *wdd, unsigned long action,
553 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd);
555 starfive_wdt_unlock(wdt);
556 /* disable watchdog, to be safe */
557 starfive_wdt_disable(wdt);
560 starfive_wdt_disable_reset(wdt);
562 starfive_wdt_enable_reset(wdt);
564 /* put initial values into count and data */
565 starfive_wdt_set_count(wdt, wdt->count);
567 /* set the watchdog to go and reset... */
568 starfive_wdt_int_clr(wdt);
569 starfive_wdt_int_enable(wdt);
570 starfive_wdt_enable(wdt);
572 /* wait for reset to assert... */
575 starfive_wdt_lock(wdt);
580 static int starfive_wdt_set_timeout(struct watchdog_device *wdd,
581 unsigned int timeout)
583 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd);
585 unsigned long freq = wdt->freq;
592 * This watchdog takes twice timeouts to reset.
593 * In order to reduce time to reset, should set half count value.
595 count = timeout * freq / 2;
597 if (count > STARFIVE_WATCHDOG_MAXCNT) {
598 dev_warn(wdt->dev, "timeout %d too big,use the MAX-timeout set.\n",
600 timeout = starfive_wdt_max_timeout(wdt);
601 count = timeout * freq;
604 dev_info(wdt->dev, "Heartbeat: timeout=%d, count/2=%d (%08x)\n",
605 timeout, count, count);
607 starfive_wdt_unlock(wdt);
608 starfive_wdt_disable(wdt);
609 starfive_wdt_set_relod_count(wdt, count);
610 starfive_wdt_enable(wdt);
611 starfive_wdt_lock(wdt);
614 wdd->timeout = timeout;
619 #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
621 static const struct watchdog_info starfive_wdt_ident = {
623 .firmware_version = 0,
624 .identity = "StarFive Watchdog",
627 static const struct watchdog_ops starfive_wdt_ops = {
628 .owner = THIS_MODULE,
629 .start = starfive_wdt_start,
630 .stop = starfive_wdt_pm_stop,
631 .ping = starfive_wdt_keepalive,
632 .set_timeout = starfive_wdt_set_timeout,
633 .restart = starfive_wdt_restart,
634 .get_timeleft = starfive_wdt_get_timeleft,
637 static const struct watchdog_device starfive_wdd = {
638 .info = &starfive_wdt_ident,
639 .ops = &starfive_wdt_ops,
640 .timeout = STARFIVE_WATCHDOG_DEFAULT_TIME,
643 static inline const struct starfive_wdt_variant *
644 starfive_get_wdt_drv_data(struct platform_device *pdev)
646 const struct starfive_wdt_variant *variant;
648 variant = of_device_get_match_data(&pdev->dev);
650 /* Device matched by platform_device_id */
651 variant = (struct starfive_wdt_variant *)
652 platform_get_device_id(pdev)->driver_data;
658 static int starfive_wdt_probe(struct platform_device *pdev)
660 struct device *dev = &pdev->dev;
661 struct starfive_wdt *wdt;
665 pm_runtime_enable(dev);
667 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
672 spin_lock_init(&wdt->lock);
673 wdt->wdt_device = starfive_wdd;
675 wdt->drv_data = starfive_get_wdt_drv_data(pdev);
677 wdt->irq = platform_get_irq(pdev, 0);
679 dev_err(dev, "can not find irq.\n");
683 /* get the memory region for the watchdog timer */
684 wdt->base = devm_platform_ioremap_resource(pdev, 0);
685 if (IS_ERR(wdt->base)) {
686 ret = PTR_ERR(wdt->base);
690 ret = starfive_wdt_enable_clock(wdt);
692 dev_warn(wdt->dev, "get & enable clk err\n");
694 starfive_wdt_get_clock_rate(wdt);
696 ret = starfive_wdt_reset_init(wdt);
698 dev_warn(wdt->dev, "get & deassert rst err\n");
700 wdt->wdt_device.min_timeout = 1;
701 wdt->wdt_device.max_timeout = starfive_wdt_max_timeout(wdt);
703 watchdog_set_drvdata(&wdt->wdt_device, wdt);
706 * see if we can actually set the requested timer margin,
707 * and if not, try the default value.
709 watchdog_init_timeout(&wdt->wdt_device, tmr_margin, dev);
711 ret = starfive_wdt_set_timeout(&wdt->wdt_device,
712 wdt->wdt_device.timeout);
714 dev_info(dev, "tmr_margin value out of range, default %d used\n",
715 STARFIVE_WATCHDOG_DEFAULT_TIME);
716 starfive_wdt_set_timeout(&wdt->wdt_device,
717 STARFIVE_WATCHDOG_DEFAULT_TIME);
720 ret = devm_request_irq(dev, wdt->irq, starfive_wdt_interrupt_handler, 0,
723 dev_err(dev, "failed to install irq (%d)\n", ret);
727 watchdog_set_nowayout(&wdt->wdt_device, nowayout);
728 watchdog_set_restart_priority(&wdt->wdt_device, 128);
730 wdt->wdt_device.parent = dev;
732 ret = watchdog_register_device(&wdt->wdt_device);
736 ret = starfive_wdt_mask_and_disable_reset(wdt, false);
740 if (tmr_atboot && started == 0) {
741 dev_info(dev, "starting watchdog timer\n");
742 starfive_wdt_start(&wdt->wdt_device);
743 } else if (!tmr_atboot) {
746 *if we're not enabling the watchdog, then ensure it is
747 * disabled if it has been left running from the bootloader
750 starfive_wdt_stop(&wdt->wdt_device);
752 clk_disable_unprepare(wdt->core_clk);
753 clk_disable_unprepare(wdt->apb_clk);
755 platform_set_drvdata(pdev, wdt);
760 watchdog_unregister_device(&wdt->wdt_device);
765 static int starfive_wdt_remove(struct platform_device *dev)
768 struct starfive_wdt *wdt = platform_get_drvdata(dev);
770 ret = starfive_wdt_mask_and_disable_reset(wdt, true);
774 watchdog_unregister_device(&wdt->wdt_device);
776 clk_disable_unprepare(wdt->core_clk);
777 clk_disable_unprepare(wdt->apb_clk);
778 pm_runtime_disable(wdt->dev);
783 static void starfive_wdt_shutdown(struct platform_device *dev)
785 struct starfive_wdt *wdt = platform_get_drvdata(dev);
787 starfive_wdt_mask_and_disable_reset(wdt, true);
789 starfive_wdt_pm_stop(&wdt->wdt_device);
792 #ifdef CONFIG_PM_SLEEP
794 static int starfive_wdt_suspend(struct device *dev)
797 struct starfive_wdt *wdt = dev_get_drvdata(dev);
799 starfive_wdt_unlock(wdt);
801 /* Save watchdog state, and turn it off. */
802 wdt->reload = starfive_wdt_get_count(wdt);
804 ret = starfive_wdt_mask_and_disable_reset(wdt, true);
808 /* Note that WTCNT doesn't need to be saved. */
809 starfive_wdt_stop(&wdt->wdt_device);
810 pm_runtime_force_suspend(dev);
812 starfive_wdt_lock(wdt);
817 static int starfive_wdt_resume(struct device *dev)
820 struct starfive_wdt *wdt = dev_get_drvdata(dev);
822 starfive_wdt_unlock(wdt);
824 /* Restore watchdog state. */
825 starfive_wdt_set_relod_count(wdt, wdt->reload);
827 pm_runtime_force_resume(dev);
829 starfive_wdt_restart(&wdt->wdt_device, 0, NULL);
831 ret = starfive_wdt_mask_and_disable_reset(wdt, false);
835 starfive_wdt_lock(wdt);
839 #endif /* CONFIG_PM_SLEEP */
843 static int starfive_wdt_runtime_suspend(struct device *dev)
845 struct starfive_wdt *wdt = dev_get_drvdata(dev);
847 clk_disable_unprepare(wdt->apb_clk);
848 clk_disable_unprepare(wdt->core_clk);
853 static int starfive_wdt_runtime_resume(struct device *dev)
855 struct starfive_wdt *wdt = dev_get_drvdata(dev);
857 clk_prepare_enable(wdt->apb_clk);
858 clk_prepare_enable(wdt->core_clk);
865 static const struct dev_pm_ops starfive_wdt_pm_ops = {
866 SET_RUNTIME_PM_OPS(starfive_wdt_runtime_suspend, starfive_wdt_runtime_resume, NULL)
867 SET_SYSTEM_SLEEP_PM_OPS(starfive_wdt_suspend, starfive_wdt_resume)
870 static struct platform_driver starfive_starfive_wdt_driver = {
871 .probe = starfive_wdt_probe,
872 .remove = starfive_wdt_remove,
873 .shutdown = starfive_wdt_shutdown,
874 .id_table = starfive_wdt_ids,
876 .name = "starfive-wdt",
877 .pm = &starfive_wdt_pm_ops,
878 .of_match_table = of_match_ptr(starfive_wdt_match),
882 module_platform_driver(starfive_starfive_wdt_driver);
884 MODULE_AUTHOR("xingyu.wu <xingyu.wu@starfivetech.com>");
885 MODULE_AUTHOR("samin.guo <samin.guo@starfivetech.com>");
886 MODULE_DESCRIPTION("StarFive Watchdog Device Driver");
887 MODULE_LICENSE("GPL v2");