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> |
| 12 | #include <asm/utils.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 14 | |
| 15 | #define DW_WDT_CR 0x00 |
| 16 | #define DW_WDT_TORR 0x04 |
| 17 | #define DW_WDT_CRR 0x0C |
| 18 | |
| 19 | #define DW_WDT_CR_EN_OFFSET 0x00 |
| 20 | #define DW_WDT_CR_RMOD_OFFSET 0x01 |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 21 | #define DW_WDT_CRR_RESTART_VAL 0x76 |
| 22 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 23 | struct designware_wdt_priv { |
| 24 | void __iomem *base; |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 25 | unsigned int clk_khz; |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 28 | /* |
| 29 | * Set the watchdog time interval. |
| 30 | * Counter is 32 bit. |
| 31 | */ |
Marek Vasut | cf8c836 | 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 | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 34 | { |
| 35 | signed int i; |
| 36 | |
| 37 | /* calculate the timeout range value */ |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 38 | i = log_2_n_round_up(timeout * clk_khz) - 16; |
| 39 | i = clamp(i, 0, 15); |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 40 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 41 | writel(i | (i << 4), base + DW_WDT_TORR); |
| 42 | |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 46 | static void designware_wdt_enable(void __iomem *base) |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 47 | { |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 48 | writel(BIT(DW_WDT_CR_EN_OFFSET), base + DW_WDT_CR); |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 49 | } |
| 50 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 51 | static unsigned int designware_wdt_is_enabled(void __iomem *base) |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 52 | { |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 53 | return readl(base + DW_WDT_CR) & BIT(0); |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 56 | static void designware_wdt_reset_common(void __iomem *base) |
| 57 | { |
| 58 | if (designware_wdt_is_enabled(base)) |
| 59 | /* restart the watchdog counter */ |
| 60 | writel(DW_WDT_CRR_RESTART_VAL, base + DW_WDT_CRR); |
| 61 | } |
| 62 | |
| 63 | #if !CONFIG_IS_ENABLED(WDT) |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 64 | void hw_watchdog_reset(void) |
| 65 | { |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 66 | designware_wdt_reset_common((void __iomem *)CONFIG_DW_WDT_BASE); |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void hw_watchdog_init(void) |
| 70 | { |
| 71 | /* reset to disable the watchdog */ |
| 72 | hw_watchdog_reset(); |
| 73 | /* set timer in miliseconds */ |
Marek Vasut | cf8c836 | 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 | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 77 | /* enable the watchdog */ |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 78 | designware_wdt_enable((void __iomem *)CONFIG_DW_WDT_BASE); |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 79 | /* reset the watchdog */ |
| 80 | hw_watchdog_reset(); |
| 81 | } |
Marek Vasut | cf8c836 | 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 | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 97 | writel(0, priv->base + DW_WDT_CR); |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int designware_wdt_start(struct udevice *dev, u64 timeout, ulong flags) |
| 103 | { |
| 104 | struct designware_wdt_priv *priv = dev_get_priv(dev); |
| 105 | |
| 106 | designware_wdt_stop(dev); |
| 107 | |
| 108 | /* set timer in miliseconds */ |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 109 | designware_wdt_settimeout(priv->base, priv->clk_khz, timeout); |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 110 | |
| 111 | designware_wdt_enable(priv->base); |
| 112 | |
| 113 | /* reset the watchdog */ |
| 114 | return designware_wdt_reset(dev); |
| 115 | } |
| 116 | |
| 117 | static int designware_wdt_probe(struct udevice *dev) |
| 118 | { |
| 119 | struct designware_wdt_priv *priv = dev_get_priv(dev); |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 120 | __maybe_unused int ret; |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 121 | |
| 122 | priv->base = dev_remap_addr(dev); |
| 123 | if (!priv->base) |
| 124 | return -EINVAL; |
| 125 | |
Marek Vasut | cf89ef8 | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 126 | #if CONFIG_IS_ENABLED(CLK) |
| 127 | struct clk clk; |
| 128 | |
| 129 | ret = clk_get_by_index(dev, 0, &clk); |
| 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | priv->clk_khz = clk_get_rate(&clk); |
| 134 | if (!priv->clk_khz) |
| 135 | return -EINVAL; |
| 136 | #else |
| 137 | priv->clk_khz = CONFIG_DW_WDT_CLOCK_KHZ; |
| 138 | #endif |
| 139 | |
| 140 | #if CONFIG_IS_ENABLED(DM_RESET) |
| 141 | struct reset_ctl_bulk resets; |
| 142 | |
| 143 | ret = reset_get_bulk(dev, &resets); |
| 144 | if (ret) |
| 145 | return ret; |
| 146 | |
| 147 | ret = reset_deassert_bulk(&resets); |
| 148 | if (ret) |
| 149 | return ret; |
| 150 | #endif |
| 151 | |
Marek Vasut | cf8c836 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 152 | /* reset to disable the watchdog */ |
| 153 | return designware_wdt_stop(dev); |
| 154 | } |
| 155 | |
| 156 | static const struct wdt_ops designware_wdt_ops = { |
| 157 | .start = designware_wdt_start, |
| 158 | .reset = designware_wdt_reset, |
| 159 | .stop = designware_wdt_stop, |
| 160 | }; |
| 161 | |
| 162 | static const struct udevice_id designware_wdt_ids[] = { |
| 163 | { .compatible = "snps,dw-wdt"}, |
| 164 | {} |
| 165 | }; |
| 166 | |
| 167 | U_BOOT_DRIVER(designware_wdt) = { |
| 168 | .name = "designware_wdt", |
| 169 | .id = UCLASS_WDT, |
| 170 | .of_match = designware_wdt_ids, |
| 171 | .priv_auto_alloc_size = sizeof(struct designware_wdt_priv), |
| 172 | .probe = designware_wdt_probe, |
| 173 | .ops = &designware_wdt_ops, |
| 174 | .flags = DM_FLAG_PRE_RELOC, |
| 175 | }; |
Chin Liang See | d8c67dc | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 176 | #endif |