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