Mark Kettenis | ee327d1 | 2022-01-12 19:55:15 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org> |
| 4 | */ |
| 5 | |
| 6 | #include <clk.h> |
| 7 | #include <dm.h> |
| 8 | #include <wdt.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <linux/delay.h> |
| 11 | |
| 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) |
| 16 | |
| 17 | struct apple_wdt_priv { |
| 18 | void *base; |
| 19 | ulong clk_rate; |
| 20 | }; |
| 21 | |
| 22 | static int apple_wdt_reset(struct udevice *dev) |
| 23 | { |
| 24 | struct apple_wdt_priv *priv = dev_get_priv(dev); |
| 25 | |
| 26 | writel(0, priv->base + APPLE_WDT_CUR_TIME); |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | static int apple_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
| 32 | { |
| 33 | struct apple_wdt_priv *priv = dev_get_priv(dev); |
| 34 | u64 timeout; |
| 35 | |
| 36 | timeout = (timeout_ms * priv->clk_rate) / 1000; |
| 37 | if (timeout > U32_MAX) |
| 38 | return -EINVAL; |
| 39 | |
| 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); |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int apple_wdt_stop(struct udevice *dev) |
| 48 | { |
| 49 | struct apple_wdt_priv *priv = dev_get_priv(dev); |
| 50 | |
| 51 | writel(0, priv->base + APPLE_WDT_CTRL); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int apple_wdt_expire_now(struct udevice *dev, ulong flags) |
| 57 | { |
| 58 | int ret; |
| 59 | |
| 60 | ret = apple_wdt_start(dev, 0, flags); |
| 61 | if (ret) |
| 62 | return ret; |
| 63 | |
| 64 | /* |
| 65 | * It can take up to 25ms until the SoC actually resets, so |
| 66 | * wait 50ms just to be sure. |
| 67 | */ |
| 68 | mdelay(50); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 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, |
| 78 | }; |
| 79 | |
| 80 | static const struct udevice_id apple_wdt_ids[] = { |
| 81 | { .compatible = "apple,wdt" }, |
| 82 | { /* sentinel */ } |
| 83 | }; |
| 84 | |
| 85 | static int apple_wdt_probe(struct udevice *dev) |
| 86 | { |
| 87 | struct apple_wdt_priv *priv = dev_get_priv(dev); |
| 88 | struct clk clk; |
| 89 | int ret; |
| 90 | |
| 91 | priv->base = dev_read_addr_ptr(dev); |
| 92 | if (!priv->base) |
| 93 | return -EINVAL; |
| 94 | |
| 95 | ret = clk_get_by_index(dev, 0, &clk); |
| 96 | if (ret) |
| 97 | return ret; |
| 98 | |
| 99 | ret = clk_enable(&clk); |
| 100 | if (ret) |
| 101 | return ret; |
| 102 | |
| 103 | priv->clk_rate = clk_get_rate(&clk); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | U_BOOT_DRIVER(apple_wdt) = { |
| 109 | .name = "apple_wdt", |
| 110 | .id = UCLASS_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, |
| 115 | }; |