blob: 6e4f19b16648b3eed43130add51faa031d613d85 [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;
141 default:
142 return ENVL_NOWHERE;
143 }
144}
145
Patrice Chotard644590f2025-04-22 17:33:42 +0200146int mmc_get_boot(void)
147{
148 struct udevice *dev;
149 u32 boot_mode = get_bootmode();
150 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
151 char cmd[20];
152 const u32 sdmmc_addr[] = {
153 STM32_SDMMC1_BASE,
154 STM32_SDMMC2_BASE,
155 STM32_SDMMC3_BASE
156 };
157
158 if (instance > ARRAY_SIZE(sdmmc_addr))
159 return 0;
160
161 /* search associated sdmmc node in devicetree */
162 snprintf(cmd, sizeof(cmd), "mmc@%x", sdmmc_addr[instance]);
163 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
164 log_err("mmc%d = %s not found in device tree!\n", instance, cmd);
165 return 0;
166 }
167
168 return dev_seq(dev);
169};
170
171int mmc_get_env_dev(void)
172{
173 const int mmc_env_dev = CONFIG_IS_ENABLED(ENV_IS_IN_MMC, (CONFIG_SYS_MMC_ENV_DEV), (-1));
174
175 if (mmc_env_dev >= 0)
176 return mmc_env_dev;
177
178 /* use boot instance to select the correct mmc device identifier */
179 return mmc_get_boot();
180}
181
Patrice Chotardd29531c2023-10-27 16:43:04 +0200182int board_late_init(void)
183{
184 const void *fdt_compat;
185 int fdt_compat_len;
186 char dtb_name[256];
187 int buf_len;
188
189 if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) {
190 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
191 &fdt_compat_len);
192 if (fdt_compat && fdt_compat_len) {
193 if (strncmp(fdt_compat, "st,", 3) != 0) {
194 env_set("board_name", fdt_compat);
195 } else {
196 env_set("board_name", fdt_compat + 3);
197
198 buf_len = sizeof(dtb_name);
199 strlcpy(dtb_name, fdt_compat + 3, buf_len);
200 buf_len -= strlen(fdt_compat + 3);
201 strlcat(dtb_name, ".dtb", buf_len);
202 env_set("fdtfile", dtb_name);
203 }
204 }
205 }
206
207 return 0;
208}
Patrick Delaunay3a4fa452022-07-26 19:26:16 +0200209
210void board_quiesce_devices(void)
211{
212 setup_led(LEDST_OFF);
213}