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