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 <power/regulator.h>
23 #include <usb/dwc2_udc.h>
25 /* SYSCFG registers */
26 #define SYSCFG_BOOTR 0x00
27 #define SYSCFG_PMCSETR 0x04
28 #define SYSCFG_IOCTRLSETR 0x18
29 #define SYSCFG_ICNR 0x1C
30 #define SYSCFG_CMPCR 0x20
31 #define SYSCFG_CMPENSETR 0x24
32 #define SYSCFG_PMCCLRR 0x44
34 #define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0)
35 #define SYSCFG_BOOTR_BOOTPD_SHIFT 4
37 #define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0)
38 #define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1)
39 #define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2)
40 #define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3)
41 #define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4)
43 #define SYSCFG_CMPCR_SW_CTRL BIT(1)
44 #define SYSCFG_CMPCR_READY BIT(8)
46 #define SYSCFG_CMPENSETR_MPU_EN BIT(0)
48 #define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
49 #define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
51 #define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
53 #define SYSCFG_PMCSETR_ETH_SEL_MASK GENMASK(23, 21)
54 #define SYSCFG_PMCSETR_ETH_SEL_GMII_MII (0 << 21)
55 #define SYSCFG_PMCSETR_ETH_SEL_RGMII (1 << 21)
56 #define SYSCFG_PMCSETR_ETH_SEL_RMII (4 << 21)
59 * Get a global data pointer
61 DECLARE_GLOBAL_DATA_PTR;
63 #define USB_WARNING_LOW_THRESHOLD_UV 660000
64 #define USB_START_LOW_THRESHOLD_UV 1230000
65 #define USB_START_HIGH_THRESHOLD_UV 2100000
73 const char *fdt_compat;
76 if (IS_ENABLED(CONFIG_STM32MP1_TRUSTED))
81 printf("Board: stm32mp1 in %s mode", mode);
82 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
84 if (fdt_compat && fdt_compat_len)
85 printf(" (%s)", fdt_compat);
88 ret = uclass_get_device_by_driver(UCLASS_MISC,
89 DM_GET_DRIVER(stm32mp_bsec),
93 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
96 printf("Board: MB%04x Var%d Rev.%c-%02d\n",
99 ((otp >> 8) & 0xF) - 1 + 'A',
106 static void board_key_check(void)
108 #if defined(CONFIG_FASTBOOT) || defined(CONFIG_CMD_STM32PROG)
110 struct gpio_desc gpio;
111 enum forced_boot_mode boot_mode = BOOT_NORMAL;
113 node = ofnode_path("/config");
114 if (!ofnode_valid(node)) {
115 debug("%s: no /config node?\n", __func__);
118 #ifdef CONFIG_FASTBOOT
119 if (gpio_request_by_name_nodev(node, "st,fastboot-gpios", 0,
120 &gpio, GPIOD_IS_IN)) {
121 debug("%s: could not find a /config/st,fastboot-gpios\n",
124 if (dm_gpio_get_value(&gpio)) {
125 puts("Fastboot key pressed, ");
126 boot_mode = BOOT_FASTBOOT;
129 dm_gpio_free(NULL, &gpio);
132 #ifdef CONFIG_CMD_STM32PROG
133 if (gpio_request_by_name_nodev(node, "st,stm32prog-gpios", 0,
134 &gpio, GPIOD_IS_IN)) {
135 debug("%s: could not find a /config/st,stm32prog-gpios\n",
138 if (dm_gpio_get_value(&gpio)) {
139 puts("STM32Programmer key pressed, ");
140 boot_mode = BOOT_STM32PROG;
142 dm_gpio_free(NULL, &gpio);
146 if (boot_mode != BOOT_NORMAL) {
147 puts("entering download mode...\n");
148 clrsetbits_le32(TAMP_BOOT_CONTEXT,
149 TAMP_BOOT_FORCED_MASK,
155 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
157 /* STMicroelectronics STUSB1600 Type-C controller */
158 #define STUSB1600_CC_CONNECTION_STATUS 0x0E
160 /* STUSB1600_CC_CONNECTION_STATUS bitfields */
161 #define STUSB1600_CC_ATTACH BIT(0)
163 static int stusb1600_init(struct udevice **dev_stusb1600)
166 struct udevice *dev, *bus;
170 *dev_stusb1600 = NULL;
172 /* if node stusb1600 is present, means DK1 or DK2 board */
173 node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
174 if (!ofnode_valid(node))
177 ret = ofnode_read_u32(node, "reg", &chip_addr);
181 ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
184 printf("bus for stusb1600 not found\n");
188 ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
190 *dev_stusb1600 = dev;
195 static int stusb1600_cable_connected(struct udevice *dev)
199 if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
202 return status & STUSB1600_CC_ATTACH;
205 #include <usb/dwc2_udc.h>
206 int g_dnl_board_usb_cable_connected(void)
208 struct udevice *stusb1600;
209 struct udevice *dwc2_udc_otg;
212 if (!stusb1600_init(&stusb1600))
213 return stusb1600_cable_connected(stusb1600);
215 ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
216 DM_GET_DRIVER(dwc2_udc_otg),
219 debug("dwc2_udc_otg init failed\n");
221 return dwc2_udc_B_session_valid(dwc2_udc_otg);
223 #endif /* CONFIG_USB_GADGET */
225 static int get_led(struct udevice **dev, char *led_string)
230 led_name = fdtdec_get_config_string(gd->fdt_blob, led_string);
232 pr_debug("%s: could not find %s config string\n",
233 __func__, led_string);
236 ret = led_get_by_label(led_name, dev);
238 debug("%s: get=%d\n", __func__, ret);
245 static int setup_led(enum led_state_t cmd)
250 ret = get_led(&dev, "u-boot,boot-led");
254 ret = led_set_state(dev, cmd);
258 static int board_check_usb_power(void)
260 struct ofnode_phandle_args adc_args;
266 int ret, uV, adc_count;
269 node = ofnode_path("/config");
270 if (!ofnode_valid(node)) {
271 debug("%s: no /config node?\n", __func__);
276 * Retrieve the ADC channels devices and get measurement
279 adc_count = ofnode_count_phandle_with_args(node, "st,adc_usb_pd",
280 "#io-channel-cells");
282 if (adc_count == -ENOENT)
285 pr_err("%s: can't find adc channel (%d)\n", __func__,
291 for (i = 0; i < adc_count; i++) {
292 if (ofnode_parse_phandle_with_args(node, "st,adc_usb_pd",
293 "#io-channel-cells", 0, i,
295 pr_debug("%s: can't find /config/st,adc_usb_pd\n",
300 ret = uclass_get_device_by_ofnode(UCLASS_ADC, adc_args.node,
304 pr_err("%s: Can't get adc device(%d)\n", __func__,
309 ret = adc_channel_single_shot(adc->name, adc_args.args[0],
312 pr_err("%s: single shot failed for %s[%d]!\n",
313 __func__, adc->name, adc_args.args[0]);
317 if (!adc_raw_to_uV(adc, raw, &uV)) {
320 pr_debug("%s: %s[%02d] = %u, %d uV\n", __func__,
321 adc->name, adc_args.args[0], raw, uV);
323 pr_err("%s: Can't get uV value for %s[%d]\n",
324 __func__, adc->name, adc_args.args[0]);
329 * If highest value is inside 1.23 Volts and 2.10 Volts, that means
330 * board is plugged on an USB-C 3A power supply and boot process can
333 if (max_uV > USB_START_LOW_THRESHOLD_UV &&
334 max_uV < USB_START_HIGH_THRESHOLD_UV)
337 /* Display warning message and make u-boot,error-led blinking */
338 pr_err("\n*******************************************\n");
340 if (max_uV < USB_WARNING_LOW_THRESHOLD_UV) {
341 pr_err("* WARNING 500mA power supply detected *\n");
344 pr_err("* WARNING 1.5A power supply detected *\n");
348 pr_err("* Current too low, use a 3A power supply! *\n");
349 pr_err("*******************************************\n\n");
351 ret = get_led(&led, "u-boot,error-led");
355 for (i = 0; i < nb_blink * 2; i++) {
356 led_set_state(led, LEDST_TOGGLE);
359 led_set_state(led, LEDST_ON);
364 static void sysconf_init(void)
366 #ifndef CONFIG_STM32MP1_TRUSTED
368 #ifdef CONFIG_DM_REGULATOR
369 struct udevice *pwr_dev;
370 struct udevice *pwr_reg;
377 syscfg = (u8 *)syscon_get_first_range(STM32MP_SYSCON_SYSCFG);
379 /* interconnect update : select master using the port 1 */
382 /* today information is hardcoded in U-Boot */
383 writel(BIT(9), syscfg + SYSCFG_ICNR);
385 /* disable Pull-Down for boot pin connected to VDD */
386 bootr = readl(syscfg + SYSCFG_BOOTR);
387 bootr &= ~(SYSCFG_BOOTR_BOOT_MASK << SYSCFG_BOOTR_BOOTPD_SHIFT);
388 bootr |= (bootr & SYSCFG_BOOTR_BOOT_MASK) << SYSCFG_BOOTR_BOOTPD_SHIFT;
389 writel(bootr, syscfg + SYSCFG_BOOTR);
391 #ifdef CONFIG_DM_REGULATOR
392 /* High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI
393 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection.
394 * The customer will have to disable this for low frequencies
395 * or if AFMUX is selected but the function not used, typically for
396 * TRACE. Otherwise, impact on power consumption.
399 * enabling High Speed mode while VDD>2.7V
400 * with the OTP product_below_2v5 (OTP 18, BIT 13)
401 * erroneously set to 1 can damage the IC!
402 * => U-Boot set the register only if VDD < 2.7V (in DT)
403 * but this value need to be consistent with board design
405 ret = syscon_get_by_driver_data(STM32MP_SYSCON_PWR, &pwr_dev);
407 ret = uclass_get_device_by_driver(UCLASS_MISC,
408 DM_GET_DRIVER(stm32mp_bsec),
411 pr_err("Can't find stm32mp_bsec driver\n");
415 ret = misc_read(dev, STM32_BSEC_SHADOW(18), &otp, 4);
419 /* get VDD = pwr-supply */
420 ret = device_get_supply_regulator(pwr_dev, "pwr-supply",
423 /* check if VDD is Low Voltage */
425 if (regulator_get_value(pwr_reg) < 2700000) {
426 writel(SYSCFG_IOCTRLSETR_HSLVEN_TRACE |
427 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI |
428 SYSCFG_IOCTRLSETR_HSLVEN_ETH |
429 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC |
430 SYSCFG_IOCTRLSETR_HSLVEN_SPI,
431 syscfg + SYSCFG_IOCTRLSETR);
434 pr_err("product_below_2v5=0: HSLVEN protected by HW\n");
437 pr_err("product_below_2v5=1: HSLVEN update is destructive, no update as VDD>2.7V\n");
440 debug("VDD unknown");
445 /* activate automatic I/O compensation
446 * warning: need to ensure CSI enabled and ready in clock driver
448 writel(SYSCFG_CMPENSETR_MPU_EN, syscfg + SYSCFG_CMPENSETR);
450 while (!(readl(syscfg + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY))
452 clrbits_le32(syscfg + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
456 /* board dependent setup after realloc */
461 /* address of boot parameters */
462 gd->bd->bi_boot_params = STM32_DDR_BASE + 0x100;
464 /* probe all PINCTRL for hog */
465 for (uclass_first_device(UCLASS_PINCTRL, &dev);
467 uclass_next_device(&dev)) {
468 pr_debug("probe pincontrol = %s\n", dev->name);
475 if (IS_ENABLED(CONFIG_LED))
481 int board_late_init(void)
483 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
484 const void *fdt_compat;
487 fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
489 if (fdt_compat && fdt_compat_len) {
490 if (strncmp(fdt_compat, "st,", 3) != 0)
491 env_set("board_name", fdt_compat);
493 env_set("board_name", fdt_compat + 3);
497 /* for DK1/DK2 boards */
498 board_check_usb_power();
503 void board_quiesce_devices(void)
505 setup_led(LEDST_OFF);