blob: 1617afd04e2578653e6b357c152d68e4ee47a52b [file] [log] [blame]
Yann Gautieree8f5422019-02-14 11:13:25 +01001/*
Yann Gautier352d8632020-09-17 11:38:09 +02002 * Copyright (c) 2015-2022, 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 Gautier35dc0772019-05-13 18:34:48 +020019/* Internal layout of the 32bit OTP word board_id */
20#define BOARD_ID_BOARD_NB_MASK GENMASK(31, 16)
21#define BOARD_ID_BOARD_NB_SHIFT 16
Patrick Delaunay7704f162020-01-08 10:05:14 +010022#define BOARD_ID_VARCPN_MASK GENMASK(15, 12)
23#define BOARD_ID_VARCPN_SHIFT 12
Yann Gautier35dc0772019-05-13 18:34:48 +020024#define BOARD_ID_REVISION_MASK GENMASK(11, 8)
25#define BOARD_ID_REVISION_SHIFT 8
Patrick Delaunay7704f162020-01-08 10:05:14 +010026#define BOARD_ID_VARFG_MASK GENMASK(7, 4)
27#define BOARD_ID_VARFG_SHIFT 4
Yann Gautier35dc0772019-05-13 18:34:48 +020028#define BOARD_ID_BOM_MASK GENMASK(3, 0)
29
30#define BOARD_ID2NB(_id) (((_id) & BOARD_ID_BOARD_NB_MASK) >> \
31 BOARD_ID_BOARD_NB_SHIFT)
Patrick Delaunay7704f162020-01-08 10:05:14 +010032#define BOARD_ID2VARCPN(_id) (((_id) & BOARD_ID_VARCPN_MASK) >> \
33 BOARD_ID_VARCPN_SHIFT)
Yann Gautier35dc0772019-05-13 18:34:48 +020034#define BOARD_ID2REV(_id) (((_id) & BOARD_ID_REVISION_MASK) >> \
35 BOARD_ID_REVISION_SHIFT)
Patrick Delaunay7704f162020-01-08 10:05:14 +010036#define BOARD_ID2VARFG(_id) (((_id) & BOARD_ID_VARFG_MASK) >> \
37 BOARD_ID_VARFG_SHIFT)
Yann Gautier35dc0772019-05-13 18:34:48 +020038#define BOARD_ID2BOM(_id) ((_id) & BOARD_ID_BOM_MASK)
39
Yann Gautier82000472020-02-05 16:24:21 +010040#if STM32MP13
41#define TAMP_BOOT_MODE_BACKUP_REG_ID U(30)
42#endif
43#if STM32MP15
Yann Gautier6eef5252021-12-10 17:04:40 +010044#define TAMP_BOOT_MODE_BACKUP_REG_ID U(20)
Yann Gautier82000472020-02-05 16:24:21 +010045#endif
Yann Gautier6eef5252021-12-10 17:04:40 +010046#define TAMP_BOOT_MODE_ITF_MASK U(0x0000FF00)
47#define TAMP_BOOT_MODE_ITF_SHIFT 8
48
Yann Gautier9bbd26a2022-03-28 17:49:38 +020049/*
50 * Backup register to store fwu update information.
51 * It should be writeable only by secure world, but also readable by non secure
52 * (so it should be in Zone 2).
53 */
54#define TAMP_BOOT_FWU_INFO_REG_ID U(10)
55#define TAMP_BOOT_FWU_INFO_IDX_MSK U(0xF)
56#define TAMP_BOOT_FWU_INFO_IDX_OFF U(0)
57#define TAMP_BOOT_FWU_INFO_CNT_MSK U(0xF0)
58#define TAMP_BOOT_FWU_INFO_CNT_OFF U(4)
Sughosh Ganu03e2f802021-12-01 15:56:27 +053059
Etienne Carriere72369b12019-12-08 08:17:56 +010060#if defined(IMAGE_BL2)
61#define MAP_SEC_SYSRAM MAP_REGION_FLAT(STM32MP_SYSRAM_BASE, \
Yann Gautiera2e2a302019-02-14 11:13:39 +010062 STM32MP_SYSRAM_SIZE, \
Yann Gautieree8f5422019-02-14 11:13:25 +010063 MT_MEMORY | \
64 MT_RW | \
65 MT_SECURE | \
66 MT_EXECUTE_NEVER)
Etienne Carriere72369b12019-12-08 08:17:56 +010067#elif defined(IMAGE_BL32)
68#define MAP_SEC_SYSRAM MAP_REGION_FLAT(STM32MP_SEC_SYSRAM_BASE, \
69 STM32MP_SEC_SYSRAM_SIZE, \
70 MT_MEMORY | \
71 MT_RW | \
72 MT_SECURE | \
73 MT_EXECUTE_NEVER)
Yann Gautieree8f5422019-02-14 11:13:25 +010074
Etienne Carriere72369b12019-12-08 08:17:56 +010075/* Non-secure SYSRAM is used a uncached memory for SCMI message transfer */
76#define MAP_NS_SYSRAM MAP_REGION_FLAT(STM32MP_NS_SYSRAM_BASE, \
77 STM32MP_NS_SYSRAM_SIZE, \
78 MT_DEVICE | \
79 MT_RW | \
80 MT_NS | \
81 MT_EXECUTE_NEVER)
82#endif
83
Yann Gautier84d994b2020-04-14 18:08:50 +020084#if STM32MP13
85#define MAP_SRAM_ALL MAP_REGION_FLAT(SRAMS_BASE, \
86 SRAMS_SIZE_2MB_ALIGNED, \
87 MT_MEMORY | \
88 MT_RW | \
89 MT_SECURE | \
90 MT_EXECUTE_NEVER)
91#endif
92
Yann Gautieree8f5422019-02-14 11:13:25 +010093#define MAP_DEVICE1 MAP_REGION_FLAT(STM32MP1_DEVICE1_BASE, \
94 STM32MP1_DEVICE1_SIZE, \
95 MT_DEVICE | \
96 MT_RW | \
97 MT_SECURE | \
98 MT_EXECUTE_NEVER)
99
100#define MAP_DEVICE2 MAP_REGION_FLAT(STM32MP1_DEVICE2_BASE, \
101 STM32MP1_DEVICE2_SIZE, \
102 MT_DEVICE | \
103 MT_RW | \
104 MT_SECURE | \
105 MT_EXECUTE_NEVER)
106
107#if defined(IMAGE_BL2)
108static const mmap_region_t stm32mp1_mmap[] = {
Etienne Carriere72369b12019-12-08 08:17:56 +0100109 MAP_SEC_SYSRAM,
Yann Gautier84d994b2020-04-14 18:08:50 +0200110#if STM32MP13
111 MAP_SRAM_ALL,
112#endif
Yann Gautieree8f5422019-02-14 11:13:25 +0100113 MAP_DEVICE1,
Yann Gautier352d8632020-09-17 11:38:09 +0200114#if STM32MP_RAW_NAND
Yann Gautieree8f5422019-02-14 11:13:25 +0100115 MAP_DEVICE2,
Yann Gautier352d8632020-09-17 11:38:09 +0200116#endif
Yann Gautieree8f5422019-02-14 11:13:25 +0100117 {0}
118};
119#endif
120#if defined(IMAGE_BL32)
121static const mmap_region_t stm32mp1_mmap[] = {
Etienne Carriere72369b12019-12-08 08:17:56 +0100122 MAP_SEC_SYSRAM,
123 MAP_NS_SYSRAM,
Yann Gautieree8f5422019-02-14 11:13:25 +0100124 MAP_DEVICE1,
125 MAP_DEVICE2,
126 {0}
127};
128#endif
129
130void configure_mmu(void)
131{
132 mmap_add(stm32mp1_mmap);
133 init_xlat_tables();
134
135 enable_mmu_svc_mon(0);
136}
Yann Gautiere3bf9132019-05-07 18:52:17 +0200137
Etienne Carriere66b04522019-12-02 10:05:02 +0100138uintptr_t stm32_get_gpio_bank_base(unsigned int bank)
139{
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100140#if STM32MP13
141 assert(GPIO_BANK_A == 0 && bank <= GPIO_BANK_I);
142#endif
143#if STM32MP15
Etienne Carriere66b04522019-12-02 10:05:02 +0100144 if (bank == GPIO_BANK_Z) {
145 return GPIOZ_BASE;
146 }
147
148 assert(GPIO_BANK_A == 0 && bank <= GPIO_BANK_K);
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100149#endif
Etienne Carriere66b04522019-12-02 10:05:02 +0100150
151 return GPIOA_BASE + (bank * GPIO_BANK_OFFSET);
152}
153
154uint32_t stm32_get_gpio_bank_offset(unsigned int bank)
155{
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100156#if STM32MP13
157 assert(GPIO_BANK_A == 0 && bank <= GPIO_BANK_I);
158#endif
159#if STM32MP15
Etienne Carriere66b04522019-12-02 10:05:02 +0100160 if (bank == GPIO_BANK_Z) {
161 return 0;
162 }
163
164 assert(GPIO_BANK_A == 0 && bank <= GPIO_BANK_K);
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100165#endif
Etienne Carriere66b04522019-12-02 10:05:02 +0100166
167 return bank * GPIO_BANK_OFFSET;
168}
169
Yann Gautier2b79c372021-06-11 10:54:56 +0200170bool stm32_gpio_is_secure_at_reset(unsigned int bank)
171{
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100172#if STM32MP13
173 return true;
174#endif
175#if STM32MP15
Yann Gautier2b79c372021-06-11 10:54:56 +0200176 if (bank == GPIO_BANK_Z) {
177 return true;
178 }
179
180 return false;
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100181#endif
Yann Gautier2b79c372021-06-11 10:54:56 +0200182}
183
Yann Gautiere3bf9132019-05-07 18:52:17 +0200184unsigned long stm32_get_gpio_bank_clock(unsigned int bank)
185{
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100186#if STM32MP13
187 assert(GPIO_BANK_A == 0 && bank <= GPIO_BANK_I);
188#endif
189#if STM32MP15
Yann Gautiere3bf9132019-05-07 18:52:17 +0200190 if (bank == GPIO_BANK_Z) {
191 return GPIOZ;
192 }
193
194 assert(GPIO_BANK_A == 0 && bank <= GPIO_BANK_K);
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100195#endif
Yann Gautiere3bf9132019-05-07 18:52:17 +0200196
197 return GPIOA + (bank - GPIO_BANK_A);
198}
Yann Gautier091eab52019-06-04 18:06:34 +0200199
Etienne Carriered81dadf2020-04-25 11:14:45 +0200200int stm32_get_gpio_bank_pinctrl_node(void *fdt, unsigned int bank)
201{
202 switch (bank) {
203 case GPIO_BANK_A:
204 case GPIO_BANK_B:
205 case GPIO_BANK_C:
206 case GPIO_BANK_D:
207 case GPIO_BANK_E:
208 case GPIO_BANK_F:
209 case GPIO_BANK_G:
210 case GPIO_BANK_H:
211 case GPIO_BANK_I:
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100212#if STM32MP15
Etienne Carriered81dadf2020-04-25 11:14:45 +0200213 case GPIO_BANK_J:
214 case GPIO_BANK_K:
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100215#endif
Etienne Carriered81dadf2020-04-25 11:14:45 +0200216 return fdt_path_offset(fdt, "/soc/pin-controller");
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100217#if STM32MP15
Etienne Carriered81dadf2020-04-25 11:14:45 +0200218 case GPIO_BANK_Z:
219 return fdt_path_offset(fdt, "/soc/pin-controller-z");
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100220#endif
Etienne Carriered81dadf2020-04-25 11:14:45 +0200221 default:
222 panic();
223 }
224}
225
Yann Gautier3d8497c2021-10-18 16:06:22 +0200226#if STM32MP_UART_PROGRAMMER || !defined(IMAGE_BL2)
Patrick Delaunaye50571b2021-10-28 13:48:52 +0200227/*
228 * UART Management
229 */
230static const uintptr_t stm32mp1_uart_addresses[8] = {
231 USART1_BASE,
232 USART2_BASE,
233 USART3_BASE,
234 UART4_BASE,
235 UART5_BASE,
236 USART6_BASE,
237 UART7_BASE,
238 UART8_BASE,
239};
240
241uintptr_t get_uart_address(uint32_t instance_nb)
242{
243 if ((instance_nb == 0U) ||
244 (instance_nb > ARRAY_SIZE(stm32mp1_uart_addresses))) {
245 return 0U;
246 }
247
248 return stm32mp1_uart_addresses[instance_nb - 1U];
249}
250#endif
251
Yann Gautiercd16df32021-06-04 14:04:05 +0200252#if STM32MP_USB_PROGRAMMER
253struct gpio_bank_pin_list {
254 uint32_t bank;
255 uint32_t pin;
256};
257
258static const struct gpio_bank_pin_list gpio_list[] = {
259 { /* USART2_RX: GPIOA3 */
260 .bank = 0U,
261 .pin = 3U,
262 },
263 { /* USART3_RX: GPIOB12 */
264 .bank = 1U,
265 .pin = 12U,
266 },
267 { /* UART4_RX: GPIOB2 */
268 .bank = 1U,
269 .pin = 2U,
270 },
271 { /* UART5_RX: GPIOB4 */
272 .bank = 1U,
273 .pin = 5U,
274 },
275 { /* USART6_RX: GPIOC7 */
276 .bank = 2U,
277 .pin = 7U,
278 },
279 { /* UART7_RX: GPIOF6 */
280 .bank = 5U,
281 .pin = 6U,
282 },
283 { /* UART8_RX: GPIOE0 */
284 .bank = 4U,
285 .pin = 0U,
286 },
287};
288
289void stm32mp1_deconfigure_uart_pins(void)
290{
291 size_t i;
292
293 for (i = 0U; i < ARRAY_SIZE(gpio_list); i++) {
294 set_gpio_reset_cfg(gpio_list[i].bank, gpio_list[i].pin);
295 }
296}
297#endif
298
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200299uint32_t stm32mp_get_chip_version(void)
Yann Gautierc7374052019-06-04 18:02:37 +0200300{
Yann Gautierf36a1d32020-04-21 15:03:59 +0200301#if STM32MP13
302 return stm32mp1_syscfg_get_chip_version();
303#endif
304#if STM32MP15
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200305 uint32_t version = 0U;
306
307 if (stm32mp1_dbgmcu_get_chip_version(&version) < 0) {
308 INFO("Cannot get CPU version, debug disabled\n");
309 return 0U;
310 }
311
312 return version;
Yann Gautierf36a1d32020-04-21 15:03:59 +0200313#endif
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200314}
Yann Gautierc7374052019-06-04 18:02:37 +0200315
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200316uint32_t stm32mp_get_chip_dev_id(void)
317{
Yann Gautierf36a1d32020-04-21 15:03:59 +0200318#if STM32MP13
319 return stm32mp1_syscfg_get_chip_dev_id();
320#endif
321#if STM32MP15
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200322 uint32_t dev_id;
Nicolas Le Bayon98f4ea02019-09-23 11:18:32 +0200323
Yann Gautierc7374052019-06-04 18:02:37 +0200324 if (stm32mp1_dbgmcu_get_chip_dev_id(&dev_id) < 0) {
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200325 INFO("Use default chip ID, debug disabled\n");
326 dev_id = STM32MP1_CHIP_ID;
Yann Gautierc7374052019-06-04 18:02:37 +0200327 }
328
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200329 return dev_id;
Yann Gautierf36a1d32020-04-21 15:03:59 +0200330#endif
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200331}
332
333static uint32_t get_part_number(void)
334{
335 static uint32_t part_number;
336
337 if (part_number != 0U) {
338 return part_number;
339 }
340
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100341 if (stm32_get_otp_value(PART_NUMBER_OTP, &part_number) != 0) {
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200342 panic();
Yann Gautierc7374052019-06-04 18:02:37 +0200343 }
344
345 part_number = (part_number & PART_NUMBER_OTP_PART_MASK) >>
346 PART_NUMBER_OTP_PART_SHIFT;
347
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200348 part_number |= stm32mp_get_chip_dev_id() << 16;
Yann Gautierc7374052019-06-04 18:02:37 +0200349
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200350 return part_number;
Yann Gautierc7374052019-06-04 18:02:37 +0200351}
352
Yann Gautier16188f32020-02-12 15:38:34 +0100353#if STM32MP15
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200354static uint32_t get_cpu_package(void)
Yann Gautierc7374052019-06-04 18:02:37 +0200355{
356 uint32_t package;
357
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100358 if (stm32_get_otp_value(PACKAGE_OTP, &package) != 0) {
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200359 panic();
Yann Gautierc7374052019-06-04 18:02:37 +0200360 }
361
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200362 package = (package & PACKAGE_OTP_PKG_MASK) >>
Yann Gautierc7374052019-06-04 18:02:37 +0200363 PACKAGE_OTP_PKG_SHIFT;
364
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200365 return package;
Yann Gautierc7374052019-06-04 18:02:37 +0200366}
Yann Gautier16188f32020-02-12 15:38:34 +0100367#endif
Yann Gautierc7374052019-06-04 18:02:37 +0200368
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200369void stm32mp_get_soc_name(char name[STM32_SOC_NAME_SIZE])
Yann Gautierc7374052019-06-04 18:02:37 +0200370{
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200371 char *cpu_s, *cpu_r, *pkg;
Yann Gautierc7374052019-06-04 18:02:37 +0200372
373 /* MPUs Part Numbers */
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200374 switch (get_part_number()) {
Yann Gautier16188f32020-02-12 15:38:34 +0100375#if STM32MP13
376 case STM32MP135F_PART_NB:
377 cpu_s = "135F";
378 break;
379 case STM32MP135D_PART_NB:
380 cpu_s = "135D";
381 break;
382 case STM32MP135C_PART_NB:
383 cpu_s = "135C";
384 break;
385 case STM32MP135A_PART_NB:
386 cpu_s = "135A";
387 break;
388 case STM32MP133F_PART_NB:
389 cpu_s = "133F";
390 break;
391 case STM32MP133D_PART_NB:
392 cpu_s = "133D";
393 break;
394 case STM32MP133C_PART_NB:
395 cpu_s = "133C";
396 break;
397 case STM32MP133A_PART_NB:
398 cpu_s = "133A";
399 break;
400 case STM32MP131F_PART_NB:
401 cpu_s = "131F";
402 break;
403 case STM32MP131D_PART_NB:
404 cpu_s = "131D";
405 break;
406 case STM32MP131C_PART_NB:
407 cpu_s = "131C";
408 break;
409 case STM32MP131A_PART_NB:
410 cpu_s = "131A";
411 break;
412#endif
413#if STM32MP15
Yann Gautierc7374052019-06-04 18:02:37 +0200414 case STM32MP157C_PART_NB:
415 cpu_s = "157C";
416 break;
417 case STM32MP157A_PART_NB:
418 cpu_s = "157A";
419 break;
420 case STM32MP153C_PART_NB:
421 cpu_s = "153C";
422 break;
423 case STM32MP153A_PART_NB:
424 cpu_s = "153A";
425 break;
426 case STM32MP151C_PART_NB:
427 cpu_s = "151C";
428 break;
429 case STM32MP151A_PART_NB:
430 cpu_s = "151A";
431 break;
Lionel Debieve7b64e3e2019-05-17 16:01:18 +0200432 case STM32MP157F_PART_NB:
433 cpu_s = "157F";
434 break;
435 case STM32MP157D_PART_NB:
436 cpu_s = "157D";
437 break;
438 case STM32MP153F_PART_NB:
439 cpu_s = "153F";
440 break;
441 case STM32MP153D_PART_NB:
442 cpu_s = "153D";
443 break;
444 case STM32MP151F_PART_NB:
445 cpu_s = "151F";
446 break;
447 case STM32MP151D_PART_NB:
448 cpu_s = "151D";
449 break;
Yann Gautier16188f32020-02-12 15:38:34 +0100450#endif
Yann Gautierc7374052019-06-04 18:02:37 +0200451 default:
452 cpu_s = "????";
453 break;
454 }
455
456 /* Package */
Yann Gautier16188f32020-02-12 15:38:34 +0100457#if STM32MP13
458 /* On STM32MP13, package is not present in OTP */
459 pkg = "";
460#endif
461#if STM32MP15
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200462 switch (get_cpu_package()) {
Yann Gautierc7374052019-06-04 18:02:37 +0200463 case PKG_AA_LFBGA448:
464 pkg = "AA";
465 break;
466 case PKG_AB_LFBGA354:
467 pkg = "AB";
468 break;
469 case PKG_AC_TFBGA361:
470 pkg = "AC";
471 break;
472 case PKG_AD_TFBGA257:
473 pkg = "AD";
474 break;
475 default:
476 pkg = "??";
477 break;
478 }
Yann Gautier16188f32020-02-12 15:38:34 +0100479#endif
Yann Gautierc7374052019-06-04 18:02:37 +0200480
481 /* REVISION */
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200482 switch (stm32mp_get_chip_version()) {
Yann Gautierc7374052019-06-04 18:02:37 +0200483 case STM32MP1_REV_B:
484 cpu_r = "B";
485 break;
Lionel Debieve2d64b532019-06-25 10:40:37 +0200486 case STM32MP1_REV_Z:
487 cpu_r = "Z";
488 break;
Yann Gautierc7374052019-06-04 18:02:37 +0200489 default:
490 cpu_r = "?";
491 break;
492 }
493
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200494 snprintf(name, STM32_SOC_NAME_SIZE,
495 "STM32MP%s%s Rev.%s", cpu_s, pkg, cpu_r);
496}
497
498void stm32mp_print_cpuinfo(void)
499{
500 char name[STM32_SOC_NAME_SIZE];
501
502 stm32mp_get_soc_name(name);
503 NOTICE("CPU: %s\n", name);
Yann Gautierc7374052019-06-04 18:02:37 +0200504}
505
Yann Gautier35dc0772019-05-13 18:34:48 +0200506void stm32mp_print_boardinfo(void)
507{
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100508 uint32_t board_id = 0;
Yann Gautier35dc0772019-05-13 18:34:48 +0200509
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100510 if (stm32_get_otp_value(BOARD_ID_OTP, &board_id) != 0) {
Yann Gautier35dc0772019-05-13 18:34:48 +0200511 return;
512 }
513
Yann Gautier35dc0772019-05-13 18:34:48 +0200514 if (board_id != 0U) {
515 char rev[2];
516
517 rev[0] = BOARD_ID2REV(board_id) - 1 + 'A';
518 rev[1] = '\0';
Yann Gautier36e9d382020-10-13 18:03:31 +0200519 NOTICE("Board: MB%04x Var%u.%u Rev.%s-%02u\n",
Yann Gautier35dc0772019-05-13 18:34:48 +0200520 BOARD_ID2NB(board_id),
Patrick Delaunay7704f162020-01-08 10:05:14 +0100521 BOARD_ID2VARCPN(board_id),
522 BOARD_ID2VARFG(board_id),
Yann Gautier35dc0772019-05-13 18:34:48 +0200523 rev,
524 BOARD_ID2BOM(board_id));
525 }
526}
527
Yann Gautieraf19ff92019-06-04 18:23:10 +0200528/* Return true when SoC provides a single Cortex-A7 core, and false otherwise */
529bool stm32mp_is_single_core(void)
530{
Yann Gautier6bd30e22020-02-06 15:34:16 +0100531#if STM32MP13
532 return true;
533#endif
534#if STM32MP15
Yann Gautier2f3b5602021-10-19 13:31:06 +0200535 bool single_core = false;
536
Yann Gautiera0a6ff62021-05-10 16:05:18 +0200537 switch (get_part_number()) {
Yann Gautieraf19ff92019-06-04 18:23:10 +0200538 case STM32MP151A_PART_NB:
539 case STM32MP151C_PART_NB:
Lionel Debieve7b64e3e2019-05-17 16:01:18 +0200540 case STM32MP151D_PART_NB:
541 case STM32MP151F_PART_NB:
Yann Gautier2f3b5602021-10-19 13:31:06 +0200542 single_core = true;
543 break;
Yann Gautieraf19ff92019-06-04 18:23:10 +0200544 default:
Yann Gautier2f3b5602021-10-19 13:31:06 +0200545 break;
Yann Gautieraf19ff92019-06-04 18:23:10 +0200546 }
Yann Gautier2f3b5602021-10-19 13:31:06 +0200547
548 return single_core;
Yann Gautier6bd30e22020-02-06 15:34:16 +0100549#endif
Yann Gautieraf19ff92019-06-04 18:23:10 +0200550}
551
Lionel Debieve0e73d732019-09-16 12:17:09 +0200552/* Return true when device is in closed state */
553bool stm32mp_is_closed_device(void)
554{
555 uint32_t value;
556
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100557 if (stm32_get_otp_value(CFG0_OTP, &value) != 0) {
Lionel Debieve0e73d732019-09-16 12:17:09 +0200558 return true;
559 }
560
Nicolas Le Bayon34cbf232020-11-26 09:57:09 +0100561#if STM32MP13
562 value = (value & CFG0_OTP_MODE_MASK) >> CFG0_OTP_MODE_SHIFT;
563
564 switch (value) {
565 case CFG0_OPEN_DEVICE:
566 return false;
567 case CFG0_CLOSED_DEVICE:
568 case CFG0_CLOSED_DEVICE_NO_BOUNDARY_SCAN:
569 case CFG0_CLOSED_DEVICE_NO_JTAG:
570 return true;
571 default:
572 panic();
573 }
574#endif
575#if STM32MP15
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100576 return (value & CFG0_CLOSED_DEVICE) == CFG0_CLOSED_DEVICE;
Nicolas Le Bayon34cbf232020-11-26 09:57:09 +0100577#endif
Lionel Debieve0e73d732019-09-16 12:17:09 +0200578}
579
Lionel Debieve06bc62d2019-12-06 12:42:20 +0100580/* Return true when device supports secure boot */
581bool stm32mp_is_auth_supported(void)
582{
583 bool supported = false;
584
585 switch (get_part_number()) {
Yann Gautier16188f32020-02-12 15:38:34 +0100586#if STM32MP13
587 case STM32MP131C_PART_NB:
588 case STM32MP131F_PART_NB:
589 case STM32MP133C_PART_NB:
590 case STM32MP133F_PART_NB:
591 case STM32MP135C_PART_NB:
592 case STM32MP135F_PART_NB:
593#endif
594#if STM32MP15
Lionel Debieve06bc62d2019-12-06 12:42:20 +0100595 case STM32MP151C_PART_NB:
596 case STM32MP151F_PART_NB:
597 case STM32MP153C_PART_NB:
598 case STM32MP153F_PART_NB:
599 case STM32MP157C_PART_NB:
600 case STM32MP157F_PART_NB:
Yann Gautier16188f32020-02-12 15:38:34 +0100601#endif
Lionel Debieve06bc62d2019-12-06 12:42:20 +0100602 supported = true;
603 break;
604 default:
605 break;
606 }
607
608 return supported;
609}
610
Yann Gautier091eab52019-06-04 18:06:34 +0200611uint32_t stm32_iwdg_get_instance(uintptr_t base)
612{
613 switch (base) {
614 case IWDG1_BASE:
615 return IWDG1_INST;
616 case IWDG2_BASE:
617 return IWDG2_INST;
618 default:
619 panic();
620 }
621}
622
623uint32_t stm32_iwdg_get_otp_config(uint32_t iwdg_inst)
624{
625 uint32_t iwdg_cfg = 0U;
626 uint32_t otp_value;
627
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100628 if (stm32_get_otp_value(HW2_OTP, &otp_value) != 0) {
Yann Gautier091eab52019-06-04 18:06:34 +0200629 panic();
630 }
631
632 if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_HW_POS)) != 0U) {
633 iwdg_cfg |= IWDG_HW_ENABLED;
634 }
635
636 if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STOP_POS)) != 0U) {
637 iwdg_cfg |= IWDG_DISABLE_ON_STOP;
638 }
639
640 if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STANDBY_POS)) != 0U) {
641 iwdg_cfg |= IWDG_DISABLE_ON_STANDBY;
642 }
643
644 return iwdg_cfg;
645}
646
647#if defined(IMAGE_BL2)
648uint32_t stm32_iwdg_shadow_update(uint32_t iwdg_inst, uint32_t flags)
649{
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100650 uint32_t otp_value;
Yann Gautier091eab52019-06-04 18:06:34 +0200651 uint32_t otp;
652 uint32_t result;
653
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100654 if (stm32_get_otp_index(HW2_OTP, &otp, NULL) != 0) {
655 panic();
656 }
657
658 if (stm32_get_otp_value(HW2_OTP, &otp_value) != 0) {
Yann Gautier091eab52019-06-04 18:06:34 +0200659 panic();
660 }
661
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100662 if ((flags & IWDG_DISABLE_ON_STOP) != 0) {
663 otp_value |= BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STOP_POS);
Yann Gautier091eab52019-06-04 18:06:34 +0200664 }
665
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100666 if ((flags & IWDG_DISABLE_ON_STANDBY) != 0) {
667 otp_value |= BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STANDBY_POS);
Yann Gautier091eab52019-06-04 18:06:34 +0200668 }
669
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100670 result = bsec_write_otp(otp_value, otp);
Yann Gautier091eab52019-06-04 18:06:34 +0200671 if (result != BSEC_OK) {
672 return result;
673 }
674
675 /* Sticky lock OTP_IWDG (read and write) */
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100676 if ((bsec_set_sr_lock(otp) != BSEC_OK) ||
677 (bsec_set_sw_lock(otp) != BSEC_OK)) {
Yann Gautier091eab52019-06-04 18:06:34 +0200678 return BSEC_LOCK_FAIL;
679 }
680
681 return BSEC_OK;
682}
683#endif
Yann Gautier8f268c82020-02-26 13:39:44 +0100684
Lionel Debieve1dc5e2e2020-09-27 21:13:53 +0200685#if STM32MP_USE_STM32IMAGE
Yann Gautier8f268c82020-02-26 13:39:44 +0100686/* Get the non-secure DDR size */
687uint32_t stm32mp_get_ddr_ns_size(void)
688{
689 static uint32_t ddr_ns_size;
690 uint32_t ddr_size;
691
692 if (ddr_ns_size != 0U) {
693 return ddr_ns_size;
694 }
695
696 ddr_size = dt_get_ddr_size();
697 if ((ddr_size <= (STM32MP_DDR_S_SIZE + STM32MP_DDR_SHMEM_SIZE)) ||
698 (ddr_size > STM32MP_DDR_MAX_SIZE)) {
699 panic();
700 }
701
702 ddr_ns_size = ddr_size - (STM32MP_DDR_S_SIZE + STM32MP_DDR_SHMEM_SIZE);
703
704 return ddr_ns_size;
705}
Lionel Debieve1dc5e2e2020-09-27 21:13:53 +0200706#endif /* STM32MP_USE_STM32IMAGE */
Yann Gautier6eef5252021-12-10 17:04:40 +0100707
708void stm32_save_boot_interface(uint32_t interface, uint32_t instance)
709{
Nicolas Toromanoffbb82b1b2022-02-09 12:26:31 +0100710 uintptr_t bkpr_itf_idx = tamp_bkpr(TAMP_BOOT_MODE_BACKUP_REG_ID);
Yann Gautier6eef5252021-12-10 17:04:40 +0100711
Yann Gautiera205a5c2021-08-30 15:06:54 +0200712 clk_enable(RTCAPB);
Yann Gautier6eef5252021-12-10 17:04:40 +0100713
714 mmio_clrsetbits_32(bkpr_itf_idx,
715 TAMP_BOOT_MODE_ITF_MASK,
716 ((interface << 4) | (instance & 0xFU)) <<
717 TAMP_BOOT_MODE_ITF_SHIFT);
718
Yann Gautiera205a5c2021-08-30 15:06:54 +0200719 clk_disable(RTCAPB);
Yann Gautier6eef5252021-12-10 17:04:40 +0100720}
Yann Gautieraaee0612020-12-16 12:04:06 +0100721
722void stm32_get_boot_interface(uint32_t *interface, uint32_t *instance)
723{
724 static uint32_t itf;
725
726 if (itf == 0U) {
Nicolas Toromanoffbb82b1b2022-02-09 12:26:31 +0100727 uintptr_t bkpr = tamp_bkpr(TAMP_BOOT_MODE_BACKUP_REG_ID);
Yann Gautieraaee0612020-12-16 12:04:06 +0100728
Yann Gautiera205a5c2021-08-30 15:06:54 +0200729 clk_enable(RTCAPB);
Yann Gautieraaee0612020-12-16 12:04:06 +0100730
731 itf = (mmio_read_32(bkpr) & TAMP_BOOT_MODE_ITF_MASK) >>
732 TAMP_BOOT_MODE_ITF_SHIFT;
733
Yann Gautiera205a5c2021-08-30 15:06:54 +0200734 clk_disable(RTCAPB);
Yann Gautieraaee0612020-12-16 12:04:06 +0100735 }
736
737 *interface = itf >> 4;
738 *instance = itf & 0xFU;
739}
Sughosh Ganu03e2f802021-12-01 15:56:27 +0530740
741#if !STM32MP_USE_STM32IMAGE && PSA_FWU_SUPPORT
742void stm32mp1_fwu_set_boot_idx(void)
743{
744 clk_enable(RTCAPB);
Yann Gautier9bbd26a2022-03-28 17:49:38 +0200745 mmio_clrsetbits_32(tamp_bkpr(TAMP_BOOT_FWU_INFO_REG_ID),
746 TAMP_BOOT_FWU_INFO_IDX_MSK,
747 (plat_fwu_get_boot_idx() << TAMP_BOOT_FWU_INFO_IDX_OFF) &
748 TAMP_BOOT_FWU_INFO_IDX_MSK);
Sughosh Ganu03e2f802021-12-01 15:56:27 +0530749 clk_disable(RTCAPB);
750}
Nicolas Toromanoff5a937cd2022-02-07 10:12:04 +0100751
752uint32_t stm32_get_and_dec_fwu_trial_boot_cnt(void)
753{
754 uintptr_t bkpr_fwu_cnt = tamp_bkpr(TAMP_BOOT_FWU_INFO_REG_ID);
755 uint32_t try_cnt;
756
757 clk_enable(RTCAPB);
758 try_cnt = (mmio_read_32(bkpr_fwu_cnt) & TAMP_BOOT_FWU_INFO_CNT_MSK) >>
759 TAMP_BOOT_FWU_INFO_CNT_OFF;
760
761 assert(try_cnt <= FWU_MAX_TRIAL_REBOOT);
762
763 if (try_cnt != 0U) {
764 mmio_clrsetbits_32(bkpr_fwu_cnt, TAMP_BOOT_FWU_INFO_CNT_MSK,
765 (try_cnt - 1U) << TAMP_BOOT_FWU_INFO_CNT_OFF);
766 }
767 clk_disable(RTCAPB);
768
769 return try_cnt;
770}
771
772void stm32_set_max_fwu_trial_boot_cnt(void)
773{
774 uintptr_t bkpr_fwu_cnt = tamp_bkpr(TAMP_BOOT_FWU_INFO_REG_ID);
775
776 clk_enable(RTCAPB);
777 mmio_clrsetbits_32(bkpr_fwu_cnt, TAMP_BOOT_FWU_INFO_CNT_MSK,
778 (FWU_MAX_TRIAL_REBOOT << TAMP_BOOT_FWU_INFO_CNT_OFF) &
779 TAMP_BOOT_FWU_INFO_CNT_MSK);
780 clk_disable(RTCAPB);
781}
Sughosh Ganu03e2f802021-12-01 15:56:27 +0530782#endif /* !STM32MP_USE_STM32IMAGE && PSA_FWU_SUPPORT */