blob: 576a958c7832a722d8ecdf812905bd1dee6c64fe [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 Delaunay3a4fa452022-07-26 19:26:16 +020011#include <led.h>
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010012#include <log.h>
Patrick Delaunay300cb922024-01-15 15:05:55 +010013#include <misc.h>
Patrice Chotard644590f2025-04-22 17:33:42 +020014#include <mmc.h>
Patrice Chotardd29531c2023-10-27 16:43:04 +020015#include <asm/global_data.h>
16#include <asm/arch/sys_proto.h>
Patrick Delaunay300cb922024-01-15 15:05:55 +010017#include <dm/device.h>
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010018#include <dm/ofnode.h>
Patrick Delaunay300cb922024-01-15 15:05:55 +010019#include <dm/uclass.h>
Patrice Chotardd29531c2023-10-27 16:43:04 +020020
21/*
22 * Get a global data pointer
23 */
24DECLARE_GLOBAL_DATA_PTR;
25
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010026int checkboard(void)
27{
Patrick Delaunay300cb922024-01-15 15:05:55 +010028 int ret;
29 u32 otp;
30 struct udevice *dev;
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010031 const char *fdt_compat;
32 int fdt_compat_len;
33
34 fdt_compat = ofnode_get_property(ofnode_root(), "compatible", &fdt_compat_len);
35
36 log_info("Board: stm32mp2 (%s)\n", fdt_compat && fdt_compat_len ? fdt_compat : "");
37
Patrick Delaunay300cb922024-01-15 15:05:55 +010038 /* display the STMicroelectronics board identification */
39 if (CONFIG_IS_ENABLED(CMD_STBOARD)) {
40 ret = uclass_get_device_by_driver(UCLASS_MISC,
41 DM_DRIVER_GET(stm32mp_bsec),
42 &dev);
43 if (!ret)
44 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
45 &otp, sizeof(otp));
46 if (ret > 0 && otp)
47 log_info("Board: MB%04x Var%d.%d Rev.%c-%02d\n",
48 otp >> 16,
49 (otp >> 12) & 0xF,
50 (otp >> 4) & 0xF,
51 ((otp >> 8) & 0xF) - 1 + 'A',
52 otp & 0xF);
53 }
54
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010055 return 0;
56}
57
Patrick Delaunay3a4fa452022-07-26 19:26:16 +020058static int get_led(struct udevice **dev, char *led_string)
59{
60 const char *led_name;
61 int ret;
62
63 led_name = ofnode_conf_read_str(led_string);
64 if (!led_name) {
65 log_debug("could not find %s config string\n", led_string);
66 return -ENOENT;
67 }
68 ret = led_get_by_label(led_name, dev);
69 if (ret) {
70 log_debug("get=%d\n", ret);
71 return ret;
72 }
73
74 return 0;
75}
76
77static int setup_led(enum led_state_t cmd)
78{
79 struct udevice *dev;
80 int ret;
81
82 if (!CONFIG_IS_ENABLED(LED))
83 return 0;
84
85 ret = get_led(&dev, "u-boot,boot-led");
86 if (ret)
87 return ret;
88
89 ret = led_set_state(dev, cmd);
90 return ret;
91}
92
Patrice Chotardd29531c2023-10-27 16:43:04 +020093/* board dependent setup after realloc */
94int board_init(void)
95{
Patrick Delaunay3a4fa452022-07-26 19:26:16 +020096 setup_led(LEDST_ON);
97
Patrice Chotardd29531c2023-10-27 16:43:04 +020098 return 0;
99}
100
Patrice Chotard0f77af62025-04-01 18:08:44 +0200101enum env_location env_get_location(enum env_operation op, int prio)
102{
103 u32 bootmode = get_bootmode();
104
105 if (prio)
106 return ENVL_UNKNOWN;
107
108 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
109 case BOOT_FLASH_SD:
110 case BOOT_FLASH_EMMC:
111 if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC))
112 return ENVL_MMC;
113 else
114 return ENVL_NOWHERE;
115 default:
116 return ENVL_NOWHERE;
117 }
118}
119
Patrice Chotard644590f2025-04-22 17:33:42 +0200120int mmc_get_boot(void)
121{
122 struct udevice *dev;
123 u32 boot_mode = get_bootmode();
124 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
125 char cmd[20];
126 const u32 sdmmc_addr[] = {
127 STM32_SDMMC1_BASE,
128 STM32_SDMMC2_BASE,
129 STM32_SDMMC3_BASE
130 };
131
132 if (instance > ARRAY_SIZE(sdmmc_addr))
133 return 0;
134
135 /* search associated sdmmc node in devicetree */
136 snprintf(cmd, sizeof(cmd), "mmc@%x", sdmmc_addr[instance]);
137 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
138 log_err("mmc%d = %s not found in device tree!\n", instance, cmd);
139 return 0;
140 }
141
142 return dev_seq(dev);
143};
144
145int mmc_get_env_dev(void)
146{
147 const int mmc_env_dev = CONFIG_IS_ENABLED(ENV_IS_IN_MMC, (CONFIG_SYS_MMC_ENV_DEV), (-1));
148
149 if (mmc_env_dev >= 0)
150 return mmc_env_dev;
151
152 /* use boot instance to select the correct mmc device identifier */
153 return mmc_get_boot();
154}
155
Patrice Chotardd29531c2023-10-27 16:43:04 +0200156int board_late_init(void)
157{
158 const void *fdt_compat;
159 int fdt_compat_len;
160 char dtb_name[256];
161 int buf_len;
162
163 if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) {
164 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
165 &fdt_compat_len);
166 if (fdt_compat && fdt_compat_len) {
167 if (strncmp(fdt_compat, "st,", 3) != 0) {
168 env_set("board_name", fdt_compat);
169 } else {
170 env_set("board_name", fdt_compat + 3);
171
172 buf_len = sizeof(dtb_name);
173 strlcpy(dtb_name, fdt_compat + 3, buf_len);
174 buf_len -= strlen(fdt_compat + 3);
175 strlcat(dtb_name, ".dtb", buf_len);
176 env_set("fdtfile", dtb_name);
177 }
178 }
179 }
180
181 return 0;
182}
Patrick Delaunay3a4fa452022-07-26 19:26:16 +0200183
184void board_quiesce_devices(void)
185{
186 setup_led(LEDST_OFF);
187}