Patrick Delaunay | 05d3693 | 2019-07-05 17:20:14 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2019, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <fdt_support.h> |
| 8 | #include <asm/arch/sys_proto.h> |
| 9 | #include <dt-bindings/pinctrl/stm32-pinfunc.h> |
Patrick Delaunay | c60f3b3 | 2019-07-05 17:20:15 +0200 | [diff] [blame] | 10 | #include <linux/io.h> |
| 11 | |
| 12 | #define ETZPC_DECPROT(n) (STM32_ETZPC_BASE + 0x10 + 4 * (n)) |
| 13 | #define ETZPC_DECPROT_NB 6 |
| 14 | |
| 15 | #define DECPROT_MASK 0x03 |
| 16 | #define NB_PROT_PER_REG 0x10 |
| 17 | #define DECPROT_NB_BITS 2 |
| 18 | |
| 19 | #define DECPROT_SECURED 0x00 |
| 20 | #define DECPROT_WRITE_SECURE 0x01 |
| 21 | #define DECPROT_MCU_ISOLATION 0x02 |
| 22 | #define DECPROT_NON_SECURED 0x03 |
| 23 | |
| 24 | #define ETZPC_RESERVED 0xffffffff |
| 25 | |
| 26 | static const u32 stm32mp1_ip_addr[] = { |
| 27 | 0x5c008000, /* 00 stgenc */ |
| 28 | 0x54000000, /* 01 bkpsram */ |
| 29 | 0x5c003000, /* 02 iwdg1 */ |
| 30 | 0x5c000000, /* 03 usart1 */ |
| 31 | 0x5c001000, /* 04 spi6 */ |
| 32 | 0x5c002000, /* 05 i2c4 */ |
| 33 | ETZPC_RESERVED, /* 06 reserved */ |
| 34 | 0x54003000, /* 07 rng1 */ |
| 35 | 0x54002000, /* 08 hash1 */ |
| 36 | 0x54001000, /* 09 cryp1 */ |
| 37 | 0x5a003000, /* 0A ddrctrl */ |
| 38 | 0x5a004000, /* 0B ddrphyc */ |
| 39 | 0x5c009000, /* 0C i2c6 */ |
| 40 | ETZPC_RESERVED, /* 0D reserved */ |
| 41 | ETZPC_RESERVED, /* 0E reserved */ |
| 42 | ETZPC_RESERVED, /* 0F reserved */ |
| 43 | 0x40000000, /* 10 tim2 */ |
| 44 | 0x40001000, /* 11 tim3 */ |
| 45 | 0x40002000, /* 12 tim4 */ |
| 46 | 0x40003000, /* 13 tim5 */ |
| 47 | 0x40004000, /* 14 tim6 */ |
| 48 | 0x40005000, /* 15 tim7 */ |
| 49 | 0x40006000, /* 16 tim12 */ |
| 50 | 0x40007000, /* 17 tim13 */ |
| 51 | 0x40008000, /* 18 tim14 */ |
| 52 | 0x40009000, /* 19 lptim1 */ |
| 53 | 0x4000a000, /* 1A wwdg1 */ |
| 54 | 0x4000b000, /* 1B spi2 */ |
| 55 | 0x4000c000, /* 1C spi3 */ |
| 56 | 0x4000d000, /* 1D spdifrx */ |
| 57 | 0x4000e000, /* 1E usart2 */ |
| 58 | 0x4000f000, /* 1F usart3 */ |
| 59 | 0x40010000, /* 20 uart4 */ |
| 60 | 0x40011000, /* 21 uart5 */ |
| 61 | 0x40012000, /* 22 i2c1 */ |
| 62 | 0x40013000, /* 23 i2c2 */ |
| 63 | 0x40014000, /* 24 i2c3 */ |
| 64 | 0x40015000, /* 25 i2c5 */ |
| 65 | 0x40016000, /* 26 cec */ |
| 66 | 0x40017000, /* 27 dac */ |
| 67 | 0x40018000, /* 28 uart7 */ |
| 68 | 0x40019000, /* 29 uart8 */ |
| 69 | ETZPC_RESERVED, /* 2A reserved */ |
| 70 | ETZPC_RESERVED, /* 2B reserved */ |
| 71 | 0x4001c000, /* 2C mdios */ |
| 72 | ETZPC_RESERVED, /* 2D reserved */ |
| 73 | ETZPC_RESERVED, /* 2E reserved */ |
| 74 | ETZPC_RESERVED, /* 2F reserved */ |
| 75 | 0x44000000, /* 30 tim1 */ |
| 76 | 0x44001000, /* 31 tim8 */ |
| 77 | ETZPC_RESERVED, /* 32 reserved */ |
| 78 | 0x44003000, /* 33 usart6 */ |
| 79 | 0x44004000, /* 34 spi1 */ |
| 80 | 0x44005000, /* 35 spi4 */ |
| 81 | 0x44006000, /* 36 tim15 */ |
| 82 | 0x44007000, /* 37 tim16 */ |
| 83 | 0x44008000, /* 38 tim17 */ |
| 84 | 0x44009000, /* 39 spi5 */ |
| 85 | 0x4400a000, /* 3A sai1 */ |
| 86 | 0x4400b000, /* 3B sai2 */ |
| 87 | 0x4400c000, /* 3C sai3 */ |
| 88 | 0x4400d000, /* 3D dfsdm */ |
| 89 | 0x4400e000, /* 3E tt_fdcan */ |
| 90 | ETZPC_RESERVED, /* 3F reserved */ |
| 91 | 0x50021000, /* 40 lptim2 */ |
| 92 | 0x50022000, /* 41 lptim3 */ |
| 93 | 0x50023000, /* 42 lptim4 */ |
| 94 | 0x50024000, /* 43 lptim5 */ |
| 95 | 0x50027000, /* 44 sai4 */ |
| 96 | 0x50025000, /* 45 vrefbuf */ |
| 97 | 0x4c006000, /* 46 dcmi */ |
| 98 | 0x4c004000, /* 47 crc2 */ |
| 99 | 0x48003000, /* 48 adc */ |
| 100 | 0x4c002000, /* 49 hash2 */ |
| 101 | 0x4c003000, /* 4A rng2 */ |
| 102 | 0x4c005000, /* 4B cryp2 */ |
| 103 | ETZPC_RESERVED, /* 4C reserved */ |
| 104 | ETZPC_RESERVED, /* 4D reserved */ |
| 105 | ETZPC_RESERVED, /* 4E reserved */ |
| 106 | ETZPC_RESERVED, /* 4F reserved */ |
| 107 | ETZPC_RESERVED, /* 50 sram1 */ |
| 108 | ETZPC_RESERVED, /* 51 sram2 */ |
| 109 | ETZPC_RESERVED, /* 52 sram3 */ |
| 110 | ETZPC_RESERVED, /* 53 sram4 */ |
| 111 | ETZPC_RESERVED, /* 54 retram */ |
| 112 | 0x49000000, /* 55 otg */ |
| 113 | 0x48004000, /* 56 sdmmc3 */ |
| 114 | 0x48005000, /* 57 dlybsd3 */ |
| 115 | 0x48000000, /* 58 dma1 */ |
| 116 | 0x48001000, /* 59 dma2 */ |
| 117 | 0x48002000, /* 5A dmamux */ |
| 118 | 0x58002000, /* 5B fmc */ |
| 119 | 0x58003000, /* 5C qspi */ |
| 120 | 0x58004000, /* 5D dlybq */ |
| 121 | 0x5800a000, /* 5E eth */ |
| 122 | ETZPC_RESERVED, /* 5F reserved */ |
| 123 | }; |
| 124 | |
| 125 | /* fdt helper */ |
| 126 | static bool fdt_disable_subnode_by_address(void *fdt, int offset, u32 addr) |
| 127 | { |
| 128 | int node; |
| 129 | |
| 130 | for (node = fdt_first_subnode(fdt, offset); |
| 131 | node >= 0; |
| 132 | node = fdt_next_subnode(fdt, node)) { |
| 133 | if (addr == (u32)fdt_getprop(fdt, node, "reg", 0)) { |
| 134 | if (fdtdec_get_is_enabled(fdt, node)) { |
| 135 | fdt_status_disabled(fdt, node); |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | static int stm32_fdt_fixup_etzpc(void *fdt) |
| 147 | { |
| 148 | const u32 *array; |
| 149 | int array_size, i; |
| 150 | int soc_node, offset, shift; |
| 151 | u32 addr, status, decprot[ETZPC_DECPROT_NB]; |
| 152 | |
| 153 | array = stm32mp1_ip_addr; |
| 154 | array_size = ARRAY_SIZE(stm32mp1_ip_addr); |
| 155 | |
| 156 | for (i = 0; i < ETZPC_DECPROT_NB; i++) |
| 157 | decprot[i] = readl(ETZPC_DECPROT(i)); |
| 158 | |
| 159 | soc_node = fdt_path_offset(fdt, "/soc"); |
| 160 | if (soc_node < 0) |
| 161 | return soc_node; |
| 162 | |
| 163 | for (i = 0; i < array_size; i++) { |
| 164 | offset = i / NB_PROT_PER_REG; |
| 165 | shift = (i % NB_PROT_PER_REG) * DECPROT_NB_BITS; |
| 166 | status = (decprot[offset] >> shift) & DECPROT_MASK; |
| 167 | addr = array[i]; |
| 168 | |
| 169 | debug("ETZPC: 0x%08x decprot %d=%d\n", addr, i, status); |
| 170 | |
| 171 | if (addr == ETZPC_RESERVED || |
| 172 | status == DECPROT_NON_SECURED) |
| 173 | continue; |
| 174 | |
| 175 | if (fdt_disable_subnode_by_address(fdt, soc_node, addr)) |
| 176 | printf("ETZPC: 0x%08x node disabled, decprot %d=%d\n", |
| 177 | addr, i, status); |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
Patrick Delaunay | 05d3693 | 2019-07-05 17:20:14 +0200 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * This function is called right before the kernel is booted. "blob" is the |
| 185 | * device tree that will be passed to the kernel. |
| 186 | */ |
| 187 | int ft_system_setup(void *blob, bd_t *bd) |
| 188 | { |
| 189 | int ret = 0; |
| 190 | u32 pkg; |
| 191 | |
Patrick Delaunay | c60f3b3 | 2019-07-05 17:20:15 +0200 | [diff] [blame] | 192 | if (CONFIG_IS_ENABLED(STM32_ETZPC)) { |
| 193 | ret = stm32_fdt_fixup_etzpc(blob); |
| 194 | if (ret) |
| 195 | return ret; |
| 196 | } |
| 197 | |
Patrick Delaunay | 05d3693 | 2019-07-05 17:20:14 +0200 | [diff] [blame] | 198 | switch (get_cpu_package()) { |
| 199 | case PKG_AA_LBGA448: |
| 200 | pkg = STM32MP_PKG_AA; |
| 201 | break; |
| 202 | case PKG_AB_LBGA354: |
| 203 | pkg = STM32MP_PKG_AB; |
| 204 | break; |
| 205 | case PKG_AC_TFBGA361: |
| 206 | pkg = STM32MP_PKG_AC; |
| 207 | break; |
| 208 | case PKG_AD_TFBGA257: |
| 209 | pkg = STM32MP_PKG_AD; |
| 210 | break; |
| 211 | default: |
| 212 | pkg = 0; |
| 213 | break; |
| 214 | } |
| 215 | if (pkg) { |
| 216 | do_fixup_by_compat_u32(blob, "st,stm32mp157-pinctrl", |
| 217 | "st,package", pkg, false); |
| 218 | do_fixup_by_compat_u32(blob, "st,stm32mp157-z-pinctrl", |
| 219 | "st,package", pkg, false); |
| 220 | } |
| 221 | |
| 222 | return ret; |
| 223 | } |