blob: 2547f2e4bb77fdf28f5c475505d79c2182765ed7 [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
Patrick Delaunayd736e402022-07-27 10:38:11 +02008#include <button.h>
Patrice Chotardd29531c2023-10-27 16:43:04 +02009#include <config.h>
Patrice Chotard0f77af62025-04-01 18:08:44 +020010#include <env_internal.h>
Patrice Chotardd29531c2023-10-27 16:43:04 +020011#include <fdt_support.h>
Patrick Delaunay3a4fa452022-07-26 19:26:16 +020012#include <led.h>
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010013#include <log.h>
Patrick Delaunay300cb922024-01-15 15:05:55 +010014#include <misc.h>
Patrice Chotard644590f2025-04-22 17:33:42 +020015#include <mmc.h>
Patrice Chotardd29531c2023-10-27 16:43:04 +020016#include <asm/global_data.h>
Patrick Delaunayd736e402022-07-27 10:38:11 +020017#include <asm/io.h>
Patrice Chotardd29531c2023-10-27 16:43:04 +020018#include <asm/arch/sys_proto.h>
Patrick Delaunay300cb922024-01-15 15:05:55 +010019#include <dm/device.h>
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010020#include <dm/ofnode.h>
Patrick Delaunay300cb922024-01-15 15:05:55 +010021#include <dm/uclass.h>
Patrick Delaunayd736e402022-07-27 10:38:11 +020022#include <linux/delay.h>
Patrice Chotardd29531c2023-10-27 16:43:04 +020023
24/*
25 * Get a global data pointer
26 */
27DECLARE_GLOBAL_DATA_PTR;
28
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010029int checkboard(void)
30{
Patrick Delaunay300cb922024-01-15 15:05:55 +010031 int ret;
32 u32 otp;
33 struct udevice *dev;
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010034 const char *fdt_compat;
35 int fdt_compat_len;
36
37 fdt_compat = ofnode_get_property(ofnode_root(), "compatible", &fdt_compat_len);
38
39 log_info("Board: stm32mp2 (%s)\n", fdt_compat && fdt_compat_len ? fdt_compat : "");
40
Patrick Delaunay300cb922024-01-15 15:05:55 +010041 /* display the STMicroelectronics board identification */
42 if (CONFIG_IS_ENABLED(CMD_STBOARD)) {
43 ret = uclass_get_device_by_driver(UCLASS_MISC,
44 DM_DRIVER_GET(stm32mp_bsec),
45 &dev);
46 if (!ret)
47 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
48 &otp, sizeof(otp));
49 if (ret > 0 && otp)
50 log_info("Board: MB%04x Var%d.%d Rev.%c-%02d\n",
51 otp >> 16,
52 (otp >> 12) & 0xF,
53 (otp >> 4) & 0xF,
54 ((otp >> 8) & 0xF) - 1 + 'A',
55 otp & 0xF);
56 }
57
Patrick Delaunayd7b448f2024-01-15 15:05:54 +010058 return 0;
59}
60
Patrick Delaunay3a4fa452022-07-26 19:26:16 +020061static int get_led(struct udevice **dev, char *led_string)
62{
63 const char *led_name;
64 int ret;
65
66 led_name = ofnode_conf_read_str(led_string);
67 if (!led_name) {
68 log_debug("could not find %s config string\n", led_string);
69 return -ENOENT;
70 }
71 ret = led_get_by_label(led_name, dev);
72 if (ret) {
73 log_debug("get=%d\n", ret);
74 return ret;
75 }
76
77 return 0;
78}
79
80static int setup_led(enum led_state_t cmd)
81{
82 struct udevice *dev;
83 int ret;
84
85 if (!CONFIG_IS_ENABLED(LED))
86 return 0;
87
88 ret = get_led(&dev, "u-boot,boot-led");
89 if (ret)
90 return ret;
91
92 ret = led_set_state(dev, cmd);
93 return ret;
94}
95
Patrick Delaunayd736e402022-07-27 10:38:11 +020096static void check_user_button(void)
97{
98 struct udevice *button;
99 int i;
100
101 if (!IS_ENABLED(CONFIG_CMD_STM32PROG) || !IS_ENABLED(CONFIG_BUTTON))
102 return;
103
104 if (button_get_by_label("User-2", &button))
105 return;
106
107 for (i = 0; i < 21; ++i) {
108 if (button_get_state(button) != BUTTON_ON)
109 return;
110 if (i < 20)
111 mdelay(50);
112 }
113
114 log_notice("entering download mode...\n");
115 clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_STM32PROG);
116}
117
Patrice Chotardd29531c2023-10-27 16:43:04 +0200118/* board dependent setup after realloc */
119int board_init(void)
120{
Patrick Delaunay3a4fa452022-07-26 19:26:16 +0200121 setup_led(LEDST_ON);
Patrick Delaunayd736e402022-07-27 10:38:11 +0200122 check_user_button();
Patrick Delaunay3a4fa452022-07-26 19:26:16 +0200123
Patrice Chotardd29531c2023-10-27 16:43:04 +0200124 return 0;
125}
126
Patrice Chotard0f77af62025-04-01 18:08:44 +0200127enum env_location env_get_location(enum env_operation op, int prio)
128{
129 u32 bootmode = get_bootmode();
130
131 if (prio)
132 return ENVL_UNKNOWN;
133
134 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
135 case BOOT_FLASH_SD:
136 case BOOT_FLASH_EMMC:
137 if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC))
138 return ENVL_MMC;
139 else
140 return ENVL_NOWHERE;
Patrice Chotard703d9b52025-04-01 18:08:44 +0200141
142 case BOOT_FLASH_NAND:
143 case BOOT_FLASH_SPINAND:
144 if (CONFIG_IS_ENABLED(ENV_IS_IN_UBI))
145 return ENVL_UBI;
146 else
147 return ENVL_NOWHERE;
148
149 case BOOT_FLASH_NOR:
150 if (CONFIG_IS_ENABLED(ENV_IS_IN_SPI_FLASH))
151 return ENVL_SPI_FLASH;
152 else
153 return ENVL_NOWHERE;
Patrice Chotard0f77af62025-04-01 18:08:44 +0200154 default:
155 return ENVL_NOWHERE;
156 }
157}
158
Patrice Chotard644590f2025-04-22 17:33:42 +0200159int mmc_get_boot(void)
160{
161 struct udevice *dev;
162 u32 boot_mode = get_bootmode();
163 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
164 char cmd[20];
165 const u32 sdmmc_addr[] = {
166 STM32_SDMMC1_BASE,
167 STM32_SDMMC2_BASE,
168 STM32_SDMMC3_BASE
169 };
170
171 if (instance > ARRAY_SIZE(sdmmc_addr))
172 return 0;
173
174 /* search associated sdmmc node in devicetree */
175 snprintf(cmd, sizeof(cmd), "mmc@%x", sdmmc_addr[instance]);
176 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
177 log_err("mmc%d = %s not found in device tree!\n", instance, cmd);
178 return 0;
179 }
180
181 return dev_seq(dev);
182};
183
184int mmc_get_env_dev(void)
185{
186 const int mmc_env_dev = CONFIG_IS_ENABLED(ENV_IS_IN_MMC, (CONFIG_SYS_MMC_ENV_DEV), (-1));
187
188 if (mmc_env_dev >= 0)
189 return mmc_env_dev;
190
191 /* use boot instance to select the correct mmc device identifier */
192 return mmc_get_boot();
193}
194
Patrice Chotardd29531c2023-10-27 16:43:04 +0200195int board_late_init(void)
196{
197 const void *fdt_compat;
198 int fdt_compat_len;
199 char dtb_name[256];
200 int buf_len;
201
202 if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) {
203 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
204 &fdt_compat_len);
205 if (fdt_compat && fdt_compat_len) {
206 if (strncmp(fdt_compat, "st,", 3) != 0) {
207 env_set("board_name", fdt_compat);
208 } else {
209 env_set("board_name", fdt_compat + 3);
210
211 buf_len = sizeof(dtb_name);
212 strlcpy(dtb_name, fdt_compat + 3, buf_len);
213 buf_len -= strlen(fdt_compat + 3);
214 strlcat(dtb_name, ".dtb", buf_len);
215 env_set("fdtfile", dtb_name);
216 }
217 }
218 }
219
220 return 0;
221}
Patrick Delaunay3a4fa452022-07-26 19:26:16 +0200222
223void board_quiesce_devices(void)
224{
225 setup_led(LEDST_OFF);
226}