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