1 // 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
8 #include <button.h>
9 #include <config.h>
10 #include <env_internal.h>
11 #include <fdt_support.h>
12 #include <led.h>
13 #include <log.h>
14 #include <misc.h>
15 #include <mmc.h>
16 #include <asm/global_data.h>
17 #include <asm/io.h>
18 #include <asm/arch/sys_proto.h>
19 #include <dm/device.h>
20 #include <dm/ofnode.h>
21 #include <dm/uclass.h>
22 #include <linux/delay.h>
23
24 /*
25 * Get a global data pointer
26 */
27 DECLARE_GLOBAL_DATA_PTR;
28
checkboard(void)29 int checkboard(void)
30 {
31 int ret;
32 u32 otp;
33 struct udevice *dev;
34 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
41 /* 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
58 return 0;
59 }
60
get_led(struct udevice ** dev,char * led_string)61 static 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
setup_led(enum led_state_t cmd)80 static 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
check_user_button(void)96 static 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
118 /* board dependent setup after realloc */
board_init(void)119 int board_init(void)
120 {
121 setup_led(LEDST_ON);
122 check_user_button();
123
124 return 0;
125 }
126
env_get_location(enum env_operation op,int prio)127 enum 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
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;
154 default:
155 return ENVL_NOWHERE;
156 }
157 }
158
mmc_get_boot(void)159 int 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
mmc_get_env_dev(void)184 int mmc_get_env_dev(void)
185 {
186 const int mmc_env_dev = CONFIG_IS_ENABLED(ENV_IS_IN_MMC, (CONFIG_ENV_MMC_DEVICE_INDEX), (-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
board_late_init(void)195 int 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 }
222
board_quiesce_devices(void)223 void board_quiesce_devices(void)
224 {
225 setup_led(LEDST_OFF);
226 }
227