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