Caleb Connolly | e6c284b | 2023-11-20 20:48:00 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Common initialisation for Qualcomm Snapdragon boards. |
| 4 | * |
| 5 | * Copyright (c) 2023 Linaro Ltd. |
| 6 | * Author: Caleb Connolly <caleb.connolly@linaro.org> |
| 7 | */ |
| 8 | |
| 9 | #define LOG_DEBUG |
| 10 | |
| 11 | #include <asm/armv8/mmu.h> |
| 12 | #include <asm/gpio.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <asm/psci.h> |
| 15 | #include <asm/system.h> |
| 16 | #include <dm/device.h> |
| 17 | #include <env.h> |
| 18 | #include <init.h> |
| 19 | #include <linux/arm-smccc.h> |
| 20 | #include <linux/bug.h> |
| 21 | #include <linux/psci.h> |
| 22 | #include <linux/sizes.h> |
Caleb Connolly | 16da8c7 | 2023-10-03 11:35:40 +0100 | [diff] [blame] | 23 | #include <lmb.h> |
Caleb Connolly | e6c284b | 2023-11-20 20:48:00 +0000 | [diff] [blame] | 24 | #include <malloc.h> |
| 25 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
| 28 | static struct mm_region rbx_mem_map[CONFIG_NR_DRAM_BANKS + 2] = { { 0 } }; |
| 29 | |
| 30 | struct mm_region *mem_map = rbx_mem_map; |
| 31 | |
| 32 | int dram_init(void) |
| 33 | { |
| 34 | return fdtdec_setup_mem_size_base(); |
| 35 | } |
| 36 | |
| 37 | int dram_init_banksize(void) |
| 38 | { |
| 39 | int ret; |
| 40 | phys_addr_t start, size; |
| 41 | |
| 42 | ret = fdtdec_setup_memory_banksize(); |
| 43 | if (ret < 0) |
| 44 | return ret; |
| 45 | |
| 46 | if (WARN(CONFIG_NR_DRAM_BANKS < 2, "CONFIG_NR_DRAM_BANKS should be at least 2")) |
| 47 | return 0; |
| 48 | |
| 49 | /* Some bootloaders populate the RAM banks in the wrong order -_- */ |
| 50 | start = gd->bd->bi_dram[1].start; |
| 51 | size = gd->bd->bi_dram[1].size; |
| 52 | if (size && start < gd->bd->bi_dram[0].start) { |
| 53 | debug("Swapping DRAM banks\n"); |
| 54 | gd->bd->bi_dram[1].start = gd->bd->bi_dram[0].start; |
| 55 | gd->bd->bi_dram[1].size = gd->bd->bi_dram[0].size; |
| 56 | gd->bd->bi_dram[0].start = start; |
| 57 | gd->bd->bi_dram[0].size = size; |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static void show_psci_version(void) |
| 64 | { |
| 65 | struct arm_smccc_res res; |
| 66 | |
| 67 | arm_smccc_smc(ARM_PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0, 0, 0, 0, 0, &res); |
| 68 | |
| 69 | debug("PSCI: v%ld.%ld\n", |
| 70 | PSCI_VERSION_MAJOR(res.a0), |
| 71 | PSCI_VERSION_MINOR(res.a0)); |
| 72 | } |
| 73 | |
| 74 | void *board_fdt_blob_setup(int *err) |
| 75 | { |
| 76 | phys_addr_t fdt; |
| 77 | /* Return DTB pointer passed by ABL */ |
| 78 | *err = 0; |
| 79 | fdt = get_prev_bl_fdt_addr(); |
| 80 | |
| 81 | /* |
| 82 | * If we bail then the board will simply not boot, instead let's |
| 83 | * try and use the FDT built into U-Boot if there is one... |
| 84 | * This avoids having a hard dependency on the previous stage bootloader |
| 85 | */ |
| 86 | if (IS_ENABLED(CONFIG_OF_SEPARATE) && (!fdt || fdt != ALIGN(fdt, SZ_4K))) { |
| 87 | debug("%s: Using built in FDT, bootloader gave us %#llx\n", __func__, fdt); |
| 88 | return (void *)gd->fdt_blob; |
| 89 | } |
| 90 | |
| 91 | return (void *)fdt; |
| 92 | } |
| 93 | |
| 94 | void reset_cpu(void) |
| 95 | { |
| 96 | psci_system_reset(); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Some boards still need board specific init code, they can implement that by |
| 101 | * overriding this function. |
| 102 | * |
| 103 | * FIXME: get rid of board specific init code |
| 104 | */ |
| 105 | void __weak qcom_board_init(void) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | int board_init(void) |
| 110 | { |
| 111 | show_psci_version(); |
| 112 | qcom_board_init(); |
| 113 | return 0; |
| 114 | } |
| 115 | |
Caleb Connolly | 28a3054 | 2023-10-17 13:03:32 +0100 | [diff] [blame] | 116 | /* Sets up the "board", and "soc" environment variables as well as constructing the devicetree |
| 117 | * path, with a few quirks to handle non-standard dtb filenames. This is not meant to be a |
| 118 | * comprehensive solution to automatically picking the DTB, but aims to be correct for the |
| 119 | * majority case. For most devices it should be possible to make this algorithm work by |
| 120 | * adjusting the root compatible property in the U-Boot DTS. Handling devices with multiple |
| 121 | * variants that are all supported by a single U-Boot image will require implementing device- |
| 122 | * specific detection. |
| 123 | */ |
| 124 | static void configure_env(void) |
| 125 | { |
| 126 | const char *first_compat, *last_compat; |
| 127 | char *tmp; |
| 128 | char buf[32] = { 0 }; |
| 129 | /* |
| 130 | * Most DTB filenames follow the scheme: qcom/<soc>-[vendor]-<board>.dtb |
| 131 | * The vendor is skipped when it's a Qualcomm reference board, or the |
| 132 | * db845c. |
| 133 | */ |
| 134 | char dt_path[64] = { 0 }; |
| 135 | int compat_count, ret; |
| 136 | ofnode root; |
| 137 | |
| 138 | root = ofnode_root(); |
| 139 | /* This is almost always 2, but be explicit that we want the first and last compatibles |
| 140 | * not the first and second. |
| 141 | */ |
| 142 | compat_count = ofnode_read_string_count(root, "compatible"); |
| 143 | if (compat_count < 2) { |
| 144 | log_warning("%s: only one root compatible bailing!\n", __func__); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | /* The most specific device compatible (e.g. "thundercomm,db845c") */ |
| 149 | ret = ofnode_read_string_index(root, "compatible", 0, &first_compat); |
| 150 | if (ret < 0) { |
| 151 | log_warning("Can't read first compatible\n"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | /* The last compatible is always the SoC compatible */ |
| 156 | ret = ofnode_read_string_index(root, "compatible", compat_count - 1, &last_compat); |
| 157 | if (ret < 0) { |
| 158 | log_warning("Can't read second compatible\n"); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | /* Copy the second compat (e.g. "qcom,sdm845") into buf */ |
| 163 | strlcpy(buf, last_compat, sizeof(buf) - 1); |
| 164 | tmp = buf; |
| 165 | |
| 166 | /* strsep() is destructive, it replaces the comma with a \0 */ |
| 167 | if (!strsep(&tmp, ",")) { |
| 168 | log_warning("second compatible '%s' has no ','\n", buf); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | /* tmp now points to just the "sdm845" part of the string */ |
| 173 | env_set("soc", tmp); |
| 174 | |
| 175 | /* Now figure out the "board" part from the first compatible */ |
| 176 | memset(buf, 0, sizeof(buf)); |
| 177 | strlcpy(buf, first_compat, sizeof(buf) - 1); |
| 178 | tmp = buf; |
| 179 | |
| 180 | /* The Qualcomm reference boards (RBx, HDK, etc) */ |
| 181 | if (!strncmp("qcom", buf, strlen("qcom"))) { |
| 182 | /* |
| 183 | * They all have the first compatible as "qcom,<soc>-<board>" |
| 184 | * (e.g. "qcom,qrb5165-rb5"). We extract just the part after |
| 185 | * the dash. |
| 186 | */ |
| 187 | if (!strsep(&tmp, "-")) { |
| 188 | log_warning("compatible '%s' has no '-'\n", buf); |
| 189 | return; |
| 190 | } |
| 191 | /* tmp is now "rb5" */ |
| 192 | env_set("board", tmp); |
| 193 | } else { |
| 194 | if (!strsep(&tmp, ",")) { |
| 195 | log_warning("compatible '%s' has no ','\n", buf); |
| 196 | return; |
| 197 | } |
| 198 | /* for thundercomm we just want the bit after the comma (e.g. "db845c"), |
| 199 | * for all other boards we replace the comma with a '-' and take both |
| 200 | * (e.g. "oneplus-enchilada") |
| 201 | */ |
| 202 | if (!strncmp("thundercomm", buf, strlen("thundercomm"))) { |
| 203 | env_set("board", tmp); |
| 204 | } else { |
| 205 | *(tmp - 1) = '-'; |
| 206 | env_set("board", buf); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* Now build the full path name */ |
| 211 | snprintf(dt_path, sizeof(dt_path), "qcom/%s-%s.dtb", |
| 212 | env_get("soc"), env_get("board")); |
| 213 | env_set("fdtfile", dt_path); |
| 214 | } |
| 215 | |
Caleb Connolly | 16da8c7 | 2023-10-03 11:35:40 +0100 | [diff] [blame] | 216 | void __weak qcom_late_init(void) |
| 217 | { |
| 218 | } |
| 219 | |
| 220 | #define KERNEL_COMP_SIZE SZ_64M |
| 221 | #define SZ_96M (SZ_64M + SZ_32M) |
Caleb Connolly | dcca2dd | 2023-12-04 15:00:37 +0000 | [diff] [blame^] | 222 | #ifdef CONFIG_FASTBOOT_BUF_SIZE |
| 223 | #define FASTBOOT_BUF_SIZE CONFIG_FASTBOOT_BUF_SIZE |
| 224 | #else |
| 225 | #define FASTBOOT_BUF_SIZE 0 |
| 226 | #endif |
Caleb Connolly | 16da8c7 | 2023-10-03 11:35:40 +0100 | [diff] [blame] | 227 | |
| 228 | #define addr_alloc(lmb, size) lmb_alloc(lmb, size, SZ_2M) |
| 229 | |
| 230 | /* Stolen from arch/arm/mach-apple/board.c */ |
| 231 | int board_late_init(void) |
| 232 | { |
| 233 | struct lmb lmb; |
| 234 | u32 status = 0; |
| 235 | |
| 236 | lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob); |
| 237 | |
| 238 | /* We need to be fairly conservative here as we support boards with just 1G of TOTAL RAM */ |
| 239 | status |= env_set_hex("kernel_addr_r", addr_alloc(&lmb, SZ_128M)); |
| 240 | status |= env_set_hex("ramdisk_addr_r", addr_alloc(&lmb, SZ_96M)); |
| 241 | status |= env_set_hex("kernel_comp_addr_r", addr_alloc(&lmb, KERNEL_COMP_SIZE)); |
| 242 | status |= env_set_hex("kernel_comp_size", KERNEL_COMP_SIZE); |
Caleb Connolly | dcca2dd | 2023-12-04 15:00:37 +0000 | [diff] [blame^] | 243 | if (IS_ENABLED(CONFIG_FASTBOOT)) |
| 244 | status |= env_set_hex("fastboot_addr_r", addr_alloc(&lmb, FASTBOOT_BUF_SIZE)); |
Caleb Connolly | 16da8c7 | 2023-10-03 11:35:40 +0100 | [diff] [blame] | 245 | status |= env_set_hex("scriptaddr", addr_alloc(&lmb, SZ_4M)); |
| 246 | status |= env_set_hex("pxefile_addr_r", addr_alloc(&lmb, SZ_4M)); |
| 247 | status |= env_set_hex("fdt_addr_r", addr_alloc(&lmb, SZ_2M)); |
| 248 | |
| 249 | if (status) |
| 250 | log_warning("%s: Failed to set run time variables\n", __func__); |
| 251 | |
Caleb Connolly | 28a3054 | 2023-10-17 13:03:32 +0100 | [diff] [blame] | 252 | configure_env(); |
Caleb Connolly | 16da8c7 | 2023-10-03 11:35:40 +0100 | [diff] [blame] | 253 | qcom_late_init(); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Caleb Connolly | e6c284b | 2023-11-20 20:48:00 +0000 | [diff] [blame] | 258 | static void build_mem_map(void) |
| 259 | { |
| 260 | int i; |
| 261 | |
| 262 | /* |
| 263 | * Ensure the peripheral block is sized to correctly cover the address range |
| 264 | * up to the first memory bank. |
| 265 | * Don't map the first page to ensure that we actually trigger an abort on a |
| 266 | * null pointer access rather than just hanging. |
| 267 | * FIXME: we should probably split this into more precise regions |
| 268 | */ |
| 269 | mem_map[0].phys = 0x1000; |
| 270 | mem_map[0].virt = mem_map[0].phys; |
| 271 | mem_map[0].size = gd->bd->bi_dram[0].start - mem_map[0].phys; |
| 272 | mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 273 | PTE_BLOCK_NON_SHARE | |
| 274 | PTE_BLOCK_PXN | PTE_BLOCK_UXN; |
| 275 | |
| 276 | debug("Configured memory map:\n"); |
| 277 | debug(" 0x%016llx - 0x%016llx: Peripheral block\n", |
| 278 | mem_map[0].phys, mem_map[0].phys + mem_map[0].size); |
| 279 | |
| 280 | /* |
| 281 | * Now add memory map entries for each DRAM bank, ensuring we don't |
| 282 | * overwrite the list terminator |
| 283 | */ |
| 284 | for (i = 0; i < ARRAY_SIZE(rbx_mem_map) - 2 && gd->bd->bi_dram[i].size; i++) { |
| 285 | if (i == ARRAY_SIZE(rbx_mem_map) - 1) { |
| 286 | log_warning("Too many DRAM banks!\n"); |
| 287 | break; |
| 288 | } |
| 289 | mem_map[i + 1].phys = gd->bd->bi_dram[i].start; |
| 290 | mem_map[i + 1].virt = mem_map[i + 1].phys; |
| 291 | mem_map[i + 1].size = gd->bd->bi_dram[i].size; |
| 292 | mem_map[i + 1].attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 293 | PTE_BLOCK_INNER_SHARE; |
| 294 | |
| 295 | debug(" 0x%016llx - 0x%016llx: DDR bank %d\n", |
| 296 | mem_map[i + 1].phys, mem_map[i + 1].phys + mem_map[i + 1].size, i); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | u64 get_page_table_size(void) |
| 301 | { |
| 302 | return SZ_64K; |
| 303 | } |
| 304 | |
| 305 | void enable_caches(void) |
| 306 | { |
| 307 | build_mem_map(); |
| 308 | |
| 309 | icache_enable(); |
| 310 | dcache_enable(); |
| 311 | } |