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