blob: 6d778ad59d3e9aa6ca295972f3d1bd34b9b454cd [file] [log] [blame]
Etienne Carriere7ad2c012019-12-08 08:14:03 +01001/*
2 * Copyright (c) 2017-2020, STMicroelectronics - All Rights Reserved
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Etienne Carrierebeb53d92020-05-13 10:13:54 +02007#include <assert.h>
8#include <stdint.h>
9
10#include <platform_def.h>
11
12#include <common/debug.h>
Etienne Carriere7a4a34f2020-05-13 10:07:45 +020013#include <drivers/st/etzpc.h>
Etienne Carrierebeb53d92020-05-13 10:13:54 +020014#include <drivers/st/stm32_gpio.h>
15
Etienne Carriere7ad2c012019-12-08 08:14:03 +010016#include <stm32mp_shared_resources.h>
17
Etienne Carriere7a4a34f2020-05-13 10:07:45 +020018/*
19 * Once one starts to get the resource registering state, one cannot register
20 * new resources. This ensures resource state cannot change.
21 */
22static bool registering_locked;
23
24/*
25 * Shared peripherals and resources registration
26 *
27 * Each resource assignation is stored in a table. The state defaults
28 * to PERIPH_UNREGISTERED if the resource is not explicitly assigned.
29 *
30 * Resource driver that as not embedded (a.k.a their related CFG_xxx build
31 * directive is disabled) are assigned to the non-secure world.
32 *
33 * Each pin of the GPIOZ bank can be secure or non-secure.
34 *
35 * It is the platform responsibility the ensure resource assignation
36 * matches the access permission firewalls configuration.
37 */
38enum shres_state {
39 SHRES_UNREGISTERED = 0,
40 SHRES_SECURE,
41 SHRES_NON_SECURE,
42};
43
44/* Force uint8_t array for array of enum shres_state for size considerations */
45static uint8_t shres_state[STM32MP1_SHRES_COUNT];
46
Etienne Carrieree704f992020-05-13 15:51:56 +020047static const char *shres2str_id_tbl[STM32MP1_SHRES_COUNT] __unused = {
48 [STM32MP1_SHRES_GPIOZ(0)] = "GPIOZ0",
49 [STM32MP1_SHRES_GPIOZ(1)] = "GPIOZ1",
50 [STM32MP1_SHRES_GPIOZ(2)] = "GPIOZ2",
51 [STM32MP1_SHRES_GPIOZ(3)] = "GPIOZ3",
52 [STM32MP1_SHRES_GPIOZ(4)] = "GPIOZ4",
53 [STM32MP1_SHRES_GPIOZ(5)] = "GPIOZ5",
54 [STM32MP1_SHRES_GPIOZ(6)] = "GPIOZ6",
55 [STM32MP1_SHRES_GPIOZ(7)] = "GPIOZ7",
56 [STM32MP1_SHRES_IWDG1] = "IWDG1",
57 [STM32MP1_SHRES_USART1] = "USART1",
58 [STM32MP1_SHRES_SPI6] = "SPI6",
59 [STM32MP1_SHRES_I2C4] = "I2C4",
60 [STM32MP1_SHRES_RNG1] = "RNG1",
61 [STM32MP1_SHRES_HASH1] = "HASH1",
62 [STM32MP1_SHRES_CRYP1] = "CRYP1",
63 [STM32MP1_SHRES_I2C6] = "I2C6",
64 [STM32MP1_SHRES_RTC] = "RTC",
65 [STM32MP1_SHRES_MCU] = "MCU",
66 [STM32MP1_SHRES_MDMA] = "MDMA",
67 [STM32MP1_SHRES_PLL3] = "PLL3",
68};
69
70static const char __unused *shres2str_id(enum stm32mp_shres id)
71{
72 assert(id < ARRAY_SIZE(shres2str_id_tbl));
73
74 return shres2str_id_tbl[id];
75}
76
77static const char __unused *shres2str_state_tbl[] = {
78 [SHRES_UNREGISTERED] = "unregistered",
79 [SHRES_NON_SECURE] = "non-secure",
80 [SHRES_SECURE] = "secure",
81};
82
83static const char __unused *shres2str_state(unsigned int state)
84{
85 assert(state < ARRAY_SIZE(shres2str_state_tbl));
86
87 return shres2str_state_tbl[state];
88}
89
Etienne Carriere7a4a34f2020-05-13 10:07:45 +020090/* Get resource state: these accesses lock the registering support */
91static void lock_registering(void)
92{
93 registering_locked = true;
94}
95
96static bool periph_is_non_secure(enum stm32mp_shres id)
97{
98 lock_registering();
99
100 return (shres_state[id] == SHRES_NON_SECURE) ||
101 (shres_state[id] == SHRES_UNREGISTERED);
102}
103
104static bool periph_is_secure(enum stm32mp_shres id)
105{
106 return !periph_is_non_secure(id);
107}
108
Etienne Carrierebeb53d92020-05-13 10:13:54 +0200109/* GPIOZ pin count is saved in RAM to prevent parsing FDT several times */
110static int8_t gpioz_nbpin = -1;
111
112static unsigned int get_gpio_nbpin(unsigned int bank)
113{
114 if (bank != GPIO_BANK_Z) {
115 int count = fdt_get_gpio_bank_pin_count(bank);
116
117 assert((count >= 0) || (count <= (GPIO_PIN_MAX + 1)));
118
119 return (unsigned int)count;
120 }
121
122 if (gpioz_nbpin < 0) {
123 int count = fdt_get_gpio_bank_pin_count(GPIO_BANK_Z);
124
125 assert((count == 0) || (count == STM32MP_GPIOZ_PIN_MAX_COUNT));
126
127 gpioz_nbpin = count;
128 }
129
130 return (unsigned int)gpioz_nbpin;
131}
132
Etienne Carriere7a4a34f2020-05-13 10:07:45 +0200133static unsigned int get_gpioz_nbpin(void)
Etienne Carrierebeb53d92020-05-13 10:13:54 +0200134{
135 return get_gpio_nbpin(GPIO_BANK_Z);
136}
137
Etienne Carriere1bbb9242020-05-13 14:22:01 +0200138static void register_periph(enum stm32mp_shres id, unsigned int state)
139{
140 assert((id < STM32MP1_SHRES_COUNT) &&
141 ((state == SHRES_SECURE) || (state == SHRES_NON_SECURE)));
142
143 if (registering_locked) {
144 if (shres_state[id] == state) {
145 return;
146 }
147 panic();
148 }
149
150 if ((shres_state[id] != SHRES_UNREGISTERED) &&
151 (shres_state[id] != state)) {
152 VERBOSE("Cannot change %s from %s to %s\n",
153 shres2str_id(id),
154 shres2str_state(shres_state[id]),
155 shres2str_state(state));
156 panic();
157 }
158
159 if (shres_state[id] == SHRES_UNREGISTERED) {
160 VERBOSE("Register %s as %s\n",
161 shres2str_id(id), shres2str_state(state));
162 }
163
164 if ((id >= STM32MP1_SHRES_GPIOZ(0)) &&
165 (id <= STM32MP1_SHRES_GPIOZ(7)) &&
166 ((id - STM32MP1_SHRES_GPIOZ(0)) >= get_gpioz_nbpin())) {
167 ERROR("Invalid GPIO pin %u, %u pin(s) available\n",
168 id - STM32MP1_SHRES_GPIOZ(0), get_gpioz_nbpin());
169 panic();
170 }
171
172 shres_state[id] = (uint8_t)state;
173
174 /* Explore clock tree to lock dependencies */
175 if (state == SHRES_SECURE) {
176 enum stm32mp_shres clock_res_id;
177
178 switch (id) {
179 case STM32MP1_SHRES_GPIOZ(0):
180 case STM32MP1_SHRES_GPIOZ(1):
181 case STM32MP1_SHRES_GPIOZ(2):
182 case STM32MP1_SHRES_GPIOZ(3):
183 case STM32MP1_SHRES_GPIOZ(4):
184 case STM32MP1_SHRES_GPIOZ(5):
185 case STM32MP1_SHRES_GPIOZ(6):
186 case STM32MP1_SHRES_GPIOZ(7):
187 clock_res_id = GPIOZ;
188 break;
189 case STM32MP1_SHRES_IWDG1:
190 clock_res_id = IWDG1;
191 break;
192 case STM32MP1_SHRES_USART1:
193 clock_res_id = USART1_K;
194 break;
195 case STM32MP1_SHRES_SPI6:
196 clock_res_id = SPI6_K;
197 break;
198 case STM32MP1_SHRES_I2C4:
199 clock_res_id = I2C4_K;
200 break;
201 case STM32MP1_SHRES_RNG1:
202 clock_res_id = RNG1_K;
203 break;
204 case STM32MP1_SHRES_HASH1:
205 clock_res_id = HASH1;
206 break;
207 case STM32MP1_SHRES_CRYP1:
208 clock_res_id = CRYP1;
209 break;
210 case STM32MP1_SHRES_I2C6:
211 clock_res_id = I2C6_K;
212 break;
213 case STM32MP1_SHRES_RTC:
214 clock_res_id = RTC;
215 break;
216 default:
217 /* No clock resource dependency */
218 return;
219 }
220
221 stm32mp1_register_clock_parents_secure(clock_res_id);
222 }
223}
224
225/* Register resource by ID */
226void stm32mp_register_secure_periph(enum stm32mp_shres id)
227{
228 register_periph(id, SHRES_SECURE);
229}
230
231void stm32mp_register_non_secure_periph(enum stm32mp_shres id)
232{
233 register_periph(id, SHRES_NON_SECURE);
234}
235
Etienne Carrierec8d47a72020-05-13 13:53:15 +0200236static bool stm32mp_gpio_bank_is_non_secure(unsigned int bank)
237{
238 unsigned int non_secure = 0U;
239 unsigned int i;
240
241 lock_registering();
242
243 if (bank != GPIO_BANK_Z) {
244 return true;
245 }
246
247 for (i = 0U; i < get_gpioz_nbpin(); i++) {
248 if (periph_is_non_secure(STM32MP1_SHRES_GPIOZ(i))) {
249 non_secure++;
250 }
251 }
252
253 return non_secure == get_gpioz_nbpin();
254}
255
Etienne Carriere875f85a2020-05-13 10:20:34 +0200256static bool stm32mp_gpio_bank_is_secure(unsigned int bank)
257{
258 unsigned int secure = 0U;
259 unsigned int i;
260
261 lock_registering();
262
263 if (bank != GPIO_BANK_Z) {
264 return false;
265 }
266
267 for (i = 0U; i < get_gpioz_nbpin(); i++) {
268 if (periph_is_secure(STM32MP1_SHRES_GPIOZ(i))) {
269 secure++;
270 }
271 }
272
273 return secure == get_gpioz_nbpin();
274}
275
Etienne Carriere7ad2c012019-12-08 08:14:03 +0100276bool stm32mp_nsec_can_access_clock(unsigned long clock_id)
277{
Etienne Carriere875f85a2020-05-13 10:20:34 +0200278 enum stm32mp_shres shres_id = STM32MP1_SHRES_COUNT;
279
280 switch (clock_id) {
281 case CK_CSI:
282 case CK_HSE:
283 case CK_HSE_DIV2:
284 case CK_HSI:
285 case CK_LSE:
286 case CK_LSI:
287 case PLL1_P:
288 case PLL1_Q:
289 case PLL1_R:
290 case PLL2_P:
291 case PLL2_Q:
292 case PLL2_R:
293 case PLL3_P:
294 case PLL3_Q:
295 case PLL3_R:
296 case RTCAPB:
297 return true;
298 case GPIOZ:
299 /* Allow clock access if at least one pin is non-secure */
300 return !stm32mp_gpio_bank_is_secure(GPIO_BANK_Z);
301 case CRYP1:
302 shres_id = STM32MP1_SHRES_CRYP1;
303 break;
304 case HASH1:
305 shres_id = STM32MP1_SHRES_HASH1;
306 break;
307 case I2C4_K:
308 shres_id = STM32MP1_SHRES_I2C4;
309 break;
310 case I2C6_K:
311 shres_id = STM32MP1_SHRES_I2C6;
312 break;
313 case IWDG1:
314 shres_id = STM32MP1_SHRES_IWDG1;
315 break;
316 case RNG1_K:
317 shres_id = STM32MP1_SHRES_RNG1;
318 break;
319 case RTC:
320 shres_id = STM32MP1_SHRES_RTC;
321 break;
322 case SPI6_K:
323 shres_id = STM32MP1_SHRES_SPI6;
324 break;
325 case USART1_K:
326 shres_id = STM32MP1_SHRES_USART1;
327 break;
328 default:
329 return false;
330 }
331
332 return periph_is_non_secure(shres_id);
Etienne Carriere7ad2c012019-12-08 08:14:03 +0100333}
334
Etienne Carriere7ad2c012019-12-08 08:14:03 +0100335bool stm32mp_nsec_can_access_reset(unsigned int reset_id)
336{
Etienne Carrierec8d47a72020-05-13 13:53:15 +0200337 enum stm32mp_shres shres_id = STM32MP1_SHRES_COUNT;
338
339 switch (reset_id) {
340 case CRYP1_R:
341 shres_id = STM32MP1_SHRES_CRYP1;
342 break;
343 case GPIOZ_R:
344 /* GPIOZ reset mandates all pins are non-secure */
345 return stm32mp_gpio_bank_is_non_secure(GPIO_BANK_Z);
346 case HASH1_R:
347 shres_id = STM32MP1_SHRES_HASH1;
348 break;
349 case I2C4_R:
350 shres_id = STM32MP1_SHRES_I2C4;
351 break;
352 case I2C6_R:
353 shres_id = STM32MP1_SHRES_I2C6;
354 break;
355 case MCU_R:
356 shres_id = STM32MP1_SHRES_MCU;
357 break;
358 case MDMA_R:
359 shres_id = STM32MP1_SHRES_MDMA;
360 break;
361 case RNG1_R:
362 shres_id = STM32MP1_SHRES_RNG1;
363 break;
364 case SPI6_R:
365 shres_id = STM32MP1_SHRES_SPI6;
366 break;
367 case USART1_R:
368 shres_id = STM32MP1_SHRES_USART1;
369 break;
370 default:
371 return false;
372 }
373
374 return periph_is_non_secure(shres_id);
Etienne Carriere7ad2c012019-12-08 08:14:03 +0100375}
Etienne Carriere7a4a34f2020-05-13 10:07:45 +0200376
377static bool mckprot_protects_periph(enum stm32mp_shres id)
378{
379 switch (id) {
380 case STM32MP1_SHRES_MCU:
381 case STM32MP1_SHRES_PLL3:
382 return true;
383 default:
384 return false;
385 }
386}
387
388/* ETZPC configuration at drivers initialization completion */
389static enum etzpc_decprot_attributes shres2decprot_attr(enum stm32mp_shres id)
390{
391 assert((id < STM32MP1_SHRES_GPIOZ(0)) ||
392 (id > STM32MP1_SHRES_GPIOZ(7)));
393
394 if (periph_is_non_secure(id)) {
395 return ETZPC_DECPROT_NS_RW;
396 }
397
398 return ETZPC_DECPROT_S_RW;
399}
400
401static void set_etzpc_secure_configuration(void)
402{
403 /* Some system peripherals shall be secure */
404 etzpc_configure_decprot(STM32MP1_ETZPC_STGENC_ID, ETZPC_DECPROT_S_RW);
405 etzpc_configure_decprot(STM32MP1_ETZPC_BKPSRAM_ID, ETZPC_DECPROT_S_RW);
406 etzpc_configure_decprot(STM32MP1_ETZPC_DDRCTRL_ID,
407 ETZPC_DECPROT_NS_R_S_W);
408 etzpc_configure_decprot(STM32MP1_ETZPC_DDRPHYC_ID,
409 ETZPC_DECPROT_NS_R_S_W);
410
411 /* Configure ETZPC with peripheral registering */
412 etzpc_configure_decprot(STM32MP1_ETZPC_CRYP1_ID,
413 shres2decprot_attr(STM32MP1_SHRES_CRYP1));
414 etzpc_configure_decprot(STM32MP1_ETZPC_HASH1_ID,
415 shres2decprot_attr(STM32MP1_SHRES_HASH1));
416 etzpc_configure_decprot(STM32MP1_ETZPC_I2C4_ID,
417 shres2decprot_attr(STM32MP1_SHRES_I2C4));
418 etzpc_configure_decprot(STM32MP1_ETZPC_I2C6_ID,
419 shres2decprot_attr(STM32MP1_SHRES_I2C6));
420 etzpc_configure_decprot(STM32MP1_ETZPC_IWDG1_ID,
421 shres2decprot_attr(STM32MP1_SHRES_IWDG1));
422 etzpc_configure_decprot(STM32MP1_ETZPC_RNG1_ID,
423 shres2decprot_attr(STM32MP1_SHRES_RNG1));
424 etzpc_configure_decprot(STM32MP1_ETZPC_USART1_ID,
425 shres2decprot_attr(STM32MP1_SHRES_USART1));
426 etzpc_configure_decprot(STM32MP1_ETZPC_SPI6_ID,
427 shres2decprot_attr(STM32MP1_SHRES_SPI6));
428}
429
430static void check_rcc_secure_configuration(void)
431{
432 uint32_t n;
433 uint32_t error = 0U;
434 bool mckprot = stm32mp1_rcc_is_mckprot();
435 bool secure = stm32mp1_rcc_is_secure();
436
437 for (n = 0U; n < ARRAY_SIZE(shres_state); n++) {
438 if (shres_state[n] != SHRES_SECURE) {
439 continue;
440 }
441
442 if (!secure || (mckprot_protects_periph(n) && (!mckprot))) {
Etienne Carrieree704f992020-05-13 15:51:56 +0200443 ERROR("RCC %s MCKPROT %s and %s secure\n",
Etienne Carriere7a4a34f2020-05-13 10:07:45 +0200444 secure ? "secure" : "non-secure",
445 mckprot ? "set" : "not set",
Etienne Carrieree704f992020-05-13 15:51:56 +0200446 shres2str_id(n));
Etienne Carriere7a4a34f2020-05-13 10:07:45 +0200447 error++;
448 }
449 }
450
451 if (error != 0U) {
452 panic();
453 }
454}
455
456static void set_gpio_secure_configuration(void)
457{
458 uint32_t pin;
459
460 for (pin = 0U; pin < get_gpioz_nbpin(); pin++) {
461 bool secure_state = periph_is_secure(STM32MP1_SHRES_GPIOZ(pin));
462
463 set_gpio_secure_cfg(GPIO_BANK_Z, pin, secure_state);
464 }
465}
466
467static void print_shared_resources_state(void)
468{
469 unsigned int id;
470
471 for (id = 0U; id < STM32MP1_SHRES_COUNT; id++) {
472 switch (shres_state[id]) {
473 case SHRES_SECURE:
Etienne Carrieree704f992020-05-13 15:51:56 +0200474 INFO("stm32mp1 %s is secure\n", shres2str_id(id));
Etienne Carriere7a4a34f2020-05-13 10:07:45 +0200475 break;
476 case SHRES_NON_SECURE:
477 case SHRES_UNREGISTERED:
Etienne Carrieree704f992020-05-13 15:51:56 +0200478 VERBOSE("stm32mp %s is non-secure\n", shres2str_id(id));
Etienne Carriere7a4a34f2020-05-13 10:07:45 +0200479 break;
480 default:
Etienne Carrieree704f992020-05-13 15:51:56 +0200481 VERBOSE("stm32mp %s is invalid\n", shres2str_id(id));
Etienne Carriere7a4a34f2020-05-13 10:07:45 +0200482 panic();
483 }
484 }
485}
486
487void stm32mp_lock_periph_registering(void)
488{
489 registering_locked = true;
490
491 print_shared_resources_state();
492
493 check_rcc_secure_configuration();
494 set_etzpc_secure_configuration();
495 set_gpio_secure_configuration();
496}