xref: /optee_os/core/drivers/rstctrl/stm32mp25_rstctrl.c (revision ef3bc69c72b8d46493eab724eab6e018423088e1)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (C) 2024, STMicroelectronics
4  */
5 
6 #include <arm.h>
7 #include <drivers/rstctrl.h>
8 #include <drivers/stm32_shared_io.h>
9 #include <drivers/stm32mp25_rcc.h>
10 #include <drivers/stm32mp_dt_bindings.h>
11 #include <io.h>
12 #include <kernel/dt.h>
13 #include <kernel/dt_driver.h>
14 #include <stm32_util.h>
15 
16 #include "stm32_rstctrl.h"
17 
18 static TEE_Result stm32_reset_update(struct rstctrl *rstctrl, bool status,
19 				     unsigned int to_us)
20 {
21 	unsigned int id = to_stm32_rstline(rstctrl)->id;
22 	const struct stm32_reset_data *data = NULL;
23 	const struct stm32_reset_cfg *rst_line = NULL;
24 	vaddr_t address = stm32_rcc_base();
25 	uint32_t bit_mask = 0;
26 	uint32_t value = 0;
27 
28 	data = to_stm32_rstline(rstctrl)->data;
29 
30 	rst_line = data->rst_lines[id];
31 	if (!rst_line)
32 		return TEE_SUCCESS;
33 
34 	address += rst_line->offset;
35 	bit_mask = BIT(rst_line->bit_index);
36 
37 	if (!status && rst_line->no_deassert)
38 		return TEE_SUCCESS;
39 
40 	status = rst_line->inverted ^ status;
41 
42 	if (status) {
43 		if (rst_line->set_clr)
44 			io_write32(address, bit_mask);
45 		else
46 			io_setbits32_stm32shregs(address, bit_mask);
47 	} else {
48 		if (rst_line->set_clr)
49 			io_write32(address + RCC_MP_ENCLRR_OFFSET, bit_mask);
50 		else
51 			io_clrbits32_stm32shregs(address, bit_mask);
52 	}
53 
54 	if (to_us && !rst_line->no_timeout) {
55 		if (IO_READ32_POLL_TIMEOUT(address, value,
56 					   ((value & bit_mask) == bit_mask) ==
57 					   status, 0, to_us))
58 			return TEE_ERROR_GENERIC;
59 	} else {
60 		/* Make sure the above write is performed */
61 		dsb();
62 	}
63 
64 	return TEE_SUCCESS;
65 }
66 
67 static TEE_Result stm32_reset_assert(struct rstctrl *rstctrl,
68 				     unsigned int to_us)
69 {
70 	return stm32_reset_update(rstctrl, true, to_us);
71 }
72 
73 static TEE_Result stm32_reset_deassert(struct rstctrl *rstctrl,
74 				       unsigned int to_us)
75 {
76 	return stm32_reset_update(rstctrl, false, to_us);
77 }
78 
79 static const struct rstctrl_ops stm32_rstctrl_ops = {
80 	.assert_level = stm32_reset_assert,
81 	.deassert_level = stm32_reset_deassert,
82 };
83 
84 #define STM32_RESET(id, _offset, _bit_index, _set_clr, _inverted, _no_deassert,\
85 		    _no_timeout)\
86 	[(id)] = &(struct stm32_reset_cfg){\
87 		.offset		= (_offset),\
88 		.bit_index	= (_bit_index),\
89 		.set_clr	= (_set_clr),\
90 		.inverted	= (_inverted),\
91 		.no_deassert	= (_no_deassert),\
92 		.no_timeout	= (_no_timeout),\
93 	}
94 
95 #define RST(id, _offset, _bit_index)\
96 	STM32_RESET((id), (_offset), (_bit_index), false, false, false, false)
97 
98 #define RST_SETR(id, _offset, _bit_index)\
99 	STM32_RESET((id), (_offset), (_bit_index), true, false, false, false)
100 
101 #define RST_INV(id, _offset, _bit_index)\
102 	STM32_RESET((id), (_offset), (_bit_index), false, true, false, false)
103 
104 #define RST_SETR_NO_DEASSERT(id, _offset, _bit_index)\
105 	STM32_RESET((id), (_offset), (_bit_index), false, false, true, false)
106 
107 #define RST_SETR_NO_DEASSERT_TIMEOUT(id, _offset, _bit_index)\
108 	STM32_RESET((id), (_offset), (_bit_index), false, false, true, true)
109 
110 static
111 const struct stm32_reset_cfg *stm32mp25_reset_cfg[STM32MP25_LAST_RESET] = {
112 	RST(TIM1_R,		RCC_TIM1CFGR,		0),
113 	RST(TIM2_R,		RCC_TIM2CFGR,		0),
114 	RST(TIM3_R,		RCC_TIM3CFGR,		0),
115 	RST(TIM4_R,		RCC_TIM4CFGR,		0),
116 	RST(TIM5_R,		RCC_TIM5CFGR,		0),
117 	RST(TIM6_R,		RCC_TIM6CFGR,		0),
118 	RST(TIM7_R,		RCC_TIM7CFGR,		0),
119 	RST(TIM8_R,		RCC_TIM8CFGR,		0),
120 	RST(TIM10_R,		RCC_TIM10CFGR,		0),
121 	RST(TIM11_R,		RCC_TIM11CFGR,		0),
122 	RST(TIM12_R,		RCC_TIM12CFGR,		0),
123 	RST(TIM13_R,		RCC_TIM13CFGR,		0),
124 	RST(TIM14_R,		RCC_TIM14CFGR,		0),
125 	RST(TIM15_R,		RCC_TIM15CFGR,		0),
126 	RST(TIM16_R,		RCC_TIM16CFGR,		0),
127 	RST(TIM17_R,		RCC_TIM17CFGR,		0),
128 	RST(TIM20_R,		RCC_TIM20CFGR,		0),
129 	RST(LPTIM1_R,		RCC_LPTIM1CFGR,		0),
130 	RST(LPTIM2_R,		RCC_LPTIM2CFGR,		0),
131 	RST(LPTIM3_R,		RCC_LPTIM3CFGR,		0),
132 	RST(LPTIM4_R,		RCC_LPTIM4CFGR,		0),
133 	RST(LPTIM5_R,		RCC_LPTIM5CFGR,		0),
134 	RST(SPI1_R,		RCC_SPI1CFGR,		0),
135 	RST(SPI2_R,		RCC_SPI2CFGR,		0),
136 	RST(SPI3_R,		RCC_SPI3CFGR,		0),
137 	RST(SPI4_R,		RCC_SPI4CFGR,		0),
138 	RST(SPI5_R,		RCC_SPI5CFGR,		0),
139 	RST(SPI6_R,		RCC_SPI6CFGR,		0),
140 	RST(SPI7_R,		RCC_SPI7CFGR,		0),
141 	RST(SPI8_R,		RCC_SPI8CFGR,		0),
142 	RST(SPDIFRX_R,		RCC_SPDIFRXCFGR,	0),
143 	RST(USART1_R,		RCC_USART1CFGR,		0),
144 	RST(USART2_R,		RCC_USART2CFGR,		0),
145 	RST(USART3_R,		RCC_USART3CFGR,		0),
146 	RST(UART4_R,		RCC_UART4CFGR,		0),
147 	RST(UART5_R,		RCC_UART5CFGR,		0),
148 	RST(USART6_R,		RCC_USART6CFGR,		0),
149 	RST(UART7_R,		RCC_UART7CFGR,		0),
150 	RST(UART8_R,		RCC_UART8CFGR,		0),
151 	RST(UART9_R,		RCC_UART9CFGR,		0),
152 	RST(LPUART1_R,		RCC_LPUART1CFGR,	0),
153 	RST(IS2M_R,		RCC_IS2MCFGR,		0),
154 	RST(I2C1_R,		RCC_I2C1CFGR,		0),
155 	RST(I2C2_R,		RCC_I2C2CFGR,		0),
156 	RST(I2C3_R,		RCC_I2C3CFGR,		0),
157 	RST(I2C4_R,		RCC_I2C4CFGR,		0),
158 	RST(I2C5_R,		RCC_I2C5CFGR,		0),
159 	RST(I2C6_R,		RCC_I2C6CFGR,		0),
160 	RST(I2C7_R,		RCC_I2C7CFGR,		0),
161 	RST(I2C8_R,		RCC_I2C8CFGR,		0),
162 	RST(SAI1_R,		RCC_SAI1CFGR,		0),
163 	RST(SAI2_R,		RCC_SAI2CFGR,		0),
164 	RST(SAI3_R,		RCC_SAI3CFGR,		0),
165 	RST(SAI4_R,		RCC_SAI4CFGR,		0),
166 	RST(MDF1_R,		RCC_MDF1CFGR,		0),
167 	RST(MDF2_R,		RCC_ADF1CFGR,		0),
168 	RST(FDCAN_R,		RCC_FDCANCFGR,		0),
169 	RST(HDP_R,		RCC_HDPCFGR,		0),
170 	RST(ADC12_R,		RCC_ADC12CFGR,		0),
171 	RST(ADC3_R,		RCC_ADC3CFGR,		0),
172 	RST(ETH1_R,		RCC_ETH1CFGR,		0),
173 	RST(ETH2_R,		RCC_ETH2CFGR,		0),
174 	RST(USB2_R,		RCC_USB2CFGR,		0),
175 	RST(USB2PHY1_R,		RCC_USB2PHY1CFGR,	0),
176 	RST(USB2PHY2_R,		RCC_USB2PHY2CFGR,	0),
177 	RST(USB3DR_R,		RCC_USB3DRCFGR,		0),
178 	RST(USB3PCIEPHY_R,	RCC_USB3PCIEPHYCFGR,	0),
179 	RST(USBTC_R,		RCC_USBTCCFGR,		0),
180 	RST(ETHSW_R,		RCC_ETHSWCFGR,		0),
181 	RST(SDMMC1_R,		RCC_SDMMC1CFGR,		0),
182 	RST(SDMMC1DLL_R,	RCC_SDMMC1CFGR,		16),
183 	RST(SDMMC2_R,		RCC_SDMMC2CFGR,		0),
184 	RST(SDMMC2DLL_R,	RCC_SDMMC2CFGR,		16),
185 	RST(SDMMC3_R,		RCC_SDMMC3CFGR,		0),
186 	RST(SDMMC3DLL_R,	RCC_SDMMC3CFGR,		16),
187 	RST(GPU_R,		RCC_GPUCFGR,		0),
188 	RST(LTDC_R,		RCC_LTDCCFGR,		0),
189 	RST(DSI_R,		RCC_DSICFGR,		0),
190 	RST(LVDS_R,		RCC_LVDSCFGR,		0),
191 	RST(CSI_R,		RCC_CSICFGR,		0),
192 	RST(DCMIPP_R,		RCC_DCMIPPCFGR,		0),
193 	RST(CCI_R,		RCC_CCICFGR,		0),
194 	RST(VDEC_R,		RCC_VDECCFGR,		0),
195 	RST(VENC_R,		RCC_VENCCFGR,		0),
196 	RST(WWDG1_R,		RCC_WWDG1CFGR,		0),
197 	RST(WWDG2_R,		RCC_WWDG2CFGR,		0),
198 	RST(VREF_R,		RCC_VREFCFGR,		0),
199 	RST(DTS_R,		RCC_DTSCFGR,		0),
200 	RST(CRC_R,		RCC_CRCCFGR,		0),
201 	RST(SERC_R,		RCC_SERCCFGR,		0),
202 	RST(OSPIIOM_R,		RCC_OSPIIOMCFGR,	0),
203 	RST(I3C1_R,		RCC_I3C1CFGR,		0),
204 	RST(I3C2_R,		RCC_I3C2CFGR,		0),
205 	RST(I3C3_R,		RCC_I3C3CFGR,		0),
206 	RST(I3C4_R,		RCC_I3C4CFGR,		0),
207 	RST(RNG_R,		RCC_RNGCFGR,		0),
208 	RST(PKA_R,		RCC_PKACFGR,		0),
209 	RST(SAES_R,		RCC_SAESCFGR,		0),
210 	RST(HASH_R,		RCC_HASHCFGR,		0),
211 	RST(CRYP1_R,		RCC_CRYP1CFGR,		0),
212 	RST(CRYP2_R,		RCC_CRYP2CFGR,		0),
213 	RST(PCIE_R,		RCC_PCIECFGR,		0),
214 	RST(OSPI1_R,		RCC_OSPI1CFGR,		0),
215 	RST(OSPI1DLL_R,		RCC_OSPI1CFGR,		16),
216 	RST(OSPI2_R,		RCC_OSPI2CFGR,		0),
217 	RST(OSPI2DLL_R,		RCC_OSPI2CFGR,		16),
218 	RST(DBG_R,		RCC_DBGCFGR,		12),
219 
220 	RST_SETR(IWDG2_KER_R,	RCC_IWDGC1CFGSETR,	18),
221 	RST_SETR(IWDG4_KER_R,	RCC_IWDGC2CFGSETR,	18),
222 	RST_SETR(IWDG1_SYS_R,	RCC_IWDGC1CFGSETR,	0),
223 	RST_SETR(IWDG2_SYS_R,	RCC_IWDGC1CFGSETR,	2),
224 	RST_SETR(IWDG3_SYS_R,	RCC_IWDGC2CFGSETR,	0),
225 	RST_SETR(IWDG4_SYS_R,	RCC_IWDGC2CFGSETR,	2),
226 
227 	RST_INV(C2_HOLDBOOT_R,	RCC_CPUBOOTCR,		0),
228 	RST_INV(C1_HOLDBOOT_R,	RCC_CPUBOOTCR,		1),
229 
230 	RST_SETR_NO_DEASSERT_TIMEOUT(C1_R,	RCC_C1RSTCSETR,		0),
231 	RST_SETR_NO_DEASSERT_TIMEOUT(C1P1POR_R,	RCC_C1P1RSTCSETR,	0),
232 	RST_SETR_NO_DEASSERT_TIMEOUT(C1P1_R,	RCC_C1P1RSTCSETR,	1),
233 	RST_SETR_NO_DEASSERT_TIMEOUT(C2_R,	RCC_C2RSTCSETR,		0),
234 	RST_SETR_NO_DEASSERT_TIMEOUT(SYS_R,	RCC_GRSTCSETR,		0),
235 
236 	/*
237 	 * Don't manage reset lines of RIF aware resources
238 	 * DDRCP_R, DDRCAPB_R, DDRPHYCAPB_R, DDRCFG_R, DDR_R,
239 	 * IPCC1_R, IPCC2_R,
240 	 * HPDMA1_R, HPDMA2_R, HPDMA3_R, LPDMA_R,
241 	 * GPIOA_R, GPIOB_R, GPIOC_R, GPIOD_R,
242 	 * GPIOE_R, GPIOF_R, GPIOG_R, GPIOH_R,
243 	 * GPIOI_R, GPIOJ_R, GPIOK_R, GPIOZ_R,
244 	 * HSEM_R,
245 	 * FMC_R,
246 	 */
247 };
248 
249 static const struct rstctrl_ops *stm32_reset_get_ops(unsigned int id __unused)
250 {
251 	return &stm32_rstctrl_ops;
252 }
253 
254 static const struct stm32_reset_data stm32mp25_reset_data = {
255 	.nb_lines = ARRAY_SIZE(stm32mp25_reset_cfg),
256 	.rst_lines = stm32mp25_reset_cfg,
257 	.get_rstctrl_ops = stm32_reset_get_ops,
258 };
259 
260 static const struct dt_device_match stm32_rstctrl_match_table[] = {
261 	{
262 		.compatible = "st,stm32mp25-rcc",
263 		.compat_data = &stm32mp25_reset_data,
264 	},
265 	{ }
266 };
267 
268 DEFINE_DT_DRIVER(stm32_rstctrl_dt_driver) = {
269 	.name = "stm32_rstctrl",
270 	.type = DT_DRIVER_RSTCTRL,
271 	.match_table = stm32_rstctrl_match_table,
272 	.probe = stm32_rstctrl_provider_probe,
273 };
274