1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
12 #include <env_internal.h>
14 #include <generic-phy.h>
26 #include <asm/arch/stm32.h>
27 #include <asm/arch/sys_proto.h>
28 #include <jffs2/load_kernel.h>
29 #include <power/regulator.h>
30 #include <usb/dwc2_udc.h>
32 /* SYSCFG registers */
33 #define SYSCFG_BOOTR 0x00
34 #define SYSCFG_PMCSETR 0x04
35 #define SYSCFG_IOCTRLSETR 0x18
36 #define SYSCFG_ICNR 0x1C
37 #define SYSCFG_CMPCR 0x20
38 #define SYSCFG_CMPENSETR 0x24
39 #define SYSCFG_PMCCLRR 0x44
41 #define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
42 #define SYSCFG_BOOTR_BOOTPD_SHIFT 4
44 #define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
45 #define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
46 #define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
47 #define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
48 #define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
50 #define SYSCFG_CMPCR_SW_CTRL BIT(1)
51 #define SYSCFG_CMPCR_READY BIT(8)
53 #define SYSCFG_CMPENSETR_MPU_EN BIT(0)
55 #define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
56 #define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
58 #define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
60 #define SYSCFG_PMCSETR_ETH_SEL_MASK GENMASK(23, 21)
61 #define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0
62 #define SYSCFG_PMCSETR_ETH_SEL_RGMII BIT(21)
63 #define SYSCFG_PMCSETR_ETH_SEL_RMII BIT(23)
66 * Get a global data pointer
68 DECLARE_GLOBAL_DATA_PTR;
70 #define USB_LOW_THRESHOLD_UV 200000
71 #define USB_WARNING_LOW_THRESHOLD_UV 660000
72 #define USB_START_LOW_THRESHOLD_UV 1230000
73 #define USB_START_HIGH_THRESHOLD_UV 2150000
81 const char *fdt_compat;
84 if (IS_ENABLED(CONFIG_STM32MP1_OPTEE))
85 mode = "trusted with OP-TEE";
86 else if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
91 printf("Board: stm32mp1 in %s mode", mode);
92 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
94 if (fdt_compat && fdt_compat_len)
95 printf(" (%s)", fdt_compat);
98 ret = uclass_get_device_by_driver(UCLASS_MISC,
99 DM_GET_DRIVER(stm32mp_bsec),
103 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
106 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
109 ((otp >> 8) & 0xF) - 1 + 'A',
116 static void board_key_check(void)
118 #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
120 struct gpio_desc gpio;
121 enum forced_boot_mode boot_mode = BOOT_NORMAL;
123 node = ofnode_path("/config");
124 if (!ofnode_valid(node)) {
125 debug("%s: no /config node?\n", __func__);
128 #ifdef CONFIG_FASTBOOT
129 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
130 &gpio, GPIOD_IS_IN)) {
131 debug("%s: could not find a /config/st,fastboot-gpios\n",
134 if (dm_gpio_get_value(&gpio)) {
135 puts("Fastboot key pressed, ");
136 boot_mode = BOOT_FASTBOOT;
139 dm_gpio_free(NULL, &gpio);
142 #ifdef CONFIG_CMD_STM32PROG
143 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
144 &gpio, GPIOD_IS_IN)) {
145 debug("%s: could not find a /config/st,stm32prog-gpios\n",
148 if (dm_gpio_get_value(&gpio)) {
149 puts("STM32Programmer key pressed, ");
150 boot_mode = BOOT_STM32PROG;
152 dm_gpio_free(NULL, &gpio);
156 if (boot_mode != BOOT_NORMAL) {
157 puts("entering download mode...\n");
158 clrsetbits_le32(TAMP_BOOT_CONTEXT,
159 TAMP_BOOT_FORCED_MASK,
165 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
167 /* STMicroelectronics STUSB1600 Type-C controller */
168 #define STUSB1600_CC_CONNECTION_STATUS 0x0E
170 /* STUSB1600_CC_CONNECTION_STATUS bitfields */
171 #define STUSB1600_CC_ATTACH BIT(0)
173 static int stusb1600_init(struct udevice **dev_stusb1600)
176 struct udevice *dev, *bus;
180 *dev_stusb1600 = NULL;
182 /* if node stusb1600 is present, means DK1 or DK2 board */
183 node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
184 if (!ofnode_valid(node))
187 ret = ofnode_read_u32(node, "reg", &chip_addr);
191 ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
194 printf("bus for stusb1600 not found\n");
198 ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
200 *dev_stusb1600 = dev;
205 static int stusb1600_cable_connected(struct udevice *dev)
209 if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
212 return status & STUSB1600_CC_ATTACH;
215 #include <usb/dwc2_udc.h>
216 int g_dnl_board_usb_cable_connected(void)
218 struct udevice *stusb1600;
219 struct udevice *dwc2_udc_otg;
222 if (!stusb1600_init(&stusb1600))
223 return stusb1600_cable_connected(stusb1600);
225 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
226 DM_GET_DRIVER(dwc2_udc_otg),
229 debug("dwc2_udc_otg init failed\n");
231 return dwc2_udc_B_session_valid(dwc2_udc_otg);
233 #endif /* CONFIG_USB_GADGET */
235 static int get_led(struct udevice **dev, char *led_string)
240 led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
242 pr_debug("%s: could not find %s config string\n",
243 __func__, led_string);
246 ret = led_get_by_label(led_name, dev);
248 debug("%s: get=%d\n", __func__, ret);
255 static int setup_led(enum led_state_t cmd)
260 ret = get_led(&dev, "u-boot,boot-led");
264 ret = led_set_state(dev, cmd);
268 static int board_check_usb_power(void)
270 struct ofnode_phandle_args adc_args;
276 int min_uV = USB_START_HIGH_THRESHOLD_UV;
277 int ret, uV, adc_count;
280 node = ofnode_path("/config");
281 if (!ofnode_valid(node)) {
282 debug("%s: no /config node?\n", __func__);
287 * Retrieve the ADC channels devices and get measurement
290 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
291 "#io-channel-cells");
293 if (adc_count == -ENOENT)
296 pr_err("%s: can't find adc channel (%d)\n", __func__,
302 for (i = 0; i < adc_count; i++) {
303 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
304 "#io-channel-cells", 0, i,
306 pr_debug("%s: can't find /config/st,adc_usb_pd\n",
311 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
315 pr_err("%s: Can't get adc device(%d)\n", __func__,
320 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
323 pr_err("%s: single shot failed for %s[%d]!\n",
324 __func__, adc->name, adc_args.args[0]);
328 if (!adc_raw_to_uV(adc, raw, &uV)) {
333 pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
334 adc->name, adc_args.args[0], raw, uV);
336 pr_err("%s: Can't get uV value for %s[%d]\n",
337 __func__, adc->name, adc_args.args[0]);
342 * If highest value is inside 1.23 Volts and 2.10 Volts, that means
343 * board is plugged on an USB-C 3A power supply and boot process can
346 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
347 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
348 min_uV <= USB_LOW_THRESHOLD_UV)
351 pr_err("****************************************************\n");
354 * If highest and lowest value are either both below
355 * USB_LOW_THRESHOLD_UV or both above USB_LOW_THRESHOLD_UV, that
356 * means USB TYPE-C is in unattached mode, this is an issue, make
357 * u-boot,error-led blinking and stop boot process.
359 if ((max_uV > USB_LOW_THRESHOLD_UV &&
360 min_uV > USB_LOW_THRESHOLD_UV) ||
361 (max_uV <= USB_LOW_THRESHOLD_UV &&
362 min_uV <= USB_LOW_THRESHOLD_UV)) {
363 pr_err("* ERROR USB TYPE-C connection in unattached mode *\n");
364 pr_err("* Check that USB TYPE-C cable is correctly plugged *\n");
365 /* with 125ms interval, led will blink for 17.02 years ....*/
369 if (max_uV > USB_LOW_THRESHOLD_UV &&
370 max_uV <= USB_WARNING_LOW_THRESHOLD_UV &&
371 min_uV <= USB_LOW_THRESHOLD_UV) {
372 pr_err("* WARNING 500mA power supply detected *\n");
376 if (max_uV > USB_WARNING_LOW_THRESHOLD_UV &&
377 max_uV <= USB_START_LOW_THRESHOLD_UV &&
378 min_uV <= USB_LOW_THRESHOLD_UV) {
379 pr_err("* WARNING 1.5mA power supply detected *\n");
384 * If highest value is above 2.15 Volts that means that the USB TypeC
385 * supplies more than 3 Amp, this is not compliant with TypeC specification
387 if (max_uV > USB_START_HIGH_THRESHOLD_UV) {
388 pr_err("* USB TYPE-C charger not compliant with *\n");
389 pr_err("* specification *\n");
390 pr_err("****************************************************\n\n");
391 /* with 125ms interval, led will blink for 17.02 years ....*/
394 pr_err("* Current too low, use a 3A power supply! *\n");
395 pr_err("****************************************************\n\n");
398 ret = get_led(&led, "u-boot,error-led");
400 /* in unattached case, the boot process must be stopped */
401 if (nb_blink == U32_MAX)
406 /* make u-boot,error-led blinking */
407 for (i = 0; i < nb_blink * 2; i++) {
408 led_set_state(led, LEDST_TOGGLE);
411 led_set_state(led, LEDST_ON);
416 static void sysconf_init(void)
418 #ifndef CONFIG_STM32MP1_TRUSTED
420 #ifdef CONFIG_DM_REGULATOR
421 struct udevice *pwr_dev;
422 struct udevice *pwr_reg;
429 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
431 /* interconnect update : select master using the port 1 */
434 /* today information is hardcoded in U-Boot */
435 writel(BIT(9), syscfg + SYSCFG_ICNR);
437 /* disable Pull-Down for boot pin connected to VDD */
438 bootr = readl(syscfg + SYSCFG_BOOTR);
439 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
440 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
441 writel(bootr, syscfg + SYSCFG_BOOTR);
443 #ifdef CONFIG_DM_REGULATOR
444 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
445 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
446 * The customer will have to disable this for low frequencies
447 * or if AFMUX is selected but the function not used, typically for
448 * TRACE. Otherwise, impact on power consumption.
451 * enabling High Speed mode while VDD>2.7V
452 * with the OTP product_below_2v5 (OTP 18, BIT 13)
453 * erroneously set to 1 can damage the IC!
454 * => U-Boot set the register only if VDD < 2.7V (in DT)
455 * but this value need to be consistent with board design
457 ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
459 ret = uclass_get_device_by_driver(UCLASS_MISC,
460 DM_GET_DRIVER(stm32mp_bsec),
463 pr_err("Can't find stm32mp_bsec driver\n");
467 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
471 /* get VDD = pwr-supply */
472 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
475 /* check if VDD is Low Voltage */
477 if (regulator_get_value(pwr_reg) < 2700000) {
478 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
479 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
480 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
481 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
482 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
483 syscfg + SYSCFG_IOCTRLSETR);
486 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
489 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
492 debug("VDD unknown");
497 /* activate automatic I/O compensation
498 * warning: need to ensure CSI enabled and ready in clock driver
500 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
502 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
504 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
508 /* board dependent setup after realloc */
513 /* address of boot parameters */
514 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
516 /* probe all PINCTRL for hog */
517 for (uclass_first_device(UCLASS_PINCTRL, &dev);
519 uclass_next_device(&dev)) {
520 pr_debug("probe pincontrol = %s\n", dev->name);
525 #ifdef CONFIG_DM_REGULATOR
526 regulators_enable_boot_on(_DEBUG);
531 if (IS_ENABLED(CONFIG_LED))
537 int board_late_init(void)
539 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
540 const void *fdt_compat;
543 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
545 if (fdt_compat && fdt_compat_len) {
546 if (strncmp(fdt_compat, "st,", 3) != 0)
547 env_set("board_name", fdt_compat);
549 env_set("board_name", fdt_compat + 3);
553 /* for DK1/DK2 boards */
554 board_check_usb_power();
559 void board_quiesce_devices(void)
561 setup_led(LEDST_OFF);
564 /* board interface eth init */
565 /* this is a weak define that we are overriding */
566 int board_interface_eth_init(phy_interface_t interface_type,
567 bool eth_clk_sel_reg, bool eth_ref_clk_sel_reg)
572 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
577 switch (interface_type) {
578 case PHY_INTERFACE_MODE_MII:
579 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
580 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
581 debug("%s: PHY_INTERFACE_MODE_MII\n", __func__);
583 case PHY_INTERFACE_MODE_GMII:
585 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII |
586 SYSCFG_PMCSETR_ETH_CLK_SEL;
588 value = SYSCFG_PMCSETR_ETH_SEL_GMII_MII;
589 debug("%s: PHY_INTERFACE_MODE_GMII\n", __func__);
591 case PHY_INTERFACE_MODE_RMII:
592 if (eth_ref_clk_sel_reg)
593 value = SYSCFG_PMCSETR_ETH_SEL_RMII |
594 SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
596 value = SYSCFG_PMCSETR_ETH_SEL_RMII;
597 debug("%s: PHY_INTERFACE_MODE_RMII\n", __func__);
599 case PHY_INTERFACE_MODE_RGMII:
600 case PHY_INTERFACE_MODE_RGMII_ID:
601 case PHY_INTERFACE_MODE_RGMII_RXID:
602 case PHY_INTERFACE_MODE_RGMII_TXID:
604 value = SYSCFG_PMCSETR_ETH_SEL_RGMII |
605 SYSCFG_PMCSETR_ETH_CLK_SEL;
607 value = SYSCFG_PMCSETR_ETH_SEL_RGMII;
608 debug("%s: PHY_INTERFACE_MODE_RGMII\n", __func__);
611 debug("%s: Do not manage %d interface\n",
612 __func__, interface_type);
613 /* Do not manage others interfaces */
617 /* clear and set ETH configuration bits */
618 writel(SYSCFG_PMCSETR_ETH_SEL_MASK | SYSCFG_PMCSETR_ETH_SELMII |
619 SYSCFG_PMCSETR_ETH_REF_CLK_SEL | SYSCFG_PMCSETR_ETH_CLK_SEL,
620 syscfg + SYSCFG_PMCCLRR);
621 writel(value, syscfg + SYSCFG_PMCSETR);
626 enum env_location env_get_location(enum env_operation op, int prio)
628 u32 bootmode = get_bootmode();
633 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
634 #ifdef CONFIG_ENV_IS_IN_EXT4
636 case BOOT_FLASH_EMMC:
639 #ifdef CONFIG_ENV_IS_IN_UBI
640 case BOOT_FLASH_NAND:
643 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
645 return ENVL_SPI_FLASH;
652 #if defined(CONFIG_ENV_IS_IN_EXT4)
653 const char *env_ext4_get_intf(void)
655 u32 bootmode = get_bootmode();
657 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
659 case BOOT_FLASH_EMMC:
666 const char *env_ext4_get_dev_part(void)
668 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
669 u32 bootmode = get_bootmode();
671 return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
675 #ifdef CONFIG_SYS_MTDPARTS_RUNTIME
677 #define MTDPARTS_LEN 256
678 #define MTDIDS_LEN 128
681 * The mtdparts_nand0 and mtdparts_nor0 variable tends to be long.
682 * If we need to access it before the env is relocated, then we need
683 * to use our own stack buffer. gd->env_buf will be too small.
685 * @param buf temporary buffer pointer MTDPARTS_LEN long
686 * @return mtdparts variable string, NULL if not found
688 static const char *env_get_mtdparts(const char *str, char *buf)
690 if (gd->flags & GD_FLG_ENV_READY)
692 if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
699 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
701 static void board_get_mtdparts(const char *dev,
705 char env_name[32] = "mtdparts_";
706 char tmp_mtdparts[MTDPARTS_LEN];
709 /* name of env variable to read = mtdparts_<dev> */
710 strcat(env_name, dev);
711 tmp = env_get_mtdparts(env_name, tmp_mtdparts);
713 /* mtdids: "<dev>=<dev>, ...." */
714 if (mtdids[0] != '\0')
720 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
721 if (mtdparts[0] != '\0')
722 strncat(mtdparts, ";", MTDPARTS_LEN);
724 strcat(mtdparts, "mtdparts=");
725 strncat(mtdparts, dev, MTDPARTS_LEN);
726 strncat(mtdparts, ":", MTDPARTS_LEN);
727 strncat(mtdparts, tmp, MTDPARTS_LEN);
731 void board_mtdparts_default(const char **mtdids, const char **mtdparts)
734 static char parts[2 * MTDPARTS_LEN + 1];
735 static char ids[MTDIDS_LEN + 1];
736 static bool mtd_initialized;
738 if (mtd_initialized) {
744 memset(parts, 0, sizeof(parts));
745 memset(ids, 0, sizeof(ids));
747 if (!uclass_get_device(UCLASS_MTD, 0, &dev))
748 board_get_mtdparts("nand0", ids, parts);
750 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
751 board_get_mtdparts("nor0", ids, parts);
753 mtd_initialized = true;
756 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
760 #if defined(CONFIG_OF_BOARD_SETUP)
761 int ft_board_setup(void *blob, bd_t *bd)
763 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
764 struct node_info nodes[] = {
765 { "st,stm32f469-qspi", MTD_DEV_TYPE_NOR, },
766 { "st,stm32mp15-fmc2", MTD_DEV_TYPE_NAND, },
768 fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));