blob: 189f83df16f5b9f0d7ddb44ced9f8d2bcd6f72ef [file] [log] [blame]
Yann Gautieree8f5422019-02-14 11:13:25 +01001/*
Yann Gautier3e334752023-02-01 15:04:30 +01002 * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
Yann Gautieree8f5422019-02-14 11:13:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautiere3bf9132019-05-07 18:52:17 +02007#include <assert.h>
8
Yann Gautiera205a5c2021-08-30 15:06:54 +02009#include <drivers/clk.h>
Yann Gautiercd16df32021-06-04 14:04:05 +020010#include <drivers/st/stm32_gpio.h>
11#include <drivers/st/stm32_iwdg.h>
Yann Gautier6eef5252021-12-10 17:04:40 +010012#include <lib/mmio.h>
Yann Gautiercd16df32021-06-04 14:04:05 +020013#include <lib/xlat_tables/xlat_tables_v2.h>
Yann Gautier0c810882021-12-17 09:53:04 +010014#include <libfdt.h>
Yann Gautier35dc0772019-05-13 18:34:48 +020015
Sughosh Ganu03e2f802021-12-01 15:56:27 +053016#include <plat/common/platform.h>
Yann Gautieree8f5422019-02-14 11:13:25 +010017#include <platform_def.h>
18
Yann Gautier82000472020-02-05 16:24:21 +010019#if STM32MP13
20#define TAMP_BOOT_MODE_BACKUP_REG_ID U(30)
21#endif
22#if STM32MP15
Yann Gautier6eef5252021-12-10 17:04:40 +010023#define TAMP_BOOT_MODE_BACKUP_REG_ID U(20)
Yann Gautier82000472020-02-05 16:24:21 +010024#endif
Yann Gautier6eef5252021-12-10 17:04:40 +010025
Yann Gautier9bbd26a2022-03-28 17:49:38 +020026/*
27 * Backup register to store fwu update information.
28 * It should be writeable only by secure world, but also readable by non secure
29 * (so it should be in Zone 2).
30 */
31#define TAMP_BOOT_FWU_INFO_REG_ID U(10)
Sughosh Ganu03e2f802021-12-01 15:56:27 +053032
Etienne Carriere72369b12019-12-08 08:17:56 +010033#if defined(IMAGE_BL2)
34#define MAP_SEC_SYSRAM MAP_REGION_FLAT(STM32MP_SYSRAM_BASE, \
Yann Gautiera2e2a302019-02-14 11:13:39 +010035 STM32MP_SYSRAM_SIZE, \
Yann Gautieree8f5422019-02-14 11:13:25 +010036 MT_MEMORY | \
37 MT_RW | \
38 MT_SECURE | \
39 MT_EXECUTE_NEVER)
Etienne Carriere72369b12019-12-08 08:17:56 +010040#elif defined(IMAGE_BL32)
41#define MAP_SEC_SYSRAM MAP_REGION_FLAT(STM32MP_SEC_SYSRAM_BASE, \
42 STM32MP_SEC_SYSRAM_SIZE, \
43 MT_MEMORY | \
44 MT_RW | \
45 MT_SECURE | \
46 MT_EXECUTE_NEVER)
Yann Gautieree8f5422019-02-14 11:13:25 +010047
Etienne Carriere72369b12019-12-08 08:17:56 +010048/* Non-secure SYSRAM is used a uncached memory for SCMI message transfer */
49#define MAP_NS_SYSRAM MAP_REGION_FLAT(STM32MP_NS_SYSRAM_BASE, \
50 STM32MP_NS_SYSRAM_SIZE, \
51 MT_DEVICE | \
52 MT_RW | \
53 MT_NS | \
54 MT_EXECUTE_NEVER)
55#endif
56
Yann Gautier84d994b2020-04-14 18:08:50 +020057#if STM32MP13
58#define MAP_SRAM_ALL MAP_REGION_FLAT(SRAMS_BASE, \
59 SRAMS_SIZE_2MB_ALIGNED, \
60 MT_MEMORY | \
61 MT_RW | \
62 MT_SECURE | \
63 MT_EXECUTE_NEVER)
64#endif
65
Yann Gautieree8f5422019-02-14 11:13:25 +010066#define MAP_DEVICE1 MAP_REGION_FLAT(STM32MP1_DEVICE1_BASE, \
67 STM32MP1_DEVICE1_SIZE, \
68 MT_DEVICE | \
69 MT_RW | \
70 MT_SECURE | \
71 MT_EXECUTE_NEVER)
72
73#define MAP_DEVICE2 MAP_REGION_FLAT(STM32MP1_DEVICE2_BASE, \
74 STM32MP1_DEVICE2_SIZE, \
75 MT_DEVICE | \
76 MT_RW | \
77 MT_SECURE | \
78 MT_EXECUTE_NEVER)
79
80#if defined(IMAGE_BL2)
81static const mmap_region_t stm32mp1_mmap[] = {
Etienne Carriere72369b12019-12-08 08:17:56 +010082 MAP_SEC_SYSRAM,
Yann Gautier84d994b2020-04-14 18:08:50 +020083#if STM32MP13
84 MAP_SRAM_ALL,
85#endif
Yann Gautieree8f5422019-02-14 11:13:25 +010086 MAP_DEVICE1,
Yann Gautier352d8632020-09-17 11:38:09 +020087#if STM32MP_RAW_NAND
Yann Gautieree8f5422019-02-14 11:13:25 +010088 MAP_DEVICE2,
Yann Gautier352d8632020-09-17 11:38:09 +020089#endif
Yann Gautieree8f5422019-02-14 11:13:25 +010090 {0}
91};
92#endif
93#if defined(IMAGE_BL32)
94static const mmap_region_t stm32mp1_mmap[] = {
Etienne Carriere72369b12019-12-08 08:17:56 +010095 MAP_SEC_SYSRAM,
96 MAP_NS_SYSRAM,
Yann Gautieree8f5422019-02-14 11:13:25 +010097 MAP_DEVICE1,
98 MAP_DEVICE2,
99 {0}
100};
101#endif
102
103void configure_mmu(void)
104{
105 mmap_add(stm32mp1_mmap);
106 init_xlat_tables();
107
108 enable_mmu_svc_mon(0);
109}
Yann Gautiere3bf9132019-05-07 18:52:17 +0200110
Etienne Carriere66b04522019-12-02 10:05:02 +0100111uintptr_t stm32_get_gpio_bank_base(unsigned int bank)
112{
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100113#if STM32MP13
Patrick Delaunay7cfc8dd2023-11-28 11:35:51 +0100114 assert(bank <= GPIO_BANK_I);
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100115#endif
116#if STM32MP15
Etienne Carriere66b04522019-12-02 10:05:02 +0100117 if (bank == GPIO_BANK_Z) {
118 return GPIOZ_BASE;
119 }
120
Patrick Delaunay7cfc8dd2023-11-28 11:35:51 +0100121 assert(bank <= GPIO_BANK_K);
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100122#endif
Etienne Carriere66b04522019-12-02 10:05:02 +0100123
124 return GPIOA_BASE + (bank * GPIO_BANK_OFFSET);
125}
126
127uint32_t stm32_get_gpio_bank_offset(unsigned int bank)
128{
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100129#if STM32MP13
Patrick Delaunay7cfc8dd2023-11-28 11:35:51 +0100130 assert(bank <= GPIO_BANK_I);
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100131#endif
132#if STM32MP15
Etienne Carriere66b04522019-12-02 10:05:02 +0100133 if (bank == GPIO_BANK_Z) {
134 return 0;
135 }
136
Patrick Delaunay7cfc8dd2023-11-28 11:35:51 +0100137 assert(bank <= GPIO_BANK_K);
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100138#endif
Etienne Carriere66b04522019-12-02 10:05:02 +0100139
140 return bank * GPIO_BANK_OFFSET;
141}
142
Yann Gautier2b79c372021-06-11 10:54:56 +0200143bool stm32_gpio_is_secure_at_reset(unsigned int bank)
144{
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100145#if STM32MP13
146 return true;
147#endif
148#if STM32MP15
Yann Gautier2b79c372021-06-11 10:54:56 +0200149 if (bank == GPIO_BANK_Z) {
150 return true;
151 }
152
153 return false;
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100154#endif
Yann Gautier2b79c372021-06-11 10:54:56 +0200155}
156
Yann Gautiere3bf9132019-05-07 18:52:17 +0200157unsigned long stm32_get_gpio_bank_clock(unsigned int bank)
158{
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100159#if STM32MP13
Patrick Delaunay7cfc8dd2023-11-28 11:35:51 +0100160 assert(bank <= GPIO_BANK_I);
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100161#endif
162#if STM32MP15
Yann Gautiere3bf9132019-05-07 18:52:17 +0200163 if (bank == GPIO_BANK_Z) {
164 return GPIOZ;
165 }
166
Patrick Delaunay7cfc8dd2023-11-28 11:35:51 +0100167 assert(bank <= GPIO_BANK_K);
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100168#endif
Yann Gautiere3bf9132019-05-07 18:52:17 +0200169
170 return GPIOA + (bank - GPIO_BANK_A);
171}
Yann Gautier091eab52019-06-04 18:06:34 +0200172
Etienne Carriered81dadf2020-04-25 11:14:45 +0200173int stm32_get_gpio_bank_pinctrl_node(void *fdt, unsigned int bank)
174{
Yann Gautiere78db3b2022-03-11 14:18:13 +0100175 const char *node_compatible = NULL;
176
Etienne Carriered81dadf2020-04-25 11:14:45 +0200177 switch (bank) {
178 case GPIO_BANK_A:
179 case GPIO_BANK_B:
180 case GPIO_BANK_C:
181 case GPIO_BANK_D:
182 case GPIO_BANK_E:
183 case GPIO_BANK_F:
184 case GPIO_BANK_G:
185 case GPIO_BANK_H:
186 case GPIO_BANK_I:
Yann Gautiere78db3b2022-03-11 14:18:13 +0100187#if STM32MP13
188 node_compatible = "st,stm32mp135-pinctrl";
189 break;
190#endif
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100191#if STM32MP15
Etienne Carriered81dadf2020-04-25 11:14:45 +0200192 case GPIO_BANK_J:
193 case GPIO_BANK_K:
Yann Gautiere78db3b2022-03-11 14:18:13 +0100194 node_compatible = "st,stm32mp157-pinctrl";
195 break;
Etienne Carriered81dadf2020-04-25 11:14:45 +0200196 case GPIO_BANK_Z:
Yann Gautiere78db3b2022-03-11 14:18:13 +0100197 node_compatible = "st,stm32mp157-z-pinctrl";
198 break;
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100199#endif
Etienne Carriered81dadf2020-04-25 11:14:45 +0200200 default:
201 panic();
202 }
Yann Gautiere78db3b2022-03-11 14:18:13 +0100203
204 return fdt_node_offset_by_compatible(fdt, -1, node_compatible);
Etienne Carriered81dadf2020-04-25 11:14:45 +0200205}
206
Yann Gautier3d8497c2021-10-18 16:06:22 +0200207#if STM32MP_UART_PROGRAMMER || !defined(IMAGE_BL2)
Patrick Delaunaye50571b2021-10-28 13:48:52 +0200208/*
209 * UART Management
210 */
211static const uintptr_t stm32mp1_uart_addresses[8] = {
212 USART1_BASE,
213 USART2_BASE,
214 USART3_BASE,
215 UART4_BASE,
216 UART5_BASE,
217 USART6_BASE,
218 UART7_BASE,
219 UART8_BASE,
220};
221
222uintptr_t get_uart_address(uint32_t instance_nb)
223{
224 if ((instance_nb == 0U) ||
225 (instance_nb > ARRAY_SIZE(stm32mp1_uart_addresses))) {
226 return 0U;
227 }
228
229 return stm32mp1_uart_addresses[instance_nb - 1U];
230}
231#endif
232
Yann Gautiercd16df32021-06-04 14:04:05 +0200233#if STM32MP_USB_PROGRAMMER
234struct gpio_bank_pin_list {
235 uint32_t bank;
236 uint32_t pin;
237};
238
239static const struct gpio_bank_pin_list gpio_list[] = {
240 { /* USART2_RX: GPIOA3 */
241 .bank = 0U,
242 .pin = 3U,
243 },
244 { /* USART3_RX: GPIOB12 */
245 .bank = 1U,
246 .pin = 12U,
247 },
248 { /* UART4_RX: GPIOB2 */
249 .bank = 1U,
250 .pin = 2U,
251 },
252 { /* UART5_RX: GPIOB4 */
253 .bank = 1U,
254 .pin = 5U,
255 },
256 { /* USART6_RX: GPIOC7 */
257 .bank = 2U,
258 .pin = 7U,
259 },
260 { /* UART7_RX: GPIOF6 */
261 .bank = 5U,
262 .pin = 6U,
263 },
264 { /* UART8_RX: GPIOE0 */
265 .bank = 4U,
266 .pin = 0U,
267 },
268};
269
270void stm32mp1_deconfigure_uart_pins(void)
271{
272 size_t i;
273
274 for (i = 0U; i < ARRAY_SIZE(gpio_list); i++) {
275 set_gpio_reset_cfg(gpio_list[i].bank, gpio_list[i].pin);
276 }
277}
278#endif
279
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200280uint32_t stm32mp_get_chip_version(void)
Yann Gautierc7374052019-06-04 18:02:37 +0200281{
Yann Gautierf36a1d32020-04-21 15:03:59 +0200282#if STM32MP13
283 return stm32mp1_syscfg_get_chip_version();
284#endif
285#if STM32MP15
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200286 uint32_t version = 0U;
287
288 if (stm32mp1_dbgmcu_get_chip_version(&version) < 0) {
289 INFO("Cannot get CPU version, debug disabled\n");
290 return 0U;
291 }
292
293 return version;
Yann Gautierf36a1d32020-04-21 15:03:59 +0200294#endif
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200295}
Yann Gautierc7374052019-06-04 18:02:37 +0200296
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200297uint32_t stm32mp_get_chip_dev_id(void)
298{
Yann Gautierf36a1d32020-04-21 15:03:59 +0200299#if STM32MP13
300 return stm32mp1_syscfg_get_chip_dev_id();
301#endif
302#if STM32MP15
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200303 uint32_t dev_id;
Nicolas Le Bayon98f4ea02019-09-23 11:18:32 +0200304
Yann Gautierc7374052019-06-04 18:02:37 +0200305 if (stm32mp1_dbgmcu_get_chip_dev_id(&dev_id) < 0) {
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200306 INFO("Use default chip ID, debug disabled\n");
307 dev_id = STM32MP1_CHIP_ID;
Yann Gautierc7374052019-06-04 18:02:37 +0200308 }
309
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200310 return dev_id;
Yann Gautierf36a1d32020-04-21 15:03:59 +0200311#endif
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200312}
313
314static uint32_t get_part_number(void)
315{
316 static uint32_t part_number;
317
318 if (part_number != 0U) {
319 return part_number;
320 }
321
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100322 if (stm32_get_otp_value(PART_NUMBER_OTP, &part_number) != 0) {
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200323 panic();
Yann Gautierc7374052019-06-04 18:02:37 +0200324 }
325
326 part_number = (part_number & PART_NUMBER_OTP_PART_MASK) >>
327 PART_NUMBER_OTP_PART_SHIFT;
328
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200329 part_number |= stm32mp_get_chip_dev_id() << 16;
Yann Gautierc7374052019-06-04 18:02:37 +0200330
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200331 return part_number;
Yann Gautierc7374052019-06-04 18:02:37 +0200332}
333
Yann Gautier16188f32020-02-12 15:38:34 +0100334#if STM32MP15
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200335static uint32_t get_cpu_package(void)
Yann Gautierc7374052019-06-04 18:02:37 +0200336{
337 uint32_t package;
338
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100339 if (stm32_get_otp_value(PACKAGE_OTP, &package) != 0) {
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200340 panic();
Yann Gautierc7374052019-06-04 18:02:37 +0200341 }
342
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200343 package = (package & PACKAGE_OTP_PKG_MASK) >>
Yann Gautierc7374052019-06-04 18:02:37 +0200344 PACKAGE_OTP_PKG_SHIFT;
345
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200346 return package;
Yann Gautierc7374052019-06-04 18:02:37 +0200347}
Yann Gautier16188f32020-02-12 15:38:34 +0100348#endif
Yann Gautierc7374052019-06-04 18:02:37 +0200349
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200350void stm32mp_get_soc_name(char name[STM32_SOC_NAME_SIZE])
Yann Gautierc7374052019-06-04 18:02:37 +0200351{
Yann Gautier4bda43f2022-11-24 19:02:23 +0100352 const char *cpu_s, *cpu_r, *pkg;
Yann Gautierc7374052019-06-04 18:02:37 +0200353
354 /* MPUs Part Numbers */
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200355 switch (get_part_number()) {
Yann Gautier16188f32020-02-12 15:38:34 +0100356#if STM32MP13
357 case STM32MP135F_PART_NB:
358 cpu_s = "135F";
359 break;
360 case STM32MP135D_PART_NB:
361 cpu_s = "135D";
362 break;
363 case STM32MP135C_PART_NB:
364 cpu_s = "135C";
365 break;
366 case STM32MP135A_PART_NB:
367 cpu_s = "135A";
368 break;
369 case STM32MP133F_PART_NB:
370 cpu_s = "133F";
371 break;
372 case STM32MP133D_PART_NB:
373 cpu_s = "133D";
374 break;
375 case STM32MP133C_PART_NB:
376 cpu_s = "133C";
377 break;
378 case STM32MP133A_PART_NB:
379 cpu_s = "133A";
380 break;
381 case STM32MP131F_PART_NB:
382 cpu_s = "131F";
383 break;
384 case STM32MP131D_PART_NB:
385 cpu_s = "131D";
386 break;
387 case STM32MP131C_PART_NB:
388 cpu_s = "131C";
389 break;
390 case STM32MP131A_PART_NB:
391 cpu_s = "131A";
392 break;
393#endif
394#if STM32MP15
Yann Gautierc7374052019-06-04 18:02:37 +0200395 case STM32MP157C_PART_NB:
396 cpu_s = "157C";
397 break;
398 case STM32MP157A_PART_NB:
399 cpu_s = "157A";
400 break;
401 case STM32MP153C_PART_NB:
402 cpu_s = "153C";
403 break;
404 case STM32MP153A_PART_NB:
405 cpu_s = "153A";
406 break;
407 case STM32MP151C_PART_NB:
408 cpu_s = "151C";
409 break;
410 case STM32MP151A_PART_NB:
411 cpu_s = "151A";
412 break;
Lionel Debieve7b64e3e2019-05-17 16:01:18 +0200413 case STM32MP157F_PART_NB:
414 cpu_s = "157F";
415 break;
416 case STM32MP157D_PART_NB:
417 cpu_s = "157D";
418 break;
419 case STM32MP153F_PART_NB:
420 cpu_s = "153F";
421 break;
422 case STM32MP153D_PART_NB:
423 cpu_s = "153D";
424 break;
425 case STM32MP151F_PART_NB:
426 cpu_s = "151F";
427 break;
428 case STM32MP151D_PART_NB:
429 cpu_s = "151D";
430 break;
Yann Gautier16188f32020-02-12 15:38:34 +0100431#endif
Yann Gautierc7374052019-06-04 18:02:37 +0200432 default:
433 cpu_s = "????";
434 break;
435 }
436
437 /* Package */
Yann Gautier16188f32020-02-12 15:38:34 +0100438#if STM32MP13
439 /* On STM32MP13, package is not present in OTP */
440 pkg = "";
441#endif
442#if STM32MP15
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200443 switch (get_cpu_package()) {
Yann Gautierc7374052019-06-04 18:02:37 +0200444 case PKG_AA_LFBGA448:
445 pkg = "AA";
446 break;
447 case PKG_AB_LFBGA354:
448 pkg = "AB";
449 break;
450 case PKG_AC_TFBGA361:
451 pkg = "AC";
452 break;
453 case PKG_AD_TFBGA257:
454 pkg = "AD";
455 break;
456 default:
457 pkg = "??";
458 break;
459 }
Yann Gautier16188f32020-02-12 15:38:34 +0100460#endif
Yann Gautierc7374052019-06-04 18:02:37 +0200461
462 /* REVISION */
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200463 switch (stm32mp_get_chip_version()) {
Yann Gautierc7374052019-06-04 18:02:37 +0200464 case STM32MP1_REV_B:
465 cpu_r = "B";
466 break;
Yann Gautier06cc7912022-05-09 17:01:11 +0200467#if STM32MP13
468 case STM32MP1_REV_Y:
469 cpu_r = "Y";
470 break;
471#endif
Lionel Debieve2d64b532019-06-25 10:40:37 +0200472 case STM32MP1_REV_Z:
473 cpu_r = "Z";
474 break;
Yann Gautierc7374052019-06-04 18:02:37 +0200475 default:
476 cpu_r = "?";
477 break;
478 }
479
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200480 snprintf(name, STM32_SOC_NAME_SIZE,
481 "STM32MP%s%s Rev.%s", cpu_s, pkg, cpu_r);
482}
483
484void stm32mp_print_cpuinfo(void)
485{
486 char name[STM32_SOC_NAME_SIZE];
487
488 stm32mp_get_soc_name(name);
489 NOTICE("CPU: %s\n", name);
Yann Gautierc7374052019-06-04 18:02:37 +0200490}
491
Yann Gautier35dc0772019-05-13 18:34:48 +0200492void stm32mp_print_boardinfo(void)
493{
Yann Gautier45b95992023-01-04 16:46:07 +0100494 uint32_t board_id = 0U;
Yann Gautier35dc0772019-05-13 18:34:48 +0200495
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100496 if (stm32_get_otp_value(BOARD_ID_OTP, &board_id) != 0) {
Yann Gautier35dc0772019-05-13 18:34:48 +0200497 return;
498 }
499
Yann Gautier35dc0772019-05-13 18:34:48 +0200500 if (board_id != 0U) {
Yann Gautier45b95992023-01-04 16:46:07 +0100501 stm32_display_board_info(board_id);
Yann Gautier35dc0772019-05-13 18:34:48 +0200502 }
503}
504
Yann Gautieraf19ff92019-06-04 18:23:10 +0200505/* Return true when SoC provides a single Cortex-A7 core, and false otherwise */
506bool stm32mp_is_single_core(void)
507{
Yann Gautier6bd30e22020-02-06 15:34:16 +0100508#if STM32MP13
509 return true;
510#endif
511#if STM32MP15
Yann Gautier2f3b5602021-10-19 13:31:06 +0200512 bool single_core = false;
513
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200514 switch (get_part_number()) {
Yann Gautieraf19ff92019-06-04 18:23:10 +0200515 case STM32MP151A_PART_NB:
516 case STM32MP151C_PART_NB:
Lionel Debieve7b64e3e2019-05-17 16:01:18 +0200517 case STM32MP151D_PART_NB:
518 case STM32MP151F_PART_NB:
Yann Gautier2f3b5602021-10-19 13:31:06 +0200519 single_core = true;
520 break;
Yann Gautieraf19ff92019-06-04 18:23:10 +0200521 default:
Yann Gautier2f3b5602021-10-19 13:31:06 +0200522 break;
Yann Gautieraf19ff92019-06-04 18:23:10 +0200523 }
Yann Gautier2f3b5602021-10-19 13:31:06 +0200524
525 return single_core;
Yann Gautier6bd30e22020-02-06 15:34:16 +0100526#endif
Yann Gautieraf19ff92019-06-04 18:23:10 +0200527}
528
Lionel Debieve0e73d732019-09-16 12:17:09 +0200529/* Return true when device is in closed state */
Yann Gautier3e334752023-02-01 15:04:30 +0100530uint32_t stm32mp_check_closed_device(void)
Lionel Debieve0e73d732019-09-16 12:17:09 +0200531{
532 uint32_t value;
533
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100534 if (stm32_get_otp_value(CFG0_OTP, &value) != 0) {
Yann Gautier3e334752023-02-01 15:04:30 +0100535 return STM32MP_CHIP_SEC_CLOSED;
Lionel Debieve0e73d732019-09-16 12:17:09 +0200536 }
537
Nicolas Le Bayon34cbf232020-11-26 09:57:09 +0100538#if STM32MP13
539 value = (value & CFG0_OTP_MODE_MASK) >> CFG0_OTP_MODE_SHIFT;
540
541 switch (value) {
542 case CFG0_OPEN_DEVICE:
Yann Gautier3e334752023-02-01 15:04:30 +0100543 return STM32MP_CHIP_SEC_OPEN;
Nicolas Le Bayon34cbf232020-11-26 09:57:09 +0100544 case CFG0_CLOSED_DEVICE:
545 case CFG0_CLOSED_DEVICE_NO_BOUNDARY_SCAN:
546 case CFG0_CLOSED_DEVICE_NO_JTAG:
Yann Gautier3e334752023-02-01 15:04:30 +0100547 return STM32MP_CHIP_SEC_CLOSED;
Nicolas Le Bayon34cbf232020-11-26 09:57:09 +0100548 default:
549 panic();
550 }
551#endif
552#if STM32MP15
Yann Gautier3e334752023-02-01 15:04:30 +0100553 if ((value & CFG0_CLOSED_DEVICE) == CFG0_CLOSED_DEVICE) {
554 return STM32MP_CHIP_SEC_CLOSED;
555 } else {
556 return STM32MP_CHIP_SEC_OPEN;
557 }
558
Nicolas Le Bayon34cbf232020-11-26 09:57:09 +0100559#endif
Lionel Debieve0e73d732019-09-16 12:17:09 +0200560}
561
Lionel Debieve06bc62d2019-12-06 12:42:20 +0100562/* Return true when device supports secure boot */
563bool stm32mp_is_auth_supported(void)
564{
565 bool supported = false;
566
567 switch (get_part_number()) {
Yann Gautier16188f32020-02-12 15:38:34 +0100568#if STM32MP13
569 case STM32MP131C_PART_NB:
570 case STM32MP131F_PART_NB:
571 case STM32MP133C_PART_NB:
572 case STM32MP133F_PART_NB:
573 case STM32MP135C_PART_NB:
574 case STM32MP135F_PART_NB:
575#endif
576#if STM32MP15
Lionel Debieve06bc62d2019-12-06 12:42:20 +0100577 case STM32MP151C_PART_NB:
578 case STM32MP151F_PART_NB:
579 case STM32MP153C_PART_NB:
580 case STM32MP153F_PART_NB:
581 case STM32MP157C_PART_NB:
582 case STM32MP157F_PART_NB:
Yann Gautier16188f32020-02-12 15:38:34 +0100583#endif
Lionel Debieve06bc62d2019-12-06 12:42:20 +0100584 supported = true;
585 break;
586 default:
587 break;
588 }
589
590 return supported;
591}
592
Yann Gautier091eab52019-06-04 18:06:34 +0200593uint32_t stm32_iwdg_get_instance(uintptr_t base)
594{
595 switch (base) {
596 case IWDG1_BASE:
597 return IWDG1_INST;
598 case IWDG2_BASE:
599 return IWDG2_INST;
600 default:
601 panic();
602 }
603}
604
605uint32_t stm32_iwdg_get_otp_config(uint32_t iwdg_inst)
606{
607 uint32_t iwdg_cfg = 0U;
608 uint32_t otp_value;
609
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100610 if (stm32_get_otp_value(HW2_OTP, &otp_value) != 0) {
Yann Gautier091eab52019-06-04 18:06:34 +0200611 panic();
612 }
613
614 if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_HW_POS)) != 0U) {
615 iwdg_cfg |= IWDG_HW_ENABLED;
616 }
617
618 if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STOP_POS)) != 0U) {
619 iwdg_cfg |= IWDG_DISABLE_ON_STOP;
620 }
621
622 if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STANDBY_POS)) != 0U) {
623 iwdg_cfg |= IWDG_DISABLE_ON_STANDBY;
624 }
625
626 return iwdg_cfg;
627}
628
629#if defined(IMAGE_BL2)
630uint32_t stm32_iwdg_shadow_update(uint32_t iwdg_inst, uint32_t flags)
631{
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100632 uint32_t otp_value;
Yann Gautier091eab52019-06-04 18:06:34 +0200633 uint32_t otp;
634 uint32_t result;
635
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100636 if (stm32_get_otp_index(HW2_OTP, &otp, NULL) != 0) {
637 panic();
638 }
639
640 if (stm32_get_otp_value(HW2_OTP, &otp_value) != 0) {
Yann Gautier091eab52019-06-04 18:06:34 +0200641 panic();
642 }
643
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100644 if ((flags & IWDG_DISABLE_ON_STOP) != 0) {
645 otp_value |= BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STOP_POS);
Yann Gautier091eab52019-06-04 18:06:34 +0200646 }
647
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100648 if ((flags & IWDG_DISABLE_ON_STANDBY) != 0) {
649 otp_value |= BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STANDBY_POS);
Yann Gautier091eab52019-06-04 18:06:34 +0200650 }
651
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100652 result = bsec_write_otp(otp_value, otp);
Yann Gautier091eab52019-06-04 18:06:34 +0200653 if (result != BSEC_OK) {
654 return result;
655 }
656
657 /* Sticky lock OTP_IWDG (read and write) */
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100658 if ((bsec_set_sr_lock(otp) != BSEC_OK) ||
659 (bsec_set_sw_lock(otp) != BSEC_OK)) {
Yann Gautier091eab52019-06-04 18:06:34 +0200660 return BSEC_LOCK_FAIL;
661 }
662
663 return BSEC_OK;
664}
665#endif
Yann Gautier8f268c82020-02-26 13:39:44 +0100666
Yann Gautier8402c292022-06-29 17:03:36 +0200667uintptr_t stm32_get_bkpr_boot_mode_addr(void)
Yann Gautier6eef5252021-12-10 17:04:40 +0100668{
Yann Gautier8402c292022-06-29 17:03:36 +0200669 return tamp_bkpr(TAMP_BOOT_MODE_BACKUP_REG_ID);
Igor Opaniukf07e8f32022-06-23 21:19:26 +0300670}
671
Yann Gautier5d2eb552022-11-14 14:14:48 +0100672#if PSA_FWU_SUPPORT
Yann Gautier8edeab02024-01-05 13:50:25 +0100673uintptr_t stm32_get_bkpr_fwu_info_addr(void)
Sughosh Ganu03e2f802021-12-01 15:56:27 +0530674{
Yann Gautier8edeab02024-01-05 13:50:25 +0100675 return tamp_bkpr(TAMP_BOOT_FWU_INFO_REG_ID);
Sughosh Ganuff877852024-02-20 14:17:57 +0530676}
Yann Gautier5d2eb552022-11-14 14:14:48 +0100677#endif /* PSA_FWU_SUPPORT */