blob: e1f4f3541f667f6388ffcf8fbe6642c92f9133da [file] [log] [blame]
Caleb Connollye6c284b2023-11-20 20:48:00 +00001// 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 Connolly16da8c72023-10-03 11:35:40 +010023#include <lmb.h>
Caleb Connollye6c284b2023-11-20 20:48:00 +000024#include <malloc.h>
25
26DECLARE_GLOBAL_DATA_PTR;
27
28static struct mm_region rbx_mem_map[CONFIG_NR_DRAM_BANKS + 2] = { { 0 } };
29
30struct mm_region *mem_map = rbx_mem_map;
31
32int dram_init(void)
33{
34 return fdtdec_setup_mem_size_base();
35}
36
37int 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
63static 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
74void *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
Caleb Connollye6c284b2023-11-20 20:48:00 +000094/*
95 * Some boards still need board specific init code, they can implement that by
96 * overriding this function.
97 *
98 * FIXME: get rid of board specific init code
99 */
100void __weak qcom_board_init(void)
101{
102}
103
104int board_init(void)
105{
106 show_psci_version();
107 qcom_board_init();
108 return 0;
109}
110
Caleb Connolly28a30542023-10-17 13:03:32 +0100111/* Sets up the "board", and "soc" environment variables as well as constructing the devicetree
112 * path, with a few quirks to handle non-standard dtb filenames. This is not meant to be a
113 * comprehensive solution to automatically picking the DTB, but aims to be correct for the
114 * majority case. For most devices it should be possible to make this algorithm work by
115 * adjusting the root compatible property in the U-Boot DTS. Handling devices with multiple
116 * variants that are all supported by a single U-Boot image will require implementing device-
117 * specific detection.
118 */
119static void configure_env(void)
120{
121 const char *first_compat, *last_compat;
122 char *tmp;
123 char buf[32] = { 0 };
124 /*
125 * Most DTB filenames follow the scheme: qcom/<soc>-[vendor]-<board>.dtb
126 * The vendor is skipped when it's a Qualcomm reference board, or the
127 * db845c.
128 */
129 char dt_path[64] = { 0 };
130 int compat_count, ret;
131 ofnode root;
132
133 root = ofnode_root();
134 /* This is almost always 2, but be explicit that we want the first and last compatibles
135 * not the first and second.
136 */
137 compat_count = ofnode_read_string_count(root, "compatible");
138 if (compat_count < 2) {
139 log_warning("%s: only one root compatible bailing!\n", __func__);
140 return;
141 }
142
143 /* The most specific device compatible (e.g. "thundercomm,db845c") */
144 ret = ofnode_read_string_index(root, "compatible", 0, &first_compat);
145 if (ret < 0) {
146 log_warning("Can't read first compatible\n");
147 return;
148 }
149
150 /* The last compatible is always the SoC compatible */
151 ret = ofnode_read_string_index(root, "compatible", compat_count - 1, &last_compat);
152 if (ret < 0) {
153 log_warning("Can't read second compatible\n");
154 return;
155 }
156
157 /* Copy the second compat (e.g. "qcom,sdm845") into buf */
158 strlcpy(buf, last_compat, sizeof(buf) - 1);
159 tmp = buf;
160
161 /* strsep() is destructive, it replaces the comma with a \0 */
162 if (!strsep(&tmp, ",")) {
163 log_warning("second compatible '%s' has no ','\n", buf);
164 return;
165 }
166
167 /* tmp now points to just the "sdm845" part of the string */
168 env_set("soc", tmp);
169
170 /* Now figure out the "board" part from the first compatible */
171 memset(buf, 0, sizeof(buf));
172 strlcpy(buf, first_compat, sizeof(buf) - 1);
173 tmp = buf;
174
175 /* The Qualcomm reference boards (RBx, HDK, etc) */
176 if (!strncmp("qcom", buf, strlen("qcom"))) {
177 /*
178 * They all have the first compatible as "qcom,<soc>-<board>"
179 * (e.g. "qcom,qrb5165-rb5"). We extract just the part after
180 * the dash.
181 */
182 if (!strsep(&tmp, "-")) {
183 log_warning("compatible '%s' has no '-'\n", buf);
184 return;
185 }
186 /* tmp is now "rb5" */
187 env_set("board", tmp);
188 } else {
189 if (!strsep(&tmp, ",")) {
190 log_warning("compatible '%s' has no ','\n", buf);
191 return;
192 }
193 /* for thundercomm we just want the bit after the comma (e.g. "db845c"),
194 * for all other boards we replace the comma with a '-' and take both
195 * (e.g. "oneplus-enchilada")
196 */
197 if (!strncmp("thundercomm", buf, strlen("thundercomm"))) {
198 env_set("board", tmp);
199 } else {
200 *(tmp - 1) = '-';
201 env_set("board", buf);
202 }
203 }
204
205 /* Now build the full path name */
206 snprintf(dt_path, sizeof(dt_path), "qcom/%s-%s.dtb",
207 env_get("soc"), env_get("board"));
208 env_set("fdtfile", dt_path);
209}
210
Caleb Connolly16da8c72023-10-03 11:35:40 +0100211void __weak qcom_late_init(void)
212{
213}
214
215#define KERNEL_COMP_SIZE SZ_64M
216#define SZ_96M (SZ_64M + SZ_32M)
Caleb Connollydcca2dd2023-12-04 15:00:37 +0000217#ifdef CONFIG_FASTBOOT_BUF_SIZE
218#define FASTBOOT_BUF_SIZE CONFIG_FASTBOOT_BUF_SIZE
219#else
220#define FASTBOOT_BUF_SIZE 0
221#endif
Caleb Connolly16da8c72023-10-03 11:35:40 +0100222
223#define addr_alloc(lmb, size) lmb_alloc(lmb, size, SZ_2M)
224
225/* Stolen from arch/arm/mach-apple/board.c */
226int board_late_init(void)
227{
228 struct lmb lmb;
229 u32 status = 0;
230
231 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
232
233 /* We need to be fairly conservative here as we support boards with just 1G of TOTAL RAM */
234 status |= env_set_hex("kernel_addr_r", addr_alloc(&lmb, SZ_128M));
235 status |= env_set_hex("ramdisk_addr_r", addr_alloc(&lmb, SZ_96M));
236 status |= env_set_hex("kernel_comp_addr_r", addr_alloc(&lmb, KERNEL_COMP_SIZE));
237 status |= env_set_hex("kernel_comp_size", KERNEL_COMP_SIZE);
Caleb Connollydcca2dd2023-12-04 15:00:37 +0000238 if (IS_ENABLED(CONFIG_FASTBOOT))
239 status |= env_set_hex("fastboot_addr_r", addr_alloc(&lmb, FASTBOOT_BUF_SIZE));
Caleb Connolly16da8c72023-10-03 11:35:40 +0100240 status |= env_set_hex("scriptaddr", addr_alloc(&lmb, SZ_4M));
241 status |= env_set_hex("pxefile_addr_r", addr_alloc(&lmb, SZ_4M));
242 status |= env_set_hex("fdt_addr_r", addr_alloc(&lmb, SZ_2M));
243
244 if (status)
245 log_warning("%s: Failed to set run time variables\n", __func__);
246
Caleb Connolly28a30542023-10-17 13:03:32 +0100247 configure_env();
Caleb Connolly16da8c72023-10-03 11:35:40 +0100248 qcom_late_init();
249
250 return 0;
251}
252
Caleb Connollye6c284b2023-11-20 20:48:00 +0000253static void build_mem_map(void)
254{
255 int i;
256
257 /*
258 * Ensure the peripheral block is sized to correctly cover the address range
259 * up to the first memory bank.
260 * Don't map the first page to ensure that we actually trigger an abort on a
261 * null pointer access rather than just hanging.
262 * FIXME: we should probably split this into more precise regions
263 */
264 mem_map[0].phys = 0x1000;
265 mem_map[0].virt = mem_map[0].phys;
266 mem_map[0].size = gd->bd->bi_dram[0].start - mem_map[0].phys;
267 mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
268 PTE_BLOCK_NON_SHARE |
269 PTE_BLOCK_PXN | PTE_BLOCK_UXN;
270
271 debug("Configured memory map:\n");
272 debug(" 0x%016llx - 0x%016llx: Peripheral block\n",
273 mem_map[0].phys, mem_map[0].phys + mem_map[0].size);
274
275 /*
276 * Now add memory map entries for each DRAM bank, ensuring we don't
277 * overwrite the list terminator
278 */
279 for (i = 0; i < ARRAY_SIZE(rbx_mem_map) - 2 && gd->bd->bi_dram[i].size; i++) {
280 if (i == ARRAY_SIZE(rbx_mem_map) - 1) {
281 log_warning("Too many DRAM banks!\n");
282 break;
283 }
284 mem_map[i + 1].phys = gd->bd->bi_dram[i].start;
285 mem_map[i + 1].virt = mem_map[i + 1].phys;
286 mem_map[i + 1].size = gd->bd->bi_dram[i].size;
287 mem_map[i + 1].attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
288 PTE_BLOCK_INNER_SHARE;
289
290 debug(" 0x%016llx - 0x%016llx: DDR bank %d\n",
291 mem_map[i + 1].phys, mem_map[i + 1].phys + mem_map[i + 1].size, i);
292 }
293}
294
295u64 get_page_table_size(void)
296{
297 return SZ_64K;
298}
299
300void enable_caches(void)
301{
302 build_mem_map();
303
304 icache_enable();
305 dcache_enable();
306}