Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2023, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
| 6 | #define LOG_CATEGORY LOGC_BOARD |
| 7 | |
Patrick Delaunay | d736e40 | 2022-07-27 10:38:11 +0200 | [diff] [blame^] | 8 | #include <button.h> |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 9 | #include <config.h> |
Patrice Chotard | 0f77af6 | 2025-04-01 18:08:44 +0200 | [diff] [blame] | 10 | #include <env_internal.h> |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 11 | #include <fdt_support.h> |
Patrick Delaunay | 3a4fa45 | 2022-07-26 19:26:16 +0200 | [diff] [blame] | 12 | #include <led.h> |
Patrick Delaunay | d7b448f | 2024-01-15 15:05:54 +0100 | [diff] [blame] | 13 | #include <log.h> |
Patrick Delaunay | 300cb92 | 2024-01-15 15:05:55 +0100 | [diff] [blame] | 14 | #include <misc.h> |
Patrice Chotard | 644590f | 2025-04-22 17:33:42 +0200 | [diff] [blame] | 15 | #include <mmc.h> |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Patrick Delaunay | d736e40 | 2022-07-27 10:38:11 +0200 | [diff] [blame^] | 17 | #include <asm/io.h> |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 18 | #include <asm/arch/sys_proto.h> |
Patrick Delaunay | 300cb92 | 2024-01-15 15:05:55 +0100 | [diff] [blame] | 19 | #include <dm/device.h> |
Patrick Delaunay | d7b448f | 2024-01-15 15:05:54 +0100 | [diff] [blame] | 20 | #include <dm/ofnode.h> |
Patrick Delaunay | 300cb92 | 2024-01-15 15:05:55 +0100 | [diff] [blame] | 21 | #include <dm/uclass.h> |
Patrick Delaunay | d736e40 | 2022-07-27 10:38:11 +0200 | [diff] [blame^] | 22 | #include <linux/delay.h> |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * Get a global data pointer |
| 26 | */ |
| 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
Patrick Delaunay | d7b448f | 2024-01-15 15:05:54 +0100 | [diff] [blame] | 29 | int checkboard(void) |
| 30 | { |
Patrick Delaunay | 300cb92 | 2024-01-15 15:05:55 +0100 | [diff] [blame] | 31 | int ret; |
| 32 | u32 otp; |
| 33 | struct udevice *dev; |
Patrick Delaunay | d7b448f | 2024-01-15 15:05:54 +0100 | [diff] [blame] | 34 | const char *fdt_compat; |
| 35 | int fdt_compat_len; |
| 36 | |
| 37 | fdt_compat = ofnode_get_property(ofnode_root(), "compatible", &fdt_compat_len); |
| 38 | |
| 39 | log_info("Board: stm32mp2 (%s)\n", fdt_compat && fdt_compat_len ? fdt_compat : ""); |
| 40 | |
Patrick Delaunay | 300cb92 | 2024-01-15 15:05:55 +0100 | [diff] [blame] | 41 | /* display the STMicroelectronics board identification */ |
| 42 | if (CONFIG_IS_ENABLED(CMD_STBOARD)) { |
| 43 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 44 | DM_DRIVER_GET(stm32mp_bsec), |
| 45 | &dev); |
| 46 | if (!ret) |
| 47 | ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD), |
| 48 | &otp, sizeof(otp)); |
| 49 | if (ret > 0 && otp) |
| 50 | log_info("Board: MB%04x Var%d.%d Rev.%c-%02d\n", |
| 51 | otp >> 16, |
| 52 | (otp >> 12) & 0xF, |
| 53 | (otp >> 4) & 0xF, |
| 54 | ((otp >> 8) & 0xF) - 1 + 'A', |
| 55 | otp & 0xF); |
| 56 | } |
| 57 | |
Patrick Delaunay | d7b448f | 2024-01-15 15:05:54 +0100 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
Patrick Delaunay | 3a4fa45 | 2022-07-26 19:26:16 +0200 | [diff] [blame] | 61 | static int get_led(struct udevice **dev, char *led_string) |
| 62 | { |
| 63 | const char *led_name; |
| 64 | int ret; |
| 65 | |
| 66 | led_name = ofnode_conf_read_str(led_string); |
| 67 | if (!led_name) { |
| 68 | log_debug("could not find %s config string\n", led_string); |
| 69 | return -ENOENT; |
| 70 | } |
| 71 | ret = led_get_by_label(led_name, dev); |
| 72 | if (ret) { |
| 73 | log_debug("get=%d\n", ret); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int setup_led(enum led_state_t cmd) |
| 81 | { |
| 82 | struct udevice *dev; |
| 83 | int ret; |
| 84 | |
| 85 | if (!CONFIG_IS_ENABLED(LED)) |
| 86 | return 0; |
| 87 | |
| 88 | ret = get_led(&dev, "u-boot,boot-led"); |
| 89 | if (ret) |
| 90 | return ret; |
| 91 | |
| 92 | ret = led_set_state(dev, cmd); |
| 93 | return ret; |
| 94 | } |
| 95 | |
Patrick Delaunay | d736e40 | 2022-07-27 10:38:11 +0200 | [diff] [blame^] | 96 | static void check_user_button(void) |
| 97 | { |
| 98 | struct udevice *button; |
| 99 | int i; |
| 100 | |
| 101 | if (!IS_ENABLED(CONFIG_CMD_STM32PROG) || !IS_ENABLED(CONFIG_BUTTON)) |
| 102 | return; |
| 103 | |
| 104 | if (button_get_by_label("User-2", &button)) |
| 105 | return; |
| 106 | |
| 107 | for (i = 0; i < 21; ++i) { |
| 108 | if (button_get_state(button) != BUTTON_ON) |
| 109 | return; |
| 110 | if (i < 20) |
| 111 | mdelay(50); |
| 112 | } |
| 113 | |
| 114 | log_notice("entering download mode...\n"); |
| 115 | clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_STM32PROG); |
| 116 | } |
| 117 | |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 118 | /* board dependent setup after realloc */ |
| 119 | int board_init(void) |
| 120 | { |
Patrick Delaunay | 3a4fa45 | 2022-07-26 19:26:16 +0200 | [diff] [blame] | 121 | setup_led(LEDST_ON); |
Patrick Delaunay | d736e40 | 2022-07-27 10:38:11 +0200 | [diff] [blame^] | 122 | check_user_button(); |
Patrick Delaunay | 3a4fa45 | 2022-07-26 19:26:16 +0200 | [diff] [blame] | 123 | |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
Patrice Chotard | 0f77af6 | 2025-04-01 18:08:44 +0200 | [diff] [blame] | 127 | enum env_location env_get_location(enum env_operation op, int prio) |
| 128 | { |
| 129 | u32 bootmode = get_bootmode(); |
| 130 | |
| 131 | if (prio) |
| 132 | return ENVL_UNKNOWN; |
| 133 | |
| 134 | switch (bootmode & TAMP_BOOT_DEVICE_MASK) { |
| 135 | case BOOT_FLASH_SD: |
| 136 | case BOOT_FLASH_EMMC: |
| 137 | if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC)) |
| 138 | return ENVL_MMC; |
| 139 | else |
| 140 | return ENVL_NOWHERE; |
| 141 | default: |
| 142 | return ENVL_NOWHERE; |
| 143 | } |
| 144 | } |
| 145 | |
Patrice Chotard | 644590f | 2025-04-22 17:33:42 +0200 | [diff] [blame] | 146 | int mmc_get_boot(void) |
| 147 | { |
| 148 | struct udevice *dev; |
| 149 | u32 boot_mode = get_bootmode(); |
| 150 | unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1; |
| 151 | char cmd[20]; |
| 152 | const u32 sdmmc_addr[] = { |
| 153 | STM32_SDMMC1_BASE, |
| 154 | STM32_SDMMC2_BASE, |
| 155 | STM32_SDMMC3_BASE |
| 156 | }; |
| 157 | |
| 158 | if (instance > ARRAY_SIZE(sdmmc_addr)) |
| 159 | return 0; |
| 160 | |
| 161 | /* search associated sdmmc node in devicetree */ |
| 162 | snprintf(cmd, sizeof(cmd), "mmc@%x", sdmmc_addr[instance]); |
| 163 | if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) { |
| 164 | log_err("mmc%d = %s not found in device tree!\n", instance, cmd); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | return dev_seq(dev); |
| 169 | }; |
| 170 | |
| 171 | int mmc_get_env_dev(void) |
| 172 | { |
| 173 | const int mmc_env_dev = CONFIG_IS_ENABLED(ENV_IS_IN_MMC, (CONFIG_SYS_MMC_ENV_DEV), (-1)); |
| 174 | |
| 175 | if (mmc_env_dev >= 0) |
| 176 | return mmc_env_dev; |
| 177 | |
| 178 | /* use boot instance to select the correct mmc device identifier */ |
| 179 | return mmc_get_boot(); |
| 180 | } |
| 181 | |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 182 | int board_late_init(void) |
| 183 | { |
| 184 | const void *fdt_compat; |
| 185 | int fdt_compat_len; |
| 186 | char dtb_name[256]; |
| 187 | int buf_len; |
| 188 | |
| 189 | if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) { |
| 190 | fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible", |
| 191 | &fdt_compat_len); |
| 192 | if (fdt_compat && fdt_compat_len) { |
| 193 | if (strncmp(fdt_compat, "st,", 3) != 0) { |
| 194 | env_set("board_name", fdt_compat); |
| 195 | } else { |
| 196 | env_set("board_name", fdt_compat + 3); |
| 197 | |
| 198 | buf_len = sizeof(dtb_name); |
| 199 | strlcpy(dtb_name, fdt_compat + 3, buf_len); |
| 200 | buf_len -= strlen(fdt_compat + 3); |
| 201 | strlcat(dtb_name, ".dtb", buf_len); |
| 202 | env_set("fdtfile", dtb_name); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
Patrick Delaunay | 3a4fa45 | 2022-07-26 19:26:16 +0200 | [diff] [blame] | 209 | |
| 210 | void board_quiesce_devices(void) |
| 211 | { |
| 212 | setup_led(LEDST_OFF); |
| 213 | } |