Lines Matching +full:wdt +full:- +full:enable +full:- +full:once
1 // SPDX-License-Identifier: GPL-2.0
35 * [0]: reset enable;
36 * [1]: interrupt enable && watchdog enable
79 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
83 unsigned int control; /* Watchdog Control Resgister for reset enable */
86 unsigned int enable; /* Watchdog Enable Register */ member
117 .enable = STARFIVE_WDT_JH7100_EN,
134 .enable = STARFIVE_WDT_JH7110_CONTROL,
146 static int starfive_wdt_enable_clock(struct starfive_wdt *wdt) in starfive_wdt_enable_clock() argument
150 ret = clk_prepare_enable(wdt->apb_clk); in starfive_wdt_enable_clock()
152 return dev_err_probe(wdt->wdd.parent, ret, "failed to enable apb clock\n"); in starfive_wdt_enable_clock()
154 ret = clk_prepare_enable(wdt->core_clk); in starfive_wdt_enable_clock()
156 clk_disable_unprepare(wdt->apb_clk); in starfive_wdt_enable_clock()
157 return dev_err_probe(wdt->wdd.parent, ret, "failed to enable core clock\n"); in starfive_wdt_enable_clock()
163 static void starfive_wdt_disable_clock(struct starfive_wdt *wdt) in starfive_wdt_disable_clock() argument
165 clk_disable_unprepare(wdt->core_clk); in starfive_wdt_disable_clock()
166 clk_disable_unprepare(wdt->apb_clk); in starfive_wdt_disable_clock()
169 static inline int starfive_wdt_get_clock(struct starfive_wdt *wdt) in starfive_wdt_get_clock() argument
171 struct device *dev = wdt->wdd.parent; in starfive_wdt_get_clock()
173 wdt->apb_clk = devm_clk_get(dev, "apb"); in starfive_wdt_get_clock()
174 if (IS_ERR(wdt->apb_clk)) in starfive_wdt_get_clock()
175 return dev_err_probe(dev, PTR_ERR(wdt->apb_clk), "failed to get apb clock\n"); in starfive_wdt_get_clock()
177 wdt->core_clk = devm_clk_get(dev, "core"); in starfive_wdt_get_clock()
178 if (IS_ERR(wdt->core_clk)) in starfive_wdt_get_clock()
179 return dev_err_probe(dev, PTR_ERR(wdt->core_clk), "failed to get core clock\n"); in starfive_wdt_get_clock()
200 static u32 starfive_wdt_ticks_to_sec(struct starfive_wdt *wdt, u32 ticks) in starfive_wdt_ticks_to_sec() argument
202 return DIV_ROUND_CLOSEST(ticks, wdt->freq); in starfive_wdt_ticks_to_sec()
205 /* Write unlock-key to unlock. Write other value to lock. */
206 static void starfive_wdt_unlock(struct starfive_wdt *wdt) in starfive_wdt_unlock() argument
207 __acquires(&wdt->lock) in starfive_wdt_unlock()
209 spin_lock(&wdt->lock); in starfive_wdt_unlock()
210 writel(wdt->variant->unlock_key, wdt->base + wdt->variant->unlock); in starfive_wdt_unlock()
213 static void starfive_wdt_lock(struct starfive_wdt *wdt) in starfive_wdt_lock() argument
214 __releases(&wdt->lock) in starfive_wdt_lock()
216 writel(~wdt->variant->unlock_key, wdt->base + wdt->variant->unlock); in starfive_wdt_lock()
217 spin_unlock(&wdt->lock); in starfive_wdt_lock()
220 /* enable watchdog interrupt to reset/reboot */
221 static void starfive_wdt_enable_reset(struct starfive_wdt *wdt) in starfive_wdt_enable_reset() argument
225 val = readl(wdt->base + wdt->variant->control); in starfive_wdt_enable_reset()
226 val |= STARFIVE_WDT_RESET_EN << wdt->variant->enrst_shift; in starfive_wdt_enable_reset()
227 writel(val, wdt->base + wdt->variant->control); in starfive_wdt_enable_reset()
231 static bool starfive_wdt_raise_irq_status(struct starfive_wdt *wdt) in starfive_wdt_raise_irq_status() argument
233 return !!readl(wdt->base + wdt->variant->int_status); in starfive_wdt_raise_irq_status()
237 static int starfive_wdt_wait_int_free(struct starfive_wdt *wdt) in starfive_wdt_wait_int_free() argument
241 return readl_poll_timeout_atomic(wdt->base + wdt->variant->int_clr, value, in starfive_wdt_wait_int_free()
242 !(value & BIT(wdt->variant->intclr_ava_shift)), in starfive_wdt_wait_int_free()
247 static int starfive_wdt_int_clr(struct starfive_wdt *wdt) in starfive_wdt_int_clr() argument
251 if (wdt->variant->intclr_check) { in starfive_wdt_int_clr()
252 ret = starfive_wdt_wait_int_free(wdt); in starfive_wdt_int_clr()
254 return dev_err_probe(wdt->wdd.parent, ret, in starfive_wdt_int_clr()
257 writel(STARFIVE_WDT_INTCLR, wdt->base + wdt->variant->int_clr); in starfive_wdt_int_clr()
262 static inline void starfive_wdt_set_count(struct starfive_wdt *wdt, u32 val) in starfive_wdt_set_count() argument
264 writel(val, wdt->base + wdt->variant->load); in starfive_wdt_set_count()
267 static inline u32 starfive_wdt_get_count(struct starfive_wdt *wdt) in starfive_wdt_get_count() argument
269 return readl(wdt->base + wdt->variant->value); in starfive_wdt_get_count()
272 /* enable watchdog */
273 static inline void starfive_wdt_enable(struct starfive_wdt *wdt) in starfive_wdt_enable() argument
277 val = readl(wdt->base + wdt->variant->enable); in starfive_wdt_enable()
278 val |= STARFIVE_WDT_ENABLE << wdt->variant->en_shift; in starfive_wdt_enable()
279 writel(val, wdt->base + wdt->variant->enable); in starfive_wdt_enable()
283 static inline void starfive_wdt_disable(struct starfive_wdt *wdt) in starfive_wdt_disable() argument
287 val = readl(wdt->base + wdt->variant->enable); in starfive_wdt_disable()
288 val &= ~(STARFIVE_WDT_ENABLE << wdt->variant->en_shift); in starfive_wdt_disable()
289 writel(val, wdt->base + wdt->variant->enable); in starfive_wdt_disable()
292 static inline void starfive_wdt_set_reload_count(struct starfive_wdt *wdt, u32 count) in starfive_wdt_set_reload_count() argument
294 starfive_wdt_set_count(wdt, count); in starfive_wdt_set_reload_count()
297 if (wdt->variant->reload) in starfive_wdt_set_reload_count()
298 writel(0x1, wdt->base + wdt->variant->reload); in starfive_wdt_set_reload_count()
301 static unsigned int starfive_wdt_max_timeout(struct starfive_wdt *wdt) in starfive_wdt_max_timeout() argument
303 if (wdt->variant->double_timeout) in starfive_wdt_max_timeout()
304 return DIV_ROUND_UP(STARFIVE_WDT_MAXCNT, (wdt->freq / 2)) - 1; in starfive_wdt_max_timeout()
306 return DIV_ROUND_UP(STARFIVE_WDT_MAXCNT, wdt->freq) - 1; in starfive_wdt_max_timeout()
311 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd); in starfive_wdt_get_timeleft() local
318 count = starfive_wdt_get_count(wdt); in starfive_wdt_get_timeleft()
319 if (wdt->variant->double_timeout && !starfive_wdt_raise_irq_status(wdt)) in starfive_wdt_get_timeleft()
320 count += wdt->count; in starfive_wdt_get_timeleft()
322 return starfive_wdt_ticks_to_sec(wdt, count); in starfive_wdt_get_timeleft()
327 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd); in starfive_wdt_keepalive() local
330 starfive_wdt_unlock(wdt); in starfive_wdt_keepalive()
331 ret = starfive_wdt_int_clr(wdt); in starfive_wdt_keepalive()
335 starfive_wdt_set_reload_count(wdt, wdt->count); in starfive_wdt_keepalive()
339 starfive_wdt_lock(wdt); in starfive_wdt_keepalive()
343 static int starfive_wdt_start(struct starfive_wdt *wdt) in starfive_wdt_start() argument
347 starfive_wdt_unlock(wdt); in starfive_wdt_start()
349 starfive_wdt_disable(wdt); in starfive_wdt_start()
351 starfive_wdt_enable_reset(wdt); in starfive_wdt_start()
352 ret = starfive_wdt_int_clr(wdt); in starfive_wdt_start()
356 starfive_wdt_set_count(wdt, wdt->count); in starfive_wdt_start()
357 starfive_wdt_enable(wdt); in starfive_wdt_start()
360 starfive_wdt_lock(wdt); in starfive_wdt_start()
364 static void starfive_wdt_stop(struct starfive_wdt *wdt) in starfive_wdt_stop() argument
366 starfive_wdt_unlock(wdt); in starfive_wdt_stop()
367 starfive_wdt_disable(wdt); in starfive_wdt_stop()
368 starfive_wdt_lock(wdt); in starfive_wdt_stop()
373 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd); in starfive_wdt_pm_start() local
374 int ret = pm_runtime_get_sync(wdd->parent); in starfive_wdt_pm_start()
379 return starfive_wdt_start(wdt); in starfive_wdt_pm_start()
384 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd); in starfive_wdt_pm_stop() local
386 starfive_wdt_stop(wdt); in starfive_wdt_pm_stop()
387 return pm_runtime_put_sync(wdd->parent); in starfive_wdt_pm_stop()
393 struct starfive_wdt *wdt = watchdog_get_drvdata(wdd); in starfive_wdt_set_timeout() local
394 unsigned long count = timeout * wdt->freq; in starfive_wdt_set_timeout()
397 if (wdt->variant->double_timeout) in starfive_wdt_set_timeout()
400 wdt->count = count; in starfive_wdt_set_timeout()
401 wdd->timeout = timeout; in starfive_wdt_set_timeout()
403 starfive_wdt_unlock(wdt); in starfive_wdt_set_timeout()
404 starfive_wdt_disable(wdt); in starfive_wdt_set_timeout()
405 starfive_wdt_set_reload_count(wdt, wdt->count); in starfive_wdt_set_timeout()
406 starfive_wdt_enable(wdt); in starfive_wdt_set_timeout()
407 starfive_wdt_lock(wdt); in starfive_wdt_set_timeout()
430 struct starfive_wdt *wdt; in starfive_wdt_probe() local
433 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL); in starfive_wdt_probe()
434 if (!wdt) in starfive_wdt_probe()
435 return -ENOMEM; in starfive_wdt_probe()
437 wdt->base = devm_platform_ioremap_resource(pdev, 0); in starfive_wdt_probe()
438 if (IS_ERR(wdt->base)) in starfive_wdt_probe()
439 return dev_err_probe(&pdev->dev, PTR_ERR(wdt->base), "error mapping registers\n"); in starfive_wdt_probe()
441 wdt->wdd.parent = &pdev->dev; in starfive_wdt_probe()
442 ret = starfive_wdt_get_clock(wdt); in starfive_wdt_probe()
446 platform_set_drvdata(pdev, wdt); in starfive_wdt_probe()
447 pm_runtime_enable(&pdev->dev); in starfive_wdt_probe()
448 if (pm_runtime_enabled(&pdev->dev)) { in starfive_wdt_probe()
449 ret = pm_runtime_get_sync(&pdev->dev); in starfive_wdt_probe()
454 ret = starfive_wdt_enable_clock(wdt); in starfive_wdt_probe()
459 ret = starfive_wdt_reset_init(&pdev->dev); in starfive_wdt_probe()
463 watchdog_set_drvdata(&wdt->wdd, wdt); in starfive_wdt_probe()
464 wdt->wdd.info = &starfive_wdt_info; in starfive_wdt_probe()
465 wdt->wdd.ops = &starfive_wdt_ops; in starfive_wdt_probe()
466 wdt->variant = of_device_get_match_data(&pdev->dev); in starfive_wdt_probe()
467 spin_lock_init(&wdt->lock); in starfive_wdt_probe()
469 wdt->freq = clk_get_rate(wdt->core_clk); in starfive_wdt_probe()
470 if (!wdt->freq) { in starfive_wdt_probe()
471 dev_err(&pdev->dev, "get clock rate failed.\n"); in starfive_wdt_probe()
472 ret = -EINVAL; in starfive_wdt_probe()
476 wdt->wdd.min_timeout = 1; in starfive_wdt_probe()
477 wdt->wdd.max_timeout = starfive_wdt_max_timeout(wdt); in starfive_wdt_probe()
478 wdt->wdd.timeout = STARFIVE_WDT_DEFAULT_TIME; in starfive_wdt_probe()
479 watchdog_init_timeout(&wdt->wdd, heartbeat, &pdev->dev); in starfive_wdt_probe()
480 starfive_wdt_set_timeout(&wdt->wdd, wdt->wdd.timeout); in starfive_wdt_probe()
482 watchdog_set_nowayout(&wdt->wdd, nowayout); in starfive_wdt_probe()
483 watchdog_stop_on_reboot(&wdt->wdd); in starfive_wdt_probe()
484 watchdog_stop_on_unregister(&wdt->wdd); in starfive_wdt_probe()
487 ret = starfive_wdt_start(wdt); in starfive_wdt_probe()
490 set_bit(WDOG_HW_RUNNING, &wdt->wdd.status); in starfive_wdt_probe()
492 starfive_wdt_stop(wdt); in starfive_wdt_probe()
495 ret = watchdog_register_device(&wdt->wdd); in starfive_wdt_probe()
500 if (pm_runtime_enabled(&pdev->dev)) { in starfive_wdt_probe()
501 ret = pm_runtime_put_sync(&pdev->dev); in starfive_wdt_probe()
510 starfive_wdt_disable_clock(wdt); in starfive_wdt_probe()
511 pm_runtime_disable(&pdev->dev); in starfive_wdt_probe()
518 struct starfive_wdt *wdt = platform_get_drvdata(pdev); in starfive_wdt_remove() local
520 starfive_wdt_stop(wdt); in starfive_wdt_remove()
521 watchdog_unregister_device(&wdt->wdd); in starfive_wdt_remove()
523 if (pm_runtime_enabled(&pdev->dev)) in starfive_wdt_remove()
524 pm_runtime_disable(&pdev->dev); in starfive_wdt_remove()
527 starfive_wdt_disable_clock(wdt); in starfive_wdt_remove()
532 struct starfive_wdt *wdt = platform_get_drvdata(pdev); in starfive_wdt_shutdown() local
534 starfive_wdt_pm_stop(&wdt->wdd); in starfive_wdt_shutdown()
539 struct starfive_wdt *wdt = dev_get_drvdata(dev); in starfive_wdt_suspend() local
542 wdt->reload = starfive_wdt_get_count(wdt); in starfive_wdt_suspend()
545 starfive_wdt_stop(wdt); in starfive_wdt_suspend()
552 struct starfive_wdt *wdt = dev_get_drvdata(dev); in starfive_wdt_resume() local
559 starfive_wdt_unlock(wdt); in starfive_wdt_resume()
561 starfive_wdt_set_reload_count(wdt, wdt->reload); in starfive_wdt_resume()
562 starfive_wdt_lock(wdt); in starfive_wdt_resume()
564 if (watchdog_active(&wdt->wdd)) in starfive_wdt_resume()
565 return starfive_wdt_start(wdt); in starfive_wdt_resume()
572 struct starfive_wdt *wdt = dev_get_drvdata(dev); in starfive_wdt_runtime_suspend() local
574 starfive_wdt_disable_clock(wdt); in starfive_wdt_runtime_suspend()
581 struct starfive_wdt *wdt = dev_get_drvdata(dev); in starfive_wdt_runtime_resume() local
583 return starfive_wdt_enable_clock(wdt); in starfive_wdt_runtime_resume()
592 { .compatible = "starfive,jh7100-wdt", .data = &starfive_wdt_jh7100_variant },
593 { .compatible = "starfive,jh7110-wdt", .data = &starfive_wdt_jh7110_variant },
603 .name = "starfive-wdt",