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_WARNING_LOW_THRESHOLD_UV 660000
66 #define USB_START_LOW_THRESHOLD_UV 1230000
67 #define USB_START_HIGH_THRESHOLD_UV 2100000
75 const char *fdt_compat;
78 if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
83 printf("Board: stm32mp1 in %s mode", mode);
84 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
86 if (fdt_compat && fdt_compat_len)
87 printf(" (%s)", fdt_compat);
90 ret = uclass_get_device_by_driver(UCLASS_MISC,
91 DM_GET_DRIVER(stm32mp_bsec),
95 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
98 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
101 ((otp >> 8) & 0xF) - 1 + 'A',
108 static void board_key_check(void)
110 #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
112 struct gpio_desc gpio;
113 enum forced_boot_mode boot_mode = BOOT_NORMAL;
115 node = ofnode_path("/config");
116 if (!ofnode_valid(node)) {
117 debug("%s: no /config node?\n", __func__);
120 #ifdef CONFIG_FASTBOOT
121 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
122 &gpio, GPIOD_IS_IN)) {
123 debug("%s: could not find a /config/st,fastboot-gpios\n",
126 if (dm_gpio_get_value(&gpio)) {
127 puts("Fastboot key pressed, ");
128 boot_mode = BOOT_FASTBOOT;
131 dm_gpio_free(NULL, &gpio);
134 #ifdef CONFIG_CMD_STM32PROG
135 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
136 &gpio, GPIOD_IS_IN)) {
137 debug("%s: could not find a /config/st,stm32prog-gpios\n",
140 if (dm_gpio_get_value(&gpio)) {
141 puts("STM32Programmer key pressed, ");
142 boot_mode = BOOT_STM32PROG;
144 dm_gpio_free(NULL, &gpio);
148 if (boot_mode != BOOT_NORMAL) {
149 puts("entering download mode...\n");
150 clrsetbits_le32(TAMP_BOOT_CONTEXT,
151 TAMP_BOOT_FORCED_MASK,
157 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
159 /* STMicroelectronics STUSB1600 Type-C controller */
160 #define STUSB1600_CC_CONNECTION_STATUS 0x0E
162 /* STUSB1600_CC_CONNECTION_STATUS bitfields */
163 #define STUSB1600_CC_ATTACH BIT(0)
165 static int stusb1600_init(struct udevice **dev_stusb1600)
168 struct udevice *dev, *bus;
172 *dev_stusb1600 = NULL;
174 /* if node stusb1600 is present, means DK1 or DK2 board */
175 node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
176 if (!ofnode_valid(node))
179 ret = ofnode_read_u32(node, "reg", &chip_addr);
183 ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
186 printf("bus for stusb1600 not found\n");
190 ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
192 *dev_stusb1600 = dev;
197 static int stusb1600_cable_connected(struct udevice *dev)
201 if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
204 return status & STUSB1600_CC_ATTACH;
207 #include <usb/dwc2_udc.h>
208 int g_dnl_board_usb_cable_connected(void)
210 struct udevice *stusb1600;
211 struct udevice *dwc2_udc_otg;
214 if (!stusb1600_init(&stusb1600))
215 return stusb1600_cable_connected(stusb1600);
217 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
218 DM_GET_DRIVER(dwc2_udc_otg),
221 debug("dwc2_udc_otg init failed\n");
223 return dwc2_udc_B_session_valid(dwc2_udc_otg);
225 #endif /* CONFIG_USB_GADGET */
227 static int get_led(struct udevice **dev, char *led_string)
232 led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
234 pr_debug("%s: could not find %s config string\n",
235 __func__, led_string);
238 ret = led_get_by_label(led_name, dev);
240 debug("%s: get=%d\n", __func__, ret);
247 static int setup_led(enum led_state_t cmd)
252 ret = get_led(&dev, "u-boot,boot-led");
256 ret = led_set_state(dev, cmd);
260 static int board_check_usb_power(void)
262 struct ofnode_phandle_args adc_args;
268 int ret, uV, adc_count;
271 node = ofnode_path("/config");
272 if (!ofnode_valid(node)) {
273 debug("%s: no /config node?\n", __func__);
278 * Retrieve the ADC channels devices and get measurement
281 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
282 "#io-channel-cells");
284 if (adc_count == -ENOENT)
287 pr_err("%s: can't find adc channel (%d)\n", __func__,
293 for (i = 0; i < adc_count; i++) {
294 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
295 "#io-channel-cells", 0, i,
297 pr_debug("%s: can't find /config/st,adc_usb_pd\n",
302 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
306 pr_err("%s: Can't get adc device(%d)\n", __func__,
311 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
314 pr_err("%s: single shot failed for %s[%d]!\n",
315 __func__, adc->name, adc_args.args[0]);
319 if (!adc_raw_to_uV(adc, raw, &uV)) {
322 pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
323 adc->name, adc_args.args[0], raw, uV);
325 pr_err("%s: Can't get uV value for %s[%d]\n",
326 __func__, adc->name, adc_args.args[0]);
331 * If highest value is inside 1.23 Volts and 2.10 Volts, that means
332 * board is plugged on an USB-C 3A power supply and boot process can
335 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
336 max_uV < USB_START_HIGH_THRESHOLD_UV)
339 /* Display warning message and make u-boot,error-led blinking */
340 pr_err("\n*******************************************\n");
342 if (max_uV < USB_WARNING_LOW_THRESHOLD_UV) {
343 pr_err("* WARNING 500mA power supply detected *\n");
346 pr_err("* WARNING 1.5A power supply detected *\n");
350 pr_err("* Current too low, use a 3A power supply! *\n");
351 pr_err("*******************************************\n\n");
353 ret = get_led(&led, "u-boot,error-led");
357 for (i = 0; i < nb_blink * 2; i++) {
358 led_set_state(led, LEDST_TOGGLE);
361 led_set_state(led, LEDST_ON);
366 static void sysconf_init(void)
368 #ifndef CONFIG_STM32MP1_TRUSTED
370 #ifdef CONFIG_DM_REGULATOR
371 struct udevice *pwr_dev;
372 struct udevice *pwr_reg;
379 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
381 /* interconnect update : select master using the port 1 */
384 /* today information is hardcoded in U-Boot */
385 writel(BIT(9), syscfg + SYSCFG_ICNR);
387 /* disable Pull-Down for boot pin connected to VDD */
388 bootr = readl(syscfg + SYSCFG_BOOTR);
389 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
390 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
391 writel(bootr, syscfg + SYSCFG_BOOTR);
393 #ifdef CONFIG_DM_REGULATOR
394 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
395 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
396 * The customer will have to disable this for low frequencies
397 * or if AFMUX is selected but the function not used, typically for
398 * TRACE. Otherwise, impact on power consumption.
401 * enabling High Speed mode while VDD>2.7V
402 * with the OTP product_below_2v5 (OTP 18, BIT 13)
403 * erroneously set to 1 can damage the IC!
404 * => U-Boot set the register only if VDD < 2.7V (in DT)
405 * but this value need to be consistent with board design
407 ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
409 ret = uclass_get_device_by_driver(UCLASS_MISC,
410 DM_GET_DRIVER(stm32mp_bsec),
413 pr_err("Can't find stm32mp_bsec driver\n");
417 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
421 /* get VDD = pwr-supply */
422 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
425 /* check if VDD is Low Voltage */
427 if (regulator_get_value(pwr_reg) < 2700000) {
428 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
429 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
430 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
431 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
432 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
433 syscfg + SYSCFG_IOCTRLSETR);
436 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
439 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
442 debug("VDD unknown");
447 /* activate automatic I/O compensation
448 * warning: need to ensure CSI enabled and ready in clock driver
450 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
452 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
454 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
458 /* board dependent setup after realloc */
463 /* address of boot parameters */
464 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
466 /* probe all PINCTRL for hog */
467 for (uclass_first_device(UCLASS_PINCTRL, &dev);
469 uclass_next_device(&dev)) {
470 pr_debug("probe pincontrol = %s\n", dev->name);
477 if (IS_ENABLED(CONFIG_LED))
483 int board_late_init(void)
485 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
486 const void *fdt_compat;
489 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
491 if (fdt_compat && fdt_compat_len) {
492 if (strncmp(fdt_compat, "st,", 3) != 0)
493 env_set("board_name", fdt_compat);
495 env_set("board_name", fdt_compat + 3);
499 /* for DK1/DK2 boards */
500 board_check_usb_power();
505 void board_quiesce_devices(void)
507 setup_led(LEDST_OFF);
510 enum env_location env_get_location(enum env_operation op, int prio)
512 u32 bootmode = get_bootmode();
517 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
518 #ifdef CONFIG_ENV_IS_IN_EXT4
520 case BOOT_FLASH_EMMC:
523 #ifdef CONFIG_ENV_IS_IN_UBI
524 case BOOT_FLASH_NAND:
527 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
529 return ENVL_SPI_FLASH;
536 #if defined(CONFIG_ENV_IS_IN_EXT4)
537 const char *env_ext4_get_intf(void)
539 u32 bootmode = get_bootmode();
541 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
543 case BOOT_FLASH_EMMC:
550 const char *env_ext4_get_dev_part(void)
552 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
553 u32 bootmode = get_bootmode();
555 return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
559 #ifdef CONFIG_SYS_MTDPARTS_RUNTIME
561 #define MTDPARTS_LEN 256
562 #define MTDIDS_LEN 128
565 * The mtdparts_nand0 and mtdparts_nor0 variable tends to be long.
566 * If we need to access it before the env is relocated, then we need
567 * to use our own stack buffer. gd->env_buf will be too small.
569 * @param buf temporary buffer pointer MTDPARTS_LEN long
570 * @return mtdparts variable string, NULL if not found
572 static const char *env_get_mtdparts(const char *str, char *buf)
574 if (gd->flags & GD_FLG_ENV_READY)
576 if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
583 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
585 static void board_get_mtdparts(const char *dev,
589 char env_name[32] = "mtdparts_";
590 char tmp_mtdparts[MTDPARTS_LEN];
593 /* name of env variable to read = mtdparts_<dev> */
594 strcat(env_name, dev);
595 tmp = env_get_mtdparts(env_name, tmp_mtdparts);
597 /* mtdids: "<dev>=<dev>, ...." */
598 if (mtdids[0] != '\0')
604 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
605 if (mtdparts[0] != '\0')
606 strncat(mtdparts, ";", MTDPARTS_LEN);
608 strcat(mtdparts, "mtdparts=");
609 strncat(mtdparts, dev, MTDPARTS_LEN);
610 strncat(mtdparts, ":", MTDPARTS_LEN);
611 strncat(mtdparts, tmp, MTDPARTS_LEN);
615 void board_mtdparts_default(const char **mtdids, const char **mtdparts)
618 static char parts[2 * MTDPARTS_LEN + 1];
619 static char ids[MTDIDS_LEN + 1];
620 static bool mtd_initialized;
622 if (mtd_initialized) {
628 memset(parts, 0, sizeof(parts));
629 memset(ids, 0, sizeof(ids));
631 if (!uclass_get_device(UCLASS_MTD, 0, &dev))
632 board_get_mtdparts("nand0", ids, parts);
634 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
635 board_get_mtdparts("nor0", ids, parts);
637 mtd_initialized = true;
640 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);