blob: db15d78237ea20556e04246989214b4e896b7d5e [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +01004 */
Patrick Delaunayf7b2a842020-11-06 19:01:59 +01005
6#define LOG_CATEGORY LOGC_BOARD
7
Patrice Chotard879cde52019-02-12 16:50:40 +01008#include <common.h>
9#include <adc.h>
Patrick Delaunay500401f2019-06-21 15:26:40 +020010#include <bootm.h>
Patrice Chotard682ffd62024-04-09 17:01:56 +020011#include <button.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020012#include <clk.h>
Patrick Delaunay266bf102019-07-30 19:16:44 +020013#include <config.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020014#include <dm.h>
Sughosh Ganue166b5e2022-10-21 18:15:58 +053015#include <efi_loader.h>
Simon Glassdb229612019-08-01 09:46:42 -060016#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060017#include <env_internal.h>
Patrick Delaunay028fddd2021-11-15 16:32:23 +010018#include <fdt_simplefb.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060019#include <fdt_support.h>
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +010020#include <g_dnl.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020021#include <generic-phy.h>
Simon Glassf11478f2019-12-28 10:45:07 -070022#include <hang.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010023#include <i2c.h>
Simon Glassa7b51302019-11-14 12:57:46 -070024#include <init.h>
Patrick Delaunay92dc1022019-02-12 11:44:41 +010025#include <led.h>
Simon Glass0f2af882020-05-10 11:40:05 -060026#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070027#include <malloc.h>
Patrick Delaunay92dc1022019-02-12 11:44:41 +010028#include <misc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060029#include <net.h>
Patrick Delaunaybff66f92019-08-01 11:29:03 +020030#include <netdev.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020031#include <phy.h>
Patrick Delaunayc17d7252019-08-02 15:07:20 +020032#include <remoteproc.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020033#include <reset.h>
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010034#include <syscon.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010035#include <usb.h>
Patrick Delaunayae0931d02019-07-30 19:16:39 +020036#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060037#include <asm/global_data.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020038#include <asm/io.h>
Patrick Delaunayf2a7b872019-02-27 17:01:18 +010039#include <asm/gpio.h>
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010040#include <asm/arch/stm32.h>
Patrice Chotarddad97bf2019-05-02 18:36:01 +020041#include <asm/arch/sys_proto.h>
Patrice Chotard682ffd62024-04-09 17:01:56 +020042#include <dm/device-internal.h>
Simon Glass0034d962021-08-07 07:24:01 -060043#include <dm/ofnode.h>
Patrick Delaunayde98cbf2019-07-02 13:26:07 +020044#include <jffs2/load_kernel.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060045#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060046#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070047#include <linux/err.h>
Patrick Delaunay181298e2020-04-22 14:29:16 +020048#include <linux/iopoll.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060049#include <linux/printk.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020050#include <power/regulator.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010051#include <usb/dwc2_udc.h>
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +010052
Patrick Delaunayfcb6b0b2020-06-29 10:34:06 +020053#include "../../st/common/stusb160x.h"
54
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010055/* SYSCFG registers */
56#define SYSCFG_BOOTR 0x00
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010057#define SYSCFG_IOCTRLSETR 0x18
58#define SYSCFG_ICNR 0x1C
59#define SYSCFG_CMPCR 0x20
60#define SYSCFG_CMPENSETR 0x24
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010061
62#define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
63#define SYSCFG_BOOTR_BOOTPD_SHIFT 4
64
65#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
66#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
67#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
68#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
69#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
70
71#define SYSCFG_CMPCR_SW_CTRL BIT(1)
72#define SYSCFG_CMPCR_READY BIT(8)
73
74#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
75
Patrice Chotardcded32f2019-04-30 18:09:38 +020076#define USB_LOW_THRESHOLD_UV 200000
Patrice Chotard879cde52019-02-12 16:50:40 +010077#define USB_WARNING_LOW_THRESHOLD_UV 660000
78#define USB_START_LOW_THRESHOLD_UV 1230000
Patrice Chotardcded32f2019-04-30 18:09:38 +020079#define USB_START_HIGH_THRESHOLD_UV 2150000
Patrice Chotard879cde52019-02-12 16:50:40 +010080
Simon Glassb8196212023-02-05 15:39:42 -070081#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
Sughosh Ganue166b5e2022-10-21 18:15:58 +053082struct efi_fw_image fw_images[1];
83
84struct efi_capsule_update_info update_info = {
Masahisa Kojima5d2438b2023-06-07 14:41:51 +090085 .num_images = ARRAY_SIZE(fw_images),
Sughosh Ganue166b5e2022-10-21 18:15:58 +053086 .images = fw_images,
87};
88
Sughosh Ganue166b5e2022-10-21 18:15:58 +053089#endif /* EFI_HAVE_CAPSULE_SUPPORT */
90
Patrick Delaunayf2f25c32020-05-25 12:19:46 +020091int board_early_init_f(void)
92{
93 /* nothing to do, only used in SPL */
94 return 0;
95}
96
Patrick Delaunay92dc1022019-02-12 11:44:41 +010097int checkboard(void)
98{
99 int ret;
100 char *mode;
101 u32 otp;
102 struct udevice *dev;
103 const char *fdt_compat;
104 int fdt_compat_len;
105
Patrick Delaunayba4b8b02021-07-26 11:21:34 +0200106 if (IS_ENABLED(CONFIG_TFABOOT)) {
Patrick Delaunay4c6fcbc2024-01-15 15:05:57 +0100107 if (IS_ENABLED(CONFIG_STM32MP15X_STM32IMAGE))
Patrick Delaunayba4b8b02021-07-26 11:21:34 +0200108 mode = "trusted - stm32image";
109 else
110 mode = "trusted";
111 } else {
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100112 mode = "basic";
Patrick Delaunayba4b8b02021-07-26 11:21:34 +0200113 }
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100114
Patrick Delaunay4e506b02022-05-19 09:07:29 +0200115 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
116 &fdt_compat_len);
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100117
118 log_info("Board: stm32mp1 in %s mode (%s)\n", mode,
119 fdt_compat && fdt_compat_len ? fdt_compat : "");
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100120
Patrick Delaunay52163d92020-03-24 09:05:00 +0100121 /* display the STMicroelectronics board identification */
Simon Glassd87b83e2023-02-05 15:36:43 -0700122 if (IS_ENABLED(CONFIG_CMD_STBOARD)) {
Patrick Delaunay0885c232020-02-12 19:37:42 +0100123 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700124 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunay0885c232020-02-12 19:37:42 +0100125 &dev);
126 if (!ret)
127 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
128 &otp, sizeof(otp));
129 if (ret > 0 && otp)
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100130 log_info("Board: MB%04x Var%d.%d Rev.%c-%02d\n",
131 otp >> 16,
132 (otp >> 12) & 0xF,
133 (otp >> 4) & 0xF,
134 ((otp >> 8) & 0xF) - 1 + 'A',
135 otp & 0xF);
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100136 }
137
138 return 0;
139}
140
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100141static void board_key_check(void)
142{
Patrice Chotard682ffd62024-04-09 17:01:56 +0200143 struct udevice *button1 = NULL, *button2 = NULL;
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100144 enum forced_boot_mode boot_mode = BOOT_NORMAL;
Patrice Chotard682ffd62024-04-09 17:01:56 +0200145 int ret;
146
147 if (!IS_ENABLED(CONFIG_BUTTON))
148 return;
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100149
Patrick Delaunayf6604852020-07-31 16:31:42 +0200150 if (!IS_ENABLED(CONFIG_FASTBOOT) && !IS_ENABLED(CONFIG_CMD_STM32PROG))
151 return;
152
Patrice Chotard682ffd62024-04-09 17:01:56 +0200153 if (IS_ENABLED(CONFIG_CMD_STM32PROG))
154 button_get_by_label("User-1", &button1);
155
156 if (IS_ENABLED(CONFIG_FASTBOOT))
157 button_get_by_label("User-2", &button2);
158
159 if (!button1 && !button2)
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100160 return;
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100161
Patrice Chotard682ffd62024-04-09 17:01:56 +0200162 if (button2) {
163 if (button_get_state(button2) == BUTTON_ON) {
164 log_notice("Fastboot key pressed, ");
165 boot_mode = BOOT_FASTBOOT;
Patrick Delaunayf6604852020-07-31 16:31:42 +0200166 }
Patrice Chotard682ffd62024-04-09 17:01:56 +0200167 /*
168 * On some boards, same gpio is shared betwwen gpio-keys and
169 * leds, remove the button device to free the gpio for led
170 * usage
171 */
172 ret = device_remove(button2, DM_REMOVE_NORMAL);
173 if (ret)
174 log_err("Can't remove button2 (%d)\n", ret);
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100175 }
Patrice Chotard682ffd62024-04-09 17:01:56 +0200176
177 if (button1) {
178 if (button_get_state(button1) == BUTTON_ON) {
179 log_notice("STM32Programmer key pressed, ");
180 boot_mode = BOOT_STM32PROG;
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100181 }
Patrice Chotard682ffd62024-04-09 17:01:56 +0200182 /*
183 * On some boards, same gpio is shared betwwen gpio-keys and
184 * leds, remove the button device to free the gpio for led
185 * usage
186 */
187 ret = device_remove(button1, DM_REMOVE_NORMAL);
188 if (ret)
189 log_err("Can't remove button1 (%d)\n", ret);
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100190 }
Patrice Chotard682ffd62024-04-09 17:01:56 +0200191
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100192 if (boot_mode != BOOT_NORMAL) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100193 log_notice("entering download mode...\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100194 clrsetbits_le32(TAMP_BOOT_CONTEXT,
195 TAMP_BOOT_FORCED_MASK,
196 boot_mode);
197 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100198}
199
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100200int g_dnl_board_usb_cable_connected(void)
Patrice Chotard204079b2018-08-10 17:12:14 +0200201{
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100202 struct udevice *dwc2_udc_otg;
Patrice Chotard204079b2018-08-10 17:12:14 +0200203 int ret;
204
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200205 if (!IS_ENABLED(CONFIG_USB_GADGET_DWC2_OTG))
206 return -ENODEV;
207
Patrice Chotardaef7c182022-04-22 09:39:18 +0200208 /*
209 * In case of USB boot device is detected, consider USB cable is
210 * connected
211 */
212 if ((get_bootmode() & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_USB)
213 return true;
214
Patrick Delaunayfcb6b0b2020-06-29 10:34:06 +0200215 /* if typec stusb160x is present, means DK1 or DK2 board */
216 ret = stusb160x_cable_connected();
217 if (ret >= 0)
218 return ret;
Patrick Delaunay7f3384d2019-03-29 15:42:24 +0100219
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100220 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700221 DM_DRIVER_GET(dwc2_udc_otg),
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100222 &dwc2_udc_otg);
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100223 if (ret) {
224 log_debug("dwc2_udc_otg init failed\n");
225 return ret;
226 }
Patrice Chotard204079b2018-08-10 17:12:14 +0200227
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100228 return dwc2_udc_B_session_valid(dwc2_udc_otg);
Patrice Chotard879cde52019-02-12 16:50:40 +0100229}
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200230
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200231#ifdef CONFIG_USB_GADGET_DOWNLOAD
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200232#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
233#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
234
235int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
236{
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200237 if (IS_ENABLED(CONFIG_DFU_OVER_USB) &&
238 !strcmp(name, "usb_dnl_dfu"))
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200239 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200240 else if (IS_ENABLED(CONFIG_FASTBOOT) &&
241 !strcmp(name, "usb_dnl_fastboot"))
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200242 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
243 &dev->idProduct);
244 else
245 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
246
247 return 0;
248}
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200249#endif /* CONFIG_USB_GADGET_DOWNLOAD */
Patrice Chotard879cde52019-02-12 16:50:40 +0100250
251static int get_led(struct udevice **dev, char *led_string)
252{
Simon Glass0034d962021-08-07 07:24:01 -0600253 const char *led_name;
Patrice Chotard879cde52019-02-12 16:50:40 +0100254 int ret;
255
Simon Glass0034d962021-08-07 07:24:01 -0600256 led_name = ofnode_conf_read_str(led_string);
Patrice Chotard879cde52019-02-12 16:50:40 +0100257 if (!led_name) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100258 log_debug("could not find %s config string\n", led_string);
Patrice Chotard879cde52019-02-12 16:50:40 +0100259 return -ENOENT;
260 }
261 ret = led_get_by_label(led_name, dev);
262 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100263 log_debug("get=%d\n", ret);
Patrice Chotard879cde52019-02-12 16:50:40 +0100264 return ret;
265 }
266
267 return 0;
268}
269
270static int setup_led(enum led_state_t cmd)
271{
272 struct udevice *dev;
273 int ret;
274
Patrick Delaunay8ae05cf2020-04-22 14:29:12 +0200275 if (!CONFIG_IS_ENABLED(LED))
276 return 0;
277
Patrice Chotard879cde52019-02-12 16:50:40 +0100278 ret = get_led(&dev, "u-boot,boot-led");
279 if (ret)
280 return ret;
281
282 ret = led_set_state(dev, cmd);
Patrice Chotard204079b2018-08-10 17:12:14 +0200283 return ret;
284}
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200285
286static void __maybe_unused led_error_blink(u32 nb_blink)
287{
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200288 int ret;
289 struct udevice *led;
290 u32 i;
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200291
292 if (!nb_blink)
293 return;
294
Patrick Delaunay8ae05cf2020-04-22 14:29:12 +0200295 if (CONFIG_IS_ENABLED(LED)) {
296 ret = get_led(&led, "u-boot,error-led");
297 if (!ret) {
298 /* make u-boot,error-led blinking */
299 /* if U32_MAX and 125ms interval, for 17.02 years */
300 for (i = 0; i < 2 * nb_blink; i++) {
301 led_set_state(led, LEDST_TOGGLE);
302 mdelay(125);
Stefan Roese80877fa2022-09-02 14:10:46 +0200303 schedule();
Patrick Delaunay8ae05cf2020-04-22 14:29:12 +0200304 }
305 led_set_state(led, LEDST_ON);
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200306 }
307 }
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200308
309 /* infinite: the boot process must be stopped */
310 if (nb_blink == U32_MAX)
311 hang();
312}
Patrice Chotard204079b2018-08-10 17:12:14 +0200313
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200314static int adc_measurement(ofnode node, int adc_count, int *min_uV, int *max_uV)
Patrice Chotard879cde52019-02-12 16:50:40 +0100315{
316 struct ofnode_phandle_args adc_args;
317 struct udevice *adc;
Patrice Chotard879cde52019-02-12 16:50:40 +0100318 unsigned int raw;
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200319 int ret, uV;
320 int i;
Patrice Chotard879cde52019-02-12 16:50:40 +0100321
322 for (i = 0; i < adc_count; i++) {
323 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
324 "#io-channel-cells", 0, i,
325 &adc_args)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100326 log_debug("can't find /config/st,adc_usb_pd\n");
Patrice Chotard879cde52019-02-12 16:50:40 +0100327 return 0;
328 }
329
330 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
331 &adc);
332
333 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100334 log_err("Can't get adc device(%d)\n", ret);
Patrice Chotard879cde52019-02-12 16:50:40 +0100335 return ret;
336 }
337
338 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
339 &raw);
340 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100341 log_err("single shot failed for %s[%d]!\n",
342 adc->name, adc_args.args[0]);
Patrice Chotard879cde52019-02-12 16:50:40 +0100343 return ret;
344 }
345 /* Convert to uV */
346 if (!adc_raw_to_uV(adc, raw, &uV)) {
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200347 if (uV > *max_uV)
348 *max_uV = uV;
349 if (uV < *min_uV)
350 *min_uV = uV;
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100351 log_debug("%s[%02d] = %u, %d uV\n",
352 adc->name, adc_args.args[0], raw, uV);
Patrice Chotard879cde52019-02-12 16:50:40 +0100353 } else {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100354 log_err("Can't get uV value for %s[%d]\n",
355 adc->name, adc_args.args[0]);
Patrice Chotard879cde52019-02-12 16:50:40 +0100356 }
357 }
358
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200359 return 0;
360}
361
362static int board_check_usb_power(void)
363{
364 ofnode node;
365 int max_uV = 0;
366 int min_uV = USB_START_HIGH_THRESHOLD_UV;
367 int adc_count, ret;
368 u32 nb_blink;
369 u8 i;
370
Patrick Delaunayf1c46652021-04-06 09:57:54 +0200371 if (!IS_ENABLED(CONFIG_ADC))
372 return -ENODEV;
373
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200374 node = ofnode_path("/config");
375 if (!ofnode_valid(node)) {
376 log_debug("no /config node?\n");
377 return -ENOENT;
378 }
379
Patrice Chotard879cde52019-02-12 16:50:40 +0100380 /*
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200381 * Retrieve the ADC channels devices and get measurement
382 * for each of them
Patrice Chotard879cde52019-02-12 16:50:40 +0100383 */
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200384 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
385 "#io-channel-cells", 0);
386 if (adc_count < 0) {
387 if (adc_count == -ENOENT)
388 return 0;
Patrice Chotard879cde52019-02-12 16:50:40 +0100389
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200390 log_err("Can't find adc channel (%d)\n", adc_count);
391
392 return adc_count;
393 }
Patrice Chotardcded32f2019-04-30 18:09:38 +0200394
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200395 /* perform maximum of 2 ADC measurements to detect power supply current */
396 for (i = 0; i < 2; i++) {
Patrick Delaunayf1c46652021-04-06 09:57:54 +0200397 ret = adc_measurement(node, adc_count, &min_uV, &max_uV);
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200398 if (ret)
399 return ret;
400
401 /*
402 * If highest value is inside 1.23 Volts and 2.10 Volts, that means
403 * board is plugged on an USB-C 3A power supply and boot process can
404 * continue.
405 */
406 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
407 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
408 min_uV <= USB_LOW_THRESHOLD_UV)
409 return 0;
410
411 if (i == 0) {
412 log_err("Previous ADC measurements was not the one expected, retry in 20ms\n");
413 mdelay(20); /* equal to max tPDDebounce duration (min 10ms - max 20ms) */
414 }
415 }
416
417 log_notice("****************************************************\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200418 /*
419 * If highest and lowest value are either both below
420 * USB_LOW_THRESHOLD_UV or both above USB_LOW_THRESHOLD_UV, that
421 * means USB TYPE-C is in unattached mode, this is an issue, make
422 * u-boot,error-led blinking and stop boot process.
423 */
424 if ((max_uV > USB_LOW_THRESHOLD_UV &&
425 min_uV > USB_LOW_THRESHOLD_UV) ||
426 (max_uV <= USB_LOW_THRESHOLD_UV &&
427 min_uV <= USB_LOW_THRESHOLD_UV)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100428 log_notice("* ERROR USB TYPE-C connection in unattached mode *\n");
429 log_notice("* Check that USB TYPE-C cable is correctly plugged *\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200430 /* with 125ms interval, led will blink for 17.02 years ....*/
431 nb_blink = U32_MAX;
432 }
Patrice Chotard879cde52019-02-12 16:50:40 +0100433
Patrice Chotardcded32f2019-04-30 18:09:38 +0200434 if (max_uV > USB_LOW_THRESHOLD_UV &&
435 max_uV <= USB_WARNING_LOW_THRESHOLD_UV &&
436 min_uV <= USB_LOW_THRESHOLD_UV) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100437 log_notice("* WARNING 500mA power supply detected *\n");
Patrice Chotard879cde52019-02-12 16:50:40 +0100438 nb_blink = 2;
Patrice Chotardcded32f2019-04-30 18:09:38 +0200439 }
440
441 if (max_uV > USB_WARNING_LOW_THRESHOLD_UV &&
442 max_uV <= USB_START_LOW_THRESHOLD_UV &&
443 min_uV <= USB_LOW_THRESHOLD_UV) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100444 log_notice("* WARNING 1.5A power supply detected *\n");
Patrice Chotard879cde52019-02-12 16:50:40 +0100445 nb_blink = 3;
446 }
447
Patrice Chotardcded32f2019-04-30 18:09:38 +0200448 /*
449 * If highest value is above 2.15 Volts that means that the USB TypeC
450 * supplies more than 3 Amp, this is not compliant with TypeC specification
451 */
452 if (max_uV > USB_START_HIGH_THRESHOLD_UV) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100453 log_notice("* USB TYPE-C charger not compliant with *\n");
454 log_notice("* specification *\n");
455 log_notice("****************************************************\n\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200456 /* with 125ms interval, led will blink for 17.02 years ....*/
457 nb_blink = U32_MAX;
458 } else {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100459 log_notice("* Current too low, use a 3A power supply! *\n");
460 log_notice("****************************************************\n\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200461 }
Patrice Chotard879cde52019-02-12 16:50:40 +0100462
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200463 led_error_blink(nb_blink);
Patrice Chotard879cde52019-02-12 16:50:40 +0100464
465 return 0;
466}
467
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100468static void sysconf_init(void)
469{
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100470 u8 *syscfg;
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100471 struct udevice *pwr_dev;
472 struct udevice *pwr_reg;
473 struct udevice *dev;
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100474 u32 otp = 0;
Patrick Delaunay181298e2020-04-22 14:29:16 +0200475 int ret;
476 u32 bootr, val;
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100477
478 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
479
480 /* interconnect update : select master using the port 1 */
481 /* LTDC = AXI_M9 */
482 /* GPU = AXI_M8 */
483 /* today information is hardcoded in U-Boot */
484 writel(BIT(9), syscfg + SYSCFG_ICNR);
485
486 /* disable Pull-Down for boot pin connected to VDD */
487 bootr = readl(syscfg + SYSCFG_BOOTR);
488 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
489 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
490 writel(bootr, syscfg + SYSCFG_BOOTR);
491
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100492 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
493 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
494 * The customer will have to disable this for low frequencies
495 * or if AFMUX is selected but the function not used, typically for
496 * TRACE. Otherwise, impact on power consumption.
497 *
498 * WARNING:
499 * enabling High Speed mode while VDD>2.7V
500 * with the OTP product_below_2v5 (OTP 18, BIT 13)
501 * erroneously set to 1 can damage the IC!
502 * => U-Boot set the register only if VDD < 2.7V (in DT)
503 * but this value need to be consistent with board design
504 */
Patrick Delaunay6b2baa02019-07-30 19:16:42 +0200505 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700506 DM_DRIVER_GET(stm32mp_pwr_pmic),
Patrick Delaunay6b2baa02019-07-30 19:16:42 +0200507 &pwr_dev);
Patrick Delaunay7c104822022-06-20 12:36:10 +0200508 if (!ret) {
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100509 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700510 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100511 &dev);
512 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100513 log_err("Can't find stm32mp_bsec driver\n");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100514 return;
515 }
516
517 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
Patrick Delaunayceb82e32019-08-02 13:08:06 +0200518 if (ret > 0)
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100519 otp = otp & BIT(13);
520
Patrick Delaunay6b2baa02019-07-30 19:16:42 +0200521 /* get VDD = vdd-supply */
522 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100523 &pwr_reg);
524
525 /* check if VDD is Low Voltage */
526 if (!ret) {
527 if (regulator_get_value(pwr_reg) < 2700000) {
528 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
529 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
530 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
531 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
532 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
533 syscfg + SYSCFG_IOCTRLSETR);
534
535 if (!otp)
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100536 log_err("product_below_2v5=0: HSLVEN protected by HW\n");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100537 } else {
538 if (otp)
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100539 log_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100540 }
541 } else {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100542 log_debug("VDD unknown");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100543 }
544 }
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100545
546 /* activate automatic I/O compensation
547 * warning: need to ensure CSI enabled and ready in clock driver
548 */
549 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
550
Patrick Delaunay181298e2020-04-22 14:29:16 +0200551 /* poll until ready (1s timeout) */
552 ret = readl_poll_timeout(syscfg + SYSCFG_CMPCR, val,
553 val & SYSCFG_CMPCR_READY,
554 1000000);
555 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100556 log_err("SYSCFG: I/O compensation failed, timeout.\n");
Patrick Delaunay181298e2020-04-22 14:29:16 +0200557 led_error_blink(10);
558 }
559
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100560 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100561}
562
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200563static int board_stm32mp15x_dk2_init(void)
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200564{
565 ofnode node;
566 struct gpio_desc hdmi, audio;
567 int ret = 0;
568
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200569 /* Fix to make I2C1 usable on DK2 for touchscreen usage in kernel */
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200570 node = ofnode_path("/soc/i2c@40012000/hdmi-transmitter@39");
571 if (!ofnode_valid(node)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100572 log_debug("no hdmi-transmitter@39 ?\n");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200573 return -ENOENT;
574 }
575
576 if (gpio_request_by_name_nodev(node, "reset-gpios", 0,
577 &hdmi, GPIOD_IS_OUT)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100578 log_debug("could not find reset-gpios\n");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200579 return -ENOENT;
580 }
581
582 node = ofnode_path("/soc/i2c@40012000/cs42l51@4a");
583 if (!ofnode_valid(node)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100584 log_debug("no cs42l51@4a ?\n");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200585 return -ENOENT;
586 }
587
588 if (gpio_request_by_name_nodev(node, "reset-gpios", 0,
589 &audio, GPIOD_IS_OUT)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100590 log_debug("could not find reset-gpios\n");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200591 return -ENOENT;
592 }
593
594 /* before power up, insure that HDMI and AUDIO IC is under reset */
595 ret = dm_gpio_set_value(&hdmi, 1);
596 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100597 log_err("can't set_value for hdmi_nrst gpio");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200598 goto error;
599 }
600 ret = dm_gpio_set_value(&audio, 1);
601 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100602 log_err("can't set_value for audio_nrst gpio");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200603 goto error;
604 }
605
606 /* power-up audio IC */
607 regulator_autoset_by_name("v1v8_audio", NULL);
608
609 /* power-up HDMI IC */
610 regulator_autoset_by_name("v1v2_hdmi", NULL);
611 regulator_autoset_by_name("v3v3_hdmi", NULL);
612
613error:
614 return ret;
615}
616
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200617static bool board_is_stm32mp15x_dk2(void)
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200618{
Patrick Delaunay4c6fcbc2024-01-15 15:05:57 +0100619 if (CONFIG_IS_ENABLED(TARGET_ST_STM32MP15X) &&
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200620 of_machine_is_compatible("st,stm32mp157c-dk2"))
621 return true;
622
623 return false;
624}
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200625
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200626static bool board_is_stm32mp15x_ev1(void)
Patrick Delaunay486ae962020-04-22 14:29:13 +0200627{
Patrick Delaunay4c6fcbc2024-01-15 15:05:57 +0100628 if (CONFIG_IS_ENABLED(TARGET_ST_STM32MP15X) &&
Patrick Delaunay486ae962020-04-22 14:29:13 +0200629 (of_machine_is_compatible("st,stm32mp157a-ev1") ||
630 of_machine_is_compatible("st,stm32mp157c-ev1") ||
631 of_machine_is_compatible("st,stm32mp157d-ev1") ||
632 of_machine_is_compatible("st,stm32mp157f-ev1")))
633 return true;
634
635 return false;
636}
637
638/* touchscreen driver: only used for pincontrol configuration */
639static const struct udevice_id goodix_ids[] = {
640 { .compatible = "goodix,gt9147", },
641 { }
642};
643
644U_BOOT_DRIVER(goodix) = {
645 .name = "goodix",
646 .id = UCLASS_NOP,
647 .of_match = goodix_ids,
648};
649
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200650static void board_stm32mp15x_ev1_init(void)
Patrick Delaunay486ae962020-04-22 14:29:13 +0200651{
652 struct udevice *dev;
653
654 /* configure IRQ line on EV1 for touchscreen before LCD reset */
Simon Glass65130cd2020-12-28 20:34:56 -0700655 uclass_get_device_by_driver(UCLASS_NOP, DM_DRIVER_GET(goodix), &dev);
Patrick Delaunay486ae962020-04-22 14:29:13 +0200656}
657
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +0100658/* board dependent setup after realloc */
659int board_init(void)
660{
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100661 board_key_check();
662
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200663 if (board_is_stm32mp15x_ev1())
664 board_stm32mp15x_ev1_init();
Patrick Delaunay486ae962020-04-22 14:29:13 +0200665
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200666 if (board_is_stm32mp15x_dk2())
667 board_stm32mp15x_dk2_init();
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200668
Patrick Delaunay7c104822022-06-20 12:36:10 +0200669 regulators_enable_boot_on(_DEBUG);
Patrick Delaunay6519e442019-07-05 17:20:09 +0200670
Patrick Delaunay72a57622021-10-11 09:52:50 +0200671 /*
672 * sysconf initialisation done only when U-Boot is running in secure
673 * done in TF-A for TFABOOT.
674 */
675 if (IS_ENABLED(CONFIG_ARMV7_NONSEC))
Patrick Delaunay4b2bfd62020-07-31 16:31:45 +0200676 sysconf_init();
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100677
Patrick Delaunay8ae05cf2020-04-22 14:29:12 +0200678 setup_led(LEDST_ON);
679
Simon Glassb8196212023-02-05 15:39:42 -0700680#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
Sughosh Ganue166b5e2022-10-21 18:15:58 +0530681 efi_guid_t image_type_guid = STM32MP_FIP_IMAGE_GUID;
682
683 guidcpy(&fw_images[0].image_type_id, &image_type_guid);
684 fw_images[0].fw_name = u"STM32MP-FIP";
685 fw_images[0].image_index = 1;
686#endif
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +0100687 return 0;
688}
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100689
690int board_late_init(void)
691{
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100692 const void *fdt_compat;
693 int fdt_compat_len;
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200694 int ret;
695 u32 otp;
696 struct udevice *dev;
697 char buf[10];
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200698 char dtb_name[256];
699 int buf_len;
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100700
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200701 if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) {
Patrick Delaunay4e506b02022-05-19 09:07:29 +0200702 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
703 &fdt_compat_len);
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200704 if (fdt_compat && fdt_compat_len) {
705 if (strncmp(fdt_compat, "st,", 3) != 0) {
706 env_set("board_name", fdt_compat);
707 } else {
708 env_set("board_name", fdt_compat + 3);
Patrick Delaunay4533a2c2020-04-22 14:29:14 +0200709
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200710 buf_len = sizeof(dtb_name);
711 strncpy(dtb_name, fdt_compat + 3, buf_len);
712 buf_len -= strlen(fdt_compat + 3);
713 strncat(dtb_name, ".dtb", buf_len);
714 env_set("fdtfile", dtb_name);
715 }
Patrick Delaunay4533a2c2020-04-22 14:29:14 +0200716 }
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200717 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700718 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200719 &dev);
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200720
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200721 if (!ret)
722 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
723 &otp, sizeof(otp));
724 if (ret > 0 && otp) {
725 snprintf(buf, sizeof(buf), "0x%04x", otp >> 16);
726 env_set("board_id", buf);
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200727
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200728 snprintf(buf, sizeof(buf), "0x%04x",
729 ((otp >> 8) & 0xF) - 1 + 0xA);
730 env_set("board_rev", buf);
731 }
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200732 }
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100733
Patrice Chotard879cde52019-02-12 16:50:40 +0100734 /* for DK1/DK2 boards */
735 board_check_usb_power();
736
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100737 return 0;
738}
Patrice Chotard879cde52019-02-12 16:50:40 +0100739
740void board_quiesce_devices(void)
741{
742 setup_led(LEDST_OFF);
743}
Patrice Chotard41443cf2019-05-02 18:07:14 +0200744
Patrice Chotard34320372019-05-02 18:28:05 +0200745enum env_location env_get_location(enum env_operation op, int prio)
746{
747 u32 bootmode = get_bootmode();
748
749 if (prio)
750 return ENVL_UNKNOWN;
751
752 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
Patrick Delaunay455b0652020-06-15 11:18:22 +0200753 case BOOT_FLASH_SD:
754 case BOOT_FLASH_EMMC:
Patrick Delaunay821a9ba2020-07-31 16:31:49 +0200755 if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC))
756 return ENVL_MMC;
757 else if (CONFIG_IS_ENABLED(ENV_IS_IN_EXT4))
758 return ENVL_EXT4;
759 else
760 return ENVL_NOWHERE;
761
Patrice Chotard34320372019-05-02 18:28:05 +0200762 case BOOT_FLASH_NAND:
Patrick Delaunayb5a7ca22020-03-18 09:22:52 +0100763 case BOOT_FLASH_SPINAND:
Simon Glassbd3ebf62023-02-05 15:39:48 -0700764 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
Patrick Delaunay821a9ba2020-07-31 16:31:49 +0200765 return ENVL_UBI;
766 else
767 return ENVL_NOWHERE;
768
Patrice Chotard2c461ec2019-05-09 14:25:36 +0200769 case BOOT_FLASH_NOR:
Patrick Delaunay821a9ba2020-07-31 16:31:49 +0200770 if (CONFIG_IS_ENABLED(ENV_IS_IN_SPI_FLASH))
771 return ENVL_SPI_FLASH;
772 else
773 return ENVL_NOWHERE;
774
Patrice Chotard34320372019-05-02 18:28:05 +0200775 default:
776 return ENVL_NOWHERE;
777 }
778}
779
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200780const char *env_ext4_get_intf(void)
781{
782 u32 bootmode = get_bootmode();
783
784 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
785 case BOOT_FLASH_SD:
786 case BOOT_FLASH_EMMC:
787 return "mmc";
788 default:
789 return "";
790 }
791}
792
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200793int mmc_get_boot(void)
794{
795 struct udevice *dev;
796 u32 boot_mode = get_bootmode();
797 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
798 char cmd[20];
799 const u32 sdmmc_addr[] = {
800 STM32_SDMMC1_BASE,
801 STM32_SDMMC2_BASE,
802 STM32_SDMMC3_BASE
803 };
804
Rasmus Villemoes6d83f3c2023-03-24 08:55:19 +0100805 if (instance >= ARRAY_SIZE(sdmmc_addr))
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200806 return 0;
807
808 /* search associated sdmmc node in devicetree */
809 snprintf(cmd, sizeof(cmd), "mmc@%x", sdmmc_addr[instance]);
810 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
811 log_err("mmc%d = %s not found in device tree!\n", instance, cmd);
812 return 0;
813 }
814
815 return dev_seq(dev);
816};
817
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200818const char *env_ext4_get_dev_part(void)
819{
Manuel Reisbe9d3e22020-11-25 10:16:20 +0000820 static char *const env_dev_part =
821#ifdef CONFIG_ENV_EXT4_DEVICE_AND_PART
822 CONFIG_ENV_EXT4_DEVICE_AND_PART;
823#else
824 "";
825#endif
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200826 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
Manuel Reisbe9d3e22020-11-25 10:16:20 +0000827
828 if (strlen(env_dev_part) > 0)
829 return env_dev_part;
830
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200831 return dev_part[mmc_get_boot()];
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200832}
Manuel Reisbe9d3e22020-11-25 10:16:20 +0000833
Patrick Delaunay455b0652020-06-15 11:18:22 +0200834int mmc_get_env_dev(void)
835{
Patrick Delaunay520e2992022-01-11 16:37:21 +0100836 const int mmc_env_dev = CONFIG_IS_ENABLED(ENV_IS_IN_MMC, (CONFIG_SYS_MMC_ENV_DEV), (-1));
837
838 if (mmc_env_dev >= 0)
839 return mmc_env_dev;
Patrick Delaunayb6bc8532021-03-01 13:17:56 +0100840
Patrick Delaunayb6bc8532021-03-01 13:17:56 +0100841 /* use boot instance to select the correct mmc device identifier */
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200842 return mmc_get_boot();
Patrick Delaunay455b0652020-06-15 11:18:22 +0200843}
Patrick Delaunay455b0652020-06-15 11:18:22 +0200844
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200845#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900846int ft_board_setup(void *blob, struct bd_info *bd)
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200847{
Patrick Delaunay67e95d52023-06-08 17:16:44 +0200848 fdt_copy_fixed_partitions(blob);
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200849
Simon Glass703f5bd2023-02-05 15:39:57 -0700850 if (IS_ENABLED(CONFIG_FDT_SIMPLEFB))
Patrick Delaunay028fddd2021-11-15 16:32:23 +0100851 fdt_simplefb_enable_and_mem_rsv(blob);
852
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200853 return 0;
854}
855#endif
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200856
857static void board_copro_image_process(ulong fw_image, size_t fw_size)
858{
859 int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
860
861 if (!rproc_is_initialized())
862 if (rproc_init()) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100863 log_err("Remote Processor %d initialization failed\n",
864 id);
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200865 return;
866 }
867
868 ret = rproc_load(id, fw_image, fw_size);
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100869 log_err("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
870 id, fw_image, fw_size, ret ? " Failed!" : " Success!");
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200871
Fabien Dessennead6cc942019-10-30 14:38:32 +0100872 if (!ret)
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200873 rproc_start(id);
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200874}
875
876U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);
Sughosh Ganu73abe8e2022-10-21 18:16:00 +0530877
878#if defined(CONFIG_FWU_MULTI_BANK_UPDATE)
879
880#include <fwu.h>
881
882/**
883 * fwu_plat_get_bootidx() - Get the value of the boot index
884 * @boot_idx: Boot index value
885 *
886 * Get the value of the bank(partition) from which the platform
887 * has booted. This value is passed to U-Boot from the earlier
888 * stage bootloader which loads and boots all the relevant
889 * firmware images
890 *
891 */
892void fwu_plat_get_bootidx(uint *boot_idx)
893{
894 *boot_idx = (readl(TAMP_FWU_BOOT_INFO_REG) >>
895 TAMP_FWU_BOOT_IDX_OFFSET) & TAMP_FWU_BOOT_IDX_MASK;
896}
897#endif /* CONFIG_FWU_MULTI_BANK_UPDATE */