blob: f284b0dfd2810bdcd925d548e0383fcb5e09c9a0 [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 Chotard204079b2018-08-10 17:12:14 +020011#include <clk.h>
Patrick Delaunay266bf102019-07-30 19:16:44 +020012#include <config.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020013#include <dm.h>
Sughosh Ganue166b5e2022-10-21 18:15:58 +053014#include <efi_loader.h>
Simon Glassdb229612019-08-01 09:46:42 -060015#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060016#include <env_internal.h>
Patrick Delaunay028fddd2021-11-15 16:32:23 +010017#include <fdt_simplefb.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060018#include <fdt_support.h>
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +010019#include <g_dnl.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020020#include <generic-phy.h>
Simon Glassf11478f2019-12-28 10:45:07 -070021#include <hang.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010022#include <i2c.h>
Simon Glassa7b51302019-11-14 12:57:46 -070023#include <init.h>
Patrick Delaunay92dc1022019-02-12 11:44:41 +010024#include <led.h>
Simon Glass0f2af882020-05-10 11:40:05 -060025#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070026#include <malloc.h>
Patrick Delaunay92dc1022019-02-12 11:44:41 +010027#include <misc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060028#include <net.h>
Patrick Delaunaybff66f92019-08-01 11:29:03 +020029#include <netdev.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020030#include <phy.h>
Patrick Delaunayc17d7252019-08-02 15:07:20 +020031#include <remoteproc.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020032#include <reset.h>
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010033#include <syscon.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010034#include <usb.h>
Patrick Delaunayae0931d02019-07-30 19:16:39 +020035#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060036#include <asm/global_data.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020037#include <asm/io.h>
Patrick Delaunayf2a7b872019-02-27 17:01:18 +010038#include <asm/gpio.h>
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010039#include <asm/arch/stm32.h>
Patrice Chotarddad97bf2019-05-02 18:36:01 +020040#include <asm/arch/sys_proto.h>
Simon Glass0034d962021-08-07 07:24:01 -060041#include <dm/ofnode.h>
Patrick Delaunayde98cbf2019-07-02 13:26:07 +020042#include <jffs2/load_kernel.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060043#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060044#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070045#include <linux/err.h>
Patrick Delaunay181298e2020-04-22 14:29:16 +020046#include <linux/iopoll.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060047#include <linux/printk.h>
Patrice Chotard204079b2018-08-10 17:12:14 +020048#include <power/regulator.h>
Patrick Delaunay7f3384d2019-03-29 15:42:24 +010049#include <usb/dwc2_udc.h>
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +010050
Patrick Delaunayfcb6b0b2020-06-29 10:34:06 +020051#include "../../st/common/stusb160x.h"
52
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010053/* SYSCFG registers */
54#define SYSCFG_BOOTR 0x00
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010055#define SYSCFG_IOCTRLSETR 0x18
56#define SYSCFG_ICNR 0x1C
57#define SYSCFG_CMPCR 0x20
58#define SYSCFG_CMPENSETR 0x24
Patrick Delaunay4ace1d12019-02-27 17:01:24 +010059
60#define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
61#define SYSCFG_BOOTR_BOOTPD_SHIFT 4
62
63#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
64#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
65#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
66#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
67#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
68
69#define SYSCFG_CMPCR_SW_CTRL BIT(1)
70#define SYSCFG_CMPCR_READY BIT(8)
71
72#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
73
Patrice Chotardcded32f2019-04-30 18:09:38 +020074#define USB_LOW_THRESHOLD_UV 200000
Patrice Chotard879cde52019-02-12 16:50:40 +010075#define USB_WARNING_LOW_THRESHOLD_UV 660000
76#define USB_START_LOW_THRESHOLD_UV 1230000
Patrice Chotardcded32f2019-04-30 18:09:38 +020077#define USB_START_HIGH_THRESHOLD_UV 2150000
Patrice Chotard879cde52019-02-12 16:50:40 +010078
Simon Glassb8196212023-02-05 15:39:42 -070079#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
Sughosh Ganue166b5e2022-10-21 18:15:58 +053080struct efi_fw_image fw_images[1];
81
82struct efi_capsule_update_info update_info = {
Masahisa Kojima5d2438b2023-06-07 14:41:51 +090083 .num_images = ARRAY_SIZE(fw_images),
Sughosh Ganue166b5e2022-10-21 18:15:58 +053084 .images = fw_images,
85};
86
Sughosh Ganue166b5e2022-10-21 18:15:58 +053087#endif /* EFI_HAVE_CAPSULE_SUPPORT */
88
Patrick Delaunayf2f25c32020-05-25 12:19:46 +020089int board_early_init_f(void)
90{
91 /* nothing to do, only used in SPL */
92 return 0;
93}
94
Patrick Delaunay92dc1022019-02-12 11:44:41 +010095int checkboard(void)
96{
97 int ret;
98 char *mode;
99 u32 otp;
100 struct udevice *dev;
101 const char *fdt_compat;
102 int fdt_compat_len;
103
Patrick Delaunayba4b8b02021-07-26 11:21:34 +0200104 if (IS_ENABLED(CONFIG_TFABOOT)) {
Patrick Delaunay4c6fcbc2024-01-15 15:05:57 +0100105 if (IS_ENABLED(CONFIG_STM32MP15X_STM32IMAGE))
Patrick Delaunayba4b8b02021-07-26 11:21:34 +0200106 mode = "trusted - stm32image";
107 else
108 mode = "trusted";
109 } else {
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100110 mode = "basic";
Patrick Delaunayba4b8b02021-07-26 11:21:34 +0200111 }
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100112
Patrick Delaunay4e506b02022-05-19 09:07:29 +0200113 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
114 &fdt_compat_len);
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100115
116 log_info("Board: stm32mp1 in %s mode (%s)\n", mode,
117 fdt_compat && fdt_compat_len ? fdt_compat : "");
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100118
Patrick Delaunay52163d92020-03-24 09:05:00 +0100119 /* display the STMicroelectronics board identification */
Simon Glassd87b83e2023-02-05 15:36:43 -0700120 if (IS_ENABLED(CONFIG_CMD_STBOARD)) {
Patrick Delaunay0885c232020-02-12 19:37:42 +0100121 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700122 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunay0885c232020-02-12 19:37:42 +0100123 &dev);
124 if (!ret)
125 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
126 &otp, sizeof(otp));
127 if (ret > 0 && otp)
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100128 log_info("Board: MB%04x Var%d.%d Rev.%c-%02d\n",
129 otp >> 16,
130 (otp >> 12) & 0xF,
131 (otp >> 4) & 0xF,
132 ((otp >> 8) & 0xF) - 1 + 'A',
133 otp & 0xF);
Patrick Delaunay92dc1022019-02-12 11:44:41 +0100134 }
135
136 return 0;
137}
138
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100139static void board_key_check(void)
140{
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100141 ofnode node;
142 struct gpio_desc gpio;
143 enum forced_boot_mode boot_mode = BOOT_NORMAL;
144
Patrick Delaunayf6604852020-07-31 16:31:42 +0200145 if (!IS_ENABLED(CONFIG_FASTBOOT) && !IS_ENABLED(CONFIG_CMD_STM32PROG))
146 return;
147
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100148 node = ofnode_path("/config");
149 if (!ofnode_valid(node)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100150 log_debug("no /config node?\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100151 return;
152 }
Patrick Delaunayf6604852020-07-31 16:31:42 +0200153 if (IS_ENABLED(CONFIG_FASTBOOT)) {
154 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
155 &gpio, GPIOD_IS_IN)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100156 log_debug("could not find a /config/st,fastboot-gpios\n");
Patrick Delaunayf6604852020-07-31 16:31:42 +0200157 } else {
Patrick Delaunay466d3af2021-07-09 09:53:37 +0200158 udelay(20);
Patrick Delaunayf6604852020-07-31 16:31:42 +0200159 if (dm_gpio_get_value(&gpio)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100160 log_notice("Fastboot key pressed, ");
Patrick Delaunayf6604852020-07-31 16:31:42 +0200161 boot_mode = BOOT_FASTBOOT;
162 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100163
Patrick Delaunayf6604852020-07-31 16:31:42 +0200164 dm_gpio_free(NULL, &gpio);
165 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100166 }
Patrick Delaunayf6604852020-07-31 16:31:42 +0200167 if (IS_ENABLED(CONFIG_CMD_STM32PROG)) {
168 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
169 &gpio, GPIOD_IS_IN)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100170 log_debug("could not find a /config/st,stm32prog-gpios\n");
Patrick Delaunayf6604852020-07-31 16:31:42 +0200171 } else {
Patrick Delaunay466d3af2021-07-09 09:53:37 +0200172 udelay(20);
Patrick Delaunayf6604852020-07-31 16:31:42 +0200173 if (dm_gpio_get_value(&gpio)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100174 log_notice("STM32Programmer key pressed, ");
Patrick Delaunayf6604852020-07-31 16:31:42 +0200175 boot_mode = BOOT_STM32PROG;
176 }
177 dm_gpio_free(NULL, &gpio);
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100178 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100179 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100180 if (boot_mode != BOOT_NORMAL) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100181 log_notice("entering download mode...\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100182 clrsetbits_le32(TAMP_BOOT_CONTEXT,
183 TAMP_BOOT_FORCED_MASK,
184 boot_mode);
185 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100186}
187
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100188int g_dnl_board_usb_cable_connected(void)
Patrice Chotard204079b2018-08-10 17:12:14 +0200189{
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100190 struct udevice *dwc2_udc_otg;
Patrice Chotard204079b2018-08-10 17:12:14 +0200191 int ret;
192
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200193 if (!IS_ENABLED(CONFIG_USB_GADGET_DWC2_OTG))
194 return -ENODEV;
195
Patrice Chotardaef7c182022-04-22 09:39:18 +0200196 /*
197 * In case of USB boot device is detected, consider USB cable is
198 * connected
199 */
200 if ((get_bootmode() & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_USB)
201 return true;
202
Patrick Delaunayfcb6b0b2020-06-29 10:34:06 +0200203 /* if typec stusb160x is present, means DK1 or DK2 board */
204 ret = stusb160x_cable_connected();
205 if (ret >= 0)
206 return ret;
Patrick Delaunay7f3384d2019-03-29 15:42:24 +0100207
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100208 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700209 DM_DRIVER_GET(dwc2_udc_otg),
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100210 &dwc2_udc_otg);
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100211 if (ret) {
212 log_debug("dwc2_udc_otg init failed\n");
213 return ret;
214 }
Patrice Chotard204079b2018-08-10 17:12:14 +0200215
Patrick Delaunay58bc0cd2019-03-29 15:42:23 +0100216 return dwc2_udc_B_session_valid(dwc2_udc_otg);
Patrice Chotard879cde52019-02-12 16:50:40 +0100217}
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200218
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200219#ifdef CONFIG_USB_GADGET_DOWNLOAD
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200220#define STM32MP1_G_DNL_DFU_PRODUCT_NUM 0xdf11
221#define STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM 0x0afb
222
223int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
224{
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200225 if (IS_ENABLED(CONFIG_DFU_OVER_USB) &&
226 !strcmp(name, "usb_dnl_dfu"))
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200227 put_unaligned(STM32MP1_G_DNL_DFU_PRODUCT_NUM, &dev->idProduct);
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200228 else if (IS_ENABLED(CONFIG_FASTBOOT) &&
229 !strcmp(name, "usb_dnl_fastboot"))
Patrick Delaunay0aafce62019-09-13 15:24:17 +0200230 put_unaligned(STM32MP1_G_DNL_FASTBOOT_PRODUCT_NUM,
231 &dev->idProduct);
232 else
233 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct);
234
235 return 0;
236}
Patrick Delaunaya1d5f202020-07-31 16:31:43 +0200237#endif /* CONFIG_USB_GADGET_DOWNLOAD */
Patrice Chotard879cde52019-02-12 16:50:40 +0100238
239static int get_led(struct udevice **dev, char *led_string)
240{
Simon Glass0034d962021-08-07 07:24:01 -0600241 const char *led_name;
Patrice Chotard879cde52019-02-12 16:50:40 +0100242 int ret;
243
Simon Glass0034d962021-08-07 07:24:01 -0600244 led_name = ofnode_conf_read_str(led_string);
Patrice Chotard879cde52019-02-12 16:50:40 +0100245 if (!led_name) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100246 log_debug("could not find %s config string\n", led_string);
Patrice Chotard879cde52019-02-12 16:50:40 +0100247 return -ENOENT;
248 }
249 ret = led_get_by_label(led_name, dev);
250 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100251 log_debug("get=%d\n", ret);
Patrice Chotard879cde52019-02-12 16:50:40 +0100252 return ret;
253 }
254
255 return 0;
256}
257
258static int setup_led(enum led_state_t cmd)
259{
260 struct udevice *dev;
261 int ret;
262
Patrick Delaunay8ae05cf2020-04-22 14:29:12 +0200263 if (!CONFIG_IS_ENABLED(LED))
264 return 0;
265
Patrice Chotard879cde52019-02-12 16:50:40 +0100266 ret = get_led(&dev, "u-boot,boot-led");
267 if (ret)
268 return ret;
269
270 ret = led_set_state(dev, cmd);
Patrice Chotard204079b2018-08-10 17:12:14 +0200271 return ret;
272}
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200273
274static void __maybe_unused led_error_blink(u32 nb_blink)
275{
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200276 int ret;
277 struct udevice *led;
278 u32 i;
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200279
280 if (!nb_blink)
281 return;
282
Patrick Delaunay8ae05cf2020-04-22 14:29:12 +0200283 if (CONFIG_IS_ENABLED(LED)) {
284 ret = get_led(&led, "u-boot,error-led");
285 if (!ret) {
286 /* make u-boot,error-led blinking */
287 /* if U32_MAX and 125ms interval, for 17.02 years */
288 for (i = 0; i < 2 * nb_blink; i++) {
289 led_set_state(led, LEDST_TOGGLE);
290 mdelay(125);
Stefan Roese80877fa2022-09-02 14:10:46 +0200291 schedule();
Patrick Delaunay8ae05cf2020-04-22 14:29:12 +0200292 }
293 led_set_state(led, LEDST_ON);
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200294 }
295 }
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200296
297 /* infinite: the boot process must be stopped */
298 if (nb_blink == U32_MAX)
299 hang();
300}
Patrice Chotard204079b2018-08-10 17:12:14 +0200301
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200302static int adc_measurement(ofnode node, int adc_count, int *min_uV, int *max_uV)
Patrice Chotard879cde52019-02-12 16:50:40 +0100303{
304 struct ofnode_phandle_args adc_args;
305 struct udevice *adc;
Patrice Chotard879cde52019-02-12 16:50:40 +0100306 unsigned int raw;
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200307 int ret, uV;
308 int i;
Patrice Chotard879cde52019-02-12 16:50:40 +0100309
310 for (i = 0; i < adc_count; i++) {
311 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
312 "#io-channel-cells", 0, i,
313 &adc_args)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100314 log_debug("can't find /config/st,adc_usb_pd\n");
Patrice Chotard879cde52019-02-12 16:50:40 +0100315 return 0;
316 }
317
318 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
319 &adc);
320
321 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100322 log_err("Can't get adc device(%d)\n", ret);
Patrice Chotard879cde52019-02-12 16:50:40 +0100323 return ret;
324 }
325
326 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
327 &raw);
328 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100329 log_err("single shot failed for %s[%d]!\n",
330 adc->name, adc_args.args[0]);
Patrice Chotard879cde52019-02-12 16:50:40 +0100331 return ret;
332 }
333 /* Convert to uV */
334 if (!adc_raw_to_uV(adc, raw, &uV)) {
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200335 if (uV > *max_uV)
336 *max_uV = uV;
337 if (uV < *min_uV)
338 *min_uV = uV;
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100339 log_debug("%s[%02d] = %u, %d uV\n",
340 adc->name, adc_args.args[0], raw, uV);
Patrice Chotard879cde52019-02-12 16:50:40 +0100341 } else {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100342 log_err("Can't get uV value for %s[%d]\n",
343 adc->name, adc_args.args[0]);
Patrice Chotard879cde52019-02-12 16:50:40 +0100344 }
345 }
346
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200347 return 0;
348}
349
350static int board_check_usb_power(void)
351{
352 ofnode node;
353 int max_uV = 0;
354 int min_uV = USB_START_HIGH_THRESHOLD_UV;
355 int adc_count, ret;
356 u32 nb_blink;
357 u8 i;
358
Patrick Delaunayf1c46652021-04-06 09:57:54 +0200359 if (!IS_ENABLED(CONFIG_ADC))
360 return -ENODEV;
361
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200362 node = ofnode_path("/config");
363 if (!ofnode_valid(node)) {
364 log_debug("no /config node?\n");
365 return -ENOENT;
366 }
367
Patrice Chotard879cde52019-02-12 16:50:40 +0100368 /*
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200369 * Retrieve the ADC channels devices and get measurement
370 * for each of them
Patrice Chotard879cde52019-02-12 16:50:40 +0100371 */
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200372 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
373 "#io-channel-cells", 0);
374 if (adc_count < 0) {
375 if (adc_count == -ENOENT)
376 return 0;
Patrice Chotard879cde52019-02-12 16:50:40 +0100377
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200378 log_err("Can't find adc channel (%d)\n", adc_count);
379
380 return adc_count;
381 }
Patrice Chotardcded32f2019-04-30 18:09:38 +0200382
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200383 /* perform maximum of 2 ADC measurements to detect power supply current */
384 for (i = 0; i < 2; i++) {
Patrick Delaunayf1c46652021-04-06 09:57:54 +0200385 ret = adc_measurement(node, adc_count, &min_uV, &max_uV);
Patrice Chotard7dfa7972020-10-16 09:28:59 +0200386 if (ret)
387 return ret;
388
389 /*
390 * If highest value is inside 1.23 Volts and 2.10 Volts, that means
391 * board is plugged on an USB-C 3A power supply and boot process can
392 * continue.
393 */
394 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
395 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
396 min_uV <= USB_LOW_THRESHOLD_UV)
397 return 0;
398
399 if (i == 0) {
400 log_err("Previous ADC measurements was not the one expected, retry in 20ms\n");
401 mdelay(20); /* equal to max tPDDebounce duration (min 10ms - max 20ms) */
402 }
403 }
404
405 log_notice("****************************************************\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200406 /*
407 * If highest and lowest value are either both below
408 * USB_LOW_THRESHOLD_UV or both above USB_LOW_THRESHOLD_UV, that
409 * means USB TYPE-C is in unattached mode, this is an issue, make
410 * u-boot,error-led blinking and stop boot process.
411 */
412 if ((max_uV > USB_LOW_THRESHOLD_UV &&
413 min_uV > USB_LOW_THRESHOLD_UV) ||
414 (max_uV <= USB_LOW_THRESHOLD_UV &&
415 min_uV <= USB_LOW_THRESHOLD_UV)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100416 log_notice("* ERROR USB TYPE-C connection in unattached mode *\n");
417 log_notice("* Check that USB TYPE-C cable is correctly plugged *\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200418 /* with 125ms interval, led will blink for 17.02 years ....*/
419 nb_blink = U32_MAX;
420 }
Patrice Chotard879cde52019-02-12 16:50:40 +0100421
Patrice Chotardcded32f2019-04-30 18:09:38 +0200422 if (max_uV > USB_LOW_THRESHOLD_UV &&
423 max_uV <= USB_WARNING_LOW_THRESHOLD_UV &&
424 min_uV <= USB_LOW_THRESHOLD_UV) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100425 log_notice("* WARNING 500mA power supply detected *\n");
Patrice Chotard879cde52019-02-12 16:50:40 +0100426 nb_blink = 2;
Patrice Chotardcded32f2019-04-30 18:09:38 +0200427 }
428
429 if (max_uV > USB_WARNING_LOW_THRESHOLD_UV &&
430 max_uV <= USB_START_LOW_THRESHOLD_UV &&
431 min_uV <= USB_LOW_THRESHOLD_UV) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100432 log_notice("* WARNING 1.5A power supply detected *\n");
Patrice Chotard879cde52019-02-12 16:50:40 +0100433 nb_blink = 3;
434 }
435
Patrice Chotardcded32f2019-04-30 18:09:38 +0200436 /*
437 * If highest value is above 2.15 Volts that means that the USB TypeC
438 * supplies more than 3 Amp, this is not compliant with TypeC specification
439 */
440 if (max_uV > USB_START_HIGH_THRESHOLD_UV) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100441 log_notice("* USB TYPE-C charger not compliant with *\n");
442 log_notice("* specification *\n");
443 log_notice("****************************************************\n\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200444 /* with 125ms interval, led will blink for 17.02 years ....*/
445 nb_blink = U32_MAX;
446 } else {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100447 log_notice("* Current too low, use a 3A power supply! *\n");
448 log_notice("****************************************************\n\n");
Patrice Chotardcded32f2019-04-30 18:09:38 +0200449 }
Patrice Chotard879cde52019-02-12 16:50:40 +0100450
Patrick Delaunayae0931d02019-07-30 19:16:39 +0200451 led_error_blink(nb_blink);
Patrice Chotard879cde52019-02-12 16:50:40 +0100452
453 return 0;
454}
455
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100456static void sysconf_init(void)
457{
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100458 u8 *syscfg;
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100459 struct udevice *pwr_dev;
460 struct udevice *pwr_reg;
461 struct udevice *dev;
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100462 u32 otp = 0;
Patrick Delaunay181298e2020-04-22 14:29:16 +0200463 int ret;
464 u32 bootr, val;
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100465
466 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
467
468 /* interconnect update : select master using the port 1 */
469 /* LTDC = AXI_M9 */
470 /* GPU = AXI_M8 */
471 /* today information is hardcoded in U-Boot */
472 writel(BIT(9), syscfg + SYSCFG_ICNR);
473
474 /* disable Pull-Down for boot pin connected to VDD */
475 bootr = readl(syscfg + SYSCFG_BOOTR);
476 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
477 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
478 writel(bootr, syscfg + SYSCFG_BOOTR);
479
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100480 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
481 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
482 * The customer will have to disable this for low frequencies
483 * or if AFMUX is selected but the function not used, typically for
484 * TRACE. Otherwise, impact on power consumption.
485 *
486 * WARNING:
487 * enabling High Speed mode while VDD>2.7V
488 * with the OTP product_below_2v5 (OTP 18, BIT 13)
489 * erroneously set to 1 can damage the IC!
490 * => U-Boot set the register only if VDD < 2.7V (in DT)
491 * but this value need to be consistent with board design
492 */
Patrick Delaunay6b2baa02019-07-30 19:16:42 +0200493 ret = uclass_get_device_by_driver(UCLASS_PMIC,
Simon Glass65130cd2020-12-28 20:34:56 -0700494 DM_DRIVER_GET(stm32mp_pwr_pmic),
Patrick Delaunay6b2baa02019-07-30 19:16:42 +0200495 &pwr_dev);
Patrick Delaunay7c104822022-06-20 12:36:10 +0200496 if (!ret) {
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100497 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700498 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100499 &dev);
500 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100501 log_err("Can't find stm32mp_bsec driver\n");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100502 return;
503 }
504
505 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
Patrick Delaunayceb82e32019-08-02 13:08:06 +0200506 if (ret > 0)
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100507 otp = otp & BIT(13);
508
Patrick Delaunay6b2baa02019-07-30 19:16:42 +0200509 /* get VDD = vdd-supply */
510 ret = device_get_supply_regulator(pwr_dev, "vdd-supply",
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100511 &pwr_reg);
512
513 /* check if VDD is Low Voltage */
514 if (!ret) {
515 if (regulator_get_value(pwr_reg) < 2700000) {
516 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
517 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
518 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
519 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
520 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
521 syscfg + SYSCFG_IOCTRLSETR);
522
523 if (!otp)
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100524 log_err("product_below_2v5=0: HSLVEN protected by HW\n");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100525 } else {
526 if (otp)
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100527 log_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100528 }
529 } else {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100530 log_debug("VDD unknown");
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100531 }
532 }
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100533
534 /* activate automatic I/O compensation
535 * warning: need to ensure CSI enabled and ready in clock driver
536 */
537 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
538
Patrick Delaunay181298e2020-04-22 14:29:16 +0200539 /* poll until ready (1s timeout) */
540 ret = readl_poll_timeout(syscfg + SYSCFG_CMPCR, val,
541 val & SYSCFG_CMPCR_READY,
542 1000000);
543 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100544 log_err("SYSCFG: I/O compensation failed, timeout.\n");
Patrick Delaunay181298e2020-04-22 14:29:16 +0200545 led_error_blink(10);
546 }
547
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100548 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100549}
550
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200551static int board_stm32mp15x_dk2_init(void)
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200552{
553 ofnode node;
554 struct gpio_desc hdmi, audio;
555 int ret = 0;
556
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200557 /* Fix to make I2C1 usable on DK2 for touchscreen usage in kernel */
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200558 node = ofnode_path("/soc/i2c@40012000/hdmi-transmitter@39");
559 if (!ofnode_valid(node)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100560 log_debug("no hdmi-transmitter@39 ?\n");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200561 return -ENOENT;
562 }
563
564 if (gpio_request_by_name_nodev(node, "reset-gpios", 0,
565 &hdmi, GPIOD_IS_OUT)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100566 log_debug("could not find reset-gpios\n");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200567 return -ENOENT;
568 }
569
570 node = ofnode_path("/soc/i2c@40012000/cs42l51@4a");
571 if (!ofnode_valid(node)) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100572 log_debug("no cs42l51@4a ?\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 &audio, 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 /* before power up, insure that HDMI and AUDIO IC is under reset */
583 ret = dm_gpio_set_value(&hdmi, 1);
584 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100585 log_err("can't set_value for hdmi_nrst gpio");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200586 goto error;
587 }
588 ret = dm_gpio_set_value(&audio, 1);
589 if (ret) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100590 log_err("can't set_value for audio_nrst gpio");
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200591 goto error;
592 }
593
594 /* power-up audio IC */
595 regulator_autoset_by_name("v1v8_audio", NULL);
596
597 /* power-up HDMI IC */
598 regulator_autoset_by_name("v1v2_hdmi", NULL);
599 regulator_autoset_by_name("v3v3_hdmi", NULL);
600
601error:
602 return ret;
603}
604
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200605static bool board_is_stm32mp15x_dk2(void)
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200606{
Patrick Delaunay4c6fcbc2024-01-15 15:05:57 +0100607 if (CONFIG_IS_ENABLED(TARGET_ST_STM32MP15X) &&
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200608 of_machine_is_compatible("st,stm32mp157c-dk2"))
609 return true;
610
611 return false;
612}
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200613
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200614static bool board_is_stm32mp15x_ev1(void)
Patrick Delaunay486ae962020-04-22 14:29:13 +0200615{
Patrick Delaunay4c6fcbc2024-01-15 15:05:57 +0100616 if (CONFIG_IS_ENABLED(TARGET_ST_STM32MP15X) &&
Patrick Delaunay486ae962020-04-22 14:29:13 +0200617 (of_machine_is_compatible("st,stm32mp157a-ev1") ||
618 of_machine_is_compatible("st,stm32mp157c-ev1") ||
619 of_machine_is_compatible("st,stm32mp157d-ev1") ||
620 of_machine_is_compatible("st,stm32mp157f-ev1")))
621 return true;
622
623 return false;
624}
625
626/* touchscreen driver: only used for pincontrol configuration */
627static const struct udevice_id goodix_ids[] = {
628 { .compatible = "goodix,gt9147", },
629 { }
630};
631
632U_BOOT_DRIVER(goodix) = {
633 .name = "goodix",
634 .id = UCLASS_NOP,
635 .of_match = goodix_ids,
636};
637
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200638static void board_stm32mp15x_ev1_init(void)
Patrick Delaunay486ae962020-04-22 14:29:13 +0200639{
640 struct udevice *dev;
641
642 /* configure IRQ line on EV1 for touchscreen before LCD reset */
Simon Glass65130cd2020-12-28 20:34:56 -0700643 uclass_get_device_by_driver(UCLASS_NOP, DM_DRIVER_GET(goodix), &dev);
Patrick Delaunay486ae962020-04-22 14:29:13 +0200644}
645
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +0100646/* board dependent setup after realloc */
647int board_init(void)
648{
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100649 board_key_check();
650
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200651 if (board_is_stm32mp15x_ev1())
652 board_stm32mp15x_ev1_init();
Patrick Delaunay486ae962020-04-22 14:29:13 +0200653
Patrick Delaunay37c4b552022-05-20 18:24:49 +0200654 if (board_is_stm32mp15x_dk2())
655 board_stm32mp15x_dk2_init();
Patrick Delaunay9f76fdf2019-07-30 19:16:38 +0200656
Patrick Delaunay7c104822022-06-20 12:36:10 +0200657 regulators_enable_boot_on(_DEBUG);
Patrick Delaunay6519e442019-07-05 17:20:09 +0200658
Patrick Delaunay72a57622021-10-11 09:52:50 +0200659 /*
660 * sysconf initialisation done only when U-Boot is running in secure
661 * done in TF-A for TFABOOT.
662 */
663 if (IS_ENABLED(CONFIG_ARMV7_NONSEC))
Patrick Delaunay4b2bfd62020-07-31 16:31:45 +0200664 sysconf_init();
Patrick Delaunay4ace1d12019-02-27 17:01:24 +0100665
Patrick Delaunay8ae05cf2020-04-22 14:29:12 +0200666 setup_led(LEDST_ON);
667
Simon Glassb8196212023-02-05 15:39:42 -0700668#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
Sughosh Ganue166b5e2022-10-21 18:15:58 +0530669 efi_guid_t image_type_guid = STM32MP_FIP_IMAGE_GUID;
670
671 guidcpy(&fw_images[0].image_type_id, &image_type_guid);
672 fw_images[0].fw_name = u"STM32MP-FIP";
673 fw_images[0].image_index = 1;
674#endif
Patrick Delaunay8eb3b1e2018-03-12 10:46:18 +0100675 return 0;
676}
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100677
678int board_late_init(void)
679{
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100680 const void *fdt_compat;
681 int fdt_compat_len;
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200682 int ret;
683 u32 otp;
684 struct udevice *dev;
685 char buf[10];
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200686 char dtb_name[256];
687 int buf_len;
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100688
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200689 if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) {
Patrick Delaunay4e506b02022-05-19 09:07:29 +0200690 fdt_compat = ofnode_get_property(ofnode_root(), "compatible",
691 &fdt_compat_len);
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200692 if (fdt_compat && fdt_compat_len) {
693 if (strncmp(fdt_compat, "st,", 3) != 0) {
694 env_set("board_name", fdt_compat);
695 } else {
696 env_set("board_name", fdt_compat + 3);
Patrick Delaunay4533a2c2020-04-22 14:29:14 +0200697
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200698 buf_len = sizeof(dtb_name);
699 strncpy(dtb_name, fdt_compat + 3, buf_len);
700 buf_len -= strlen(fdt_compat + 3);
701 strncat(dtb_name, ".dtb", buf_len);
702 env_set("fdtfile", dtb_name);
703 }
Patrick Delaunay4533a2c2020-04-22 14:29:14 +0200704 }
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200705 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65130cd2020-12-28 20:34:56 -0700706 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200707 &dev);
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200708
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200709 if (!ret)
710 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
711 &otp, sizeof(otp));
712 if (ret > 0 && otp) {
713 snprintf(buf, sizeof(buf), "0x%04x", otp >> 16);
714 env_set("board_id", buf);
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200715
Patrick Delaunayf118e4a2020-07-31 16:31:48 +0200716 snprintf(buf, sizeof(buf), "0x%04x",
717 ((otp >> 8) & 0xF) - 1 + 0xA);
718 env_set("board_rev", buf);
719 }
Patrick Delaunaye8566ec2019-07-30 19:16:37 +0200720 }
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100721
Patrice Chotard879cde52019-02-12 16:50:40 +0100722 /* for DK1/DK2 boards */
723 board_check_usb_power();
724
Patrick Delaunayd70e3f82019-02-27 17:01:11 +0100725 return 0;
726}
Patrice Chotard879cde52019-02-12 16:50:40 +0100727
728void board_quiesce_devices(void)
729{
730 setup_led(LEDST_OFF);
731}
Patrice Chotard41443cf2019-05-02 18:07:14 +0200732
Patrice Chotard34320372019-05-02 18:28:05 +0200733enum env_location env_get_location(enum env_operation op, int prio)
734{
735 u32 bootmode = get_bootmode();
736
737 if (prio)
738 return ENVL_UNKNOWN;
739
740 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
Patrick Delaunay455b0652020-06-15 11:18:22 +0200741 case BOOT_FLASH_SD:
742 case BOOT_FLASH_EMMC:
Patrick Delaunay821a9ba2020-07-31 16:31:49 +0200743 if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC))
744 return ENVL_MMC;
745 else if (CONFIG_IS_ENABLED(ENV_IS_IN_EXT4))
746 return ENVL_EXT4;
747 else
748 return ENVL_NOWHERE;
749
Patrice Chotard34320372019-05-02 18:28:05 +0200750 case BOOT_FLASH_NAND:
Patrick Delaunayb5a7ca22020-03-18 09:22:52 +0100751 case BOOT_FLASH_SPINAND:
Simon Glassbd3ebf62023-02-05 15:39:48 -0700752 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
Patrick Delaunay821a9ba2020-07-31 16:31:49 +0200753 return ENVL_UBI;
754 else
755 return ENVL_NOWHERE;
756
Patrice Chotard2c461ec2019-05-09 14:25:36 +0200757 case BOOT_FLASH_NOR:
Patrick Delaunay821a9ba2020-07-31 16:31:49 +0200758 if (CONFIG_IS_ENABLED(ENV_IS_IN_SPI_FLASH))
759 return ENVL_SPI_FLASH;
760 else
761 return ENVL_NOWHERE;
762
Patrice Chotard34320372019-05-02 18:28:05 +0200763 default:
764 return ENVL_NOWHERE;
765 }
766}
767
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200768const char *env_ext4_get_intf(void)
769{
770 u32 bootmode = get_bootmode();
771
772 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
773 case BOOT_FLASH_SD:
774 case BOOT_FLASH_EMMC:
775 return "mmc";
776 default:
777 return "";
778 }
779}
780
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200781int mmc_get_boot(void)
782{
783 struct udevice *dev;
784 u32 boot_mode = get_bootmode();
785 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
786 char cmd[20];
787 const u32 sdmmc_addr[] = {
788 STM32_SDMMC1_BASE,
789 STM32_SDMMC2_BASE,
790 STM32_SDMMC3_BASE
791 };
792
Rasmus Villemoes6d83f3c2023-03-24 08:55:19 +0100793 if (instance >= ARRAY_SIZE(sdmmc_addr))
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200794 return 0;
795
796 /* search associated sdmmc node in devicetree */
797 snprintf(cmd, sizeof(cmd), "mmc@%x", sdmmc_addr[instance]);
798 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
799 log_err("mmc%d = %s not found in device tree!\n", instance, cmd);
800 return 0;
801 }
802
803 return dev_seq(dev);
804};
805
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200806const char *env_ext4_get_dev_part(void)
807{
Manuel Reisbe9d3e22020-11-25 10:16:20 +0000808 static char *const env_dev_part =
809#ifdef CONFIG_ENV_EXT4_DEVICE_AND_PART
810 CONFIG_ENV_EXT4_DEVICE_AND_PART;
811#else
812 "";
813#endif
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200814 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
Manuel Reisbe9d3e22020-11-25 10:16:20 +0000815
816 if (strlen(env_dev_part) > 0)
817 return env_dev_part;
818
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200819 return dev_part[mmc_get_boot()];
Patrice Chotarddad97bf2019-05-02 18:36:01 +0200820}
Manuel Reisbe9d3e22020-11-25 10:16:20 +0000821
Patrick Delaunay455b0652020-06-15 11:18:22 +0200822int mmc_get_env_dev(void)
823{
Patrick Delaunay520e2992022-01-11 16:37:21 +0100824 const int mmc_env_dev = CONFIG_IS_ENABLED(ENV_IS_IN_MMC, (CONFIG_SYS_MMC_ENV_DEV), (-1));
825
826 if (mmc_env_dev >= 0)
827 return mmc_env_dev;
Patrick Delaunayb6bc8532021-03-01 13:17:56 +0100828
Patrick Delaunayb6bc8532021-03-01 13:17:56 +0100829 /* use boot instance to select the correct mmc device identifier */
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200830 return mmc_get_boot();
Patrick Delaunay455b0652020-06-15 11:18:22 +0200831}
Patrick Delaunay455b0652020-06-15 11:18:22 +0200832
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200833#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900834int ft_board_setup(void *blob, struct bd_info *bd)
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200835{
Patrick Delaunay67e95d52023-06-08 17:16:44 +0200836 fdt_copy_fixed_partitions(blob);
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200837
Simon Glass703f5bd2023-02-05 15:39:57 -0700838 if (IS_ENABLED(CONFIG_FDT_SIMPLEFB))
Patrick Delaunay028fddd2021-11-15 16:32:23 +0100839 fdt_simplefb_enable_and_mem_rsv(blob);
840
Patrick Delaunayde98cbf2019-07-02 13:26:07 +0200841 return 0;
842}
843#endif
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200844
845static void board_copro_image_process(ulong fw_image, size_t fw_size)
846{
847 int ret, id = 0; /* Copro id fixed to 0 as only one coproc on mp1 */
848
849 if (!rproc_is_initialized())
850 if (rproc_init()) {
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100851 log_err("Remote Processor %d initialization failed\n",
852 id);
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200853 return;
854 }
855
856 ret = rproc_load(id, fw_image, fw_size);
Patrick Delaunayf7b2a842020-11-06 19:01:59 +0100857 log_err("Load Remote Processor %d with data@addr=0x%08lx %u bytes:%s\n",
858 id, fw_image, fw_size, ret ? " Failed!" : " Success!");
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200859
Fabien Dessennead6cc942019-10-30 14:38:32 +0100860 if (!ret)
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200861 rproc_start(id);
Patrick Delaunayc17d7252019-08-02 15:07:20 +0200862}
863
864U_BOOT_FIT_LOADABLE_HANDLER(IH_TYPE_COPRO, board_copro_image_process);
Sughosh Ganu73abe8e2022-10-21 18:16:00 +0530865
866#if defined(CONFIG_FWU_MULTI_BANK_UPDATE)
867
868#include <fwu.h>
869
870/**
871 * fwu_plat_get_bootidx() - Get the value of the boot index
872 * @boot_idx: Boot index value
873 *
874 * Get the value of the bank(partition) from which the platform
875 * has booted. This value is passed to U-Boot from the earlier
876 * stage bootloader which loads and boots all the relevant
877 * firmware images
878 *
879 */
880void fwu_plat_get_bootidx(uint *boot_idx)
881{
882 *boot_idx = (readl(TAMP_FWU_BOOT_INFO_REG) >>
883 TAMP_FWU_BOOT_IDX_OFFSET) & TAMP_FWU_BOOT_IDX_MASK;
884}
885#endif /* CONFIG_FWU_MULTI_BANK_UPDATE */