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