blob: 6db48ad04a56f61deb01c91fb5d40fb83df16e10 [file] [log] [blame]
Atish Patrad4ea6492020-04-21 11:15:01 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2020 Western Digital Corporation or its affiliates
4 *
5 */
6
7#include <common.h>
8#include <fdt_support.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Atish Patrad4ea6492020-04-21 11:15:01 -070010#include <mapmem.h>
11
12DECLARE_GLOBAL_DATA_PTR;
13
14/**
15 * riscv_fdt_copy_resv_mem_node() - Copy reserve memory node entry
16 * @src: Pointer to the source device tree from which reserved memory node
17 * needs to be copied.
18 * @dst: Pointer to the destination device tree to which reserved memory node
19 * needs to be copied.
20 *
21 * Return: 0 on success or if source doesn't have reserved memory node.
22 * Error if copy process failed.
23 */
24int riscv_fdt_copy_resv_mem_node(const void *src, void *dst)
25{
26 u32 phandle;
27 struct fdt_memory pmp_mem;
28 fdt_addr_t addr;
29 fdt_size_t size;
30 int offset, node, err, rmem_offset;
31 bool nomap = true;
32 char basename[32] = {0};
33 int bname_len;
34 int max_len = sizeof(basename);
35 const char *name;
36 char *temp;
37
38 offset = fdt_path_offset(src, "/reserved-memory");
39 if (offset < 0) {
40 printf("No reserved memory region found in source FDT\n");
41 return 0;
42 }
43
44 fdt_for_each_subnode(node, src, offset) {
45 name = fdt_get_name(src, node, NULL);
46
47 addr = fdtdec_get_addr_size_auto_noparent(src, node,
48 "reg", 0, &size,
49 false);
50 if (addr == FDT_ADDR_T_NONE) {
51 debug("failed to read address/size for %s\n", name);
52 continue;
53 }
54 strncpy(basename, name, max_len);
55 temp = strchr(basename, '@');
56 if (temp) {
57 bname_len = strnlen(basename, max_len) - strnlen(temp,
58 max_len);
59 *(basename + bname_len) = '\0';
60 }
61 pmp_mem.start = addr;
62 pmp_mem.end = addr + size - 1;
63 err = fdtdec_add_reserved_memory(dst, basename, &pmp_mem,
64 &phandle);
65 if (err < 0) {
66 printf("failed to add reserved memory: %d\n", err);
67 return err;
68 }
69 if (!fdt_getprop(src, node, "no-map", NULL))
70 nomap = false;
71 if (nomap) {
72 rmem_offset = fdt_node_offset_by_phandle(dst, phandle);
73 fdt_setprop_empty(dst, rmem_offset, "no-map");
74 }
75 }
76
77 return 0;
78}
79
80/**
81 * riscv_board_reserved_mem_fixup() - Fix up reserved memory node for a board
82 * @fdt: Pointer to the device tree in which reserved memory node needs to be
83 * added.
84 *
85 * In RISC-V, any board compiled with OF_SEPARATE needs to copy the reserved
86 * memory node from the device tree provided by the firmware to the device tree
87 * used by U-Boot. This is a common function that individual board fixup
88 * functions can invoke.
89 *
90 * Return: 0 on success or error otherwise.
91 */
92int riscv_board_reserved_mem_fixup(void *fdt)
93{
94 int err;
95 void *src_fdt_addr;
96
97 src_fdt_addr = map_sysmem(gd->arch.firmware_fdt_addr, 0);
98 err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt);
99 if (err < 0)
100 return err;
101
102 return 0;
103}
Atish Patra0cb27852020-04-21 11:15:02 -0700104
105#ifdef CONFIG_OF_BOARD_FIXUP
106int board_fix_fdt(void *fdt)
107{
108 int err;
109
110 err = riscv_board_reserved_mem_fixup(fdt);
111 if (err < 0) {
112 printf("failed to fixup DT for reserved memory: %d\n", err);
113 return err;
114 }
115
116 return 0;
117}
118#endif
Atish Patra177c53f2020-04-21 11:15:04 -0700119
120int arch_fixup_fdt(void *blob)
121{
122 int err;
123#ifdef CONFIG_EFI_LOADER
124 u32 size;
125 int chosen_offset;
126
127 size = fdt_totalsize(blob);
128 err = fdt_open_into(blob, blob, size + 32);
129 if (err < 0) {
130 printf("Device Tree can't be expanded to accommodate new node");
131 return err;
132 }
133 chosen_offset = fdt_path_offset(blob, "/chosen");
134 if (chosen_offset < 0) {
135 err = fdt_add_subnode(blob, 0, "chosen");
136 if (err < 0) {
137 printf("chosen node can not be added\n");
138 return err;
139 }
140 }
141 /* Overwrite the boot-hartid as U-Boot is the last stage BL */
142 fdt_setprop_u32(blob, chosen_offset, "boot-hartid", gd->arch.boot_hart);
143#endif
144
145 /* Copy the reserved-memory node to the DT used by OS */
146 err = riscv_fdt_copy_resv_mem_node(gd->fdt_blob, blob);
147 if (err < 0)
148 return err;
149
150 return 0;
151}