1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
8 #include <linux/libfdt.h>
9 #include <dm/of_access.h>
10 #include <dm/of_extra.h>
11 #include <dm/ofnode.h>
13 int ofnode_read_fmap_entry(ofnode node, struct fmap_entry *entry)
18 if (ofnode_read_u32_array(node, "reg", reg, 2)) {
19 debug("Node '%s' has bad/missing 'reg' property\n",
20 ofnode_get_name(node));
21 return -log_ret(ENOENT);
23 entry->offset = reg[0];
24 entry->length = reg[1];
25 entry->used = ofnode_read_s32_default(node, "used", entry->length);
26 prop = ofnode_read_string(node, "compress");
27 entry->compress_algo = prop && !strcmp(prop, "lzo") ?
28 FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
29 prop = ofnode_read_string(node, "hash");
31 entry->hash_size = strlen(prop);
32 entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
33 entry->hash = (uint8_t *)prop;
38 int ofnode_decode_region(ofnode node, const char *prop_name, fdt_addr_t *basep,
41 const fdt_addr_t *cell;
44 debug("%s: %s: %s\n", __func__, ofnode_get_name(node), prop_name);
45 cell = ofnode_get_property(node, prop_name, &len);
46 if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
47 debug("cell=%p, len=%d\n", cell, len);
51 *basep = fdt_addr_to_cpu(*cell);
52 *sizep = fdt_size_to_cpu(cell[1]);
53 debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
59 int ofnode_decode_memory_region(ofnode config_node, const char *mem_type,
60 const char *suffix, fdt_addr_t *basep,
65 fdt_size_t size, offset_size;
66 fdt_addr_t base, offset;
69 if (!ofnode_valid(config_node)) {
70 config_node = ofnode_path("/config");
71 if (!ofnode_valid(config_node)) {
72 debug("%s: Cannot find /config node\n", __func__);
79 snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
81 mem = ofnode_read_string(config_node, prop_name);
83 debug("%s: No memory type for '%s', using /memory\n", __func__,
88 node = ofnode_path(mem);
89 if (!ofnode_valid(node)) {
90 debug("%s: Failed to find node '%s'\n", __func__, mem);
95 * Not strictly correct - the memory may have multiple banks. We just
98 if (ofnode_decode_region(node, "reg", &base, &size)) {
99 debug("%s: Failed to decode memory region %s\n", __func__,
104 snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
106 if (ofnode_decode_region(config_node, prop_name, &offset,
108 debug("%s: Failed to decode memory region '%s'\n", __func__,
113 *basep = base + offset;
114 *sizep = offset_size;