Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Freescale Semiconductor, Inc. |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 9a3b4ce | 2019-12-28 10:45:01 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 8 | #include <asm/io.h> |
| 9 | #include <asm/arch/imx-regs.h> |
Alice Guo | 5e112c7 | 2022-10-21 16:41:18 +0800 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <wdt.h> |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 12 | |
| 13 | /* |
| 14 | * MX7ULP WDOG Register Map |
| 15 | */ |
| 16 | struct wdog_regs { |
Breno Lima | edf95bd | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 17 | u32 cs; |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 18 | u32 cnt; |
| 19 | u32 toval; |
| 20 | u32 win; |
| 21 | }; |
| 22 | |
Alice Guo | 5e112c7 | 2022-10-21 16:41:18 +0800 | [diff] [blame] | 23 | struct ulp_wdt_priv { |
| 24 | struct wdog_regs *wdog; |
| 25 | u32 clk_rate; |
| 26 | }; |
| 27 | |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 28 | #define REFRESH_WORD0 0xA602 /* 1st refresh word */ |
| 29 | #define REFRESH_WORD1 0xB480 /* 2nd refresh word */ |
| 30 | |
| 31 | #define UNLOCK_WORD0 0xC520 /* 1st unlock word */ |
| 32 | #define UNLOCK_WORD1 0xD928 /* 2nd unlock word */ |
| 33 | |
Ye Li | a79f200 | 2022-10-21 16:41:15 +0800 | [diff] [blame] | 34 | #define UNLOCK_WORD 0xD928C520 /* unlock word */ |
| 35 | #define REFRESH_WORD 0xB480A602 /* refresh word */ |
| 36 | |
Breno Lima | edf95bd | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 37 | #define WDGCS_WDGE BIT(7) |
| 38 | #define WDGCS_WDGUPDATE BIT(5) |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 39 | |
Breno Lima | edf95bd | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 40 | #define WDGCS_RCS BIT(10) |
| 41 | #define WDGCS_ULK BIT(11) |
Alice Guo | ef0ad9b | 2022-10-21 16:41:16 +0800 | [diff] [blame] | 42 | #define WDOG_CS_PRES BIT(12) |
Ye Li | a79f200 | 2022-10-21 16:41:15 +0800 | [diff] [blame] | 43 | #define WDGCS_CMD32EN BIT(13) |
Breno Lima | edf95bd | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 44 | #define WDGCS_FLG BIT(14) |
Alice Guo | a7fd633 | 2022-10-21 16:41:17 +0800 | [diff] [blame] | 45 | #define WDGCS_INT BIT(6) |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 46 | |
| 47 | #define WDG_BUS_CLK (0x0) |
| 48 | #define WDG_LPO_CLK (0x1) |
| 49 | #define WDG_32KHZ_CLK (0x2) |
| 50 | #define WDG_EXT_CLK (0x3) |
| 51 | |
Alice Guo | 5e112c7 | 2022-10-21 16:41:18 +0800 | [diff] [blame] | 52 | #define CLK_RATE_1KHZ 1000 |
| 53 | #define CLK_RATE_32KHZ 125 |
| 54 | |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 55 | void hw_watchdog_set_timeout(u16 val) |
| 56 | { |
| 57 | /* setting timeout value */ |
| 58 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE_ADDR; |
| 59 | |
| 60 | writel(val, &wdog->toval); |
| 61 | } |
| 62 | |
Alice Guo | 5e112c7 | 2022-10-21 16:41:18 +0800 | [diff] [blame] | 63 | void ulp_watchdog_reset(struct wdog_regs *wdog) |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 64 | { |
Ye Li | a79f200 | 2022-10-21 16:41:15 +0800 | [diff] [blame] | 65 | if (readl(&wdog->cs) & WDGCS_CMD32EN) { |
| 66 | writel(REFRESH_WORD, &wdog->cnt); |
| 67 | } else { |
| 68 | dmb(); |
| 69 | __raw_writel(REFRESH_WORD0, &wdog->cnt); |
| 70 | __raw_writel(REFRESH_WORD1, &wdog->cnt); |
| 71 | dmb(); |
| 72 | } |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 73 | } |
| 74 | |
Alice Guo | 5e112c7 | 2022-10-21 16:41:18 +0800 | [diff] [blame] | 75 | void ulp_watchdog_init(struct wdog_regs *wdog, u16 timeout) |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 76 | { |
Ye Li | a79f200 | 2022-10-21 16:41:15 +0800 | [diff] [blame] | 77 | u32 cmd32 = 0; |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 78 | |
Ye Li | a79f200 | 2022-10-21 16:41:15 +0800 | [diff] [blame] | 79 | if (readl(&wdog->cs) & WDGCS_CMD32EN) { |
| 80 | writel(UNLOCK_WORD, &wdog->cnt); |
| 81 | cmd32 = WDGCS_CMD32EN; |
| 82 | } else { |
| 83 | dmb(); |
| 84 | __raw_writel(UNLOCK_WORD0, &wdog->cnt); |
| 85 | __raw_writel(UNLOCK_WORD1, &wdog->cnt); |
| 86 | dmb(); |
| 87 | } |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 88 | |
Breno Lima | edf95bd | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 89 | /* Wait WDOG Unlock */ |
| 90 | while (!(readl(&wdog->cs) & WDGCS_ULK)) |
| 91 | ; |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 92 | |
Alice Guo | 5e112c7 | 2022-10-21 16:41:18 +0800 | [diff] [blame] | 93 | hw_watchdog_set_timeout(timeout); |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 94 | writel(0, &wdog->win); |
| 95 | |
Breno Lima | edf95bd | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 96 | /* setting 1-kHz clock source, enable counter running, and clear interrupt */ |
Alice Guo | ef0ad9b | 2022-10-21 16:41:16 +0800 | [diff] [blame] | 97 | if (IS_ENABLED(CONFIG_ARCH_IMX9)) |
| 98 | writel((cmd32 | WDGCS_WDGE | WDGCS_WDGUPDATE | (WDG_LPO_CLK << 8) | |
Alice Guo | a7fd633 | 2022-10-21 16:41:17 +0800 | [diff] [blame] | 99 | WDGCS_FLG | WDOG_CS_PRES | WDGCS_INT), &wdog->cs); |
Alice Guo | ef0ad9b | 2022-10-21 16:41:16 +0800 | [diff] [blame] | 100 | else |
| 101 | writel((cmd32 | WDGCS_WDGE | WDGCS_WDGUPDATE | (WDG_LPO_CLK << 8) | |
| 102 | WDGCS_FLG), &wdog->cs); |
Breno Lima | edf95bd | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 103 | |
| 104 | /* Wait WDOG reconfiguration */ |
| 105 | while (!(readl(&wdog->cs) & WDGCS_RCS)) |
| 106 | ; |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 107 | |
Alice Guo | 5e112c7 | 2022-10-21 16:41:18 +0800 | [diff] [blame] | 108 | ulp_watchdog_reset(wdog); |
| 109 | } |
| 110 | |
| 111 | void hw_watchdog_reset(void) |
| 112 | { |
| 113 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE_ADDR; |
| 114 | |
| 115 | ulp_watchdog_reset(wdog); |
| 116 | } |
| 117 | |
| 118 | void hw_watchdog_init(void) |
| 119 | { |
| 120 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE_ADDR; |
| 121 | |
| 122 | ulp_watchdog_init(wdog, CONFIG_WATCHDOG_TIMEOUT_MSECS); |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 123 | } |
| 124 | |
Peng Fan | fee8cf2 | 2023-04-28 12:08:26 +0800 | [diff] [blame] | 125 | #if !CONFIG_IS_ENABLED(SYSRESET) |
Harald Seiler | 35b65dd | 2020-12-15 16:47:52 +0100 | [diff] [blame] | 126 | void reset_cpu(void) |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 127 | { |
| 128 | struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE_ADDR; |
Ye Li | a79f200 | 2022-10-21 16:41:15 +0800 | [diff] [blame] | 129 | u32 cmd32 = 0; |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 130 | |
Ye Li | a79f200 | 2022-10-21 16:41:15 +0800 | [diff] [blame] | 131 | if (readl(&wdog->cs) & WDGCS_CMD32EN) { |
| 132 | writel(UNLOCK_WORD, &wdog->cnt); |
| 133 | cmd32 = WDGCS_CMD32EN; |
| 134 | } else { |
| 135 | dmb(); |
| 136 | __raw_writel(UNLOCK_WORD0, &wdog->cnt); |
| 137 | __raw_writel(UNLOCK_WORD1, &wdog->cnt); |
| 138 | dmb(); |
| 139 | } |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 140 | |
Breno Lima | edf95bd | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 141 | /* Wait WDOG Unlock */ |
| 142 | while (!(readl(&wdog->cs) & WDGCS_ULK)) |
| 143 | ; |
| 144 | |
Alice Guo | ef0ad9b | 2022-10-21 16:41:16 +0800 | [diff] [blame] | 145 | hw_watchdog_set_timeout(5); /* 5ms timeout for general; 40ms timeout for imx93 */ |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 146 | writel(0, &wdog->win); |
| 147 | |
Breno Lima | edf95bd | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 148 | /* enable counter running */ |
Alice Guo | ef0ad9b | 2022-10-21 16:41:16 +0800 | [diff] [blame] | 149 | if (IS_ENABLED(CONFIG_ARCH_IMX9)) |
Alice Guo | a7fd633 | 2022-10-21 16:41:17 +0800 | [diff] [blame] | 150 | writel((cmd32 | WDGCS_WDGE | (WDG_LPO_CLK << 8) | WDOG_CS_PRES | |
| 151 | WDGCS_INT), &wdog->cs); |
Alice Guo | ef0ad9b | 2022-10-21 16:41:16 +0800 | [diff] [blame] | 152 | else |
| 153 | writel((cmd32 | WDGCS_WDGE | (WDG_LPO_CLK << 8)), &wdog->cs); |
Breno Lima | edf95bd | 2021-06-29 10:32:35 +0800 | [diff] [blame] | 154 | |
| 155 | /* Wait WDOG reconfiguration */ |
| 156 | while (!(readl(&wdog->cs) & WDGCS_RCS)) |
| 157 | ; |
Ye Li | 253531b | 2017-02-22 16:21:48 +0800 | [diff] [blame] | 158 | |
| 159 | hw_watchdog_reset(); |
| 160 | |
| 161 | while (1); |
| 162 | } |
Peng Fan | fee8cf2 | 2023-04-28 12:08:26 +0800 | [diff] [blame] | 163 | #endif |
Alice Guo | 5e112c7 | 2022-10-21 16:41:18 +0800 | [diff] [blame] | 164 | |
| 165 | static int ulp_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
| 166 | { |
| 167 | struct ulp_wdt_priv *priv = dev_get_priv(dev); |
| 168 | u64 timeout = 0; |
| 169 | |
| 170 | timeout = (timeout_ms * priv->clk_rate) / 1000; |
| 171 | if (timeout > U16_MAX) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | ulp_watchdog_init(priv->wdog, (u16)timeout); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int ulp_wdt_reset(struct udevice *dev) |
| 180 | { |
| 181 | struct ulp_wdt_priv *priv = dev_get_priv(dev); |
| 182 | |
| 183 | ulp_watchdog_reset(priv->wdog); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int ulp_wdt_probe(struct udevice *dev) |
| 189 | { |
| 190 | struct ulp_wdt_priv *priv = dev_get_priv(dev); |
| 191 | |
| 192 | priv->wdog = dev_read_addr_ptr(dev); |
| 193 | if (!priv->wdog) |
| 194 | return -EINVAL; |
| 195 | |
| 196 | priv->clk_rate = (u32)dev_get_driver_data(dev); |
| 197 | if (!priv->clk_rate) |
| 198 | return -EINVAL; |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static const struct wdt_ops ulp_wdt_ops = { |
| 204 | .start = ulp_wdt_start, |
| 205 | .reset = ulp_wdt_reset, |
| 206 | }; |
| 207 | |
| 208 | static const struct udevice_id ulp_wdt_ids[] = { |
| 209 | { .compatible = "fsl,imx7ulp-wdt", .data = CLK_RATE_1KHZ }, |
| 210 | { .compatible = "fsl,imx8ulp-wdt", .data = CLK_RATE_1KHZ }, |
| 211 | { .compatible = "fsl,imx93-wdt", .data = CLK_RATE_32KHZ }, |
| 212 | {} |
| 213 | }; |
| 214 | |
| 215 | U_BOOT_DRIVER(ulp_wdt) = { |
| 216 | .name = "ulp_wdt", |
| 217 | .id = UCLASS_WDT, |
| 218 | .of_match = ulp_wdt_ids, |
| 219 | .priv_auto = sizeof(struct ulp_wdt_priv), |
| 220 | .probe = ulp_wdt_probe, |
| 221 | .ops = &ulp_wdt_ops, |
| 222 | }; |