blob: 6420e6ec448091ebe62d679539e68776a88c9373 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass0fbf66b2017-05-18 20:09:02 -06002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass0fbf66b2017-05-18 20:09:02 -06005 */
6
7#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +09009#include <linux/libfdt.h>
Simon Glass0fbf66b2017-05-18 20:09:02 -060010#include <dm/of_access.h>
11#include <dm/of_extra.h>
12#include <dm/ofnode.h>
13
Simon Glass4b580932018-06-11 13:07:17 -060014int ofnode_read_fmap_entry(ofnode node, struct fmap_entry *entry)
Simon Glass0fbf66b2017-05-18 20:09:02 -060015{
16 const char *prop;
Simon Glass0fbf66b2017-05-18 20:09:02 -060017
Simon Glass699c9ca2018-10-01 12:22:08 -060018 if (ofnode_read_u32(node, "image-pos", &entry->offset)) {
19 debug("Node '%s' has bad/missing 'image-pos' property\n",
Simon Glass4b580932018-06-11 13:07:17 -060020 ofnode_get_name(node));
Simon Glass699c9ca2018-10-01 12:22:08 -060021 return log_ret(-ENOENT);
Simon Glass0fbf66b2017-05-18 20:09:02 -060022 }
Simon Glass699c9ca2018-10-01 12:22:08 -060023 if (ofnode_read_u32(node, "size", &entry->length)) {
24 debug("Node '%s' has bad/missing 'size' property\n",
25 ofnode_get_name(node));
26 return log_ret(-ENOENT);
27 }
Simon Glass0fbf66b2017-05-18 20:09:02 -060028 entry->used = ofnode_read_s32_default(node, "used", entry->length);
29 prop = ofnode_read_string(node, "compress");
Simon Glass699c9ca2018-10-01 12:22:08 -060030 if (prop) {
31 if (!strcmp(prop, "lz4"))
32 entry->compress_algo = FMAP_COMPRESS_LZ4;
33 else
34 return log_msg_ret("Unknown compression algo",
35 -EINVAL);
36 } else {
37 entry->compress_algo = FMAP_COMPRESS_NONE;
38 }
39 entry->unc_length = ofnode_read_s32_default(node, "uncomp-size",
40 entry->length);
Simon Glass0fbf66b2017-05-18 20:09:02 -060041 prop = ofnode_read_string(node, "hash");
42 if (prop)
43 entry->hash_size = strlen(prop);
44 entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
45 entry->hash = (uint8_t *)prop;
46
47 return 0;
48}
Simon Glass2e4d8722018-06-11 13:07:18 -060049
50int ofnode_decode_region(ofnode node, const char *prop_name, fdt_addr_t *basep,
51 fdt_size_t *sizep)
52{
53 const fdt_addr_t *cell;
54 int len;
55
56 debug("%s: %s: %s\n", __func__, ofnode_get_name(node), prop_name);
57 cell = ofnode_get_property(node, prop_name, &len);
58 if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
59 debug("cell=%p, len=%d\n", cell, len);
60 return -1;
61 }
62
63 *basep = fdt_addr_to_cpu(*cell);
64 *sizep = fdt_size_to_cpu(cell[1]);
65 debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
66 (ulong)*sizep);
67
68 return 0;
69}
70
71int ofnode_decode_memory_region(ofnode config_node, const char *mem_type,
72 const char *suffix, fdt_addr_t *basep,
73 fdt_size_t *sizep)
74{
75 char prop_name[50];
76 const char *mem;
77 fdt_size_t size, offset_size;
78 fdt_addr_t base, offset;
79 ofnode node;
80
81 if (!ofnode_valid(config_node)) {
82 config_node = ofnode_path("/config");
83 if (!ofnode_valid(config_node)) {
84 debug("%s: Cannot find /config node\n", __func__);
85 return -ENOENT;
86 }
87 }
88 if (!suffix)
89 suffix = "";
90
91 snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
92 suffix);
93 mem = ofnode_read_string(config_node, prop_name);
94 if (!mem) {
95 debug("%s: No memory type for '%s', using /memory\n", __func__,
96 prop_name);
97 mem = "/memory";
98 }
99
100 node = ofnode_path(mem);
101 if (!ofnode_valid(node)) {
102 debug("%s: Failed to find node '%s'\n", __func__, mem);
103 return -ENOENT;
104 }
105
106 /*
107 * Not strictly correct - the memory may have multiple banks. We just
108 * use the first
109 */
110 if (ofnode_decode_region(node, "reg", &base, &size)) {
111 debug("%s: Failed to decode memory region %s\n", __func__,
112 mem);
113 return -EINVAL;
114 }
115
116 snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
117 suffix);
118 if (ofnode_decode_region(config_node, prop_name, &offset,
119 &offset_size)) {
120 debug("%s: Failed to decode memory region '%s'\n", __func__,
121 prop_name);
122 return -EINVAL;
123 }
124
125 *basep = base + offset;
126 *sizep = offset_size;
127
128 return 0;
129}