xref: /optee_os/core/arch/arm/plat-stm32mp1/stm32_util.h (revision e2e497d4f0b3d71790ba14341aceb45e2389879f)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (c) 2018-2022, STMicroelectronics
4  */
5 
6 #ifndef __STM32_UTIL_H__
7 #define __STM32_UTIL_H__
8 
9 #include <assert.h>
10 #include <drivers/clk.h>
11 #include <drivers/stm32_bsec.h>
12 #include <kernel/panic.h>
13 #include <stdint.h>
14 #include <types_ext.h>
15 
16 /* Backup registers and RAM utils */
17 vaddr_t stm32mp_bkpreg(unsigned int idx);
18 
19 /*
20  * SYSCFG IO compensation.
21  * These functions assume non-secure world is suspended.
22  */
23 void stm32mp_syscfg_enable_io_compensation(void);
24 void stm32mp_syscfg_disable_io_compensation(void);
25 
26 /* Platform util for the RCC drivers */
27 vaddr_t stm32_rcc_base(void);
28 
29 /* Platform util for the GIC */
30 vaddr_t get_gicd_base(void);
31 
32 /*
33  * Platform util functions for the GPIO driver
34  * @bank: Target GPIO bank ID as per DT bindings
35  *
36  * Platform shall implement these functions to provide to stm32_gpio
37  * driver the resource reference for a target GPIO bank. That are
38  * memory mapped interface base address, interface offset (see below)
39  * and clock identifier.
40  *
41  * stm32_get_gpio_bank_offset() returns a bank offset that is used to
42  * check DT configuration matches platform implementation of the banks
43  * description.
44  */
45 vaddr_t stm32_get_gpio_bank_base(unsigned int bank);
46 unsigned int stm32_get_gpio_bank_offset(unsigned int bank);
47 unsigned int stm32_get_gpio_bank_clock(unsigned int bank);
48 struct clk *stm32_get_gpio_bank_clk(unsigned int bank);
49 
50 /* Platform util for PMIC support */
51 bool stm32mp_with_pmic(void);
52 
53 /* Power management service */
54 #ifdef CFG_PSCI_ARM32
55 void stm32mp_register_online_cpu(void);
56 #else
57 static inline void stm32mp_register_online_cpu(void)
58 {
59 }
60 #endif
61 
62 /*
63  * Generic spinlock function that bypass spinlock if MMU is disabled or
64  * lock is NULL.
65  */
66 uint32_t may_spin_lock(unsigned int *lock);
67 void may_spin_unlock(unsigned int *lock, uint32_t exceptions);
68 
69 /* Helper from platform RCC clock driver */
70 struct clk *stm32mp_rcc_clock_id_to_clk(unsigned long clock_id);
71 
72 #ifdef CFG_STM32MP1_SHARED_RESOURCES
73 /* Return true if @clock_id is shared by secure and non-secure worlds */
74 bool stm32mp_nsec_can_access_clock(unsigned long clock_id);
75 #else /* CFG_STM32MP1_SHARED_RESOURCES */
76 static inline bool stm32mp_nsec_can_access_clock(unsigned long clock_id
77 						 __unused)
78 {
79 	return true;
80 }
81 #endif /* CFG_STM32MP1_SHARED_RESOURCES */
82 
83 extern const struct clk_ops stm32mp1_clk_ops;
84 
85 #if defined(CFG_STPMIC1)
86 /* Return true if non-secure world can manipulate regulator @pmic_regu_name */
87 bool stm32mp_nsec_can_access_pmic_regu(const char *pmic_regu_name);
88 #else
89 static inline bool stm32mp_nsec_can_access_pmic_regu(const char *name __unused)
90 {
91 	return false;
92 }
93 #endif
94 
95 #ifdef CFG_STM32MP1_SHARED_RESOURCES
96 /* Return true if and only if @reset_id relates to a non-secure peripheral */
97 bool stm32mp_nsec_can_access_reset(unsigned int reset_id);
98 #else /* CFG_STM32MP1_SHARED_RESOURCES */
99 static inline bool stm32mp_nsec_can_access_reset(unsigned int reset_id __unused)
100 {
101 	return true;
102 }
103 #endif /* CFG_STM32MP1_SHARED_RESOURCES */
104 
105 /* Return rstctrl instance related to RCC reset controller DT binding ID */
106 struct rstctrl *stm32mp_rcc_reset_id_to_rstctrl(unsigned int binding_id);
107 
108 /*
109  * Structure and API function for BSEC driver to get some platform data.
110  *
111  * @base: BSEC interface registers physical base address
112  * @upper_start: Base ID for the BSEC upper words in the platform
113  * @max_id: Max value for BSEC word ID for the platform
114  */
115 struct stm32_bsec_static_cfg {
116 	paddr_t base;
117 	unsigned int upper_start;
118 	unsigned int max_id;
119 };
120 
121 void stm32mp_get_bsec_static_cfg(struct stm32_bsec_static_cfg *cfg);
122 
123 /*
124  * Return true if platform is in closed_device mode
125  */
126 bool stm32mp_is_closed_device(void);
127 
128 /*
129  * Shared reference counter: increments by 2 on secure increment
130  * request, decrements by 2 on secure decrement request. Bit #0
131  * is set to 1 on non-secure increment request and reset to 0 on
132  * non-secure decrement request. These counters initialize to
133  * either 0, 1 or 2 upon their expect default state.
134  * Counters saturate to UINT_MAX / 2.
135  */
136 #define SHREFCNT_NONSECURE_FLAG		0x1ul
137 #define SHREFCNT_SECURE_STEP		0x2ul
138 #define SHREFCNT_MAX			(UINT_MAX / 2)
139 
140 /* Return 1 if refcnt increments from 0, else return 0 */
141 static inline int incr_shrefcnt(unsigned int *refcnt, bool secure)
142 {
143 	int rc = !*refcnt;
144 
145 	if (secure) {
146 		if (*refcnt < SHREFCNT_MAX) {
147 			*refcnt += SHREFCNT_SECURE_STEP;
148 			assert(*refcnt < SHREFCNT_MAX);
149 		}
150 	} else {
151 		*refcnt |= SHREFCNT_NONSECURE_FLAG;
152 	}
153 
154 	return rc;
155 }
156 
157 /* Return 1 if refcnt decrements to 0, else return 0 */
158 static inline int decr_shrefcnt(unsigned int *refcnt, bool secure)
159 {
160 	int  rc = 0;
161 
162 	if (secure) {
163 		if (*refcnt < SHREFCNT_MAX) {
164 			if (*refcnt < SHREFCNT_SECURE_STEP)
165 				panic();
166 
167 			*refcnt -= SHREFCNT_SECURE_STEP;
168 			rc = !*refcnt;
169 		}
170 	} else {
171 		rc = (*refcnt == SHREFCNT_NONSECURE_FLAG);
172 		*refcnt &= ~SHREFCNT_NONSECURE_FLAG;
173 	}
174 
175 	return rc;
176 }
177 
178 static inline int incr_refcnt(unsigned int *refcnt)
179 {
180 	return incr_shrefcnt(refcnt, true);
181 }
182 
183 static inline int decr_refcnt(unsigned int *refcnt)
184 {
185 	return decr_shrefcnt(refcnt, true);
186 }
187 
188 /*
189  * Shared peripherals and resources registration
190  *
191  * Resources listed in enum stm32mp_shres assigned at run-time to the
192  * non-secure world, to the secure world or shared by both worlds.
193  * In the later case, there must exist a secure service in OP-TEE
194  * for the non-secure world to access the resource.
195  *
196  * Resources may be a peripheral, a bus, a clock or a memory.
197  *
198  * Shared resources driver API functions allows drivers to register the
199  * resource as secure, non-secure or shared and to get the resource
200  * assignation state.
201  */
202 #define STM32MP1_SHRES_GPIOZ(i)		(STM32MP1_SHRES_GPIOZ_0 + i)
203 
204 enum stm32mp_shres {
205 	STM32MP1_SHRES_GPIOZ_0 = 0,
206 	STM32MP1_SHRES_GPIOZ_1,
207 	STM32MP1_SHRES_GPIOZ_2,
208 	STM32MP1_SHRES_GPIOZ_3,
209 	STM32MP1_SHRES_GPIOZ_4,
210 	STM32MP1_SHRES_GPIOZ_5,
211 	STM32MP1_SHRES_GPIOZ_6,
212 	STM32MP1_SHRES_GPIOZ_7,
213 	STM32MP1_SHRES_IWDG1,
214 	STM32MP1_SHRES_USART1,
215 	STM32MP1_SHRES_SPI6,
216 	STM32MP1_SHRES_I2C4,
217 	STM32MP1_SHRES_RNG1,
218 	STM32MP1_SHRES_HASH1,
219 	STM32MP1_SHRES_CRYP1,
220 	STM32MP1_SHRES_I2C6,
221 	STM32MP1_SHRES_RTC,
222 	STM32MP1_SHRES_MCU,
223 	STM32MP1_SHRES_PLL3,
224 	STM32MP1_SHRES_MDMA,
225 
226 	STM32MP1_SHRES_COUNT
227 };
228 
229 #ifdef CFG_STM32MP1_SHARED_RESOURCES
230 /* Register resource @id as a secure peripheral */
231 void stm32mp_register_secure_periph(enum stm32mp_shres id);
232 
233 /* Register resource @id as a non-secure peripheral */
234 void stm32mp_register_non_secure_periph(enum stm32mp_shres id);
235 
236 /*
237  * Register resource identified by @base as a secure peripheral
238  * @base: IOMEM physical base address of the resource
239  */
240 void stm32mp_register_secure_periph_iomem(vaddr_t base);
241 
242 /*
243  * Register resource identified by @base as a non-secure peripheral
244  * @base: IOMEM physical base address of the resource
245  */
246 void stm32mp_register_non_secure_periph_iomem(vaddr_t base);
247 
248 /*
249  * Register GPIO resource as a secure peripheral
250  * @bank: Bank of the target GPIO
251  * @pin: Bit position of the target GPIO in the bank
252  */
253 void stm32mp_register_secure_gpio(unsigned int bank, unsigned int pin);
254 
255 /*
256  * Register GPIO resource as a non-secure peripheral
257  * @bank: Bank of the target GPIO
258  * @pin: Bit position of the target GPIO in the bank
259  */
260 void stm32mp_register_non_secure_gpio(unsigned int bank, unsigned int pin);
261 
262 /* Return true if and only if resource @id is registered as secure */
263 bool stm32mp_periph_is_secure(enum stm32mp_shres id);
264 
265 /* Return true if and only if GPIO bank @bank is registered as secure */
266 bool stm32mp_gpio_bank_is_secure(unsigned int bank);
267 
268 /* Return true if and only if GPIO bank @bank is registered as shared */
269 bool stm32mp_gpio_bank_is_shared(unsigned int bank);
270 
271 /* Return true if and only if GPIO bank @bank is registered as non-secure */
272 bool stm32mp_gpio_bank_is_non_secure(unsigned int bank);
273 
274 /* Register parent clocks of @clock (ID used in clock DT bindings) as secure */
275 void stm32mp_register_clock_parents_secure(unsigned long clock_id);
276 
277 #else /* CFG_STM32MP1_SHARED_RESOURCES */
278 
279 static inline void stm32mp_register_secure_periph(enum stm32mp_shres id
280 						  __unused)
281 {
282 }
283 
284 static inline void stm32mp_register_non_secure_periph(enum stm32mp_shres id
285 						      __unused)
286 {
287 }
288 
289 static inline void stm32mp_register_secure_periph_iomem(vaddr_t base __unused)
290 {
291 }
292 
293 static inline void stm32mp_register_non_secure_periph_iomem(vaddr_t base
294 							    __unused)
295 {
296 }
297 
298 static inline void stm32mp_register_secure_gpio(unsigned int bank __unused,
299 						unsigned int pin __unused)
300 {
301 }
302 
303 static inline void stm32mp_register_non_secure_gpio(unsigned int bank __unused,
304 						    unsigned int pin __unused)
305 {
306 }
307 
308 static inline bool stm32mp_periph_is_secure(enum stm32mp_shres id __unused)
309 {
310 	return true;
311 }
312 
313 static inline bool stm32mp_gpio_bank_is_secure(unsigned int bank __unused)
314 {
315 	return true;
316 }
317 
318 static inline bool stm32mp_gpio_bank_is_shared(unsigned int bank __unused)
319 {
320 	return false;
321 }
322 
323 static inline bool stm32mp_gpio_bank_is_non_secure(unsigned int bank __unused)
324 {
325 	return false;
326 }
327 
328 static inline void stm32mp_register_clock_parents_secure(unsigned long clock_id
329 							 __unused)
330 {
331 }
332 
333 #endif /* CFG_STM32MP1_SHARED_RESOURCES */
334 #endif /*__STM32_UTIL_H__*/
335