1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
11 #include <generic-phy.h>
21 #include <asm/arch/stm32.h>
22 #include <asm/arch/sys_proto.h>
23 #include <power/regulator.h>
24 #include <usb/dwc2_udc.h>
26 /* SYSCFG registers */
27 #define SYSCFG_BOOTR 0x00
28 #define SYSCFG_PMCSETR 0x04
29 #define SYSCFG_IOCTRLSETR 0x18
30 #define SYSCFG_ICNR 0x1C
31 #define SYSCFG_CMPCR 0x20
32 #define SYSCFG_CMPENSETR 0x24
33 #define SYSCFG_PMCCLRR 0x44
35 #define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
36 #define SYSCFG_BOOTR_BOOTPD_SHIFT 4
38 #define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
39 #define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
40 #define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
41 #define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
42 #define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
44 #define SYSCFG_CMPCR_SW_CTRL BIT(1)
45 #define SYSCFG_CMPCR_READY BIT(8)
47 #define SYSCFG_CMPENSETR_MPU_EN BIT(0)
49 #define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
50 #define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
52 #define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
54 #define SYSCFG_PMCSETR_ETH_SEL_MASK GENMASK(23, 21)
55 #define SYSCFG_PMCSETR_ETH_SEL_GMII_MII (0 << 21)
56 #define SYSCFG_PMCSETR_ETH_SEL_RGMII (1 << 21)
57 #define SYSCFG_PMCSETR_ETH_SEL_RMII (4 << 21)
60 * Get a global data pointer
62 DECLARE_GLOBAL_DATA_PTR;
64 #define USB_WARNING_LOW_THRESHOLD_UV 660000
65 #define USB_START_LOW_THRESHOLD_UV 1230000
66 #define USB_START_HIGH_THRESHOLD_UV 2100000
74 const char *fdt_compat;
77 if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
82 printf("Board: stm32mp1 in %s mode", mode);
83 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
85 if (fdt_compat && fdt_compat_len)
86 printf(" (%s)", fdt_compat);
89 ret = uclass_get_device_by_driver(UCLASS_MISC,
90 DM_GET_DRIVER(stm32mp_bsec),
94 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
97 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
100 ((otp >> 8) & 0xF) - 1 + 'A',
107 static void board_key_check(void)
109 #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
111 struct gpio_desc gpio;
112 enum forced_boot_mode boot_mode = BOOT_NORMAL;
114 node = ofnode_path("/config");
115 if (!ofnode_valid(node)) {
116 debug("%s: no /config node?\n", __func__);
119 #ifdef CONFIG_FASTBOOT
120 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
121 &gpio, GPIOD_IS_IN)) {
122 debug("%s: could not find a /config/st,fastboot-gpios\n",
125 if (dm_gpio_get_value(&gpio)) {
126 puts("Fastboot key pressed, ");
127 boot_mode = BOOT_FASTBOOT;
130 dm_gpio_free(NULL, &gpio);
133 #ifdef CONFIG_CMD_STM32PROG
134 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
135 &gpio, GPIOD_IS_IN)) {
136 debug("%s: could not find a /config/st,stm32prog-gpios\n",
139 if (dm_gpio_get_value(&gpio)) {
140 puts("STM32Programmer key pressed, ");
141 boot_mode = BOOT_STM32PROG;
143 dm_gpio_free(NULL, &gpio);
147 if (boot_mode != BOOT_NORMAL) {
148 puts("entering download mode...\n");
149 clrsetbits_le32(TAMP_BOOT_CONTEXT,
150 TAMP_BOOT_FORCED_MASK,
156 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
158 /* STMicroelectronics STUSB1600 Type-C controller */
159 #define STUSB1600_CC_CONNECTION_STATUS 0x0E
161 /* STUSB1600_CC_CONNECTION_STATUS bitfields */
162 #define STUSB1600_CC_ATTACH BIT(0)
164 static int stusb1600_init(struct udevice **dev_stusb1600)
167 struct udevice *dev, *bus;
171 *dev_stusb1600 = NULL;
173 /* if node stusb1600 is present, means DK1 or DK2 board */
174 node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
175 if (!ofnode_valid(node))
178 ret = ofnode_read_u32(node, "reg", &chip_addr);
182 ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
185 printf("bus for stusb1600 not found\n");
189 ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
191 *dev_stusb1600 = dev;
196 static int stusb1600_cable_connected(struct udevice *dev)
200 if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
203 return status & STUSB1600_CC_ATTACH;
206 #include <usb/dwc2_udc.h>
207 int g_dnl_board_usb_cable_connected(void)
209 struct udevice *stusb1600;
210 struct udevice *dwc2_udc_otg;
213 if (!stusb1600_init(&stusb1600))
214 return stusb1600_cable_connected(stusb1600);
216 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
217 DM_GET_DRIVER(dwc2_udc_otg),
220 debug("dwc2_udc_otg init failed\n");
222 return dwc2_udc_B_session_valid(dwc2_udc_otg);
224 #endif /* CONFIG_USB_GADGET */
226 static int get_led(struct udevice **dev, char *led_string)
231 led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
233 pr_debug("%s: could not find %s config string\n",
234 __func__, led_string);
237 ret = led_get_by_label(led_name, dev);
239 debug("%s: get=%d\n", __func__, ret);
246 static int setup_led(enum led_state_t cmd)
251 ret = get_led(&dev, "u-boot,boot-led");
255 ret = led_set_state(dev, cmd);
259 static int board_check_usb_power(void)
261 struct ofnode_phandle_args adc_args;
267 int ret, uV, adc_count;
270 node = ofnode_path("/config");
271 if (!ofnode_valid(node)) {
272 debug("%s: no /config node?\n", __func__);
277 * Retrieve the ADC channels devices and get measurement
280 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
281 "#io-channel-cells");
283 if (adc_count == -ENOENT)
286 pr_err("%s: can't find adc channel (%d)\n", __func__,
292 for (i = 0; i < adc_count; i++) {
293 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
294 "#io-channel-cells", 0, i,
296 pr_debug("%s: can't find /config/st,adc_usb_pd\n",
301 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
305 pr_err("%s: Can't get adc device(%d)\n", __func__,
310 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
313 pr_err("%s: single shot failed for %s[%d]!\n",
314 __func__, adc->name, adc_args.args[0]);
318 if (!adc_raw_to_uV(adc, raw, &uV)) {
321 pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
322 adc->name, adc_args.args[0], raw, uV);
324 pr_err("%s: Can't get uV value for %s[%d]\n",
325 __func__, adc->name, adc_args.args[0]);
330 * If highest value is inside 1.23 Volts and 2.10 Volts, that means
331 * board is plugged on an USB-C 3A power supply and boot process can
334 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
335 max_uV < USB_START_HIGH_THRESHOLD_UV)
338 /* Display warning message and make u-boot,error-led blinking */
339 pr_err("\n*******************************************\n");
341 if (max_uV < USB_WARNING_LOW_THRESHOLD_UV) {
342 pr_err("* WARNING 500mA power supply detected *\n");
345 pr_err("* WARNING 1.5A power supply detected *\n");
349 pr_err("* Current too low, use a 3A power supply! *\n");
350 pr_err("*******************************************\n\n");
352 ret = get_led(&led, "u-boot,error-led");
356 for (i = 0; i < nb_blink * 2; i++) {
357 led_set_state(led, LEDST_TOGGLE);
360 led_set_state(led, LEDST_ON);
365 static void sysconf_init(void)
367 #ifndef CONFIG_STM32MP1_TRUSTED
369 #ifdef CONFIG_DM_REGULATOR
370 struct udevice *pwr_dev;
371 struct udevice *pwr_reg;
378 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
380 /* interconnect update : select master using the port 1 */
383 /* today information is hardcoded in U-Boot */
384 writel(BIT(9), syscfg + SYSCFG_ICNR);
386 /* disable Pull-Down for boot pin connected to VDD */
387 bootr = readl(syscfg + SYSCFG_BOOTR);
388 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
389 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
390 writel(bootr, syscfg + SYSCFG_BOOTR);
392 #ifdef CONFIG_DM_REGULATOR
393 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
394 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
395 * The customer will have to disable this for low frequencies
396 * or if AFMUX is selected but the function not used, typically for
397 * TRACE. Otherwise, impact on power consumption.
400 * enabling High Speed mode while VDD>2.7V
401 * with the OTP product_below_2v5 (OTP 18, BIT 13)
402 * erroneously set to 1 can damage the IC!
403 * => U-Boot set the register only if VDD < 2.7V (in DT)
404 * but this value need to be consistent with board design
406 ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
408 ret = uclass_get_device_by_driver(UCLASS_MISC,
409 DM_GET_DRIVER(stm32mp_bsec),
412 pr_err("Can't find stm32mp_bsec driver\n");
416 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
420 /* get VDD = pwr-supply */
421 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
424 /* check if VDD is Low Voltage */
426 if (regulator_get_value(pwr_reg) < 2700000) {
427 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
428 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
429 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
430 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
431 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
432 syscfg + SYSCFG_IOCTRLSETR);
435 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
438 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
441 debug("VDD unknown");
446 /* activate automatic I/O compensation
447 * warning: need to ensure CSI enabled and ready in clock driver
449 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
451 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
453 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
457 /* board dependent setup after realloc */
462 /* address of boot parameters */
463 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
465 /* probe all PINCTRL for hog */
466 for (uclass_first_device(UCLASS_PINCTRL, &dev);
468 uclass_next_device(&dev)) {
469 pr_debug("probe pincontrol = %s\n", dev->name);
476 if (IS_ENABLED(CONFIG_LED))
482 int board_late_init(void)
484 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
485 const void *fdt_compat;
488 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
490 if (fdt_compat && fdt_compat_len) {
491 if (strncmp(fdt_compat, "st,", 3) != 0)
492 env_set("board_name", fdt_compat);
494 env_set("board_name", fdt_compat + 3);
498 /* for DK1/DK2 boards */
499 board_check_usb_power();
504 void board_quiesce_devices(void)
506 setup_led(LEDST_OFF);
509 #if defined(CONFIG_ENV_IS_IN_EXT4)
510 const char *env_ext4_get_intf(void)
512 u32 bootmode = get_bootmode();
514 switch (bootmode & TAMP_BOOT_DEVICE_MASK) {
516 case BOOT_FLASH_EMMC:
523 const char *env_ext4_get_dev_part(void)
525 static char *const dev_part[] = {"0:auto", "1:auto", "2:auto"};
526 u32 bootmode = get_bootmode();
528 return dev_part[(bootmode & TAMP_BOOT_INSTANCE_MASK) - 1];
532 #ifdef CONFIG_SYS_MTDPARTS_RUNTIME
534 #define MTDPARTS_LEN 256
535 #define MTDIDS_LEN 128
538 * The mtdparts_nand0 and mtdparts_nor0 variable tends to be long.
539 * If we need to access it before the env is relocated, then we need
540 * to use our own stack buffer. gd->env_buf will be too small.
542 * @param buf temporary buffer pointer MTDPARTS_LEN long
543 * @return mtdparts variable string, NULL if not found
545 static const char *env_get_mtdparts(const char *str, char *buf)
547 if (gd->flags & GD_FLG_ENV_READY)
549 if (env_get_f(str, buf, MTDPARTS_LEN) != -1)
556 * update the variables "mtdids" and "mtdparts" with content of mtdparts_<dev>
558 static void board_get_mtdparts(const char *dev,
562 char env_name[32] = "mtdparts_";
563 char tmp_mtdparts[MTDPARTS_LEN];
566 /* name of env variable to read = mtdparts_<dev> */
567 strcat(env_name, dev);
568 tmp = env_get_mtdparts(env_name, tmp_mtdparts);
570 /* mtdids: "<dev>=<dev>, ...." */
571 if (mtdids[0] != '\0')
577 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
578 if (mtdparts[0] != '\0')
579 strncat(mtdparts, ";", MTDPARTS_LEN);
581 strcat(mtdparts, "mtdparts=");
582 strncat(mtdparts, dev, MTDPARTS_LEN);
583 strncat(mtdparts, ":", MTDPARTS_LEN);
584 strncat(mtdparts, tmp, MTDPARTS_LEN);
588 void board_mtdparts_default(const char **mtdids, const char **mtdparts)
591 static char parts[2 * MTDPARTS_LEN + 1];
592 static char ids[MTDIDS_LEN + 1];
593 static bool mtd_initialized;
595 if (mtd_initialized) {
601 memset(parts, 0, sizeof(parts));
602 memset(ids, 0, sizeof(ids));
604 if (!uclass_get_device(UCLASS_MTD, 0, &dev))
605 board_get_mtdparts("nand0", ids, parts);
607 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
608 board_get_mtdparts("nor0", ids, parts);
610 mtd_initialized = true;
613 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);