1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
11 #include <environment.h>
13 #include <generic-phy.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>
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
40 #define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
41 #define SYSCFG_BOOTR_BOOTPD_SHIFT 4
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)
49 #define SYSCFG_CMPCR_SW_CTRL BIT(1)
50 #define SYSCFG_CMPCR_READY BIT(8)
52 #define SYSCFG_CMPENSETR_MPU_EN BIT(0)
54 #define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
55 #define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
57 #define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
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)
65 * Get a global data pointer
67 DECLARE_GLOBAL_DATA_PTR;
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
80 const char *fdt_compat;
83 if (IS_ENABLED(CONFIG_STM32MP1_OPTEE))
84 mode = "trusted with OP-TEE";
85 else if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
90 printf("Board: stm32mp1 in %s mode", mode);
91 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
93 if (fdt_compat && fdt_compat_len)
94 printf(" (%s)", fdt_compat);
97 ret = uclass_get_device_by_driver(UCLASS_MISC,
98 DM_GET_DRIVER(stm32mp_bsec),
102 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
105 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
108 ((otp >> 8) & 0xF) - 1 + 'A',
115 static void board_key_check(void)
117 #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
119 struct gpio_desc gpio;
120 enum forced_boot_mode boot_mode = BOOT_NORMAL;
122 node = ofnode_path("/config");
123 if (!ofnode_valid(node)) {
124 debug("%s: no /config node?\n", __func__);
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",
133 if (dm_gpio_get_value(&gpio)) {
134 puts("Fastboot key pressed, ");
135 boot_mode = BOOT_FASTBOOT;
138 dm_gpio_free(NULL, &gpio);
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",
147 if (dm_gpio_get_value(&gpio)) {
148 puts("STM32Programmer key pressed, ");
149 boot_mode = BOOT_STM32PROG;
151 dm_gpio_free(NULL, &gpio);
155 if (boot_mode != BOOT_NORMAL) {
156 puts("entering download mode...\n");
157 clrsetbits_le32(TAMP_BOOT_CONTEXT,
158 TAMP_BOOT_FORCED_MASK,
164 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
166 /* STMicroelectronics STUSB1600 Type-C controller */
167 #define STUSB1600_CC_CONNECTION_STATUS 0x0E
169 /* STUSB1600_CC_CONNECTION_STATUS bitfields */
170 #define STUSB1600_CC_ATTACH BIT(0)
172 static int stusb1600_init(struct udevice **dev_stusb1600)
175 struct udevice *dev, *bus;
179 *dev_stusb1600 = NULL;
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))
186 ret = ofnode_read_u32(node, "reg", &chip_addr);
190 ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
193 printf("bus for stusb1600 not found\n");
197 ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
199 *dev_stusb1600 = dev;
204 static int stusb1600_cable_connected(struct udevice *dev)
208 if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
211 return status & STUSB1600_CC_ATTACH;
214 #include <usb/dwc2_udc.h>
215 int g_dnl_board_usb_cable_connected(void)
217 struct udevice *stusb1600;
218 struct udevice *dwc2_udc_otg;
221 if (!stusb1600_init(&stusb1600))
222 return stusb1600_cable_connected(stusb1600);
224 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
225 DM_GET_DRIVER(dwc2_udc_otg),
228 debug("dwc2_udc_otg init failed\n");
230 return dwc2_udc_B_session_valid(dwc2_udc_otg);
232 #endif /* CONFIG_USB_GADGET */
234 static int get_led(struct udevice **dev, char *led_string)
239 led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
241 pr_debug("%s: could not find %s config string\n",
242 __func__, led_string);
245 ret = led_get_by_label(led_name, dev);
247 debug("%s: get=%d\n", __func__, ret);
254 static int setup_led(enum led_state_t cmd)
259 ret = get_led(&dev, "u-boot,boot-led");
263 ret = led_set_state(dev, cmd);
267 static int board_check_usb_power(void)
269 struct ofnode_phandle_args adc_args;
275 int min_uV = USB_START_HIGH_THRESHOLD_UV;
276 int ret, uV, adc_count;
279 node = ofnode_path("/config");
280 if (!ofnode_valid(node)) {
281 debug("%s: no /config node?\n", __func__);
286 * Retrieve the ADC channels devices and get measurement
289 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
290 "#io-channel-cells");
292 if (adc_count == -ENOENT)
295 pr_err("%s: can't find adc channel (%d)\n", __func__,
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,
305 pr_debug("%s: can't find /config/st,adc_usb_pd\n",
310 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
314 pr_err("%s: Can't get adc device(%d)\n", __func__,
319 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
322 pr_err("%s: single shot failed for %s[%d]!\n",
323 __func__, adc->name, adc_args.args[0]);
327 if (!adc_raw_to_uV(adc, raw, &uV)) {
332 pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
333 adc->name, adc_args.args[0], raw, uV);
335 pr_err("%s: Can't get uV value for %s[%d]\n",
336 __func__, adc->name, adc_args.args[0]);
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
345 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
346 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
347 min_uV <= USB_LOW_THRESHOLD_UV)
350 pr_err("****************************************************\n");
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.
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 ....*/
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");
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");
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
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 ....*/
393 pr_err("* Current too low, use a 3A power supply! *\n");
394 pr_err("****************************************************\n\n");
397 ret = get_led(&led, "u-boot,error-led");
399 /* in unattached case, the boot process must be stopped */
400 if (nb_blink == U32_MAX)
405 /* make u-boot,error-led blinking */
406 for (i = 0; i < nb_blink * 2; i++) {
407 led_set_state(led, LEDST_TOGGLE);
410 led_set_state(led, LEDST_ON);
415 static void sysconf_init(void)
417 #ifndef CONFIG_STM32MP1_TRUSTED
419 #ifdef CONFIG_DM_REGULATOR
420 struct udevice *pwr_dev;
421 struct udevice *pwr_reg;
428 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
430 /* interconnect update : select master using the port 1 */
433 /* today information is hardcoded in U-Boot */
434 writel(BIT(9), syscfg + SYSCFG_ICNR);
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);
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.
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
456 ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
458 ret = uclass_get_device_by_driver(UCLASS_MISC,
459 DM_GET_DRIVER(stm32mp_bsec),
462 pr_err("Can't find stm32mp_bsec driver\n");
466 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
470 /* get VDD = pwr-supply */
471 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
474 /* check if VDD is Low Voltage */
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);
485 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
488 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
491 debug("VDD unknown");
496 /* activate automatic I/O compensation
497 * warning: need to ensure CSI enabled and ready in clock driver
499 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
501 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
503 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
507 /* board dependent setup after realloc */
512 /* address of boot parameters */
513 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
515 /* probe all PINCTRL for hog */
516 for (uclass_first_device(UCLASS_PINCTRL, &dev);
518 uclass_next_device(&dev)) {
519 pr_debug("probe pincontrol = %s\n", dev->name);
524 #ifdef CONFIG_DM_REGULATOR
525 regulators_enable_boot_on(_DEBUG);
530 if (IS_ENABLED(CONFIG_LED))
536 int board_late_init(void)
538 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
539 const void *fdt_compat;
542 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
544 if (fdt_compat && fdt_compat_len) {
545 if (strncmp(fdt_compat, "st,", 3) != 0)
546 env_set("board_name", fdt_compat);
548 env_set("board_name", fdt_compat + 3);
552 /* for DK1/DK2 boards */
553 board_check_usb_power();
558 void board_quiesce_devices(void)
560 setup_led(LEDST_OFF);
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)
571 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
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__);
582 case PHY_INTERFACE_MODE_GMII:
584 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
585 SYSCFG_PMCSETR_ETH_CLK_SEL;
587 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
588 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
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;
595 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
596 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
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:
603 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
604 SYSCFG_PMCSETR_ETH_CLK_SEL;
606 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
607 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
610 debug("%s: Do not manage %d interface\n",
611 __func__, interface_type);
612 /* Do not manage others interfaces */
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);
625 enum env_location env_get_location(enum env_operation op, int prio)
627 u32 bootmode = get_bootmode();
632 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
633 #ifdef CONFIG_ENV_IS_IN_EXT4
635 case BOOT_FLASH_EMMC:
638 #ifdef CONFIG_ENV_IS_IN_UBI
639 case BOOT_FLASH_NAND:
642 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
644 return ENVL_SPI_FLASH;
651 #if defined(CONFIG_ENV_IS_IN_EXT4)
652 const char *env_ext4_get_intf(void)
654 u32 bootmode = get_bootmode();
656 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
658 case BOOT_FLASH_EMMC:
665 const char *env_ext4_get_dev_part(void)
667 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
668 u32 bootmode = get_bootmode();
670 return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
674 #ifdef CONFIG_SYS_MTDPARTS_RUNTIME
676 #define MTDPARTS_LEN 256
677 #define MTDIDS_LEN 128
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.
684 * @param buf temporary buffer pointer MTDPARTS_LEN long
685 * @return mtdparts variable string, NULL if not found
687 static const char *env_get_mtdparts(const char *str, char *buf)
689 if (gd->flags & GD_FLG_ENV_READY)
691 if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
698 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
700 static void board_get_mtdparts(const char *dev,
704 char env_name[32] = "mtdparts_";
705 char tmp_mtdparts[MTDPARTS_LEN];
708 /* name of env variable to read = mtdparts_<dev> */
709 strcat(env_name, dev);
710 tmp = env_get_mtdparts(env_name, tmp_mtdparts);
712 /* mtdids: "<dev>=<dev>, ...." */
713 if (mtdids[0] != '\0')
719 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
720 if (mtdparts[0] != '\0')
721 strncat(mtdparts, ";", MTDPARTS_LEN);
723 strcat(mtdparts, "mtdparts=");
724 strncat(mtdparts, dev, MTDPARTS_LEN);
725 strncat(mtdparts, ":", MTDPARTS_LEN);
726 strncat(mtdparts, tmp, MTDPARTS_LEN);
730 void board_mtdparts_default(const char **mtdids, const char **mtdparts)
733 static char parts[2 * MTDPARTS_LEN + 1];
734 static char ids[MTDIDS_LEN + 1];
735 static bool mtd_initialized;
737 if (mtd_initialized) {
743 memset(parts, 0, sizeof(parts));
744 memset(ids, 0, sizeof(ids));
746 if (!uclass_get_device(UCLASS_MTD, 0, &dev))
747 board_get_mtdparts("nand0", ids, parts);
749 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
750 board_get_mtdparts("nor0", ids, parts);
752 mtd_initialized = true;
755 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
759 #if defined(CONFIG_OF_BOARD_SETUP)
760 int ft_board_setup(void *blob, bd_t *bd)
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, },
767 fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));