Atish Patra | d4ea649 | 2020-04-21 11:15:01 -0700 | [diff] [blame] | 1 | // 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> |
| 9 | #include <mapmem.h> |
| 10 | |
| 11 | DECLARE_GLOBAL_DATA_PTR; |
| 12 | |
| 13 | /** |
| 14 | * riscv_fdt_copy_resv_mem_node() - Copy reserve memory node entry |
| 15 | * @src: Pointer to the source device tree from which reserved memory node |
| 16 | * needs to be copied. |
| 17 | * @dst: Pointer to the destination device tree to which reserved memory node |
| 18 | * needs to be copied. |
| 19 | * |
| 20 | * Return: 0 on success or if source doesn't have reserved memory node. |
| 21 | * Error if copy process failed. |
| 22 | */ |
| 23 | int riscv_fdt_copy_resv_mem_node(const void *src, void *dst) |
| 24 | { |
| 25 | u32 phandle; |
| 26 | struct fdt_memory pmp_mem; |
| 27 | fdt_addr_t addr; |
| 28 | fdt_size_t size; |
| 29 | int offset, node, err, rmem_offset; |
| 30 | bool nomap = true; |
| 31 | char basename[32] = {0}; |
| 32 | int bname_len; |
| 33 | int max_len = sizeof(basename); |
| 34 | const char *name; |
| 35 | char *temp; |
| 36 | |
| 37 | offset = fdt_path_offset(src, "/reserved-memory"); |
| 38 | if (offset < 0) { |
| 39 | printf("No reserved memory region found in source FDT\n"); |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | fdt_for_each_subnode(node, src, offset) { |
| 44 | name = fdt_get_name(src, node, NULL); |
| 45 | |
| 46 | addr = fdtdec_get_addr_size_auto_noparent(src, node, |
| 47 | "reg", 0, &size, |
| 48 | false); |
| 49 | if (addr == FDT_ADDR_T_NONE) { |
| 50 | debug("failed to read address/size for %s\n", name); |
| 51 | continue; |
| 52 | } |
| 53 | strncpy(basename, name, max_len); |
| 54 | temp = strchr(basename, '@'); |
| 55 | if (temp) { |
| 56 | bname_len = strnlen(basename, max_len) - strnlen(temp, |
| 57 | max_len); |
| 58 | *(basename + bname_len) = '\0'; |
| 59 | } |
| 60 | pmp_mem.start = addr; |
| 61 | pmp_mem.end = addr + size - 1; |
| 62 | err = fdtdec_add_reserved_memory(dst, basename, &pmp_mem, |
| 63 | &phandle); |
| 64 | if (err < 0) { |
| 65 | printf("failed to add reserved memory: %d\n", err); |
| 66 | return err; |
| 67 | } |
| 68 | if (!fdt_getprop(src, node, "no-map", NULL)) |
| 69 | nomap = false; |
| 70 | if (nomap) { |
| 71 | rmem_offset = fdt_node_offset_by_phandle(dst, phandle); |
| 72 | fdt_setprop_empty(dst, rmem_offset, "no-map"); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * riscv_board_reserved_mem_fixup() - Fix up reserved memory node for a board |
| 81 | * @fdt: Pointer to the device tree in which reserved memory node needs to be |
| 82 | * added. |
| 83 | * |
| 84 | * In RISC-V, any board compiled with OF_SEPARATE needs to copy the reserved |
| 85 | * memory node from the device tree provided by the firmware to the device tree |
| 86 | * used by U-Boot. This is a common function that individual board fixup |
| 87 | * functions can invoke. |
| 88 | * |
| 89 | * Return: 0 on success or error otherwise. |
| 90 | */ |
| 91 | int 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); |
| 97 | err = riscv_fdt_copy_resv_mem_node(src_fdt_addr, fdt); |
| 98 | if (err < 0) |
| 99 | return err; |
| 100 | |
| 101 | return 0; |
| 102 | } |
Atish Patra | 0cb2785 | 2020-04-21 11:15:02 -0700 | [diff] [blame^] | 103 | |
| 104 | #ifdef CONFIG_OF_BOARD_FIXUP |
| 105 | int board_fix_fdt(void *fdt) |
| 106 | { |
| 107 | int err; |
| 108 | |
| 109 | err = riscv_board_reserved_mem_fixup(fdt); |
| 110 | if (err < 0) { |
| 111 | printf("failed to fixup DT for reserved memory: %d\n", err); |
| 112 | return err; |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | #endif |