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