1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
10 #include <environment.h>
12 #include <generic-phy.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>
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
36 #define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
37 #define SYSCFG_BOOTR_BOOTPD_SHIFT 4
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)
45 #define SYSCFG_CMPCR_SW_CTRL BIT(1)
46 #define SYSCFG_CMPCR_READY BIT(8)
48 #define SYSCFG_CMPENSETR_MPU_EN BIT(0)
50 #define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
51 #define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
53 #define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
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)
61 * Get a global data pointer
63 DECLARE_GLOBAL_DATA_PTR;
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
76 const char *fdt_compat;
79 if (IS_ENABLED(CONFIG_STM32MP1_OPTEE))
80 mode = "trusted with OP-TEE";
81 else if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
86 printf("Board: stm32mp1 in %s mode", mode);
87 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
89 if (fdt_compat && fdt_compat_len)
90 printf(" (%s)", fdt_compat);
93 ret = uclass_get_device_by_driver(UCLASS_MISC,
94 DM_GET_DRIVER(stm32mp_bsec),
98 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
101 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
104 ((otp >> 8) & 0xF) - 1 + 'A',
111 static void board_key_check(void)
113 #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
115 struct gpio_desc gpio;
116 enum forced_boot_mode boot_mode = BOOT_NORMAL;
118 node = ofnode_path("/config");
119 if (!ofnode_valid(node)) {
120 debug("%s: no /config node?\n", __func__);
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",
129 if (dm_gpio_get_value(&gpio)) {
130 puts("Fastboot key pressed, ");
131 boot_mode = BOOT_FASTBOOT;
134 dm_gpio_free(NULL, &gpio);
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",
143 if (dm_gpio_get_value(&gpio)) {
144 puts("STM32Programmer key pressed, ");
145 boot_mode = BOOT_STM32PROG;
147 dm_gpio_free(NULL, &gpio);
151 if (boot_mode != BOOT_NORMAL) {
152 puts("entering download mode...\n");
153 clrsetbits_le32(TAMP_BOOT_CONTEXT,
154 TAMP_BOOT_FORCED_MASK,
160 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
162 /* STMicroelectronics STUSB1600 Type-C controller */
163 #define STUSB1600_CC_CONNECTION_STATUS 0x0E
165 /* STUSB1600_CC_CONNECTION_STATUS bitfields */
166 #define STUSB1600_CC_ATTACH BIT(0)
168 static int stusb1600_init(struct udevice **dev_stusb1600)
171 struct udevice *dev, *bus;
175 *dev_stusb1600 = NULL;
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))
182 ret = ofnode_read_u32(node, "reg", &chip_addr);
186 ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
189 printf("bus for stusb1600 not found\n");
193 ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
195 *dev_stusb1600 = dev;
200 static int stusb1600_cable_connected(struct udevice *dev)
204 if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
207 return status & STUSB1600_CC_ATTACH;
210 #include <usb/dwc2_udc.h>
211 int g_dnl_board_usb_cable_connected(void)
213 struct udevice *stusb1600;
214 struct udevice *dwc2_udc_otg;
217 if (!stusb1600_init(&stusb1600))
218 return stusb1600_cable_connected(stusb1600);
220 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
221 DM_GET_DRIVER(dwc2_udc_otg),
224 debug("dwc2_udc_otg init failed\n");
226 return dwc2_udc_B_session_valid(dwc2_udc_otg);
228 #endif /* CONFIG_USB_GADGET */
230 static int get_led(struct udevice **dev, char *led_string)
235 led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
237 pr_debug("%s: could not find %s config string\n",
238 __func__, led_string);
241 ret = led_get_by_label(led_name, dev);
243 debug("%s: get=%d\n", __func__, ret);
250 static int setup_led(enum led_state_t cmd)
255 ret = get_led(&dev, "u-boot,boot-led");
259 ret = led_set_state(dev, cmd);
263 static int board_check_usb_power(void)
265 struct ofnode_phandle_args adc_args;
271 int min_uV = USB_START_HIGH_THRESHOLD_UV;
272 int ret, uV, adc_count;
275 node = ofnode_path("/config");
276 if (!ofnode_valid(node)) {
277 debug("%s: no /config node?\n", __func__);
282 * Retrieve the ADC channels devices and get measurement
285 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
286 "#io-channel-cells");
288 if (adc_count == -ENOENT)
291 pr_err("%s: can't find adc channel (%d)\n", __func__,
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,
301 pr_debug("%s: can't find /config/st,adc_usb_pd\n",
306 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
310 pr_err("%s: Can't get adc device(%d)\n", __func__,
315 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
318 pr_err("%s: single shot failed for %s[%d]!\n",
319 __func__, adc->name, adc_args.args[0]);
323 if (!adc_raw_to_uV(adc, raw, &uV)) {
328 pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
329 adc->name, adc_args.args[0], raw, uV);
331 pr_err("%s: Can't get uV value for %s[%d]\n",
332 __func__, adc->name, adc_args.args[0]);
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
341 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
342 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
343 min_uV <= USB_LOW_THRESHOLD_UV)
346 pr_err("****************************************************\n");
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.
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 ....*/
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");
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");
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
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 ....*/
389 pr_err("* Current too low, use a 3A power supply! *\n");
390 pr_err("****************************************************\n\n");
393 ret = get_led(&led, "u-boot,error-led");
395 /* in unattached case, the boot process must be stopped */
396 if (nb_blink == U32_MAX)
401 /* make u-boot,error-led blinking */
402 for (i = 0; i < nb_blink * 2; i++) {
403 led_set_state(led, LEDST_TOGGLE);
406 led_set_state(led, LEDST_ON);
411 static void sysconf_init(void)
413 #ifndef CONFIG_STM32MP1_TRUSTED
415 #ifdef CONFIG_DM_REGULATOR
416 struct udevice *pwr_dev;
417 struct udevice *pwr_reg;
424 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
426 /* interconnect update : select master using the port 1 */
429 /* today information is hardcoded in U-Boot */
430 writel(BIT(9), syscfg + SYSCFG_ICNR);
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);
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.
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
452 ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
454 ret = uclass_get_device_by_driver(UCLASS_MISC,
455 DM_GET_DRIVER(stm32mp_bsec),
458 pr_err("Can't find stm32mp_bsec driver\n");
462 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
466 /* get VDD = pwr-supply */
467 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
470 /* check if VDD is Low Voltage */
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);
481 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
484 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
487 debug("VDD unknown");
492 /* activate automatic I/O compensation
493 * warning: need to ensure CSI enabled and ready in clock driver
495 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
497 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
499 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
503 /* board dependent setup after realloc */
508 /* address of boot parameters */
509 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
511 /* probe all PINCTRL for hog */
512 for (uclass_first_device(UCLASS_PINCTRL, &dev);
514 uclass_next_device(&dev)) {
515 pr_debug("probe pincontrol = %s\n", dev->name);
520 #ifdef CONFIG_DM_REGULATOR
521 regulators_enable_boot_on(_DEBUG);
526 if (IS_ENABLED(CONFIG_LED))
532 int board_late_init(void)
534 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
535 const void *fdt_compat;
538 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
540 if (fdt_compat && fdt_compat_len) {
541 if (strncmp(fdt_compat, "st,", 3) != 0)
542 env_set("board_name", fdt_compat);
544 env_set("board_name", fdt_compat + 3);
548 /* for DK1/DK2 boards */
549 board_check_usb_power();
554 void board_quiesce_devices(void)
556 setup_led(LEDST_OFF);
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)
567 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
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__);
578 case PHY_INTERFACE_MODE_GMII:
580 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
581 SYSCFG_PMCSETR_ETH_CLK_SEL;
583 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
584 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
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;
591 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
592 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
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:
599 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
600 SYSCFG_PMCSETR_ETH_CLK_SEL;
602 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
603 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
606 debug("%s: Do not manage %d interface\n",
607 __func__, interface_type);
608 /* Do not manage others interfaces */
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);
621 enum env_location env_get_location(enum env_operation op, int prio)
623 u32 bootmode = get_bootmode();
628 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
629 #ifdef CONFIG_ENV_IS_IN_EXT4
631 case BOOT_FLASH_EMMC:
634 #ifdef CONFIG_ENV_IS_IN_UBI
635 case BOOT_FLASH_NAND:
638 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
640 return ENVL_SPI_FLASH;
647 #if defined(CONFIG_ENV_IS_IN_EXT4)
648 const char *env_ext4_get_intf(void)
650 u32 bootmode = get_bootmode();
652 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
654 case BOOT_FLASH_EMMC:
661 const char *env_ext4_get_dev_part(void)
663 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
664 u32 bootmode = get_bootmode();
666 return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
670 #ifdef CONFIG_SYS_MTDPARTS_RUNTIME
672 #define MTDPARTS_LEN 256
673 #define MTDIDS_LEN 128
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.
680 * @param buf temporary buffer pointer MTDPARTS_LEN long
681 * @return mtdparts variable string, NULL if not found
683 static const char *env_get_mtdparts(const char *str, char *buf)
685 if (gd->flags & GD_FLG_ENV_READY)
687 if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
694 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
696 static void board_get_mtdparts(const char *dev,
700 char env_name[32] = "mtdparts_";
701 char tmp_mtdparts[MTDPARTS_LEN];
704 /* name of env variable to read = mtdparts_<dev> */
705 strcat(env_name, dev);
706 tmp = env_get_mtdparts(env_name, tmp_mtdparts);
708 /* mtdids: "<dev>=<dev>, ...." */
709 if (mtdids[0] != '\0')
715 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
716 if (mtdparts[0] != '\0')
717 strncat(mtdparts, ";", MTDPARTS_LEN);
719 strcat(mtdparts, "mtdparts=");
720 strncat(mtdparts, dev, MTDPARTS_LEN);
721 strncat(mtdparts, ":", MTDPARTS_LEN);
722 strncat(mtdparts, tmp, MTDPARTS_LEN);
726 void board_mtdparts_default(const char **mtdids, const char **mtdparts)
729 static char parts[2 * MTDPARTS_LEN + 1];
730 static char ids[MTDIDS_LEN + 1];
731 static bool mtd_initialized;
733 if (mtd_initialized) {
739 memset(parts, 0, sizeof(parts));
740 memset(ids, 0, sizeof(ids));
742 if (!uclass_get_device(UCLASS_MTD, 0, &dev))
743 board_get_mtdparts("nand0", ids, parts);
745 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
746 board_get_mtdparts("nor0", ids, parts);
748 mtd_initialized = true;
751 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);