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>
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>
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
39 #define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
40 #define SYSCFG_BOOTR_BOOTPD_SHIFT 4
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)
48 #define SYSCFG_CMPCR_SW_CTRL BIT(1)
49 #define SYSCFG_CMPCR_READY BIT(8)
51 #define SYSCFG_CMPENSETR_MPU_EN BIT(0)
53 #define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
54 #define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
56 #define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
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)
64 * Get a global data pointer
66 DECLARE_GLOBAL_DATA_PTR;
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
79 const char *fdt_compat;
82 if (IS_ENABLED(CONFIG_STM32MP1_OPTEE))
83 mode = "trusted with OP-TEE";
84 else if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
89 printf("Board: stm32mp1 in %s mode", mode);
90 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
92 if (fdt_compat && fdt_compat_len)
93 printf(" (%s)", fdt_compat);
96 ret = uclass_get_device_by_driver(UCLASS_MISC,
97 DM_GET_DRIVER(stm32mp_bsec),
101 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
104 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
107 ((otp >> 8) & 0xF) - 1 + 'A',
114 static void board_key_check(void)
116 #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
118 struct gpio_desc gpio;
119 enum forced_boot_mode boot_mode = BOOT_NORMAL;
121 node = ofnode_path("/config");
122 if (!ofnode_valid(node)) {
123 debug("%s: no /config node?\n", __func__);
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",
132 if (dm_gpio_get_value(&gpio)) {
133 puts("Fastboot key pressed, ");
134 boot_mode = BOOT_FASTBOOT;
137 dm_gpio_free(NULL, &gpio);
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",
146 if (dm_gpio_get_value(&gpio)) {
147 puts("STM32Programmer key pressed, ");
148 boot_mode = BOOT_STM32PROG;
150 dm_gpio_free(NULL, &gpio);
154 if (boot_mode != BOOT_NORMAL) {
155 puts("entering download mode...\n");
156 clrsetbits_le32(TAMP_BOOT_CONTEXT,
157 TAMP_BOOT_FORCED_MASK,
163 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
165 /* STMicroelectronics STUSB1600 Type-C controller */
166 #define STUSB1600_CC_CONNECTION_STATUS 0x0E
168 /* STUSB1600_CC_CONNECTION_STATUS bitfields */
169 #define STUSB1600_CC_ATTACH BIT(0)
171 static int stusb1600_init(struct udevice **dev_stusb1600)
174 struct udevice *dev, *bus;
178 *dev_stusb1600 = NULL;
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))
185 ret = ofnode_read_u32(node, "reg", &chip_addr);
189 ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
192 printf("bus for stusb1600 not found\n");
196 ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
198 *dev_stusb1600 = dev;
203 static int stusb1600_cable_connected(struct udevice *dev)
207 if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
210 return status & STUSB1600_CC_ATTACH;
213 #include <usb/dwc2_udc.h>
214 int g_dnl_board_usb_cable_connected(void)
216 struct udevice *stusb1600;
217 struct udevice *dwc2_udc_otg;
220 if (!stusb1600_init(&stusb1600))
221 return stusb1600_cable_connected(stusb1600);
223 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
224 DM_GET_DRIVER(dwc2_udc_otg),
227 debug("dwc2_udc_otg init failed\n");
229 return dwc2_udc_B_session_valid(dwc2_udc_otg);
231 #endif /* CONFIG_USB_GADGET */
233 static int get_led(struct udevice **dev, char *led_string)
238 led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
240 pr_debug("%s: could not find %s config string\n",
241 __func__, led_string);
244 ret = led_get_by_label(led_name, dev);
246 debug("%s: get=%d\n", __func__, ret);
253 static int setup_led(enum led_state_t cmd)
258 ret = get_led(&dev, "u-boot,boot-led");
262 ret = led_set_state(dev, cmd);
266 static int board_check_usb_power(void)
268 struct ofnode_phandle_args adc_args;
274 int min_uV = USB_START_HIGH_THRESHOLD_UV;
275 int ret, uV, adc_count;
278 node = ofnode_path("/config");
279 if (!ofnode_valid(node)) {
280 debug("%s: no /config node?\n", __func__);
285 * Retrieve the ADC channels devices and get measurement
288 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
289 "#io-channel-cells");
291 if (adc_count == -ENOENT)
294 pr_err("%s: can't find adc channel (%d)\n", __func__,
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,
304 pr_debug("%s: can't find /config/st,adc_usb_pd\n",
309 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
313 pr_err("%s: Can't get adc device(%d)\n", __func__,
318 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
321 pr_err("%s: single shot failed for %s[%d]!\n",
322 __func__, adc->name, adc_args.args[0]);
326 if (!adc_raw_to_uV(adc, raw, &uV)) {
331 pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
332 adc->name, adc_args.args[0], raw, uV);
334 pr_err("%s: Can't get uV value for %s[%d]\n",
335 __func__, adc->name, adc_args.args[0]);
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
344 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
345 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
346 min_uV <= USB_LOW_THRESHOLD_UV)
349 pr_err("****************************************************\n");
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.
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 ....*/
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");
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");
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
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 ....*/
392 pr_err("* Current too low, use a 3A power supply! *\n");
393 pr_err("****************************************************\n\n");
396 ret = get_led(&led, "u-boot,error-led");
398 /* in unattached case, the boot process must be stopped */
399 if (nb_blink == U32_MAX)
404 /* make u-boot,error-led blinking */
405 for (i = 0; i < nb_blink * 2; i++) {
406 led_set_state(led, LEDST_TOGGLE);
409 led_set_state(led, LEDST_ON);
414 static void sysconf_init(void)
416 #ifndef CONFIG_STM32MP1_TRUSTED
418 #ifdef CONFIG_DM_REGULATOR
419 struct udevice *pwr_dev;
420 struct udevice *pwr_reg;
427 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
429 /* interconnect update : select master using the port 1 */
432 /* today information is hardcoded in U-Boot */
433 writel(BIT(9), syscfg + SYSCFG_ICNR);
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);
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.
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
455 ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
457 ret = uclass_get_device_by_driver(UCLASS_MISC,
458 DM_GET_DRIVER(stm32mp_bsec),
461 pr_err("Can't find stm32mp_bsec driver\n");
465 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
469 /* get VDD = pwr-supply */
470 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
473 /* check if VDD is Low Voltage */
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);
484 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
487 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
490 debug("VDD unknown");
495 /* activate automatic I/O compensation
496 * warning: need to ensure CSI enabled and ready in clock driver
498 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
500 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
502 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
506 /* board dependent setup after realloc */
511 /* address of boot parameters */
512 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
514 /* probe all PINCTRL for hog */
515 for (uclass_first_device(UCLASS_PINCTRL, &dev);
517 uclass_next_device(&dev)) {
518 pr_debug("probe pincontrol = %s\n", dev->name);
523 #ifdef CONFIG_DM_REGULATOR
524 regulators_enable_boot_on(_DEBUG);
529 if (IS_ENABLED(CONFIG_LED))
535 int board_late_init(void)
537 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
538 const void *fdt_compat;
541 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
543 if (fdt_compat && fdt_compat_len) {
544 if (strncmp(fdt_compat, "st,", 3) != 0)
545 env_set("board_name", fdt_compat);
547 env_set("board_name", fdt_compat + 3);
551 /* for DK1/DK2 boards */
552 board_check_usb_power();
557 void board_quiesce_devices(void)
559 setup_led(LEDST_OFF);
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)
570 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
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__);
581 case PHY_INTERFACE_MODE_GMII:
583 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
584 SYSCFG_PMCSETR_ETH_CLK_SEL;
586 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
587 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
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;
594 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
595 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
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:
602 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
603 SYSCFG_PMCSETR_ETH_CLK_SEL;
605 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
606 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
609 debug("%s: Do not manage %d interface\n",
610 __func__, interface_type);
611 /* Do not manage others interfaces */
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);
624 enum env_location env_get_location(enum env_operation op, int prio)
626 u32 bootmode = get_bootmode();
631 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
632 #ifdef CONFIG_ENV_IS_IN_EXT4
634 case BOOT_FLASH_EMMC:
637 #ifdef CONFIG_ENV_IS_IN_UBI
638 case BOOT_FLASH_NAND:
641 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
643 return ENVL_SPI_FLASH;
650 #if defined(CONFIG_ENV_IS_IN_EXT4)
651 const char *env_ext4_get_intf(void)
653 u32 bootmode = get_bootmode();
655 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
657 case BOOT_FLASH_EMMC:
664 const char *env_ext4_get_dev_part(void)
666 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
667 u32 bootmode = get_bootmode();
669 return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
673 #ifdef CONFIG_SYS_MTDPARTS_RUNTIME
675 #define MTDPARTS_LEN 256
676 #define MTDIDS_LEN 128
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.
683 * @param buf temporary buffer pointer MTDPARTS_LEN long
684 * @return mtdparts variable string, NULL if not found
686 static const char *env_get_mtdparts(const char *str, char *buf)
688 if (gd->flags & GD_FLG_ENV_READY)
690 if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
697 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
699 static void board_get_mtdparts(const char *dev,
703 char env_name[32] = "mtdparts_";
704 char tmp_mtdparts[MTDPARTS_LEN];
707 /* name of env variable to read = mtdparts_<dev> */
708 strcat(env_name, dev);
709 tmp = env_get_mtdparts(env_name, tmp_mtdparts);
711 /* mtdids: "<dev>=<dev>, ...." */
712 if (mtdids[0] != '\0')
718 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
719 if (mtdparts[0] != '\0')
720 strncat(mtdparts, ";", MTDPARTS_LEN);
722 strcat(mtdparts, "mtdparts=");
723 strncat(mtdparts, dev, MTDPARTS_LEN);
724 strncat(mtdparts, ":", MTDPARTS_LEN);
725 strncat(mtdparts, tmp, MTDPARTS_LEN);
729 void board_mtdparts_default(const char **mtdids, const char **mtdparts)
732 static char parts[2 * MTDPARTS_LEN + 1];
733 static char ids[MTDIDS_LEN + 1];
734 static bool mtd_initialized;
736 if (mtd_initialized) {
742 memset(parts, 0, sizeof(parts));
743 memset(ids, 0, sizeof(ids));
745 if (!uclass_get_device(UCLASS_MTD, 0, &dev))
746 board_get_mtdparts("nand0", ids, parts);
748 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
749 board_get_mtdparts("nor0", ids, parts);
751 mtd_initialized = true;
754 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
758 #if defined(CONFIG_OF_BOARD_SETUP)
759 int ft_board_setup(void *blob, bd_t *bd)
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, },
766 fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));