279c7b77979987006010f518541fbdc242b9d86e
[oweals/u-boot.git] / board / st / stm32mp1 / stm32mp1.c
1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4  */
5 #include <common.h>
6 #include <adc.h>
7 #include <bootm.h>
8 #include <config.h>
9 #include <clk.h>
10 #include <dm.h>
11 #include <env.h>
12 #include <env_internal.h>
13 #include <g_dnl.h>
14 #include <generic-phy.h>
15 #include <i2c.h>
16 #include <led.h>
17 #include <misc.h>
18 #include <mtd.h>
19 #include <mtd_node.h>
20 #include <phy.h>
21 #include <reset.h>
22 #include <syscon.h>
23 #include <usb.h>
24 #include <asm/io.h>
25 #include <asm/gpio.h>
26 #include <asm/arch/stm32.h>
27 #include <asm/arch/sys_proto.h>
28 #include <jffs2/load_kernel.h>
29 #include <power/regulator.h>
30 #include <usb/dwc2_udc.h>
31
32 /* SYSCFG registers */
33 #define SYSCFG_BOOTR            0x00
34 #define SYSCFG_PMCSETR          0x04
35 #define SYSCFG_IOCTRLSETR       0x18
36 #define SYSCFG_ICNR             0x1C
37 #define SYSCFG_CMPCR            0x20
38 #define SYSCFG_CMPENSETR        0x24
39 #define SYSCFG_PMCCLRR          0x44
40
41 #define SYSCFG_BOOTR_BOOT_MASK          GENMASK(2, 0)
42 #define SYSCFG_BOOTR_BOOTPD_SHIFT       4
43
44 #define SYSCFG_IOCTRLSETR_HSLVEN_TRACE          BIT(0)
45 #define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI        BIT(1)
46 #define SYSCFG_IOCTRLSETR_HSLVEN_ETH            BIT(2)
47 #define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC          BIT(3)
48 #define SYSCFG_IOCTRLSETR_HSLVEN_SPI            BIT(4)
49
50 #define SYSCFG_CMPCR_SW_CTRL            BIT(1)
51 #define SYSCFG_CMPCR_READY              BIT(8)
52
53 #define SYSCFG_CMPENSETR_MPU_EN         BIT(0)
54
55 #define SYSCFG_PMCSETR_ETH_CLK_SEL      BIT(16)
56 #define SYSCFG_PMCSETR_ETH_REF_CLK_SEL  BIT(17)
57
58 #define SYSCFG_PMCSETR_ETH_SELMII       BIT(20)
59
60 #define SYSCFG_PMCSETR_ETH_SEL_MASK     GENMASK(23, 21)
61 #define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0
62 #define SYSCFG_PMCSETR_ETH_SEL_RGMII    BIT(21)
63 #define SYSCFG_PMCSETR_ETH_SEL_RMII     BIT(23)
64
65 /*
66  * Get a global data pointer
67  */
68 DECLARE_GLOBAL_DATA_PTR;
69
70 #define USB_LOW_THRESHOLD_UV            200000
71 #define USB_WARNING_LOW_THRESHOLD_UV    660000
72 #define USB_START_LOW_THRESHOLD_UV      1230000
73 #define USB_START_HIGH_THRESHOLD_UV     2150000
74
75 int checkboard(void)
76 {
77         int ret;
78         char *mode;
79         u32 otp;
80         struct udevice *dev;
81         const char *fdt_compat;
82         int fdt_compat_len;
83
84         if (IS_ENABLED(CONFIG_STM32MP1_OPTEE))
85                 mode = "trusted with OP-TEE";
86         else if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
87                 mode = "trusted";
88         else
89                 mode = "basic";
90
91         printf("Board: stm32mp1 in %s mode", mode);
92         fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
93                                  &fdt_compat_len);
94         if (fdt_compat && fdt_compat_len)
95                 printf(" (%s)", fdt_compat);
96         puts("\n");
97
98         ret = uclass_get_device_by_driver(UCLASS_MISC,
99                                           DM_GET_DRIVER(stm32mp_bsec),
100                                           &dev);
101
102         if (!ret)
103                 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
104                                 &otp, sizeof(otp));
105         if (!ret && otp) {
106                 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
107                        otp >> 16,
108                        (otp >> 12) & 0xF,
109                        ((otp >> 8) & 0xF) - 1 + 'A',
110                        otp & 0xF);
111         }
112
113         return 0;
114 }
115
116 static void board_key_check(void)
117 {
118 #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
119         ofnode node;
120         struct gpio_desc gpio;
121         enum forced_boot_mode boot_mode = BOOT_NORMAL;
122
123         node = ofnode_path("/config");
124         if (!ofnode_valid(node)) {
125                 debug("%s: no /config node?\n", __func__);
126                 return;
127         }
128 #ifdef CONFIG_FASTBOOT
129         if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
130                                        &gpio, GPIOD_IS_IN)) {
131                 debug("%s: could not find a /config/st,fastboot-gpios\n",
132                       __func__);
133         } else {
134                 if (dm_gpio_get_value(&gpio)) {
135                         puts("Fastboot key pressed, ");
136                         boot_mode = BOOT_FASTBOOT;
137                 }
138
139                 dm_gpio_free(NULL, &gpio);
140         }
141 #endif
142 #ifdef CONFIG_CMD_STM32PROG
143         if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
144                                        &gpio, GPIOD_IS_IN)) {
145                 debug("%s: could not find a /config/st,stm32prog-gpios\n",
146                       __func__);
147         } else {
148                 if (dm_gpio_get_value(&gpio)) {
149                         puts("STM32Programmer key pressed, ");
150                         boot_mode = BOOT_STM32PROG;
151                 }
152                 dm_gpio_free(NULL, &gpio);
153         }
154 #endif
155
156         if (boot_mode != BOOT_NORMAL) {
157                 puts("entering download mode...\n");
158                 clrsetbits_le32(TAMP_BOOT_CONTEXT,
159                                 TAMP_BOOT_FORCED_MASK,
160                                 boot_mode);
161         }
162 #endif
163 }
164
165 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
166
167 /* STMicroelectronics STUSB1600 Type-C controller */
168 #define STUSB1600_CC_CONNECTION_STATUS          0x0E
169
170 /* STUSB1600_CC_CONNECTION_STATUS bitfields */
171 #define STUSB1600_CC_ATTACH                     BIT(0)
172
173 static int stusb1600_init(struct udevice **dev_stusb1600)
174 {
175         ofnode node;
176         struct udevice *dev, *bus;
177         int ret;
178         u32 chip_addr;
179
180         *dev_stusb1600 = NULL;
181
182         /* if node stusb1600 is present, means DK1 or DK2 board */
183         node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
184         if (!ofnode_valid(node))
185                 return -ENODEV;
186
187         ret = ofnode_read_u32(node, "reg", &chip_addr);
188         if (ret)
189                 return -EINVAL;
190
191         ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
192                                           &bus);
193         if (ret) {
194                 printf("bus for stusb1600 not found\n");
195                 return -ENODEV;
196         }
197
198         ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
199         if (!ret)
200                 *dev_stusb1600 = dev;
201
202         return ret;
203 }
204
205 static int stusb1600_cable_connected(struct udevice *dev)
206 {
207         u8 status;
208
209         if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
210                 return 0;
211
212         return status & STUSB1600_CC_ATTACH;
213 }
214
215 #include <usb/dwc2_udc.h>
216 int g_dnl_board_usb_cable_connected(void)
217 {
218         struct udevice *stusb1600;
219         struct udevice *dwc2_udc_otg;
220         int ret;
221
222         if (!stusb1600_init(&stusb1600))
223                 return stusb1600_cable_connected(stusb1600);
224
225         ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
226                                           DM_GET_DRIVER(dwc2_udc_otg),
227                                           &dwc2_udc_otg);
228         if (!ret)
229                 debug("dwc2_udc_otg init failed\n");
230
231         return dwc2_udc_B_session_valid(dwc2_udc_otg);
232 }
233 #endif /* CONFIG_USB_GADGET */
234
235 static int get_led(struct udevice **dev, char *led_string)
236 {
237         char *led_name;
238         int ret;
239
240         led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
241         if (!led_name) {
242                 pr_debug("%s: could not find %s config string\n",
243                          __func__, led_string);
244                 return -ENOENT;
245         }
246         ret = led_get_by_label(led_name, dev);
247         if (ret) {
248                 debug("%s: get=%d\n", __func__, ret);
249                 return ret;
250         }
251
252         return 0;
253 }
254
255 static int setup_led(enum led_state_t cmd)
256 {
257         struct udevice *dev;
258         int ret;
259
260         ret = get_led(&dev, "u-boot,boot-led");
261         if (ret)
262                 return ret;
263
264         ret = led_set_state(dev, cmd);
265         return ret;
266 }
267
268 static int board_check_usb_power(void)
269 {
270         struct ofnode_phandle_args adc_args;
271         struct udevice *adc;
272         struct udevice *led;
273         ofnode node;
274         unsigned int raw;
275         int max_uV = 0;
276         int min_uV = USB_START_HIGH_THRESHOLD_UV;
277         int ret, uV, adc_count;
278         u32 nb_blink;
279         u8 i;
280         node = ofnode_path("/config");
281         if (!ofnode_valid(node)) {
282                 debug("%s: no /config node?\n", __func__);
283                 return -ENOENT;
284         }
285
286         /*
287          * Retrieve the ADC channels devices and get measurement
288          * for each of them
289          */
290         adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
291                                                    "#io-channel-cells");
292         if (adc_count < 0) {
293                 if (adc_count == -ENOENT)
294                         return 0;
295
296                 pr_err("%s: can't find adc channel (%d)\n", __func__,
297                        adc_count);
298
299                 return adc_count;
300         }
301
302         for (i = 0; i < adc_count; i++) {
303                 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
304                                                    "#io-channel-cells", 0, i,
305                                                    &adc_args)) {
306                         pr_debug("%s: can't find /config/st,adc_usb_pd\n",
307                                  __func__);
308                         return 0;
309                 }
310
311                 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
312                                                   &adc);
313
314                 if (ret) {
315                         pr_err("%s: Can't get adc device(%d)\n", __func__,
316                                ret);
317                         return ret;
318                 }
319
320                 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
321                                               &raw);
322                 if (ret) {
323                         pr_err("%s: single shot failed for %s[%d]!\n",
324                                __func__, adc->name, adc_args.args[0]);
325                         return ret;
326                 }
327                 /* Convert to uV */
328                 if (!adc_raw_to_uV(adc, raw, &uV)) {
329                         if (uV > max_uV)
330                                 max_uV = uV;
331                         if (uV < min_uV)
332                                 min_uV = uV;
333                         pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
334                                  adc->name, adc_args.args[0], raw, uV);
335                 } else {
336                         pr_err("%s: Can't get uV value for %s[%d]\n",
337                                __func__, adc->name, adc_args.args[0]);
338                 }
339         }
340
341         /*
342          * If highest value is inside 1.23 Volts and 2.10 Volts, that means
343          * board is plugged on an USB-C 3A power supply and boot process can
344          * continue.
345          */
346         if (max_uV > USB_START_LOW_THRESHOLD_UV &&
347             max_uV <= USB_START_HIGH_THRESHOLD_UV &&
348             min_uV <= USB_LOW_THRESHOLD_UV)
349                 return 0;
350
351         pr_err("****************************************************\n");
352
353         /*
354          * If highest and lowest value are either both below
355          * USB_LOW_THRESHOLD_UV or both above USB_LOW_THRESHOLD_UV, that
356          * means USB TYPE-C is in unattached mode, this is an issue, make
357          * u-boot,error-led blinking and stop boot process.
358          */
359         if ((max_uV > USB_LOW_THRESHOLD_UV &&
360              min_uV > USB_LOW_THRESHOLD_UV) ||
361              (max_uV <= USB_LOW_THRESHOLD_UV &&
362              min_uV <= USB_LOW_THRESHOLD_UV)) {
363                 pr_err("* ERROR USB TYPE-C connection in unattached mode   *\n");
364                 pr_err("* Check that USB TYPE-C cable is correctly plugged *\n");
365                 /* with 125ms interval, led will blink for 17.02 years ....*/
366                 nb_blink = U32_MAX;
367         }
368
369         if (max_uV > USB_LOW_THRESHOLD_UV &&
370             max_uV <= USB_WARNING_LOW_THRESHOLD_UV &&
371             min_uV <= USB_LOW_THRESHOLD_UV) {
372                 pr_err("*        WARNING 500mA power supply detected       *\n");
373                 nb_blink = 2;
374         }
375
376         if (max_uV > USB_WARNING_LOW_THRESHOLD_UV &&
377             max_uV <= USB_START_LOW_THRESHOLD_UV &&
378             min_uV <= USB_LOW_THRESHOLD_UV) {
379                 pr_err("*       WARNING 1.5mA power supply detected        *\n");
380                 nb_blink = 3;
381         }
382
383         /*
384          * If highest value is above 2.15 Volts that means that the USB TypeC
385          * supplies more than 3 Amp, this is not compliant with TypeC specification
386          */
387         if (max_uV > USB_START_HIGH_THRESHOLD_UV) {
388                 pr_err("*      USB TYPE-C charger not compliant with       *\n");
389                 pr_err("*                   specification                  *\n");
390                 pr_err("****************************************************\n\n");
391                 /* with 125ms interval, led will blink for 17.02 years ....*/
392                 nb_blink = U32_MAX;
393         } else {
394                 pr_err("*     Current too low, use a 3A power supply!      *\n");
395                 pr_err("****************************************************\n\n");
396         }
397
398         ret = get_led(&led, "u-boot,error-led");
399         if (ret) {
400                 /* in unattached case, the boot process must be stopped */
401                 if (nb_blink == U32_MAX)
402                         hang();
403                 return ret;
404         }
405
406         /* make u-boot,error-led blinking */
407         for (i = 0; i < nb_blink * 2; i++) {
408                 led_set_state(led, LEDST_TOGGLE);
409                 mdelay(125);
410         }
411         led_set_state(led, LEDST_ON);
412
413         return 0;
414 }
415
416 static void sysconf_init(void)
417 {
418 #ifndef CONFIG_STM32MP1_TRUSTED
419         u8 *syscfg;
420 #ifdef CONFIG_DM_REGULATOR
421         struct udevice *pwr_dev;
422         struct udevice *pwr_reg;
423         struct udevice *dev;
424         int ret;
425         u32 otp = 0;
426 #endif
427         u32 bootr;
428
429         syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
430
431         /* interconnect update : select master using the port 1 */
432         /* LTDC = AXI_M9 */
433         /* GPU  = AXI_M8 */
434         /* today information is hardcoded in U-Boot */
435         writel(BIT(9), syscfg + SYSCFG_ICNR);
436
437         /* disable Pull-Down for boot pin connected to VDD */
438         bootr = readl(syscfg + SYSCFG_BOOTR);
439         bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
440         bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
441         writel(bootr, syscfg + SYSCFG_BOOTR);
442
443 #ifdef CONFIG_DM_REGULATOR
444         /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
445          * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
446          * The customer will have to disable this for low frequencies
447          * or if AFMUX is selected but the function not used, typically for
448          * TRACE. Otherwise, impact on power consumption.
449          *
450          * WARNING:
451          *   enabling High Speed mode while VDD>2.7V
452          *   with the OTP product_below_2v5 (OTP 18, BIT 13)
453          *   erroneously set to 1 can damage the IC!
454          *   => U-Boot set the register only if VDD < 2.7V (in DT)
455          *      but this value need to be consistent with board design
456          */
457         ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
458         if (!ret) {
459                 ret = uclass_get_device_by_driver(UCLASS_MISC,
460                                                   DM_GET_DRIVER(stm32mp_bsec),
461                                                   &dev);
462                 if (ret) {
463                         pr_err("Can't find stm32mp_bsec driver\n");
464                         return;
465                 }
466
467                 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
468                 if (!ret)
469                         otp = otp & BIT(13);
470
471                 /* get VDD = pwr-supply */
472                 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
473                                                   &pwr_reg);
474
475                 /* check if VDD is Low Voltage */
476                 if (!ret) {
477                         if (regulator_get_value(pwr_reg) < 2700000) {
478                                 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
479                                        SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
480                                        SYSCFG_IOCTRLSETR_HSLVEN_ETH |
481                                        SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
482                                        SYSCFG_IOCTRLSETR_HSLVEN_SPI,
483                                        syscfg + SYSCFG_IOCTRLSETR);
484
485                                 if (!otp)
486                                         pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
487                         } else {
488                                 if (otp)
489                                         pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
490                         }
491                 } else {
492                         debug("VDD unknown");
493                 }
494         }
495 #endif
496
497         /* activate automatic I/O compensation
498          * warning: need to ensure CSI enabled and ready in clock driver
499          */
500         writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
501
502         while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
503                 ;
504         clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
505 #endif
506 }
507
508 /* board dependent setup after realloc */
509 int board_init(void)
510 {
511         struct udevice *dev;
512
513         /* address of boot parameters */
514         gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
515
516         /* probe all PINCTRL for hog */
517         for (uclass_first_device(UCLASS_PINCTRL, &dev);
518              dev;
519              uclass_next_device(&dev)) {
520                 pr_debug("probe pincontrol = %s\n", dev->name);
521         }
522
523         board_key_check();
524
525 #ifdef CONFIG_DM_REGULATOR
526         regulators_enable_boot_on(_DEBUG);
527 #endif
528
529         sysconf_init();
530
531         if (IS_ENABLED(CONFIG_LED))
532                 led_default_state();
533
534         return 0;
535 }
536
537 int board_late_init(void)
538 {
539 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
540         const void *fdt_compat;
541         int fdt_compat_len;
542
543         fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
544                                  &fdt_compat_len);
545         if (fdt_compat && fdt_compat_len) {
546                 if (strncmp(fdt_compat, "st,", 3) != 0)
547                         env_set("board_name", fdt_compat);
548                 else
549                         env_set("board_name", fdt_compat + 3);
550         }
551 #endif
552
553         /* for DK1/DK2 boards */
554         board_check_usb_power();
555
556         return 0;
557 }
558
559 void board_quiesce_devices(void)
560 {
561         setup_led(LEDST_OFF);
562 }
563
564 /* board interface eth init */
565 /* this is a weak define that we are overriding */
566 int board_interface_eth_init(phy_interface_t interface_type,
567                              bool eth_clk_sel_reg, bool eth_ref_clk_sel_reg)
568 {
569         u8 *syscfg;
570         u32 value;
571
572         syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
573
574         if (!syscfg)
575                 return -ENODEV;
576
577         switch (interface_type) {
578         case PHY_INTERFACE_MODE_MII:
579                 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
580                         SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
581                 debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
582                 break;
583         case PHY_INTERFACE_MODE_GMII:
584                 if (eth_clk_sel_reg)
585                         value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
586                                 SYSCFG_PMCSETR_ETH_CLK_SEL;
587                 else
588                         value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
589                 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
590                 break;
591         case PHY_INTERFACE_MODE_RMII:
592                 if (eth_ref_clk_sel_reg)
593                         value = SYSCFG_PMCSETR_ETH_SEL_RMII |
594                                 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
595                 else
596                         value = SYSCFG_PMCSETR_ETH_SEL_RMII;
597                 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
598                 break;
599         case PHY_INTERFACE_MODE_RGMII:
600         case PHY_INTERFACE_MODE_RGMII_ID:
601         case PHY_INTERFACE_MODE_RGMII_RXID:
602         case PHY_INTERFACE_MODE_RGMII_TXID:
603                 if (eth_clk_sel_reg)
604                         value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
605                                 SYSCFG_PMCSETR_ETH_CLK_SEL;
606                 else
607                         value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
608                 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
609                 break;
610         default:
611                 debug("%s: Do not manage %d interface\n",
612                       __func__, interface_type);
613                 /* Do not manage others interfaces */
614                 return -EINVAL;
615         }
616
617         /* clear and set ETH configuration bits */
618         writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
619                SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
620                syscfg + SYSCFG_PMCCLRR);
621         writel(value, syscfg + SYSCFG_PMCSETR);
622
623         return 0;
624 }
625
626 enum env_location env_get_location(enum env_operation op, int prio)
627 {
628         u32 bootmode = get_bootmode();
629
630         if (prio)
631                 return ENVL_UNKNOWN;
632
633         switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
634 #ifdef CONFIG_ENV_IS_IN_EXT4
635         case BOOT_FLASH_SD:
636         case BOOT_FLASH_EMMC:
637                 return ENVL_EXT4;
638 #endif
639 #ifdef CONFIG_ENV_IS_IN_UBI
640         case BOOT_FLASH_NAND:
641                 return ENVL_UBI;
642 #endif
643 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
644         case BOOT_FLASH_NOR:
645                 return ENVL_SPI_FLASH;
646 #endif
647         default:
648                 return ENVL_NOWHERE;
649         }
650 }
651
652 #if defined(CONFIG_ENV_IS_IN_EXT4)
653 const char *env_ext4_get_intf(void)
654 {
655         u32 bootmode = get_bootmode();
656
657         switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
658         case BOOT_FLASH_SD:
659         case BOOT_FLASH_EMMC:
660                 return "mmc";
661         default:
662                 return "";
663         }
664 }
665
666 const char *env_ext4_get_dev_part(void)
667 {
668         static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
669         u32 bootmode = get_bootmode();
670
671         return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
672 }
673 #endif
674
675 #ifdef CONFIG_SYS_MTDPARTS_RUNTIME
676
677 #define MTDPARTS_LEN            256
678 #define MTDIDS_LEN              128
679
680 /**
681  * The mtdparts_nand0 and mtdparts_nor0 variable tends to be long.
682  * If we need to access it before the env is relocated, then we need
683  * to use our own stack buffer. gd->env_buf will be too small.
684  *
685  * @param buf temporary buffer pointer MTDPARTS_LEN long
686  * @return mtdparts variable string, NULL if not found
687  */
688 static const char *env_get_mtdparts(const char *str, char *buf)
689 {
690         if (gd->flags & GD_FLG_ENV_READY)
691                 return env_get(str);
692         if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
693                 return buf;
694
695         return NULL;
696 }
697
698 /**
699  * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
700  */
701 static void board_get_mtdparts(const char *dev,
702                                char *mtdids,
703                                char *mtdparts)
704 {
705         char env_name[32] = "mtdparts_";
706         char tmp_mtdparts[MTDPARTS_LEN];
707         const char *tmp;
708
709         /* name of env variable to read = mtdparts_<dev> */
710         strcat(env_name, dev);
711         tmp = env_get_mtdparts(env_name, tmp_mtdparts);
712         if (tmp) {
713                 /* mtdids: "<dev>=<dev>, ...." */
714                 if (mtdids[0] != '\0')
715                         strcat(mtdids, ",");
716                 strcat(mtdids, dev);
717                 strcat(mtdids, "=");
718                 strcat(mtdids, dev);
719
720                 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
721                 if (mtdparts[0] != '\0')
722                         strncat(mtdparts, ";", MTDPARTS_LEN);
723                 else
724                         strcat(mtdparts, "mtdparts=");
725                 strncat(mtdparts, dev, MTDPARTS_LEN);
726                 strncat(mtdparts, ":", MTDPARTS_LEN);
727                 strncat(mtdparts, tmp, MTDPARTS_LEN);
728         }
729 }
730
731 void board_mtdparts_default(const char **mtdids, const char **mtdparts)
732 {
733         struct udevice *dev;
734         static char parts[2 * MTDPARTS_LEN + 1];
735         static char ids[MTDIDS_LEN + 1];
736         static bool mtd_initialized;
737
738         if (mtd_initialized) {
739                 *mtdids = ids;
740                 *mtdparts = parts;
741                 return;
742         }
743
744         memset(parts, 0, sizeof(parts));
745         memset(ids, 0, sizeof(ids));
746
747         if (!uclass_get_device(UCLASS_MTD, 0, &dev))
748                 board_get_mtdparts("nand0", ids, parts);
749
750         if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
751                 board_get_mtdparts("nor0", ids, parts);
752
753         mtd_initialized = true;
754         *mtdids = ids;
755         *mtdparts = parts;
756         debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
757 }
758 #endif
759
760 #if defined(CONFIG_OF_BOARD_SETUP)
761 int ft_board_setup(void *blob, bd_t *bd)
762 {
763 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
764         struct node_info nodes[] = {
765                 { "st,stm32f469-qspi",          MTD_DEV_TYPE_NOR,  },
766                 { "st,stm32mp15-fmc2",          MTD_DEV_TYPE_NAND, },
767         };
768         fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
769 #endif
770
771         return 0;
772 }
773 #endif