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; |
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 | #if !CONFIG_IS_ENABLED(WDT) |
| 63 | void hw_watchdog_reset(void) |
| 64 | { |
| 65 | designware_wdt_reset_common((void __iomem *)CONFIG_DW_WDT_BASE); |
| 66 | } |
| 67 | |
Chin Liang See | 9d80009 | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 68 | void hw_watchdog_init(void) |
| 69 | { |
| 70 | /* reset to disable the watchdog */ |
| 71 | hw_watchdog_reset(); |
| 72 | /* set timer in miliseconds */ |
Marek Vasut | 8655f67 | 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 | 9d80009 | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 76 | /* enable the watchdog */ |
Marek Vasut | 8655f67 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 77 | designware_wdt_enable((void __iomem *)CONFIG_DW_WDT_BASE); |
Chin Liang See | 9d80009 | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 78 | /* reset the watchdog */ |
| 79 | hw_watchdog_reset(); |
| 80 | } |
Marek Vasut | 8655f67 | 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 | b1fdd3a | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 96 | writel(0, priv->base + DW_WDT_CR); |
Marek Vasut | 8655f67 | 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 | b1fdd3a | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 108 | designware_wdt_settimeout(priv->base, priv->clk_khz, timeout); |
Marek Vasut | 8655f67 | 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 | b1fdd3a | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 119 | __maybe_unused int ret; |
Marek Vasut | 8655f67 | 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 | b1fdd3a | 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 | |
Jack Mitchell | f23419c | 2020-09-17 10:30:40 +0100 | [diff] [blame] | 132 | priv->clk_khz = clk_get_rate(&clk) / 1000; |
Marek Vasut | b1fdd3a | 2019-10-03 14:47:07 +0200 | [diff] [blame] | 133 | if (!priv->clk_khz) |
| 134 | return -EINVAL; |
| 135 | #else |
| 136 | priv->clk_khz = CONFIG_DW_WDT_CLOCK_KHZ; |
| 137 | #endif |
| 138 | |
| 139 | #if CONFIG_IS_ENABLED(DM_RESET) |
| 140 | struct reset_ctl_bulk resets; |
| 141 | |
| 142 | ret = reset_get_bulk(dev, &resets); |
| 143 | if (ret) |
| 144 | return ret; |
| 145 | |
| 146 | ret = reset_deassert_bulk(&resets); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | #endif |
| 150 | |
Marek Vasut | 8655f67 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 151 | /* reset to disable the watchdog */ |
| 152 | return designware_wdt_stop(dev); |
| 153 | } |
| 154 | |
| 155 | static const struct wdt_ops designware_wdt_ops = { |
| 156 | .start = designware_wdt_start, |
| 157 | .reset = designware_wdt_reset, |
| 158 | .stop = designware_wdt_stop, |
| 159 | }; |
| 160 | |
| 161 | static const struct udevice_id designware_wdt_ids[] = { |
| 162 | { .compatible = "snps,dw-wdt"}, |
| 163 | {} |
| 164 | }; |
| 165 | |
| 166 | U_BOOT_DRIVER(designware_wdt) = { |
| 167 | .name = "designware_wdt", |
| 168 | .id = UCLASS_WDT, |
| 169 | .of_match = designware_wdt_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 170 | .priv_auto = sizeof(struct designware_wdt_priv), |
Marek Vasut | 8655f67 | 2019-06-27 01:19:23 +0200 | [diff] [blame] | 171 | .probe = designware_wdt_probe, |
| 172 | .ops = &designware_wdt_ops, |
| 173 | .flags = DM_FLAG_PRE_RELOC, |
| 174 | }; |
Chin Liang See | 9d80009 | 2014-06-10 01:10:21 -0500 | [diff] [blame] | 175 | #endif |