Patrick Delaunay | e4bdd54 | 2022-05-20 18:24:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2021, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
| 6 | #define LOG_CATEGORY LOGC_ARCH |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <env.h> |
| 10 | #include <log.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <asm/arch/bsec.h> |
| 13 | #include <asm/arch/stm32.h> |
| 14 | #include <asm/arch/sys_proto.h> |
| 15 | #include <dm/device.h> |
| 16 | #include <dm/uclass.h> |
| 17 | |
| 18 | /* RCC register */ |
| 19 | #define RCC_TZCR (STM32_RCC_BASE + 0x00) |
| 20 | #define RCC_BDCR (STM32_RCC_BASE + 0x0140) |
| 21 | #define RCC_MP_APB5ENSETR (STM32_RCC_BASE + 0x0208) |
| 22 | #define RCC_MP_AHB5ENSETR (STM32_RCC_BASE + 0x0210) |
| 23 | #define RCC_DBGCFGR (STM32_RCC_BASE + 0x080C) |
| 24 | |
| 25 | #define RCC_BDCR_VSWRST BIT(31) |
| 26 | #define RCC_BDCR_RTCSRC GENMASK(17, 16) |
| 27 | |
| 28 | #define RCC_DBGCFGR_DBGCKEN BIT(8) |
| 29 | |
| 30 | /* DBGMCU register */ |
| 31 | #define DBGMCU_IDC (STM32_DBGMCU_BASE + 0x00) |
| 32 | #define DBGMCU_APB4FZ1 (STM32_DBGMCU_BASE + 0x2C) |
| 33 | #define DBGMCU_APB4FZ1_IWDG2 BIT(2) |
| 34 | |
| 35 | /* Security register */ |
| 36 | #define ETZPC_TZMA1_SIZE (STM32_ETZPC_BASE + 0x04) |
| 37 | #define ETZPC_DECPROT0 (STM32_ETZPC_BASE + 0x10) |
| 38 | |
| 39 | #define TZC_GATE_KEEPER (STM32_TZC_BASE + 0x008) |
| 40 | #define TZC_REGION_ATTRIBUTE0 (STM32_TZC_BASE + 0x110) |
| 41 | #define TZC_REGION_ID_ACCESS0 (STM32_TZC_BASE + 0x114) |
| 42 | |
| 43 | #define TAMP_CR1 (STM32_TAMP_BASE + 0x00) |
| 44 | |
| 45 | #define PWR_CR1 (STM32_PWR_BASE + 0x00) |
| 46 | #define PWR_MCUCR (STM32_PWR_BASE + 0x14) |
| 47 | #define PWR_CR1_DBP BIT(8) |
| 48 | #define PWR_MCUCR_SBF BIT(6) |
| 49 | |
| 50 | /* GPIOZ registers */ |
| 51 | #define GPIOZ_SECCFGR 0x54004030 |
| 52 | |
| 53 | /* DBGMCU register */ |
| 54 | #define DBGMCU_IDC (STM32_DBGMCU_BASE + 0x00) |
| 55 | #define DBGMCU_IDC_DEV_ID_MASK GENMASK(11, 0) |
| 56 | #define DBGMCU_IDC_DEV_ID_SHIFT 0 |
| 57 | #define DBGMCU_IDC_REV_ID_MASK GENMASK(31, 16) |
| 58 | #define DBGMCU_IDC_REV_ID_SHIFT 16 |
| 59 | |
| 60 | /* boot interface from Bootrom |
| 61 | * - boot instance = bit 31:16 |
| 62 | * - boot device = bit 15:0 |
| 63 | */ |
| 64 | #define BOOTROM_PARAM_ADDR 0x2FFC0078 |
| 65 | #define BOOTROM_MODE_MASK GENMASK(15, 0) |
| 66 | #define BOOTROM_MODE_SHIFT 0 |
| 67 | #define BOOTROM_INSTANCE_MASK GENMASK(31, 16) |
| 68 | #define BOOTROM_INSTANCE_SHIFT 16 |
| 69 | |
| 70 | /* Device Part Number (RPN) = OTP_DATA1 lower 8 bits */ |
| 71 | #define RPN_SHIFT 0 |
| 72 | #define RPN_MASK GENMASK(7, 0) |
| 73 | |
| 74 | /* Package = bit 27:29 of OTP16 => STM32MP15_PKG defines |
| 75 | * - 100: LBGA448 (FFI) => AA = LFBGA 18x18mm 448 balls p. 0.8mm |
| 76 | * - 011: LBGA354 (LCI) => AB = LFBGA 16x16mm 359 balls p. 0.8mm |
| 77 | * - 010: TFBGA361 (FFC) => AC = TFBGA 12x12mm 361 balls p. 0.5mm |
| 78 | * - 001: TFBGA257 (LCC) => AD = TFBGA 10x10mm 257 balls p. 0.5mm |
| 79 | * - others: Reserved |
| 80 | */ |
| 81 | #define PKG_SHIFT 27 |
| 82 | #define PKG_MASK GENMASK(2, 0) |
| 83 | |
| 84 | static void security_init(void) |
| 85 | { |
| 86 | /* Disable the backup domain write protection */ |
| 87 | /* the protection is enable at each reset by hardware */ |
| 88 | /* And must be disable by software */ |
| 89 | setbits_le32(PWR_CR1, PWR_CR1_DBP); |
| 90 | |
| 91 | while (!(readl(PWR_CR1) & PWR_CR1_DBP)) |
| 92 | ; |
| 93 | |
| 94 | /* If RTC clock isn't enable so this is a cold boot then we need |
| 95 | * to reset the backup domain |
| 96 | */ |
| 97 | if (!(readl(RCC_BDCR) & RCC_BDCR_RTCSRC)) { |
| 98 | setbits_le32(RCC_BDCR, RCC_BDCR_VSWRST); |
| 99 | while (!(readl(RCC_BDCR) & RCC_BDCR_VSWRST)) |
| 100 | ; |
| 101 | clrbits_le32(RCC_BDCR, RCC_BDCR_VSWRST); |
| 102 | } |
| 103 | |
| 104 | /* allow non secure access in Write/Read for all peripheral */ |
| 105 | writel(GENMASK(25, 0), ETZPC_DECPROT0); |
| 106 | |
| 107 | /* Open SYSRAM for no secure access */ |
| 108 | writel(0x0, ETZPC_TZMA1_SIZE); |
| 109 | |
| 110 | /* enable TZC1 TZC2 clock */ |
| 111 | writel(BIT(11) | BIT(12), RCC_MP_APB5ENSETR); |
| 112 | |
| 113 | /* Region 0 set to no access by default */ |
| 114 | /* bit 0 / 16 => nsaid0 read/write Enable |
| 115 | * bit 1 / 17 => nsaid1 read/write Enable |
| 116 | * ... |
| 117 | * bit 15 / 31 => nsaid15 read/write Enable |
| 118 | */ |
| 119 | writel(0xFFFFFFFF, TZC_REGION_ID_ACCESS0); |
| 120 | /* bit 30 / 31 => Secure Global Enable : write/read */ |
| 121 | /* bit 0 / 1 => Region Enable for filter 0/1 */ |
| 122 | writel(BIT(0) | BIT(1) | BIT(30) | BIT(31), TZC_REGION_ATTRIBUTE0); |
| 123 | |
| 124 | /* Enable Filter 0 and 1 */ |
| 125 | setbits_le32(TZC_GATE_KEEPER, BIT(0) | BIT(1)); |
| 126 | |
| 127 | /* RCC trust zone deactivated */ |
| 128 | writel(0x0, RCC_TZCR); |
| 129 | |
| 130 | /* TAMP: deactivate the internal tamper |
| 131 | * Bit 23 ITAMP8E: monotonic counter overflow |
| 132 | * Bit 20 ITAMP5E: RTC calendar overflow |
| 133 | * Bit 19 ITAMP4E: HSE monitoring |
| 134 | * Bit 18 ITAMP3E: LSE monitoring |
| 135 | * Bit 16 ITAMP1E: RTC power domain supply monitoring |
| 136 | */ |
| 137 | writel(0x0, TAMP_CR1); |
| 138 | |
| 139 | /* GPIOZ: deactivate the security */ |
| 140 | writel(BIT(0), RCC_MP_AHB5ENSETR); |
| 141 | writel(0x0, GPIOZ_SECCFGR); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Debug init |
| 146 | */ |
| 147 | void dbgmcu_init(void) |
| 148 | { |
| 149 | /* |
| 150 | * Freeze IWDG2 if Cortex-A7 is in debug mode |
| 151 | * done in TF-A for TRUSTED boot and |
| 152 | * DBGMCU access is controlled by BSEC_DENABLE.DBGSWENABLE |
| 153 | */ |
| 154 | if (bsec_dbgswenable()) { |
| 155 | setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN); |
| 156 | setbits_le32(DBGMCU_APB4FZ1, DBGMCU_APB4FZ1_IWDG2); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void spl_board_init(void) |
| 161 | { |
| 162 | struct udevice *dev; |
| 163 | int ret; |
| 164 | |
| 165 | dbgmcu_init(); |
| 166 | |
| 167 | /* force probe of BSEC driver to shadow the upper OTP */ |
| 168 | ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(stm32mp_bsec), &dev); |
| 169 | if (ret) |
| 170 | log_warning("BSEC probe failed: %d\n", ret); |
| 171 | } |
| 172 | |
| 173 | /* get bootmode from ROM code boot context: saved in TAMP register */ |
| 174 | static void update_bootmode(void) |
| 175 | { |
| 176 | u32 boot_mode; |
| 177 | u32 bootrom_itf = readl(BOOTROM_PARAM_ADDR); |
| 178 | u32 bootrom_device, bootrom_instance; |
| 179 | |
| 180 | /* enable TAMP clock = RTCAPBEN */ |
| 181 | writel(BIT(8), RCC_MP_APB5ENSETR); |
| 182 | |
| 183 | /* read bootrom context */ |
| 184 | bootrom_device = |
| 185 | (bootrom_itf & BOOTROM_MODE_MASK) >> BOOTROM_MODE_SHIFT; |
| 186 | bootrom_instance = |
| 187 | (bootrom_itf & BOOTROM_INSTANCE_MASK) >> BOOTROM_INSTANCE_SHIFT; |
| 188 | boot_mode = |
| 189 | ((bootrom_device << BOOT_TYPE_SHIFT) & BOOT_TYPE_MASK) | |
| 190 | ((bootrom_instance << BOOT_INSTANCE_SHIFT) & |
| 191 | BOOT_INSTANCE_MASK); |
| 192 | |
| 193 | /* save the boot mode in TAMP backup register */ |
| 194 | clrsetbits_le32(TAMP_BOOT_CONTEXT, |
| 195 | TAMP_BOOT_MODE_MASK, |
| 196 | boot_mode << TAMP_BOOT_MODE_SHIFT); |
| 197 | } |
| 198 | |
| 199 | /* weak function: STM32MP15x mach init for boot without TFA */ |
| 200 | void stm32mp_cpu_init(void) |
| 201 | { |
| 202 | if (IS_ENABLED(CONFIG_SPL_BUILD)) { |
| 203 | security_init(); |
| 204 | update_bootmode(); |
| 205 | } |
| 206 | |
| 207 | /* reset copro state in SPL, when used, or in U-Boot */ |
| 208 | if (!IS_ENABLED(CONFIG_SPL) || IS_ENABLED(CONFIG_SPL_BUILD)) { |
| 209 | /* Reset Coprocessor state unless it wakes up from Standby power mode */ |
| 210 | if (!(readl(PWR_MCUCR) & PWR_MCUCR_SBF)) { |
| 211 | writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE); |
| 212 | writel(0, TAMP_COPRO_RSC_TBL_ADDRESS); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static u32 read_idc(void) |
| 218 | { |
| 219 | /* DBGMCU access is controlled by BSEC_DENABLE.DBGSWENABLE */ |
| 220 | if (bsec_dbgswenable()) { |
| 221 | setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN); |
| 222 | |
| 223 | return readl(DBGMCU_IDC); |
| 224 | } |
| 225 | |
| 226 | return CPU_DEV_STM32MP15; /* STM32MP15x and unknown revision */ |
| 227 | } |
| 228 | |
| 229 | u32 get_cpu_dev(void) |
| 230 | { |
| 231 | return (read_idc() & DBGMCU_IDC_DEV_ID_MASK) >> DBGMCU_IDC_DEV_ID_SHIFT; |
| 232 | } |
| 233 | |
| 234 | u32 get_cpu_rev(void) |
| 235 | { |
| 236 | return (read_idc() & DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT; |
| 237 | } |
| 238 | |
| 239 | /* Get Device Part Number (RPN) from OTP */ |
| 240 | static u32 get_cpu_rpn(void) |
| 241 | { |
| 242 | return get_otp(BSEC_OTP_RPN, RPN_SHIFT, RPN_MASK); |
| 243 | } |
| 244 | |
| 245 | u32 get_cpu_type(void) |
| 246 | { |
| 247 | return (get_cpu_dev() << 16) | get_cpu_rpn(); |
| 248 | } |
| 249 | |
Patrick Delaunay | 6425f58 | 2022-05-20 18:24:47 +0200 | [diff] [blame] | 250 | int get_eth_nb(void) |
| 251 | { |
| 252 | return 1; |
| 253 | } |
| 254 | |
Patrick Delaunay | e4bdd54 | 2022-05-20 18:24:42 +0200 | [diff] [blame] | 255 | /* Get Package options from OTP */ |
| 256 | u32 get_cpu_package(void) |
| 257 | { |
| 258 | return get_otp(BSEC_OTP_PKG, PKG_SHIFT, PKG_MASK); |
| 259 | } |
| 260 | |
| 261 | static const char * const soc_type[] = { |
| 262 | "????", |
| 263 | "151C", "151A", "151F", "151D", |
| 264 | "153C", "153A", "153F", "153D", |
| 265 | "157C", "157A", "157F", "157D" |
| 266 | }; |
| 267 | |
| 268 | static const char * const soc_pkg[] = { "??", "AD", "AC", "AB", "AA" }; |
| 269 | static const char * const soc_rev[] = { "?", "A", "B", "Z" }; |
| 270 | |
| 271 | static void get_cpu_string_offsets(unsigned int *type, unsigned int *pkg, |
| 272 | unsigned int *rev) |
| 273 | { |
| 274 | u32 cpu_type = get_cpu_type(); |
| 275 | u32 ct = cpu_type & ~(BIT(7) | BIT(0)); |
| 276 | u32 cm = ((cpu_type & BIT(7)) >> 6) | (cpu_type & BIT(0)); |
Patrick Delaunay | e4bdd54 | 2022-05-20 18:24:42 +0200 | [diff] [blame] | 277 | |
| 278 | /* Bits 0 and 7 are the ACDF, 00:C 01:A 10:F 11:D */ |
| 279 | switch (ct) { |
| 280 | case CPU_STM32MP151Cxx: |
| 281 | *type = cm + 1; |
| 282 | break; |
| 283 | case CPU_STM32MP153Cxx: |
| 284 | *type = cm + 5; |
| 285 | break; |
| 286 | case CPU_STM32MP157Cxx: |
| 287 | *type = cm + 9; |
| 288 | break; |
| 289 | default: |
| 290 | *type = 0; |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | /* Package */ |
Patrick Delaunay | 14704dc | 2022-06-20 09:50:01 +0200 | [diff] [blame] | 295 | *pkg = get_cpu_package(); |
| 296 | if (*pkg > STM32MP15_PKG_AA_LBGA448) |
| 297 | *pkg = STM32MP15_PKG_UNKNOWN; |
Patrick Delaunay | e4bdd54 | 2022-05-20 18:24:42 +0200 | [diff] [blame] | 298 | |
| 299 | /* Revision */ |
| 300 | switch (get_cpu_rev()) { |
| 301 | case CPU_REV1: |
| 302 | *rev = 1; |
| 303 | break; |
| 304 | case CPU_REV2: |
| 305 | *rev = 2; |
| 306 | break; |
| 307 | case CPU_REV2_1: |
| 308 | *rev = 3; |
| 309 | break; |
| 310 | default: |
| 311 | *rev = 0; |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void get_soc_name(char name[SOC_NAME_SIZE]) |
| 317 | { |
| 318 | unsigned int type, pkg, rev; |
| 319 | |
| 320 | get_cpu_string_offsets(&type, &pkg, &rev); |
| 321 | |
| 322 | snprintf(name, SOC_NAME_SIZE, "STM32MP%s%s Rev.%s", |
| 323 | soc_type[type], soc_pkg[pkg], soc_rev[rev]); |
| 324 | } |
| 325 | |
| 326 | static void setup_soc_type_pkg_rev(void) |
| 327 | { |
| 328 | unsigned int type, pkg, rev; |
| 329 | |
| 330 | get_cpu_string_offsets(&type, &pkg, &rev); |
| 331 | |
| 332 | env_set("soc_type", soc_type[type]); |
| 333 | env_set("soc_pkg", soc_pkg[pkg]); |
| 334 | env_set("soc_rev", soc_rev[rev]); |
| 335 | } |
| 336 | |
| 337 | /* weak function called in arch_misc_init */ |
| 338 | void stm32mp_misc_init(void) |
| 339 | { |
| 340 | setup_soc_type_pkg_rev(); |
| 341 | } |