blob: c21aa3af55fa1754f6bc5e431ede70a6465e47d5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ye Li62862b62017-02-22 16:21:48 +08002/*
3 * Copyright (C) 2016 Freescale Semiconductor, Inc.
Ye Li62862b62017-02-22 16:21:48 +08004 */
5
6#include <common.h>
Simon Glassafb02152019-12-28 10:45:01 -07007#include <cpu_func.h>
Ye Li62862b62017-02-22 16:21:48 +08008#include <asm/io.h>
9#include <asm/arch/imx-regs.h>
Alice Guoab63a6e2022-10-21 16:41:18 +080010#include <dm.h>
11#include <wdt.h>
Ye Li62862b62017-02-22 16:21:48 +080012
13/*
14 * MX7ULP WDOG Register Map
15 */
16struct wdog_regs {
Breno Lima4a686892021-06-29 10:32:35 +080017 u32 cs;
Ye Li62862b62017-02-22 16:21:48 +080018 u32 cnt;
19 u32 toval;
20 u32 win;
21};
22
Alice Guoab63a6e2022-10-21 16:41:18 +080023struct ulp_wdt_priv {
24 struct wdog_regs *wdog;
25 u32 clk_rate;
26};
27
Ye Li62862b62017-02-22 16:21:48 +080028#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 Li67439162022-10-21 16:41:15 +080034#define UNLOCK_WORD 0xD928C520 /* unlock word */
35#define REFRESH_WORD 0xB480A602 /* refresh word */
36
Breno Lima4a686892021-06-29 10:32:35 +080037#define WDGCS_WDGE BIT(7)
38#define WDGCS_WDGUPDATE BIT(5)
Ye Li62862b62017-02-22 16:21:48 +080039
Breno Lima4a686892021-06-29 10:32:35 +080040#define WDGCS_RCS BIT(10)
41#define WDGCS_ULK BIT(11)
Alice Guodea73ea2022-10-21 16:41:16 +080042#define WDOG_CS_PRES BIT(12)
Ye Li67439162022-10-21 16:41:15 +080043#define WDGCS_CMD32EN BIT(13)
Breno Lima4a686892021-06-29 10:32:35 +080044#define WDGCS_FLG BIT(14)
Alice Guo71e0e6c2022-10-21 16:41:17 +080045#define WDGCS_INT BIT(6)
Ye Li62862b62017-02-22 16:21:48 +080046
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 Guoab63a6e2022-10-21 16:41:18 +080052#define CLK_RATE_1KHZ 1000
53#define CLK_RATE_32KHZ 125
54
Ye Li62862b62017-02-22 16:21:48 +080055void 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 Guoab63a6e2022-10-21 16:41:18 +080063void ulp_watchdog_reset(struct wdog_regs *wdog)
Ye Li62862b62017-02-22 16:21:48 +080064{
Ye Li67439162022-10-21 16:41:15 +080065 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 Li62862b62017-02-22 16:21:48 +080073}
74
Alice Guoab63a6e2022-10-21 16:41:18 +080075void ulp_watchdog_init(struct wdog_regs *wdog, u16 timeout)
Ye Li62862b62017-02-22 16:21:48 +080076{
Ye Li67439162022-10-21 16:41:15 +080077 u32 cmd32 = 0;
Ye Li62862b62017-02-22 16:21:48 +080078
Ye Li67439162022-10-21 16:41:15 +080079 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 Li62862b62017-02-22 16:21:48 +080088
Breno Lima4a686892021-06-29 10:32:35 +080089 /* Wait WDOG Unlock */
90 while (!(readl(&wdog->cs) & WDGCS_ULK))
91 ;
Ye Li62862b62017-02-22 16:21:48 +080092
Alice Guoab63a6e2022-10-21 16:41:18 +080093 hw_watchdog_set_timeout(timeout);
Ye Li62862b62017-02-22 16:21:48 +080094 writel(0, &wdog->win);
95
Breno Lima4a686892021-06-29 10:32:35 +080096 /* setting 1-kHz clock source, enable counter running, and clear interrupt */
Alice Guodea73ea2022-10-21 16:41:16 +080097 if (IS_ENABLED(CONFIG_ARCH_IMX9))
98 writel((cmd32 | WDGCS_WDGE | WDGCS_WDGUPDATE | (WDG_LPO_CLK << 8) |
Alice Guo71e0e6c2022-10-21 16:41:17 +080099 WDGCS_FLG | WDOG_CS_PRES | WDGCS_INT), &wdog->cs);
Alice Guodea73ea2022-10-21 16:41:16 +0800100 else
101 writel((cmd32 | WDGCS_WDGE | WDGCS_WDGUPDATE | (WDG_LPO_CLK << 8) |
102 WDGCS_FLG), &wdog->cs);
Breno Lima4a686892021-06-29 10:32:35 +0800103
104 /* Wait WDOG reconfiguration */
105 while (!(readl(&wdog->cs) & WDGCS_RCS))
106 ;
Ye Li62862b62017-02-22 16:21:48 +0800107
Alice Guoab63a6e2022-10-21 16:41:18 +0800108 ulp_watchdog_reset(wdog);
109}
110
111void hw_watchdog_reset(void)
112{
113 struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE_ADDR;
114
115 ulp_watchdog_reset(wdog);
Ye Li62862b62017-02-22 16:21:48 +0800116}
117
Alice Guoab63a6e2022-10-21 16:41:18 +0800118void 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);
123}
124
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100125void reset_cpu(void)
Ye Li62862b62017-02-22 16:21:48 +0800126{
127 struct wdog_regs *wdog = (struct wdog_regs *)WDOG_BASE_ADDR;
Ye Li67439162022-10-21 16:41:15 +0800128 u32 cmd32 = 0;
Ye Li62862b62017-02-22 16:21:48 +0800129
Ye Li67439162022-10-21 16:41:15 +0800130 if (readl(&wdog->cs) & WDGCS_CMD32EN) {
131 writel(UNLOCK_WORD, &wdog->cnt);
132 cmd32 = WDGCS_CMD32EN;
133 } else {
134 dmb();
135 __raw_writel(UNLOCK_WORD0, &wdog->cnt);
136 __raw_writel(UNLOCK_WORD1, &wdog->cnt);
137 dmb();
138 }
Ye Li62862b62017-02-22 16:21:48 +0800139
Breno Lima4a686892021-06-29 10:32:35 +0800140 /* Wait WDOG Unlock */
141 while (!(readl(&wdog->cs) & WDGCS_ULK))
142 ;
143
Alice Guodea73ea2022-10-21 16:41:16 +0800144 hw_watchdog_set_timeout(5); /* 5ms timeout for general; 40ms timeout for imx93 */
Ye Li62862b62017-02-22 16:21:48 +0800145 writel(0, &wdog->win);
146
Breno Lima4a686892021-06-29 10:32:35 +0800147 /* enable counter running */
Alice Guodea73ea2022-10-21 16:41:16 +0800148 if (IS_ENABLED(CONFIG_ARCH_IMX9))
Alice Guo71e0e6c2022-10-21 16:41:17 +0800149 writel((cmd32 | WDGCS_WDGE | (WDG_LPO_CLK << 8) | WDOG_CS_PRES |
150 WDGCS_INT), &wdog->cs);
Alice Guodea73ea2022-10-21 16:41:16 +0800151 else
152 writel((cmd32 | WDGCS_WDGE | (WDG_LPO_CLK << 8)), &wdog->cs);
Breno Lima4a686892021-06-29 10:32:35 +0800153
154 /* Wait WDOG reconfiguration */
155 while (!(readl(&wdog->cs) & WDGCS_RCS))
156 ;
Ye Li62862b62017-02-22 16:21:48 +0800157
158 hw_watchdog_reset();
159
160 while (1);
161}
Alice Guoab63a6e2022-10-21 16:41:18 +0800162
163static int ulp_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
164{
165 struct ulp_wdt_priv *priv = dev_get_priv(dev);
166 u64 timeout = 0;
167
168 timeout = (timeout_ms * priv->clk_rate) / 1000;
169 if (timeout > U16_MAX)
170 return -EINVAL;
171
172 ulp_watchdog_init(priv->wdog, (u16)timeout);
173
174 return 0;
175}
176
177static int ulp_wdt_reset(struct udevice *dev)
178{
179 struct ulp_wdt_priv *priv = dev_get_priv(dev);
180
181 ulp_watchdog_reset(priv->wdog);
182
183 return 0;
184}
185
186static int ulp_wdt_probe(struct udevice *dev)
187{
188 struct ulp_wdt_priv *priv = dev_get_priv(dev);
189
190 priv->wdog = dev_read_addr_ptr(dev);
191 if (!priv->wdog)
192 return -EINVAL;
193
194 priv->clk_rate = (u32)dev_get_driver_data(dev);
195 if (!priv->clk_rate)
196 return -EINVAL;
197
198 return 0;
199}
200
201static const struct wdt_ops ulp_wdt_ops = {
202 .start = ulp_wdt_start,
203 .reset = ulp_wdt_reset,
204};
205
206static const struct udevice_id ulp_wdt_ids[] = {
207 { .compatible = "fsl,imx7ulp-wdt", .data = CLK_RATE_1KHZ },
208 { .compatible = "fsl,imx8ulp-wdt", .data = CLK_RATE_1KHZ },
209 { .compatible = "fsl,imx93-wdt", .data = CLK_RATE_32KHZ },
210 {}
211};
212
213U_BOOT_DRIVER(ulp_wdt) = {
214 .name = "ulp_wdt",
215 .id = UCLASS_WDT,
216 .of_match = ulp_wdt_ids,
217 .priv_auto = sizeof(struct ulp_wdt_priv),
218 .probe = ulp_wdt_probe,
219 .ops = &ulp_wdt_ops,
220};