blob: a3ebe9e9c2455895b67bc5f8db995d2f15dd4738 [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
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +09008#include <linux/libfdt.h>
Simon Glass0fbf66b2017-05-18 20:09:02 -06009#include <dm/of_access.h>
10#include <dm/of_extra.h>
11#include <dm/ofnode.h>
12
Simon Glass4b580932018-06-11 13:07:17 -060013int ofnode_read_fmap_entry(ofnode node, struct fmap_entry *entry)
Simon Glass0fbf66b2017-05-18 20:09:02 -060014{
15 const char *prop;
Simon Glassbf0a6922021-01-21 13:57:14 -070016 ofnode subnode;
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 Glassbf0a6922021-01-21 13:57:14 -070021 return log_msg_ret("image-pos", -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));
Simon Glassbf0a6922021-01-21 13:57:14 -070026 return log_msg_ret("size", -ENOENT);
Simon Glass699c9ca2018-10-01 12:22:08 -060027 }
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;
Simon Glass49775172021-07-05 16:32:40 -060033 else if (!strcmp(prop, "lzma"))
34 entry->compress_algo = FMAP_COMPRESS_LZMA;
Simon Glass699c9ca2018-10-01 12:22:08 -060035 else
Simon Glassbf0a6922021-01-21 13:57:14 -070036 return log_msg_ret("compression algo", -EINVAL);
Simon Glass699c9ca2018-10-01 12:22:08 -060037 } else {
38 entry->compress_algo = FMAP_COMPRESS_NONE;
39 }
40 entry->unc_length = ofnode_read_s32_default(node, "uncomp-size",
41 entry->length);
Simon Glassbf0a6922021-01-21 13:57:14 -070042 subnode = ofnode_find_subnode(node, "hash");
43 if (ofnode_valid(subnode)) {
44 prop = ofnode_read_prop(subnode, "value", &entry->hash_size);
45
46 /* Assume it is sha256 */
47 entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
48 entry->hash = (uint8_t *)prop;
49 }
Simon Glass0fbf66b2017-05-18 20:09:02 -060050
51 return 0;
52}
Simon Glass2e4d8722018-06-11 13:07:18 -060053
54int ofnode_decode_region(ofnode node, const char *prop_name, fdt_addr_t *basep,
55 fdt_size_t *sizep)
56{
57 const fdt_addr_t *cell;
58 int len;
59
60 debug("%s: %s: %s\n", __func__, ofnode_get_name(node), prop_name);
61 cell = ofnode_get_property(node, prop_name, &len);
62 if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
63 debug("cell=%p, len=%d\n", cell, len);
64 return -1;
65 }
66
67 *basep = fdt_addr_to_cpu(*cell);
68 *sizep = fdt_size_to_cpu(cell[1]);
69 debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
70 (ulong)*sizep);
71
72 return 0;
73}
74
75int ofnode_decode_memory_region(ofnode config_node, const char *mem_type,
76 const char *suffix, fdt_addr_t *basep,
77 fdt_size_t *sizep)
78{
79 char prop_name[50];
80 const char *mem;
81 fdt_size_t size, offset_size;
82 fdt_addr_t base, offset;
83 ofnode node;
84
85 if (!ofnode_valid(config_node)) {
86 config_node = ofnode_path("/config");
87 if (!ofnode_valid(config_node)) {
88 debug("%s: Cannot find /config node\n", __func__);
89 return -ENOENT;
90 }
91 }
92 if (!suffix)
93 suffix = "";
94
95 snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
96 suffix);
97 mem = ofnode_read_string(config_node, prop_name);
98 if (!mem) {
99 debug("%s: No memory type for '%s', using /memory\n", __func__,
100 prop_name);
101 mem = "/memory";
102 }
103
104 node = ofnode_path(mem);
105 if (!ofnode_valid(node)) {
106 debug("%s: Failed to find node '%s'\n", __func__, mem);
107 return -ENOENT;
108 }
109
110 /*
111 * Not strictly correct - the memory may have multiple banks. We just
112 * use the first
113 */
114 if (ofnode_decode_region(node, "reg", &base, &size)) {
115 debug("%s: Failed to decode memory region %s\n", __func__,
116 mem);
117 return -EINVAL;
118 }
119
120 snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
121 suffix);
122 if (ofnode_decode_region(config_node, prop_name, &offset,
123 &offset_size)) {
124 debug("%s: Failed to decode memory region '%s'\n", __func__,
125 prop_name);
126 return -EINVAL;
127 }
128
129 *basep = base + offset;
130 *sizep = offset_size;
131
132 return 0;
133}
Bin Meng72deec02021-03-14 20:14:46 +0800134
135bool ofnode_phy_is_fixed_link(ofnode eth_node, ofnode *phy_node)
136{
137 ofnode node, subnode;
138 int len;
139
140 subnode = ofnode_find_subnode(eth_node, "fixed-link");
141 if (ofnode_valid(subnode)) {
142 /* new binding */
143 node = subnode;
144 } else if (ofnode_get_property(eth_node, "fixed-link", &len) &&
145 len == (5 * sizeof(__be32))) {
146 /* old binding */
147 node = eth_node;
148 } else {
149 return false;
150 }
151
152 if (phy_node)
153 *phy_node = node;
154
155 return true;
156}
Vladimir Olteanc0c35a02021-09-29 18:04:39 +0300157
158bool ofnode_eth_uses_inband_aneg(ofnode eth_node)
159{
160 bool inband_aneg = false;
161 const char *managed;
162
163 managed = ofnode_read_string(eth_node, "managed");
164 if (managed && !strcmp(managed, "in-band-status"))
165 inband_aneg = true;
166
167 return inband_aneg;
168}