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 << 21)
57 #define SYSCFG_PMCSETR_ETH_SEL_RGMII (1 << 21)
58 #define SYSCFG_PMCSETR_ETH_SEL_RMII (4 << 21)
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_TRUSTED))
84 printf("Board: stm32mp1 in %s mode", mode);
85 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
87 if (fdt_compat && fdt_compat_len)
88 printf(" (%s)", fdt_compat);
91 ret = uclass_get_device_by_driver(UCLASS_MISC,
92 DM_GET_DRIVER(stm32mp_bsec),
96 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
99 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
102 ((otp >> 8) & 0xF) - 1 + 'A',
109 static void board_key_check(void)
111 #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
113 struct gpio_desc gpio;
114 enum forced_boot_mode boot_mode = BOOT_NORMAL;
116 node = ofnode_path("/config");
117 if (!ofnode_valid(node)) {
118 debug("%s: no /config node?\n", __func__);
121 #ifdef CONFIG_FASTBOOT
122 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
123 &gpio, GPIOD_IS_IN)) {
124 debug("%s: could not find a /config/st,fastboot-gpios\n",
127 if (dm_gpio_get_value(&gpio)) {
128 puts("Fastboot key pressed, ");
129 boot_mode = BOOT_FASTBOOT;
132 dm_gpio_free(NULL, &gpio);
135 #ifdef CONFIG_CMD_STM32PROG
136 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
137 &gpio, GPIOD_IS_IN)) {
138 debug("%s: could not find a /config/st,stm32prog-gpios\n",
141 if (dm_gpio_get_value(&gpio)) {
142 puts("STM32Programmer key pressed, ");
143 boot_mode = BOOT_STM32PROG;
145 dm_gpio_free(NULL, &gpio);
149 if (boot_mode != BOOT_NORMAL) {
150 puts("entering download mode...\n");
151 clrsetbits_le32(TAMP_BOOT_CONTEXT,
152 TAMP_BOOT_FORCED_MASK,
158 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
160 /* STMicroelectronics STUSB1600 Type-C controller */
161 #define STUSB1600_CC_CONNECTION_STATUS 0x0E
163 /* STUSB1600_CC_CONNECTION_STATUS bitfields */
164 #define STUSB1600_CC_ATTACH BIT(0)
166 static int stusb1600_init(struct udevice **dev_stusb1600)
169 struct udevice *dev, *bus;
173 *dev_stusb1600 = NULL;
175 /* if node stusb1600 is present, means DK1 or DK2 board */
176 node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
177 if (!ofnode_valid(node))
180 ret = ofnode_read_u32(node, "reg", &chip_addr);
184 ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
187 printf("bus for stusb1600 not found\n");
191 ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
193 *dev_stusb1600 = dev;
198 static int stusb1600_cable_connected(struct udevice *dev)
202 if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
205 return status & STUSB1600_CC_ATTACH;
208 #include <usb/dwc2_udc.h>
209 int g_dnl_board_usb_cable_connected(void)
211 struct udevice *stusb1600;
212 struct udevice *dwc2_udc_otg;
215 if (!stusb1600_init(&stusb1600))
216 return stusb1600_cable_connected(stusb1600);
218 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
219 DM_GET_DRIVER(dwc2_udc_otg),
222 debug("dwc2_udc_otg init failed\n");
224 return dwc2_udc_B_session_valid(dwc2_udc_otg);
226 #endif /* CONFIG_USB_GADGET */
228 static int get_led(struct udevice **dev, char *led_string)
233 led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
235 pr_debug("%s: could not find %s config string\n",
236 __func__, led_string);
239 ret = led_get_by_label(led_name, dev);
241 debug("%s: get=%d\n", __func__, ret);
248 static int setup_led(enum led_state_t cmd)
253 ret = get_led(&dev, "u-boot,boot-led");
257 ret = led_set_state(dev, cmd);
261 static int board_check_usb_power(void)
263 struct ofnode_phandle_args adc_args;
269 int min_uV = USB_START_HIGH_THRESHOLD_UV;
270 int ret, uV, adc_count;
273 node = ofnode_path("/config");
274 if (!ofnode_valid(node)) {
275 debug("%s: no /config node?\n", __func__);
280 * Retrieve the ADC channels devices and get measurement
283 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
284 "#io-channel-cells");
286 if (adc_count == -ENOENT)
289 pr_err("%s: can't find adc channel (%d)\n", __func__,
295 for (i = 0; i < adc_count; i++) {
296 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
297 "#io-channel-cells", 0, i,
299 pr_debug("%s: can't find /config/st,adc_usb_pd\n",
304 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
308 pr_err("%s: Can't get adc device(%d)\n", __func__,
313 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
316 pr_err("%s: single shot failed for %s[%d]!\n",
317 __func__, adc->name, adc_args.args[0]);
321 if (!adc_raw_to_uV(adc, raw, &uV)) {
326 pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
327 adc->name, adc_args.args[0], raw, uV);
329 pr_err("%s: Can't get uV value for %s[%d]\n",
330 __func__, adc->name, adc_args.args[0]);
335 * If highest value is inside 1.23 Volts and 2.10 Volts, that means
336 * board is plugged on an USB-C 3A power supply and boot process can
339 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
340 max_uV <= USB_START_HIGH_THRESHOLD_UV &&
341 min_uV <= USB_LOW_THRESHOLD_UV)
344 pr_err("****************************************************\n");
347 * If highest and lowest value are either both below
348 * USB_LOW_THRESHOLD_UV or both above USB_LOW_THRESHOLD_UV, that
349 * means USB TYPE-C is in unattached mode, this is an issue, make
350 * u-boot,error-led blinking and stop boot process.
352 if ((max_uV > USB_LOW_THRESHOLD_UV &&
353 min_uV > USB_LOW_THRESHOLD_UV) ||
354 (max_uV <= USB_LOW_THRESHOLD_UV &&
355 min_uV <= USB_LOW_THRESHOLD_UV)) {
356 pr_err("* ERROR USB TYPE-C connection in unattached mode *\n");
357 pr_err("* Check that USB TYPE-C cable is correctly plugged *\n");
358 /* with 125ms interval, led will blink for 17.02 years ....*/
362 if (max_uV > USB_LOW_THRESHOLD_UV &&
363 max_uV <= USB_WARNING_LOW_THRESHOLD_UV &&
364 min_uV <= USB_LOW_THRESHOLD_UV) {
365 pr_err("* WARNING 500mA power supply detected *\n");
369 if (max_uV > USB_WARNING_LOW_THRESHOLD_UV &&
370 max_uV <= USB_START_LOW_THRESHOLD_UV &&
371 min_uV <= USB_LOW_THRESHOLD_UV) {
372 pr_err("* WARNING 1.5mA power supply detected *\n");
377 * If highest value is above 2.15 Volts that means that the USB TypeC
378 * supplies more than 3 Amp, this is not compliant with TypeC specification
380 if (max_uV > USB_START_HIGH_THRESHOLD_UV) {
381 pr_err("* USB TYPE-C charger not compliant with *\n");
382 pr_err("* specification *\n");
383 pr_err("****************************************************\n\n");
384 /* with 125ms interval, led will blink for 17.02 years ....*/
387 pr_err("* Current too low, use a 3A power supply! *\n");
388 pr_err("****************************************************\n\n");
391 ret = get_led(&led, "u-boot,error-led");
393 /* in unattached case, the boot process must be stopped */
394 if (nb_blink == U32_MAX)
399 /* make u-boot,error-led blinking */
400 for (i = 0; i < nb_blink * 2; i++) {
401 led_set_state(led, LEDST_TOGGLE);
404 led_set_state(led, LEDST_ON);
409 static void sysconf_init(void)
411 #ifndef CONFIG_STM32MP1_TRUSTED
413 #ifdef CONFIG_DM_REGULATOR
414 struct udevice *pwr_dev;
415 struct udevice *pwr_reg;
422 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
424 /* interconnect update : select master using the port 1 */
427 /* today information is hardcoded in U-Boot */
428 writel(BIT(9), syscfg + SYSCFG_ICNR);
430 /* disable Pull-Down for boot pin connected to VDD */
431 bootr = readl(syscfg + SYSCFG_BOOTR);
432 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
433 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
434 writel(bootr, syscfg + SYSCFG_BOOTR);
436 #ifdef CONFIG_DM_REGULATOR
437 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
438 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
439 * The customer will have to disable this for low frequencies
440 * or if AFMUX is selected but the function not used, typically for
441 * TRACE. Otherwise, impact on power consumption.
444 * enabling High Speed mode while VDD>2.7V
445 * with the OTP product_below_2v5 (OTP 18, BIT 13)
446 * erroneously set to 1 can damage the IC!
447 * => U-Boot set the register only if VDD < 2.7V (in DT)
448 * but this value need to be consistent with board design
450 ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
452 ret = uclass_get_device_by_driver(UCLASS_MISC,
453 DM_GET_DRIVER(stm32mp_bsec),
456 pr_err("Can't find stm32mp_bsec driver\n");
460 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
464 /* get VDD = pwr-supply */
465 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
468 /* check if VDD is Low Voltage */
470 if (regulator_get_value(pwr_reg) < 2700000) {
471 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
472 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
473 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
474 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
475 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
476 syscfg + SYSCFG_IOCTRLSETR);
479 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
482 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
485 debug("VDD unknown");
490 /* activate automatic I/O compensation
491 * warning: need to ensure CSI enabled and ready in clock driver
493 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
495 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
497 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
501 /* board dependent setup after realloc */
506 /* address of boot parameters */
507 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
509 /* probe all PINCTRL for hog */
510 for (uclass_first_device(UCLASS_PINCTRL, &dev);
512 uclass_next_device(&dev)) {
513 pr_debug("probe pincontrol = %s\n", dev->name);
520 if (IS_ENABLED(CONFIG_LED))
526 int board_late_init(void)
528 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
529 const void *fdt_compat;
532 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
534 if (fdt_compat && fdt_compat_len) {
535 if (strncmp(fdt_compat, "st,", 3) != 0)
536 env_set("board_name", fdt_compat);
538 env_set("board_name", fdt_compat + 3);
542 /* for DK1/DK2 boards */
543 board_check_usb_power();
548 void board_quiesce_devices(void)
550 setup_led(LEDST_OFF);
553 enum env_location env_get_location(enum env_operation op, int prio)
555 u32 bootmode = get_bootmode();
560 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
561 #ifdef CONFIG_ENV_IS_IN_EXT4
563 case BOOT_FLASH_EMMC:
566 #ifdef CONFIG_ENV_IS_IN_UBI
567 case BOOT_FLASH_NAND:
570 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
572 return ENVL_SPI_FLASH;
579 #if defined(CONFIG_ENV_IS_IN_EXT4)
580 const char *env_ext4_get_intf(void)
582 u32 bootmode = get_bootmode();
584 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
586 case BOOT_FLASH_EMMC:
593 const char *env_ext4_get_dev_part(void)
595 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
596 u32 bootmode = get_bootmode();
598 return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
602 #ifdef CONFIG_SYS_MTDPARTS_RUNTIME
604 #define MTDPARTS_LEN 256
605 #define MTDIDS_LEN 128
608 * The mtdparts_nand0 and mtdparts_nor0 variable tends to be long.
609 * If we need to access it before the env is relocated, then we need
610 * to use our own stack buffer. gd->env_buf will be too small.
612 * @param buf temporary buffer pointer MTDPARTS_LEN long
613 * @return mtdparts variable string, NULL if not found
615 static const char *env_get_mtdparts(const char *str, char *buf)
617 if (gd->flags & GD_FLG_ENV_READY)
619 if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
626 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
628 static void board_get_mtdparts(const char *dev,
632 char env_name[32] = "mtdparts_";
633 char tmp_mtdparts[MTDPARTS_LEN];
636 /* name of env variable to read = mtdparts_<dev> */
637 strcat(env_name, dev);
638 tmp = env_get_mtdparts(env_name, tmp_mtdparts);
640 /* mtdids: "<dev>=<dev>, ...." */
641 if (mtdids[0] != '\0')
647 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
648 if (mtdparts[0] != '\0')
649 strncat(mtdparts, ";", MTDPARTS_LEN);
651 strcat(mtdparts, "mtdparts=");
652 strncat(mtdparts, dev, MTDPARTS_LEN);
653 strncat(mtdparts, ":", MTDPARTS_LEN);
654 strncat(mtdparts, tmp, MTDPARTS_LEN);
658 void board_mtdparts_default(const char **mtdids, const char **mtdparts)
661 static char parts[2 * MTDPARTS_LEN + 1];
662 static char ids[MTDIDS_LEN + 1];
663 static bool mtd_initialized;
665 if (mtd_initialized) {
671 memset(parts, 0, sizeof(parts));
672 memset(ids, 0, sizeof(ids));
674 if (!uclass_get_device(UCLASS_MTD, 0, &dev))
675 board_get_mtdparts("nand0", ids, parts);
677 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
678 board_get_mtdparts("nor0", ids, parts);
680 mtd_initialized = true;
683 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);