Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Altera Corporation <www.altera.com> |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 4 | */ |
| 5 | |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 6 | #include <clk.h> |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 7 | #include <common.h> |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 8 | #include <dm.h> |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 9 | #include <reset.h> |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 10 | #include <wdt.h> |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 11 | #include <asm/io.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 13 | |
| 14 | #define DW_WDT_CR 0x00 |
| 15 | #define DW_WDT_TORR 0x04 |
| 16 | #define DW_WDT_CRR 0x0C |
| 17 | |
| 18 | #define DW_WDT_CR_EN_OFFSET 0x00 |
| 19 | #define DW_WDT_CR_RMOD_OFFSET 0x01 |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 20 | #define DW_WDT_CRR_RESTART_VAL 0x76 |
| 21 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 22 | struct designware_wdt_priv { |
| 23 | void __iomem *base; |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 24 | unsigned int clk_khz; |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 25 | }; |
| 26 | |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 27 | /* |
| 28 | * Set the watchdog time interval. |
| 29 | * Counter is 32 bit. |
| 30 | */ |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 31 | static int designware_wdt_settimeout(void __iomem *base, unsigned int clk_khz, |
| 32 | unsigned int timeout) |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 33 | { |
| 34 | signed int i; |
| 35 | |
| 36 | /* calculate the timeout range value */ |
Sean Anderson | cb57811 | 2021-03-10 21:02:17 -0500 | [diff] [blame] | 37 | i = fls(timeout * clk_khz - 1) - 16; |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 38 | i = clamp(i, 0, 15); |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 39 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 40 | writel(i | (i << 4), base + DW_WDT_TORR); |
| 41 | |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 45 | static void designware_wdt_enable(void __iomem *base) |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 46 | { |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 47 | writel(BIT(DW_WDT_CR_EN_OFFSET), base + DW_WDT_CR); |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 50 | static unsigned int designware_wdt_is_enabled(void __iomem *base) |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 51 | { |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 52 | return readl(base + DW_WDT_CR) & BIT(0); |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 55 | static void designware_wdt_reset_common(void __iomem *base) |
| 56 | { |
| 57 | if (designware_wdt_is_enabled(base)) |
| 58 | /* restart the watchdog counter */ |
| 59 | writel(DW_WDT_CRR_RESTART_VAL, base + DW_WDT_CRR); |
| 60 | } |
| 61 | |
| 62 | #if !CONFIG_IS_ENABLED(WDT) |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 63 | void hw_watchdog_reset(void) |
| 64 | { |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 65 | designware_wdt_reset_common((void __iomem *)CONFIG_DW_WDT_BASE); |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void hw_watchdog_init(void) |
| 69 | { |
| 70 | /* reset to disable the watchdog */ |
| 71 | hw_watchdog_reset(); |
| 72 | /* set timer in miliseconds */ |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 73 | designware_wdt_settimeout((void __iomem *)CONFIG_DW_WDT_BASE, |
| 74 | CONFIG_DW_WDT_CLOCK_KHZ, |
| 75 | CONFIG_WATCHDOG_TIMEOUT_MSECS); |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 76 | /* enable the watchdog */ |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 77 | designware_wdt_enable((void __iomem *)CONFIG_DW_WDT_BASE); |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 78 | /* reset the watchdog */ |
| 79 | hw_watchdog_reset(); |
| 80 | } |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 81 | #else |
| 82 | static int designware_wdt_reset(struct udevice *dev) |
| 83 | { |
| 84 | struct designware_wdt_priv *priv = dev_get_priv(dev); |
| 85 | |
| 86 | designware_wdt_reset_common(priv->base); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int designware_wdt_stop(struct udevice *dev) |
| 92 | { |
| 93 | struct designware_wdt_priv *priv = dev_get_priv(dev); |
| 94 | |
| 95 | designware_wdt_reset(dev); |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 96 | writel(0, priv->base + DW_WDT_CR); |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int designware_wdt_start(struct udevice *dev, u64 timeout, ulong flags) |
| 102 | { |
| 103 | struct designware_wdt_priv *priv = dev_get_priv(dev); |
| 104 | |
| 105 | designware_wdt_stop(dev); |
| 106 | |
| 107 | /* set timer in miliseconds */ |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 108 | designware_wdt_settimeout(priv->base, priv->clk_khz, timeout); |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 109 | |
| 110 | designware_wdt_enable(priv->base); |
| 111 | |
| 112 | /* reset the watchdog */ |
| 113 | return designware_wdt_reset(dev); |
| 114 | } |
| 115 | |
| 116 | static int designware_wdt_probe(struct udevice *dev) |
| 117 | { |
| 118 | struct designware_wdt_priv *priv = dev_get_priv(dev); |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 119 | __maybe_unused int ret; |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 120 | |
| 121 | priv->base = dev_remap_addr(dev); |
| 122 | if (!priv->base) |
| 123 | return -EINVAL; |
| 124 | |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 125 | #if CONFIG_IS_ENABLED(CLK) |
| 126 | struct clk clk; |
| 127 | |
| 128 | ret = clk_get_by_index(dev, 0, &clk); |
| 129 | if (ret) |
| 130 | return ret; |
| 131 | |
Sean Anderson | 4cb0ab4 | 2021-03-10 21:02:19 -0500 | [diff] [blame] | 132 | ret = clk_enable(&clk); |
| 133 | if (ret) |
Sean Anderson | 97bcdd2 | 2021-03-10 21:02:20 -0500 | [diff] [blame] | 134 | goto err; |
Sean Anderson | 4cb0ab4 | 2021-03-10 21:02:19 -0500 | [diff] [blame] | 135 | |
Jack Mitchell | d9b9c91 | 2020-09-17 10:30:40 +0100 | [diff] [blame] | 136 | priv->clk_khz = clk_get_rate(&clk) / 1000; |
Sean Anderson | 97bcdd2 | 2021-03-10 21:02:20 -0500 | [diff] [blame] | 137 | if (!priv->clk_khz) { |
| 138 | ret = -EINVAL; |
| 139 | goto err; |
| 140 | } |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 141 | #else |
| 142 | priv->clk_khz = CONFIG_DW_WDT_CLOCK_KHZ; |
| 143 | #endif |
| 144 | |
Sean Anderson | 7d83943 | 2021-03-10 21:02:18 -0500 | [diff] [blame] | 145 | if (CONFIG_IS_ENABLED(DM_RESET)) { |
| 146 | struct reset_ctl_bulk resets; |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 147 | |
Sean Anderson | 7d83943 | 2021-03-10 21:02:18 -0500 | [diff] [blame] | 148 | ret = reset_get_bulk(dev, &resets); |
| 149 | if (ret) |
Sean Anderson | 97bcdd2 | 2021-03-10 21:02:20 -0500 | [diff] [blame] | 150 | goto err; |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 151 | |
Sean Anderson | 7d83943 | 2021-03-10 21:02:18 -0500 | [diff] [blame] | 152 | ret = reset_deassert_bulk(&resets); |
| 153 | if (ret) |
Sean Anderson | 97bcdd2 | 2021-03-10 21:02:20 -0500 | [diff] [blame] | 154 | goto err; |
Sean Anderson | 7d83943 | 2021-03-10 21:02:18 -0500 | [diff] [blame] | 155 | } |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 156 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 157 | /* reset to disable the watchdog */ |
| 158 | return designware_wdt_stop(dev); |
Sean Anderson | 97bcdd2 | 2021-03-10 21:02:20 -0500 | [diff] [blame] | 159 | |
| 160 | err: |
| 161 | #if CONFIG_IS_ENABLED(CLK) |
| 162 | clk_free(&clk); |
| 163 | #endif |
| 164 | return ret; |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static const struct wdt_ops designware_wdt_ops = { |
| 168 | .start = designware_wdt_start, |
| 169 | .reset = designware_wdt_reset, |
| 170 | .stop = designware_wdt_stop, |
| 171 | }; |
| 172 | |
| 173 | static const struct udevice_id designware_wdt_ids[] = { |
| 174 | { .compatible = "snps,dw-wdt"}, |
| 175 | {} |
| 176 | }; |
| 177 | |
| 178 | U_BOOT_DRIVER(designware_wdt) = { |
| 179 | .name = "designware_wdt", |
| 180 | .id = UCLASS_WDT, |
| 181 | .of_match = designware_wdt_ids, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 182 | .priv_auto = sizeof(struct designware_wdt_priv), |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 183 | .probe = designware_wdt_probe, |
| 184 | .ops = &designware_wdt_ops, |
| 185 | .flags = DM_FLAG_PRE_RELOC, |
| 186 | }; |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 187 | #endif |