1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
10 #include <linux/delay.h>
12 #define APPLE_WDT_CUR_TIME 0x10
13 #define APPLE_WDT_BARK_TIME 0x14
14 #define APPLE_WDT_CTRL 0x1c
15 #define APPLE_WDT_CTRL_RESET_EN BIT(2)
17 struct apple_wdt_priv {
22 static int apple_wdt_reset(struct udevice *dev)
24 struct apple_wdt_priv *priv = dev_get_priv(dev);
26 writel(0, priv->base + APPLE_WDT_CUR_TIME);
31 static int apple_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
33 struct apple_wdt_priv *priv = dev_get_priv(dev);
36 timeout = (timeout_ms * priv->clk_rate) / 1000;
37 if (timeout > U32_MAX)
40 writel(0, priv->base + APPLE_WDT_CUR_TIME);
41 writel(timeout, priv->base + APPLE_WDT_BARK_TIME);
42 writel(APPLE_WDT_CTRL_RESET_EN, priv->base + APPLE_WDT_CTRL);
47 static int apple_wdt_stop(struct udevice *dev)
49 struct apple_wdt_priv *priv = dev_get_priv(dev);
51 writel(0, priv->base + APPLE_WDT_CTRL);
56 static int apple_wdt_expire_now(struct udevice *dev, ulong flags)
60 ret = apple_wdt_start(dev, 0, flags);
65 * It can take up to 25ms until the SoC actually resets, so
66 * wait 50ms just to be sure.
73 static const struct wdt_ops apple_wdt_ops = {
74 .reset = apple_wdt_reset,
75 .start = apple_wdt_start,
76 .stop = apple_wdt_stop,
77 .expire_now = apple_wdt_expire_now,
80 static const struct udevice_id apple_wdt_ids[] = {
81 { .compatible = "apple,wdt" },
85 static int apple_wdt_probe(struct udevice *dev)
87 struct apple_wdt_priv *priv = dev_get_priv(dev);
91 priv->base = dev_read_addr_ptr(dev);
95 ret = clk_get_by_index(dev, 0, &clk);
99 ret = clk_enable(&clk);
103 priv->clk_rate = clk_get_rate(&clk);
108 U_BOOT_DRIVER(apple_wdt) = {
111 .of_match = apple_wdt_ids,
112 .priv_auto = sizeof(struct apple_wdt_priv),
113 .probe = apple_wdt_probe,
114 .ops = &apple_wdt_ops,