blob: 160ccca76e02d2b93524d9c3e3cf77d435c17bf3 [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
Bin Meng7a36bd82020-06-25 18:16:07 -070044 /*
45 * Extend the FDT by the following estimated size:
46 *
47 * Each PMP memory region entry occupies 64 bytes.
48 * With 16 PMP memory regions we need 64 * 16 = 1024 bytes.
49 */
50 err = fdt_open_into(dst, dst, fdt_totalsize(dst) + 1024);
51 if (err < 0) {
52 printf("Device Tree can't be expanded to accommodate new node");
53 return err;
54 }
55
Atish Patra111b8042020-04-21 11:15:01 -070056 fdt_for_each_subnode(node, src, offset) {
57 name = fdt_get_name(src, node, NULL);
58
59 addr = fdtdec_get_addr_size_auto_noparent(src, node,
60 "reg", 0, &size,
61 false);
62 if (addr == FDT_ADDR_T_NONE) {
63 debug("failed to read address/size for %s\n", name);
64 continue;
65 }
66 strncpy(basename, name, max_len);
67 temp = strchr(basename, '@');
68 if (temp) {
69 bname_len = strnlen(basename, max_len) - strnlen(temp,
70 max_len);
71 *(basename + bname_len) = '\0';
72 }
73 pmp_mem.start = addr;
74 pmp_mem.end = addr + size - 1;
75 err = fdtdec_add_reserved_memory(dst, basename, &pmp_mem,
76 &phandle);
77 if (err < 0) {
78 printf("failed to add reserved memory: %d\n", err);
79 return err;
80 }
81 if (!fdt_getprop(src, node, "no-map", NULL))
82 nomap = false;
83 if (nomap) {
84 rmem_offset = fdt_node_offset_by_phandle(dst, phandle);
85 fdt_setprop_empty(dst, rmem_offset, "no-map");
86 }
87 }
88
89 return 0;
90}
91
92/**
93 * riscv_board_reserved_mem_fixup() - Fix up reserved memory node for a board
94 * @fdt: Pointer to the device tree in which reserved memory node needs to be
95 * added.
96 *
Bin Meng77073f42020-06-25 18:16:06 -070097 * In RISC-V, any board needs to copy the reserved memory node from the device
98 * tree provided by the firmware to the device tree used by U-Boot. This is a
99 * common function that individual board fixup functions can invoke.
Atish Patra111b8042020-04-21 11:15:01 -0700100 *
101 * Return: 0 on success or error otherwise.
102 */
103int riscv_board_reserved_mem_fixup(void *fdt)
104{
105 int err;
106 void *src_fdt_addr;
107
108 src_fdt_addr = map_sysmem(gd->arch.firmware_fdt_addr, 0);
Bin Meng77073f42020-06-25 18:16:06 -0700109
110 /* avoid the copy if we are using the same device tree */
111 if (src_fdt_addr == fdt)
112 return 0;
113
Atish Patra111b8042020-04-21 11:15:01 -0700114 err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt);
115 if (err < 0)
116 return err;
117
118 return 0;
119}
Atish Patra73791922020-04-21 11:15:02 -0700120
121#ifdef CONFIG_OF_BOARD_FIXUP
122int board_fix_fdt(void *fdt)
123{
124 int err;
125
126 err = riscv_board_reserved_mem_fixup(fdt);
127 if (err < 0) {
128 printf("failed to fixup DT for reserved memory: %d\n", err);
129 return err;
130 }
131
132 return 0;
133}
134#endif
Atish Patra82ed8ef2020-04-21 11:15:04 -0700135
136int arch_fixup_fdt(void *blob)
137{
138 int err;
139#ifdef CONFIG_EFI_LOADER
140 u32 size;
141 int chosen_offset;
142
143 size = fdt_totalsize(blob);
144 err = fdt_open_into(blob, blob, size + 32);
145 if (err < 0) {
146 printf("Device Tree can't be expanded to accommodate new node");
147 return err;
148 }
149 chosen_offset = fdt_path_offset(blob, "/chosen");
150 if (chosen_offset < 0) {
151 err = fdt_add_subnode(blob, 0, "chosen");
152 if (err < 0) {
153 printf("chosen node can not be added\n");
154 return err;
155 }
156 }
157 /* Overwrite the boot-hartid as U-Boot is the last stage BL */
158 fdt_setprop_u32(blob, chosen_offset, "boot-hartid", gd->arch.boot_hart);
159#endif
160
161 /* Copy the reserved-memory node to the DT used by OS */
162 err = riscv_fdt_copy_resv_mem_node(gd->fdt_blob, blob);
163 if (err < 0)
164 return err;
165
166 return 0;
167}