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 | |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 8 | #include <config.h> |
Patrice Chotard | 0f77af6 | 2025-04-01 18:08:44 +0200 | [diff] [blame] | 9 | #include <env_internal.h> |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 10 | #include <fdt_support.h> |
Patrick Delaunay | d7b448f | 2024-01-15 15:05:54 +0100 | [diff] [blame] | 11 | #include <log.h> |
Patrick Delaunay | 300cb92 | 2024-01-15 15:05:55 +0100 | [diff] [blame] | 12 | #include <misc.h> |
Patrice Chotard | 644590f | 2025-04-22 17:33:42 +0200 | [diff] [blame^] | 13 | #include <mmc.h> |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 14 | #include <asm/global_data.h> |
| 15 | #include <asm/arch/sys_proto.h> |
Patrick Delaunay | 300cb92 | 2024-01-15 15:05:55 +0100 | [diff] [blame] | 16 | #include <dm/device.h> |
Patrick Delaunay | d7b448f | 2024-01-15 15:05:54 +0100 | [diff] [blame] | 17 | #include <dm/ofnode.h> |
Patrick Delaunay | 300cb92 | 2024-01-15 15:05:55 +0100 | [diff] [blame] | 18 | #include <dm/uclass.h> |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Get a global data pointer |
| 22 | */ |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Patrick Delaunay | d7b448f | 2024-01-15 15:05:54 +0100 | [diff] [blame] | 25 | int checkboard(void) |
| 26 | { |
Patrick Delaunay | 300cb92 | 2024-01-15 15:05:55 +0100 | [diff] [blame] | 27 | int ret; |
| 28 | u32 otp; |
| 29 | struct udevice *dev; |
Patrick Delaunay | d7b448f | 2024-01-15 15:05:54 +0100 | [diff] [blame] | 30 | const char *fdt_compat; |
| 31 | int fdt_compat_len; |
| 32 | |
| 33 | fdt_compat = ofnode_get_property(ofnode_root(), "compatible", &fdt_compat_len); |
| 34 | |
| 35 | log_info("Board: stm32mp2 (%s)\n", fdt_compat && fdt_compat_len ? fdt_compat : ""); |
| 36 | |
Patrick Delaunay | 300cb92 | 2024-01-15 15:05:55 +0100 | [diff] [blame] | 37 | /* display the STMicroelectronics board identification */ |
| 38 | if (CONFIG_IS_ENABLED(CMD_STBOARD)) { |
| 39 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 40 | DM_DRIVER_GET(stm32mp_bsec), |
| 41 | &dev); |
| 42 | if (!ret) |
| 43 | ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD), |
| 44 | &otp, sizeof(otp)); |
| 45 | if (ret > 0 && otp) |
| 46 | log_info("Board: MB%04x Var%d.%d Rev.%c-%02d\n", |
| 47 | otp >> 16, |
| 48 | (otp >> 12) & 0xF, |
| 49 | (otp >> 4) & 0xF, |
| 50 | ((otp >> 8) & 0xF) - 1 + 'A', |
| 51 | otp & 0xF); |
| 52 | } |
| 53 | |
Patrick Delaunay | d7b448f | 2024-01-15 15:05:54 +0100 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 57 | /* board dependent setup after realloc */ |
| 58 | int board_init(void) |
| 59 | { |
| 60 | return 0; |
| 61 | } |
| 62 | |
Patrice Chotard | 0f77af6 | 2025-04-01 18:08:44 +0200 | [diff] [blame] | 63 | enum env_location env_get_location(enum env_operation op, int prio) |
| 64 | { |
| 65 | u32 bootmode = get_bootmode(); |
| 66 | |
| 67 | if (prio) |
| 68 | return ENVL_UNKNOWN; |
| 69 | |
| 70 | switch (bootmode & TAMP_BOOT_DEVICE_MASK) { |
| 71 | case BOOT_FLASH_SD: |
| 72 | case BOOT_FLASH_EMMC: |
| 73 | if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC)) |
| 74 | return ENVL_MMC; |
| 75 | else |
| 76 | return ENVL_NOWHERE; |
| 77 | default: |
| 78 | return ENVL_NOWHERE; |
| 79 | } |
| 80 | } |
| 81 | |
Patrice Chotard | 644590f | 2025-04-22 17:33:42 +0200 | [diff] [blame^] | 82 | int mmc_get_boot(void) |
| 83 | { |
| 84 | struct udevice *dev; |
| 85 | u32 boot_mode = get_bootmode(); |
| 86 | unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1; |
| 87 | char cmd[20]; |
| 88 | const u32 sdmmc_addr[] = { |
| 89 | STM32_SDMMC1_BASE, |
| 90 | STM32_SDMMC2_BASE, |
| 91 | STM32_SDMMC3_BASE |
| 92 | }; |
| 93 | |
| 94 | if (instance > ARRAY_SIZE(sdmmc_addr)) |
| 95 | return 0; |
| 96 | |
| 97 | /* search associated sdmmc node in devicetree */ |
| 98 | snprintf(cmd, sizeof(cmd), "mmc@%x", sdmmc_addr[instance]); |
| 99 | if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) { |
| 100 | log_err("mmc%d = %s not found in device tree!\n", instance, cmd); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | return dev_seq(dev); |
| 105 | }; |
| 106 | |
| 107 | int mmc_get_env_dev(void) |
| 108 | { |
| 109 | const int mmc_env_dev = CONFIG_IS_ENABLED(ENV_IS_IN_MMC, (CONFIG_SYS_MMC_ENV_DEV), (-1)); |
| 110 | |
| 111 | if (mmc_env_dev >= 0) |
| 112 | return mmc_env_dev; |
| 113 | |
| 114 | /* use boot instance to select the correct mmc device identifier */ |
| 115 | return mmc_get_boot(); |
| 116 | } |
| 117 | |
Patrice Chotard | d29531c | 2023-10-27 16:43:04 +0200 | [diff] [blame] | 118 | int board_late_init(void) |
| 119 | { |
| 120 | const void *fdt_compat; |
| 121 | int fdt_compat_len; |
| 122 | char dtb_name[256]; |
| 123 | int buf_len; |
| 124 | |
| 125 | if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) { |
| 126 | fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible", |
| 127 | &fdt_compat_len); |
| 128 | if (fdt_compat && fdt_compat_len) { |
| 129 | if (strncmp(fdt_compat, "st,", 3) != 0) { |
| 130 | env_set("board_name", fdt_compat); |
| 131 | } else { |
| 132 | env_set("board_name", fdt_compat + 3); |
| 133 | |
| 134 | buf_len = sizeof(dtb_name); |
| 135 | strlcpy(dtb_name, fdt_compat + 3, buf_len); |
| 136 | buf_len -= strlen(fdt_compat + 3); |
| 137 | strlcat(dtb_name, ".dtb", buf_len); |
| 138 | env_set("fdtfile", dtb_name); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |