blob: 9bd9bcad77e0fda8fee805f750cab58ce4c50c42 [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
Caleb Connollyb55c0f82023-12-07 00:13:07 +000026#include "qcom-priv.h"
27
Caleb Connollye6c284b2023-11-20 20:48:00 +000028DECLARE_GLOBAL_DATA_PTR;
29
30static struct mm_region rbx_mem_map[CONFIG_NR_DRAM_BANKS + 2] = { { 0 } };
31
32struct mm_region *mem_map = rbx_mem_map;
33
34int dram_init(void)
35{
36 return fdtdec_setup_mem_size_base();
37}
38
39int dram_init_banksize(void)
40{
41 int ret;
42 phys_addr_t start, size;
43
44 ret = fdtdec_setup_memory_banksize();
45 if (ret < 0)
46 return ret;
47
48 if (WARN(CONFIG_NR_DRAM_BANKS < 2, "CONFIG_NR_DRAM_BANKS should be at least 2"))
49 return 0;
50
51 /* Some bootloaders populate the RAM banks in the wrong order -_- */
52 start = gd->bd->bi_dram[1].start;
53 size = gd->bd->bi_dram[1].size;
54 if (size && start < gd->bd->bi_dram[0].start) {
55 debug("Swapping DRAM banks\n");
56 gd->bd->bi_dram[1].start = gd->bd->bi_dram[0].start;
57 gd->bd->bi_dram[1].size = gd->bd->bi_dram[0].size;
58 gd->bd->bi_dram[0].start = start;
59 gd->bd->bi_dram[0].size = size;
60 }
61
62 return 0;
63}
64
65static void show_psci_version(void)
66{
67 struct arm_smccc_res res;
68
69 arm_smccc_smc(ARM_PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
70
71 debug("PSCI: v%ld.%ld\n",
72 PSCI_VERSION_MAJOR(res.a0),
73 PSCI_VERSION_MINOR(res.a0));
74}
75
76void *board_fdt_blob_setup(int *err)
77{
78 phys_addr_t fdt;
79 /* Return DTB pointer passed by ABL */
80 *err = 0;
81 fdt = get_prev_bl_fdt_addr();
82
83 /*
84 * If we bail then the board will simply not boot, instead let's
85 * try and use the FDT built into U-Boot if there is one...
86 * This avoids having a hard dependency on the previous stage bootloader
87 */
88 if (IS_ENABLED(CONFIG_OF_SEPARATE) && (!fdt || fdt != ALIGN(fdt, SZ_4K))) {
89 debug("%s: Using built in FDT, bootloader gave us %#llx\n", __func__, fdt);
90 return (void *)gd->fdt_blob;
91 }
92
93 return (void *)fdt;
94}
95
Caleb Connollye6c284b2023-11-20 20:48:00 +000096/*
97 * Some boards still need board specific init code, they can implement that by
98 * overriding this function.
99 *
100 * FIXME: get rid of board specific init code
101 */
102void __weak qcom_board_init(void)
103{
104}
105
106int board_init(void)
107{
108 show_psci_version();
109 qcom_board_init();
110 return 0;
111}
112
Caleb Connolly28a30542023-10-17 13:03:32 +0100113/* Sets up the "board", and "soc" environment variables as well as constructing the devicetree
114 * path, with a few quirks to handle non-standard dtb filenames. This is not meant to be a
115 * comprehensive solution to automatically picking the DTB, but aims to be correct for the
116 * majority case. For most devices it should be possible to make this algorithm work by
117 * adjusting the root compatible property in the U-Boot DTS. Handling devices with multiple
118 * variants that are all supported by a single U-Boot image will require implementing device-
119 * specific detection.
120 */
121static void configure_env(void)
122{
123 const char *first_compat, *last_compat;
124 char *tmp;
125 char buf[32] = { 0 };
126 /*
127 * Most DTB filenames follow the scheme: qcom/<soc>-[vendor]-<board>.dtb
128 * The vendor is skipped when it's a Qualcomm reference board, or the
129 * db845c.
130 */
131 char dt_path[64] = { 0 };
132 int compat_count, ret;
133 ofnode root;
134
135 root = ofnode_root();
136 /* This is almost always 2, but be explicit that we want the first and last compatibles
137 * not the first and second.
138 */
139 compat_count = ofnode_read_string_count(root, "compatible");
140 if (compat_count < 2) {
141 log_warning("%s: only one root compatible bailing!\n", __func__);
142 return;
143 }
144
145 /* The most specific device compatible (e.g. "thundercomm,db845c") */
146 ret = ofnode_read_string_index(root, "compatible", 0, &first_compat);
147 if (ret < 0) {
148 log_warning("Can't read first compatible\n");
149 return;
150 }
151
152 /* The last compatible is always the SoC compatible */
153 ret = ofnode_read_string_index(root, "compatible", compat_count - 1, &last_compat);
154 if (ret < 0) {
155 log_warning("Can't read second compatible\n");
156 return;
157 }
158
159 /* Copy the second compat (e.g. "qcom,sdm845") into buf */
160 strlcpy(buf, last_compat, sizeof(buf) - 1);
161 tmp = buf;
162
163 /* strsep() is destructive, it replaces the comma with a \0 */
164 if (!strsep(&tmp, ",")) {
165 log_warning("second compatible '%s' has no ','\n", buf);
166 return;
167 }
168
169 /* tmp now points to just the "sdm845" part of the string */
170 env_set("soc", tmp);
171
172 /* Now figure out the "board" part from the first compatible */
173 memset(buf, 0, sizeof(buf));
174 strlcpy(buf, first_compat, sizeof(buf) - 1);
175 tmp = buf;
176
177 /* The Qualcomm reference boards (RBx, HDK, etc) */
178 if (!strncmp("qcom", buf, strlen("qcom"))) {
179 /*
180 * They all have the first compatible as "qcom,<soc>-<board>"
181 * (e.g. "qcom,qrb5165-rb5"). We extract just the part after
182 * the dash.
183 */
184 if (!strsep(&tmp, "-")) {
185 log_warning("compatible '%s' has no '-'\n", buf);
186 return;
187 }
188 /* tmp is now "rb5" */
189 env_set("board", tmp);
190 } else {
191 if (!strsep(&tmp, ",")) {
192 log_warning("compatible '%s' has no ','\n", buf);
193 return;
194 }
195 /* for thundercomm we just want the bit after the comma (e.g. "db845c"),
196 * for all other boards we replace the comma with a '-' and take both
197 * (e.g. "oneplus-enchilada")
198 */
199 if (!strncmp("thundercomm", buf, strlen("thundercomm"))) {
200 env_set("board", tmp);
201 } else {
202 *(tmp - 1) = '-';
203 env_set("board", buf);
204 }
205 }
206
207 /* Now build the full path name */
208 snprintf(dt_path, sizeof(dt_path), "qcom/%s-%s.dtb",
209 env_get("soc"), env_get("board"));
210 env_set("fdtfile", dt_path);
211}
212
Caleb Connolly16da8c72023-10-03 11:35:40 +0100213void __weak qcom_late_init(void)
214{
215}
216
217#define KERNEL_COMP_SIZE SZ_64M
218#define SZ_96M (SZ_64M + SZ_32M)
Caleb Connollydcca2dd2023-12-04 15:00:37 +0000219#ifdef CONFIG_FASTBOOT_BUF_SIZE
220#define FASTBOOT_BUF_SIZE CONFIG_FASTBOOT_BUF_SIZE
221#else
222#define FASTBOOT_BUF_SIZE 0
223#endif
Caleb Connolly16da8c72023-10-03 11:35:40 +0100224
225#define addr_alloc(lmb, size) lmb_alloc(lmb, size, SZ_2M)
226
227/* Stolen from arch/arm/mach-apple/board.c */
228int board_late_init(void)
229{
230 struct lmb lmb;
231 u32 status = 0;
232
233 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
234
235 /* We need to be fairly conservative here as we support boards with just 1G of TOTAL RAM */
236 status |= env_set_hex("kernel_addr_r", addr_alloc(&lmb, SZ_128M));
237 status |= env_set_hex("ramdisk_addr_r", addr_alloc(&lmb, SZ_96M));
238 status |= env_set_hex("kernel_comp_addr_r", addr_alloc(&lmb, KERNEL_COMP_SIZE));
239 status |= env_set_hex("kernel_comp_size", KERNEL_COMP_SIZE);
Caleb Connollydcca2dd2023-12-04 15:00:37 +0000240 if (IS_ENABLED(CONFIG_FASTBOOT))
241 status |= env_set_hex("fastboot_addr_r", addr_alloc(&lmb, FASTBOOT_BUF_SIZE));
Caleb Connolly16da8c72023-10-03 11:35:40 +0100242 status |= env_set_hex("scriptaddr", addr_alloc(&lmb, SZ_4M));
243 status |= env_set_hex("pxefile_addr_r", addr_alloc(&lmb, SZ_4M));
244 status |= env_set_hex("fdt_addr_r", addr_alloc(&lmb, SZ_2M));
245
246 if (status)
247 log_warning("%s: Failed to set run time variables\n", __func__);
248
Caleb Connolly28a30542023-10-17 13:03:32 +0100249 configure_env();
Caleb Connolly16da8c72023-10-03 11:35:40 +0100250 qcom_late_init();
251
Caleb Connollyb55c0f82023-12-07 00:13:07 +0000252 /* Configure the dfu_string for capsule updates */
253 qcom_configure_capsule_updates();
254
Caleb Connolly16da8c72023-10-03 11:35:40 +0100255 return 0;
256}
257
Caleb Connollye6c284b2023-11-20 20:48:00 +0000258static 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
300u64 get_page_table_size(void)
301{
302 return SZ_64K;
303}
304
305void enable_caches(void)
306{
307 build_mem_map();
308
309 icache_enable();
310 dcache_enable();
311}