blob: 8446b8fd3d6c0f8d7cc3a158567bc507a211dfc9 [file] [log] [blame]
Patrice Chotardd29531c2023-10-27 16:43:04 +02001// SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause
2/*
3 * Copyright (C) 2023, STMicroelectronics - All Rights Reserved
4 */
5
6#define LOG_CATEGORY LOGC_BOARD
7
Patrice Chotardd29531c2023-10-27 16:43:04 +02008#include <config.h>
Patrice Chotard0f77af62025-04-01 18:08:44 +02009#include <env_internal.h>
Patrice Chotardd29531c2023-10-27 16:43:04 +020010#include <fdt_support.h>
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010011#include <log.h>
Patrick Delaunay300cb922024-01-15 15:05:55 +010012#include <misc.h>
Patrice Chotard644590f2025-04-22 17:33:42 +020013#include <mmc.h>
Patrice Chotardd29531c2023-10-27 16:43:04 +020014#include <asm/global_data.h>
15#include <asm/arch/sys_proto.h>
Patrick Delaunay300cb922024-01-15 15:05:55 +010016#include <dm/device.h>
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010017#include <dm/ofnode.h>
Patrick Delaunay300cb922024-01-15 15:05:55 +010018#include <dm/uclass.h>
Patrice Chotardd29531c2023-10-27 16:43:04 +020019
20/*
21 * Get a global data pointer
22 */
23DECLARE_GLOBAL_DATA_PTR;
24
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010025int checkboard(void)
26{
Patrick Delaunay300cb922024-01-15 15:05:55 +010027 int ret;
28 u32 otp;
29 struct udevice *dev;
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010030 const char *fdt_compat;
31 int fdt_compat_len;
32
33 fdt_compat = ofnode_get_property(ofnode_root(), "compatible", &fdt_compat_len);
34
35 log_info("Board: stm32mp2 (%s)\n", fdt_compat && fdt_compat_len ? fdt_compat : "");
36
Patrick Delaunay300cb922024-01-15 15:05:55 +010037 /* display the STMicroelectronics board identification */
38 if (CONFIG_IS_ENABLED(CMD_STBOARD)) {
39 ret = uclass_get_device_by_driver(UCLASS_MISC,
40 DM_DRIVER_GET(stm32mp_bsec),
41 &dev);
42 if (!ret)
43 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
44 &otp, sizeof(otp));
45 if (ret > 0 && otp)
46 log_info("Board: MB%04x Var%d.%d Rev.%c-%02d\n",
47 otp >> 16,
48 (otp >> 12) & 0xF,
49 (otp >> 4) & 0xF,
50 ((otp >> 8) & 0xF) - 1 + 'A',
51 otp & 0xF);
52 }
53
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010054 return 0;
55}
56
Patrice Chotardd29531c2023-10-27 16:43:04 +020057/* board dependent setup after realloc */
58int board_init(void)
59{
60 return 0;
61}
62
Patrice Chotard0f77af62025-04-01 18:08:44 +020063enum env_location env_get_location(enum env_operation op, int prio)
64{
65 u32 bootmode = get_bootmode();
66
67 if (prio)
68 return ENVL_UNKNOWN;
69
70 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
71 case BOOT_FLASH_SD:
72 case BOOT_FLASH_EMMC:
73 if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC))
74 return ENVL_MMC;
75 else
76 return ENVL_NOWHERE;
77 default:
78 return ENVL_NOWHERE;
79 }
80}
81
Patrice Chotard644590f2025-04-22 17:33:42 +020082int mmc_get_boot(void)
83{
84 struct udevice *dev;
85 u32 boot_mode = get_bootmode();
86 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
87 char cmd[20];
88 const u32 sdmmc_addr[] = {
89 STM32_SDMMC1_BASE,
90 STM32_SDMMC2_BASE,
91 STM32_SDMMC3_BASE
92 };
93
94 if (instance > ARRAY_SIZE(sdmmc_addr))
95 return 0;
96
97 /* search associated sdmmc node in devicetree */
98 snprintf(cmd, sizeof(cmd), "mmc@%x", sdmmc_addr[instance]);
99 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
100 log_err("mmc%d = %s not found in device tree!\n", instance, cmd);
101 return 0;
102 }
103
104 return dev_seq(dev);
105};
106
107int mmc_get_env_dev(void)
108{
109 const int mmc_env_dev = CONFIG_IS_ENABLED(ENV_IS_IN_MMC, (CONFIG_SYS_MMC_ENV_DEV), (-1));
110
111 if (mmc_env_dev >= 0)
112 return mmc_env_dev;
113
114 /* use boot instance to select the correct mmc device identifier */
115 return mmc_get_boot();
116}
117
Patrice Chotardd29531c2023-10-27 16:43:04 +0200118int board_late_init(void)
119{
120 const void *fdt_compat;
121 int fdt_compat_len;
122 char dtb_name[256];
123 int buf_len;
124
125 if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) {
126 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
127 &fdt_compat_len);
128 if (fdt_compat && fdt_compat_len) {
129 if (strncmp(fdt_compat, "st,", 3) != 0) {
130 env_set("board_name", fdt_compat);
131 } else {
132 env_set("board_name", fdt_compat + 3);
133
134 buf_len = sizeof(dtb_name);
135 strlcpy(dtb_name, fdt_compat + 3, buf_len);
136 buf_len -= strlen(fdt_compat + 3);
137 strlcat(dtb_name, ".dtb", buf_len);
138 env_set("fdtfile", dtb_name);
139 }
140 }
141 }
142
143 return 0;
144}