blob: 5f523f0e07d5e2c6fecdead37b393bb0f92ae98f [file] [log] [blame]
Atish Patra111b8042020-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 Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Atish Patra111b8042020-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 *
Bin Meng77073f42020-06-25 18:16:06 -070085 * In RISC-V, any board needs to copy the reserved memory node from the device
86 * tree provided by the firmware to the device tree used by U-Boot. This is a
87 * common function that individual board fixup functions can invoke.
Atish Patra111b8042020-04-21 11:15:01 -070088 *
89 * Return: 0 on success or error otherwise.
90 */
91int riscv_board_reserved_mem_fixup(void *fdt)
92{
93 int err;
94 void *src_fdt_addr;
95
96 src_fdt_addr = map_sysmem(gd->arch.firmware_fdt_addr, 0);
Bin Meng77073f42020-06-25 18:16:06 -070097
98 /* avoid the copy if we are using the same device tree */
99 if (src_fdt_addr == fdt)
100 return 0;
101
Atish Patra111b8042020-04-21 11:15:01 -0700102 err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt);
103 if (err < 0)
104 return err;
105
106 return 0;
107}
Atish Patra73791922020-04-21 11:15:02 -0700108
109#ifdef CONFIG_OF_BOARD_FIXUP
110int board_fix_fdt(void *fdt)
111{
112 int err;
113
114 err = riscv_board_reserved_mem_fixup(fdt);
115 if (err < 0) {
116 printf("failed to fixup DT for reserved memory: %d\n", err);
117 return err;
118 }
119
120 return 0;
121}
122#endif
Atish Patra82ed8ef2020-04-21 11:15:04 -0700123
124int arch_fixup_fdt(void *blob)
125{
126 int err;
127#ifdef CONFIG_EFI_LOADER
128 u32 size;
129 int chosen_offset;
130
131 size = fdt_totalsize(blob);
132 err = fdt_open_into(blob, blob, size + 32);
133 if (err < 0) {
134 printf("Device Tree can't be expanded to accommodate new node");
135 return err;
136 }
137 chosen_offset = fdt_path_offset(blob, "/chosen");
138 if (chosen_offset < 0) {
139 err = fdt_add_subnode(blob, 0, "chosen");
140 if (err < 0) {
141 printf("chosen node can not be added\n");
142 return err;
143 }
144 }
145 /* Overwrite the boot-hartid as U-Boot is the last stage BL */
146 fdt_setprop_u32(blob, chosen_offset, "boot-hartid", gd->arch.boot_hart);
147#endif
148
149 /* Copy the reserved-memory node to the DT used by OS */
150 err = riscv_fdt_copy_resv_mem_node(gd->fdt_blob, blob);
151 if (err < 0)
152 return err;
153
154 return 0;
155}