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