xref: /rk3399_ARM-atf/plat/allwinner/common/sunxi_prepare_dtb.c (revision e2b18771fc2a0528dda18dbdaac08dd8530df25a)
16fa8e72eSAndre Przywara /*
26fa8e72eSAndre Przywara  * Copyright (c) 2021, ARM Limited. All rights reserved.
36fa8e72eSAndre Przywara  *
46fa8e72eSAndre Przywara  * SPDX-License-Identifier: BSD-3-Clause
56fa8e72eSAndre Przywara  */
66fa8e72eSAndre Przywara 
76fa8e72eSAndre Przywara #include <libfdt.h>
86fa8e72eSAndre Przywara 
96fa8e72eSAndre Przywara #include <common/debug.h>
106fa8e72eSAndre Przywara #include <common/fdt_fixup.h>
116fa8e72eSAndre Przywara #include <common/fdt_wrappers.h>
126fa8e72eSAndre Przywara 
136fa8e72eSAndre Przywara #include <sunxi_private.h>
146fa8e72eSAndre Przywara 
156fa8e72eSAndre Przywara void sunxi_prepare_dtb(void *fdt)
166fa8e72eSAndre Przywara {
176fa8e72eSAndre Przywara 	int ret;
186fa8e72eSAndre Przywara 
196fa8e72eSAndre Przywara 	if (fdt == NULL || fdt_check_header(fdt) != 0) {
206fa8e72eSAndre Przywara 		return;
216fa8e72eSAndre Przywara 	}
226fa8e72eSAndre Przywara 
236fa8e72eSAndre Przywara 	ret = fdt_open_into(fdt, fdt, 0x10000);
246fa8e72eSAndre Przywara 	if (ret < 0) {
256fa8e72eSAndre Przywara 		ERROR("Preparing devicetree at %p: error %d\n", fdt, ret);
266fa8e72eSAndre Przywara 		return;
276fa8e72eSAndre Przywara 	}
286fa8e72eSAndre Przywara 
296fa8e72eSAndre Przywara #ifdef SUNXI_BL31_IN_DRAM
306fa8e72eSAndre Przywara 	/* Reserve memory used by Trusted Firmware. */
316fa8e72eSAndre Przywara 	if (fdt_add_reserved_memory(fdt, "tf-a@40000000", BL31_BASE,
326fa8e72eSAndre Przywara 				    BL31_LIMIT - BL31_BASE)) {
336fa8e72eSAndre Przywara 		WARN("Failed to add reserved memory nodes to DT.\n");
346fa8e72eSAndre Przywara 	}
356fa8e72eSAndre Przywara #endif
366fa8e72eSAndre Przywara 
37*e2b18771SSamuel Holland 	if (sunxi_psci_is_scpi()) {
38*e2b18771SSamuel Holland 		ret = fdt_add_cpu_idle_states(fdt, sunxi_idle_states);
39*e2b18771SSamuel Holland 		if (ret < 0) {
40*e2b18771SSamuel Holland 			WARN("Failed to add idle states to DT: %d\n", ret);
41*e2b18771SSamuel Holland 		}
42*e2b18771SSamuel Holland 	}
43*e2b18771SSamuel Holland 
446fa8e72eSAndre Przywara 	ret = fdt_pack(fdt);
456fa8e72eSAndre Przywara 	if (ret < 0) {
466fa8e72eSAndre Przywara 		ERROR("Failed to pack devicetree at %p: error %d\n",
476fa8e72eSAndre Przywara 		      fdt, ret);
4879808f10SSamuel Holland 	}
4979808f10SSamuel Holland 
506fa8e72eSAndre Przywara 	clean_dcache_range((uintptr_t)fdt, fdt_blob_size(fdt));
516fa8e72eSAndre Przywara 	INFO("Changed devicetree.\n");
526fa8e72eSAndre Przywara }
53