blob: 1f3f23621c33ed00416c39abda58b97345adee3f [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
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +02007#define LOG_CATEGORY LOGC_ARCH
8
Atish Patrad4ea6492020-04-21 11:15:01 -07009#include <common.h>
10#include <fdt_support.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Atish Patrad4ea6492020-04-21 11:15:01 -070012#include <mapmem.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Atish Patrad4ea6492020-04-21 11:15:01 -070014
15DECLARE_GLOBAL_DATA_PTR;
16
17/**
18 * riscv_fdt_copy_resv_mem_node() - Copy reserve memory node entry
19 * @src: Pointer to the source device tree from which reserved memory node
20 * needs to be copied.
21 * @dst: Pointer to the destination device tree to which reserved memory node
22 * needs to be copied.
23 *
24 * Return: 0 on success or if source doesn't have reserved memory node.
25 * Error if copy process failed.
26 */
27int riscv_fdt_copy_resv_mem_node(const void *src, void *dst)
28{
29 u32 phandle;
30 struct fdt_memory pmp_mem;
31 fdt_addr_t addr;
32 fdt_size_t size;
33 int offset, node, err, rmem_offset;
34 bool nomap = true;
35 char basename[32] = {0};
36 int bname_len;
37 int max_len = sizeof(basename);
38 const char *name;
39 char *temp;
40
41 offset = fdt_path_offset(src, "/reserved-memory");
42 if (offset < 0) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +020043 log_debug("No reserved memory region found in source FDT\n");
Atish Patrad4ea6492020-04-21 11:15:01 -070044 return 0;
45 }
46
Bin Menga8492e22020-06-25 18:16:07 -070047 /*
48 * Extend the FDT by the following estimated size:
49 *
50 * Each PMP memory region entry occupies 64 bytes.
51 * With 16 PMP memory regions we need 64 * 16 = 1024 bytes.
52 */
53 err = fdt_open_into(dst, dst, fdt_totalsize(dst) + 1024);
54 if (err < 0) {
55 printf("Device Tree can't be expanded to accommodate new node");
56 return err;
57 }
58
Atish Patrad4ea6492020-04-21 11:15:01 -070059 fdt_for_each_subnode(node, src, offset) {
60 name = fdt_get_name(src, node, NULL);
61
Atish Patraedf4fc22020-06-24 14:56:15 -070062 addr = fdtdec_get_addr_size_auto_parent(src, offset, node,
63 "reg", 0, &size,
64 false);
Atish Patrad4ea6492020-04-21 11:15:01 -070065 if (addr == FDT_ADDR_T_NONE) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +020066 log_debug("failed to read address/size for %s\n", name);
Atish Patrad4ea6492020-04-21 11:15:01 -070067 continue;
68 }
69 strncpy(basename, name, max_len);
70 temp = strchr(basename, '@');
71 if (temp) {
72 bname_len = strnlen(basename, max_len) - strnlen(temp,
73 max_len);
74 *(basename + bname_len) = '\0';
75 }
76 pmp_mem.start = addr;
77 pmp_mem.end = addr + size - 1;
78 err = fdtdec_add_reserved_memory(dst, basename, &pmp_mem,
Etienne Carriereccaa5742020-09-10 10:49:59 +020079 &phandle, false);
Atish Patra7eb4bcc2020-06-24 14:56:14 -070080 if (err < 0 && err != -FDT_ERR_EXISTS) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +020081 log_err("failed to add reserved memory: %d\n", err);
Atish Patrad4ea6492020-04-21 11:15:01 -070082 return err;
83 }
84 if (!fdt_getprop(src, node, "no-map", NULL))
85 nomap = false;
86 if (nomap) {
87 rmem_offset = fdt_node_offset_by_phandle(dst, phandle);
88 fdt_setprop_empty(dst, rmem_offset, "no-map");
89 }
90 }
91
92 return 0;
93}
94
95/**
96 * riscv_board_reserved_mem_fixup() - Fix up reserved memory node for a board
97 * @fdt: Pointer to the device tree in which reserved memory node needs to be
98 * added.
99 *
Bin Mengc4f7c502020-06-25 18:16:06 -0700100 * In RISC-V, any board needs to copy the reserved memory node from the device
101 * tree provided by the firmware to the device tree used by U-Boot. This is a
102 * common function that individual board fixup functions can invoke.
Atish Patrad4ea6492020-04-21 11:15:01 -0700103 *
104 * Return: 0 on success or error otherwise.
105 */
106int riscv_board_reserved_mem_fixup(void *fdt)
107{
108 int err;
109 void *src_fdt_addr;
110
111 src_fdt_addr = map_sysmem(gd->arch.firmware_fdt_addr, 0);
Bin Mengc4f7c502020-06-25 18:16:06 -0700112
113 /* avoid the copy if we are using the same device tree */
114 if (src_fdt_addr == fdt)
115 return 0;
116
Atish Patrad4ea6492020-04-21 11:15:01 -0700117 err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt);
118 if (err < 0)
119 return err;
120
121 return 0;
122}
Atish Patra0cb27852020-04-21 11:15:02 -0700123
124#ifdef CONFIG_OF_BOARD_FIXUP
125int board_fix_fdt(void *fdt)
126{
127 int err;
128
129 err = riscv_board_reserved_mem_fixup(fdt);
130 if (err < 0) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +0200131 log_err("failed to fixup DT for reserved memory: %d\n", err);
Atish Patra0cb27852020-04-21 11:15:02 -0700132 return err;
133 }
134
135 return 0;
136}
137#endif
Atish Patra177c53f2020-04-21 11:15:04 -0700138
139int arch_fixup_fdt(void *blob)
140{
141 int err;
142#ifdef CONFIG_EFI_LOADER
143 u32 size;
144 int chosen_offset;
145
146 size = fdt_totalsize(blob);
147 err = fdt_open_into(blob, blob, size + 32);
148 if (err < 0) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +0200149 log_err("Device Tree can't be expanded to accommodate new node");
Atish Patra177c53f2020-04-21 11:15:04 -0700150 return err;
151 }
152 chosen_offset = fdt_path_offset(blob, "/chosen");
153 if (chosen_offset < 0) {
154 err = fdt_add_subnode(blob, 0, "chosen");
155 if (err < 0) {
Heinrich Schuchardtc5a44422020-06-30 11:30:59 +0200156 log_err("chosen node cannot be added\n");
Atish Patra177c53f2020-04-21 11:15:04 -0700157 return err;
158 }
159 }
160 /* Overwrite the boot-hartid as U-Boot is the last stage BL */
161 fdt_setprop_u32(blob, chosen_offset, "boot-hartid", gd->arch.boot_hart);
162#endif
163
164 /* Copy the reserved-memory node to the DT used by OS */
165 err = riscv_fdt_copy_resv_mem_node(gd->fdt_blob, blob);
166 if (err < 0)
167 return err;
168
169 return 0;
170}