Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <fdtdec.h> |
| 10 | #include <fdt_support.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 13 | #include <linux/libfdt.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 14 | #include <dm/of_access.h> |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 15 | #include <dm/of_addr.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 16 | #include <dm/ofnode.h> |
| 17 | #include <linux/err.h> |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 18 | #include <linux/ioport.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 19 | #include <asm/global_data.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 20 | |
| 21 | int ofnode_read_u32(ofnode node, const char *propname, u32 *outp) |
| 22 | { |
Dario Binacchi | b3f1cdd | 2020-03-29 18:04:42 +0200 | [diff] [blame] | 23 | return ofnode_read_u32_index(node, propname, 0, outp); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 24 | } |
| 25 | |
Trent Piepho | 5b77541 | 2019-05-10 17:48:20 +0000 | [diff] [blame] | 26 | u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 27 | { |
| 28 | assert(ofnode_valid(node)); |
Dario Binacchi | b3f1cdd | 2020-03-29 18:04:42 +0200 | [diff] [blame] | 29 | ofnode_read_u32_index(node, propname, 0, &def); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 30 | |
| 31 | return def; |
| 32 | } |
| 33 | |
Dario Binacchi | 81d80b5 | 2020-03-29 18:04:41 +0200 | [diff] [blame] | 34 | int ofnode_read_u32_index(ofnode node, const char *propname, int index, |
| 35 | u32 *outp) |
| 36 | { |
| 37 | const fdt32_t *cell; |
| 38 | int len; |
| 39 | |
| 40 | assert(ofnode_valid(node)); |
| 41 | debug("%s: %s: ", __func__, propname); |
| 42 | |
| 43 | if (ofnode_is_np(node)) |
| 44 | return of_read_u32_index(ofnode_to_np(node), propname, index, |
| 45 | outp); |
| 46 | |
| 47 | cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname, |
| 48 | &len); |
| 49 | if (!cell) { |
| 50 | debug("(not found)\n"); |
| 51 | return -EINVAL; |
| 52 | } |
| 53 | |
| 54 | if (len < (sizeof(int) * (index + 1))) { |
| 55 | debug("(not large enough)\n"); |
| 56 | return -EOVERFLOW; |
| 57 | } |
| 58 | |
| 59 | *outp = fdt32_to_cpu(cell[index]); |
| 60 | debug("%#x (%d)\n", *outp, *outp); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index, |
| 66 | u32 def) |
| 67 | { |
| 68 | assert(ofnode_valid(node)); |
| 69 | ofnode_read_u32_index(node, propname, index, &def); |
| 70 | |
| 71 | return def; |
| 72 | } |
| 73 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 74 | int ofnode_read_s32_default(ofnode node, const char *propname, s32 def) |
| 75 | { |
| 76 | assert(ofnode_valid(node)); |
| 77 | ofnode_read_u32(node, propname, (u32 *)&def); |
| 78 | |
| 79 | return def; |
| 80 | } |
| 81 | |
Simon Glass | 9d54a7a | 2018-06-11 13:07:10 -0600 | [diff] [blame] | 82 | int ofnode_read_u64(ofnode node, const char *propname, u64 *outp) |
| 83 | { |
Jean-Jacques Hiblot | 487f917 | 2019-10-22 10:05:22 +0200 | [diff] [blame] | 84 | const unaligned_fdt64_t *cell; |
Simon Glass | 9d54a7a | 2018-06-11 13:07:10 -0600 | [diff] [blame] | 85 | int len; |
| 86 | |
| 87 | assert(ofnode_valid(node)); |
| 88 | debug("%s: %s: ", __func__, propname); |
| 89 | |
| 90 | if (ofnode_is_np(node)) |
| 91 | return of_read_u64(ofnode_to_np(node), propname, outp); |
| 92 | |
| 93 | cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname, |
| 94 | &len); |
| 95 | if (!cell || len < sizeof(*cell)) { |
| 96 | debug("(not found)\n"); |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | *outp = fdt64_to_cpu(cell[0]); |
| 100 | debug("%#llx (%lld)\n", (unsigned long long)*outp, |
| 101 | (unsigned long long)*outp); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
T Karthik Reddy | 478860d | 2019-09-02 16:34:30 +0200 | [diff] [blame] | 106 | u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def) |
Simon Glass | 9d54a7a | 2018-06-11 13:07:10 -0600 | [diff] [blame] | 107 | { |
| 108 | assert(ofnode_valid(node)); |
| 109 | ofnode_read_u64(node, propname, &def); |
| 110 | |
| 111 | return def; |
| 112 | } |
| 113 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 114 | bool ofnode_read_bool(ofnode node, const char *propname) |
| 115 | { |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 116 | const void *prop; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 117 | |
| 118 | assert(ofnode_valid(node)); |
| 119 | debug("%s: %s: ", __func__, propname); |
| 120 | |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 121 | prop = ofnode_get_property(node, propname, NULL); |
| 122 | |
| 123 | debug("%s\n", prop ? "true" : "false"); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 124 | |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 125 | return prop ? true : false; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 126 | } |
| 127 | |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 128 | const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 129 | { |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 130 | const char *val = NULL; |
| 131 | int len; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 132 | |
| 133 | assert(ofnode_valid(node)); |
| 134 | debug("%s: %s: ", __func__, propname); |
| 135 | |
| 136 | if (ofnode_is_np(node)) { |
| 137 | struct property *prop = of_find_property( |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 138 | ofnode_to_np(node), propname, &len); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 139 | |
| 140 | if (prop) { |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 141 | val = prop->value; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 142 | len = prop->length; |
| 143 | } |
| 144 | } else { |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 145 | val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 146 | propname, &len); |
| 147 | } |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 148 | if (!val) { |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 149 | debug("<not found>\n"); |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 150 | if (sizep) |
| 151 | *sizep = -FDT_ERR_NOTFOUND; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 152 | return NULL; |
| 153 | } |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 154 | if (sizep) |
| 155 | *sizep = len; |
| 156 | |
| 157 | return val; |
| 158 | } |
| 159 | |
| 160 | const char *ofnode_read_string(ofnode node, const char *propname) |
| 161 | { |
| 162 | const char *str; |
| 163 | int len; |
| 164 | |
| 165 | str = ofnode_read_prop(node, propname, &len); |
| 166 | if (!str) |
| 167 | return NULL; |
| 168 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 169 | if (strnlen(str, len) >= len) { |
| 170 | debug("<invalid>\n"); |
| 171 | return NULL; |
| 172 | } |
| 173 | debug("%s\n", str); |
| 174 | |
| 175 | return str; |
| 176 | } |
| 177 | |
Simon Glass | 81c54b3 | 2020-01-27 08:49:45 -0700 | [diff] [blame] | 178 | int ofnode_read_size(ofnode node, const char *propname) |
| 179 | { |
| 180 | int len; |
| 181 | |
| 182 | if (!ofnode_read_prop(node, propname, &len)) |
| 183 | return -EINVAL; |
| 184 | |
| 185 | return len; |
| 186 | } |
| 187 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 188 | ofnode ofnode_find_subnode(ofnode node, const char *subnode_name) |
| 189 | { |
| 190 | ofnode subnode; |
| 191 | |
| 192 | assert(ofnode_valid(node)); |
| 193 | debug("%s: %s: ", __func__, subnode_name); |
| 194 | |
| 195 | if (ofnode_is_np(node)) { |
| 196 | const struct device_node *np = ofnode_to_np(node); |
| 197 | |
| 198 | for (np = np->child; np; np = np->sibling) { |
| 199 | if (!strcmp(subnode_name, np->name)) |
| 200 | break; |
| 201 | } |
| 202 | subnode = np_to_ofnode(np); |
| 203 | } else { |
| 204 | int ooffset = fdt_subnode_offset(gd->fdt_blob, |
| 205 | ofnode_to_offset(node), subnode_name); |
| 206 | subnode = offset_to_ofnode(ooffset); |
| 207 | } |
| 208 | debug("%s\n", ofnode_valid(subnode) ? |
| 209 | ofnode_get_name(subnode) : "<none>"); |
| 210 | |
| 211 | return subnode; |
| 212 | } |
| 213 | |
| 214 | int ofnode_read_u32_array(ofnode node, const char *propname, |
| 215 | u32 *out_values, size_t sz) |
| 216 | { |
| 217 | assert(ofnode_valid(node)); |
| 218 | debug("%s: %s: ", __func__, propname); |
| 219 | |
| 220 | if (ofnode_is_np(node)) { |
| 221 | return of_read_u32_array(ofnode_to_np(node), propname, |
| 222 | out_values, sz); |
| 223 | } else { |
| 224 | return fdtdec_get_int_array(gd->fdt_blob, |
| 225 | ofnode_to_offset(node), propname, |
| 226 | out_values, sz); |
| 227 | } |
| 228 | } |
| 229 | |
Simon Glass | 39f1d28 | 2020-12-16 17:25:06 -0700 | [diff] [blame] | 230 | #if !CONFIG_IS_ENABLED(DM_INLINE_OFNODE) |
Simon Glass | 5de5b3b | 2020-11-28 17:50:02 -0700 | [diff] [blame] | 231 | bool ofnode_is_enabled(ofnode node) |
| 232 | { |
| 233 | if (ofnode_is_np(node)) { |
| 234 | return of_device_is_available(ofnode_to_np(node)); |
| 235 | } else { |
| 236 | return fdtdec_get_is_enabled(gd->fdt_blob, |
| 237 | ofnode_to_offset(node)); |
| 238 | } |
| 239 | } |
| 240 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 241 | ofnode ofnode_first_subnode(ofnode node) |
| 242 | { |
| 243 | assert(ofnode_valid(node)); |
| 244 | if (ofnode_is_np(node)) |
| 245 | return np_to_ofnode(node.np->child); |
| 246 | |
| 247 | return offset_to_ofnode( |
| 248 | fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node))); |
| 249 | } |
| 250 | |
| 251 | ofnode ofnode_next_subnode(ofnode node) |
| 252 | { |
| 253 | assert(ofnode_valid(node)); |
| 254 | if (ofnode_is_np(node)) |
| 255 | return np_to_ofnode(node.np->sibling); |
| 256 | |
| 257 | return offset_to_ofnode( |
| 258 | fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node))); |
| 259 | } |
Simon Glass | 39f1d28 | 2020-12-16 17:25:06 -0700 | [diff] [blame] | 260 | #endif /* !DM_INLINE_OFNODE */ |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 261 | |
Philipp Tomsich | 6fce1dd | 2018-02-23 17:38:49 +0100 | [diff] [blame] | 262 | ofnode ofnode_get_parent(ofnode node) |
| 263 | { |
| 264 | ofnode parent; |
| 265 | |
| 266 | assert(ofnode_valid(node)); |
| 267 | if (ofnode_is_np(node)) |
| 268 | parent = np_to_ofnode(of_get_parent(ofnode_to_np(node))); |
| 269 | else |
| 270 | parent.of_offset = fdt_parent_offset(gd->fdt_blob, |
| 271 | ofnode_to_offset(node)); |
| 272 | |
| 273 | return parent; |
| 274 | } |
| 275 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 276 | const char *ofnode_get_name(ofnode node) |
| 277 | { |
Kever Yang | 8d7976d | 2019-07-19 11:23:47 +0800 | [diff] [blame] | 278 | if (!ofnode_valid(node)) { |
| 279 | debug("%s node not valid\n", __func__); |
| 280 | return NULL; |
| 281 | } |
| 282 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 283 | if (ofnode_is_np(node)) |
| 284 | return strrchr(node.np->full_name, '/') + 1; |
| 285 | |
| 286 | return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL); |
| 287 | } |
| 288 | |
Marek Behún | e897e3c | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 289 | int ofnode_get_path(ofnode node, char *buf, int buflen) |
| 290 | { |
| 291 | assert(ofnode_valid(node)); |
| 292 | |
| 293 | if (ofnode_is_np(node)) { |
| 294 | if (strlen(node.np->full_name) >= buflen) |
| 295 | return -ENOSPC; |
| 296 | |
| 297 | strcpy(buf, node.np->full_name); |
| 298 | |
| 299 | return 0; |
| 300 | } else { |
| 301 | int res; |
| 302 | |
| 303 | res = fdt_get_path(gd->fdt_blob, ofnode_to_offset(node), buf, |
| 304 | buflen); |
| 305 | if (!res) |
| 306 | return res; |
| 307 | else if (res == -FDT_ERR_NOSPACE) |
| 308 | return -ENOSPC; |
| 309 | else |
| 310 | return -EINVAL; |
| 311 | } |
| 312 | } |
| 313 | |
Kever Yang | 37df0e0 | 2018-02-23 17:38:50 +0100 | [diff] [blame] | 314 | ofnode ofnode_get_by_phandle(uint phandle) |
| 315 | { |
| 316 | ofnode node; |
| 317 | |
| 318 | if (of_live_active()) |
| 319 | node = np_to_ofnode(of_find_node_by_phandle(phandle)); |
| 320 | else |
| 321 | node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob, |
| 322 | phandle); |
| 323 | |
| 324 | return node; |
| 325 | } |
| 326 | |
Marek Behún | 177ab7f | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 327 | static fdt_addr_t __ofnode_get_addr_size_index(ofnode node, int index, |
| 328 | fdt_size_t *size, bool translate) |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 329 | { |
Keerthy | 34222a3 | 2018-11-19 11:44:47 +0530 | [diff] [blame] | 330 | int na, ns; |
Keerthy | 34222a3 | 2018-11-19 11:44:47 +0530 | [diff] [blame] | 331 | |
Chen Guanqiao | 2d1034b | 2021-07-12 15:40:20 +0800 | [diff] [blame] | 332 | if (size) |
| 333 | *size = FDT_SIZE_T_NONE; |
Chen Guanqiao | 223f17d | 2021-04-12 14:51:11 +0800 | [diff] [blame] | 334 | |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 335 | if (ofnode_is_np(node)) { |
| 336 | const __be32 *prop_val; |
Simon Glass | 47f85de | 2019-09-25 08:55:50 -0600 | [diff] [blame] | 337 | u64 size64; |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 338 | uint flags; |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 339 | |
Simon Glass | 47f85de | 2019-09-25 08:55:50 -0600 | [diff] [blame] | 340 | prop_val = of_get_address(ofnode_to_np(node), index, &size64, |
| 341 | &flags); |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 342 | if (!prop_val) |
| 343 | return FDT_ADDR_T_NONE; |
Chen Guanqiao | 2d1034b | 2021-07-12 15:40:20 +0800 | [diff] [blame] | 344 | |
Simon Glass | 47f85de | 2019-09-25 08:55:50 -0600 | [diff] [blame] | 345 | if (size) |
| 346 | *size = size64; |
Mario Six | d007ebc | 2017-12-20 09:52:12 +0100 | [diff] [blame] | 347 | |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 348 | ns = of_n_size_cells(ofnode_to_np(node)); |
| 349 | |
Marek Behún | 177ab7f | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 350 | if (translate && IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) { |
Mario Six | d007ebc | 2017-12-20 09:52:12 +0100 | [diff] [blame] | 351 | return of_translate_address(ofnode_to_np(node), prop_val); |
| 352 | } else { |
| 353 | na = of_n_addr_cells(ofnode_to_np(node)); |
| 354 | return of_read_number(prop_val, na); |
| 355 | } |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 356 | } else { |
Keerthy | 34222a3 | 2018-11-19 11:44:47 +0530 | [diff] [blame] | 357 | na = ofnode_read_simple_addr_cells(ofnode_get_parent(node)); |
| 358 | ns = ofnode_read_simple_size_cells(ofnode_get_parent(node)); |
| 359 | return fdtdec_get_addr_size_fixed(gd->fdt_blob, |
| 360 | ofnode_to_offset(node), "reg", |
Marek Behún | 177ab7f | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 361 | index, na, ns, size, |
| 362 | translate); |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 363 | } |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 364 | } |
| 365 | |
Marek Behún | 177ab7f | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 366 | fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size) |
| 367 | { |
| 368 | return __ofnode_get_addr_size_index(node, index, size, true); |
| 369 | } |
| 370 | |
| 371 | fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index, |
| 372 | fdt_size_t *size) |
| 373 | { |
| 374 | return __ofnode_get_addr_size_index(node, index, size, false); |
| 375 | } |
| 376 | |
Keerthy | d332e6e | 2019-04-24 17:19:53 +0530 | [diff] [blame] | 377 | fdt_addr_t ofnode_get_addr_index(ofnode node, int index) |
| 378 | { |
| 379 | fdt_size_t size; |
| 380 | |
| 381 | return ofnode_get_addr_size_index(node, index, &size); |
| 382 | } |
| 383 | |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 384 | fdt_addr_t ofnode_get_addr(ofnode node) |
| 385 | { |
| 386 | return ofnode_get_addr_index(node, 0); |
| 387 | } |
| 388 | |
Chen Guanqiao | 223f17d | 2021-04-12 14:51:11 +0800 | [diff] [blame] | 389 | fdt_size_t ofnode_get_size(ofnode node) |
| 390 | { |
| 391 | fdt_size_t size; |
| 392 | |
| 393 | ofnode_get_addr_size_index(node, 0, &size); |
| 394 | |
| 395 | return size; |
| 396 | } |
| 397 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 398 | int ofnode_stringlist_search(ofnode node, const char *property, |
| 399 | const char *string) |
| 400 | { |
| 401 | if (ofnode_is_np(node)) { |
| 402 | return of_property_match_string(ofnode_to_np(node), |
| 403 | property, string); |
| 404 | } else { |
| 405 | int ret; |
| 406 | |
| 407 | ret = fdt_stringlist_search(gd->fdt_blob, |
| 408 | ofnode_to_offset(node), property, |
| 409 | string); |
| 410 | if (ret == -FDT_ERR_NOTFOUND) |
| 411 | return -ENODATA; |
| 412 | else if (ret < 0) |
| 413 | return -EINVAL; |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | int ofnode_read_string_index(ofnode node, const char *property, int index, |
| 420 | const char **outp) |
| 421 | { |
| 422 | if (ofnode_is_np(node)) { |
| 423 | return of_property_read_string_index(ofnode_to_np(node), |
| 424 | property, index, outp); |
| 425 | } else { |
| 426 | int len; |
| 427 | |
| 428 | *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node), |
| 429 | property, index, &len); |
| 430 | if (len < 0) |
| 431 | return -EINVAL; |
| 432 | return 0; |
| 433 | } |
| 434 | } |
| 435 | |
Simon Glass | 5fdb005 | 2017-06-12 06:21:28 -0600 | [diff] [blame] | 436 | int ofnode_read_string_count(ofnode node, const char *property) |
| 437 | { |
| 438 | if (ofnode_is_np(node)) { |
| 439 | return of_property_count_strings(ofnode_to_np(node), property); |
| 440 | } else { |
| 441 | return fdt_stringlist_count(gd->fdt_blob, |
| 442 | ofnode_to_offset(node), property); |
| 443 | } |
| 444 | } |
| 445 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 446 | static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in, |
| 447 | struct ofnode_phandle_args *out) |
| 448 | { |
| 449 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 450 | out->node = offset_to_ofnode(in->node); |
| 451 | out->args_count = in->args_count; |
| 452 | memcpy(out->args, in->args, sizeof(out->args)); |
| 453 | } |
| 454 | |
| 455 | static void ofnode_from_of_phandle_args(struct of_phandle_args *in, |
| 456 | struct ofnode_phandle_args *out) |
| 457 | { |
| 458 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 459 | out->node = np_to_ofnode(in->np); |
| 460 | out->args_count = in->args_count; |
| 461 | memcpy(out->args, in->args, sizeof(out->args)); |
| 462 | } |
| 463 | |
| 464 | int ofnode_parse_phandle_with_args(ofnode node, const char *list_name, |
| 465 | const char *cells_name, int cell_count, |
| 466 | int index, |
| 467 | struct ofnode_phandle_args *out_args) |
| 468 | { |
| 469 | if (ofnode_is_np(node)) { |
| 470 | struct of_phandle_args args; |
| 471 | int ret; |
| 472 | |
| 473 | ret = of_parse_phandle_with_args(ofnode_to_np(node), |
Patrick Delaunay | 21e3b04 | 2020-09-10 18:26:17 +0200 | [diff] [blame] | 474 | list_name, cells_name, |
| 475 | cell_count, index, |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 476 | &args); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 477 | if (ret) |
| 478 | return ret; |
| 479 | ofnode_from_of_phandle_args(&args, out_args); |
| 480 | } else { |
| 481 | struct fdtdec_phandle_args args; |
| 482 | int ret; |
| 483 | |
| 484 | ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 485 | ofnode_to_offset(node), |
| 486 | list_name, cells_name, |
| 487 | cell_count, index, &args); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 488 | if (ret) |
| 489 | return ret; |
| 490 | ofnode_from_fdtdec_phandle_args(&args, out_args); |
| 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 496 | int ofnode_count_phandle_with_args(ofnode node, const char *list_name, |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 497 | const char *cells_name, int cell_count) |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 498 | { |
| 499 | if (ofnode_is_np(node)) |
| 500 | return of_count_phandle_with_args(ofnode_to_np(node), |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 501 | list_name, cells_name, cell_count); |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 502 | else |
| 503 | return fdtdec_parse_phandle_with_args(gd->fdt_blob, |
| 504 | ofnode_to_offset(node), list_name, cells_name, |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 505 | cell_count, -1, NULL); |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 506 | } |
| 507 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 508 | ofnode ofnode_path(const char *path) |
| 509 | { |
| 510 | if (of_live_active()) |
| 511 | return np_to_ofnode(of_find_node_by_path(path)); |
| 512 | else |
| 513 | return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path)); |
| 514 | } |
| 515 | |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 516 | const void *ofnode_read_chosen_prop(const char *propname, int *sizep) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 517 | { |
| 518 | ofnode chosen_node; |
| 519 | |
| 520 | chosen_node = ofnode_path("/chosen"); |
| 521 | |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 522 | return ofnode_read_prop(chosen_node, propname, sizep); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 523 | } |
| 524 | |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 525 | const char *ofnode_read_chosen_string(const char *propname) |
| 526 | { |
| 527 | return ofnode_read_chosen_prop(propname, NULL); |
| 528 | } |
| 529 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 530 | ofnode ofnode_get_chosen_node(const char *name) |
| 531 | { |
| 532 | const char *prop; |
| 533 | |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 534 | prop = ofnode_read_chosen_prop(name, NULL); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 535 | if (!prop) |
| 536 | return ofnode_null(); |
| 537 | |
| 538 | return ofnode_path(prop); |
| 539 | } |
| 540 | |
Michal Simek | 92a8862 | 2020-07-28 12:51:08 +0200 | [diff] [blame] | 541 | const void *ofnode_read_aliases_prop(const char *propname, int *sizep) |
| 542 | { |
| 543 | ofnode node; |
| 544 | |
| 545 | node = ofnode_path("/aliases"); |
| 546 | |
| 547 | return ofnode_read_prop(node, propname, sizep); |
| 548 | } |
| 549 | |
| 550 | ofnode ofnode_get_aliases_node(const char *name) |
| 551 | { |
| 552 | const char *prop; |
| 553 | |
| 554 | prop = ofnode_read_aliases_prop(name, NULL); |
| 555 | if (!prop) |
| 556 | return ofnode_null(); |
| 557 | |
| 558 | debug("%s: node_path: %s\n", __func__, prop); |
| 559 | |
| 560 | return ofnode_path(prop); |
| 561 | } |
| 562 | |
developer | d93c8b4 | 2020-05-02 11:35:09 +0200 | [diff] [blame] | 563 | int ofnode_get_child_count(ofnode parent) |
| 564 | { |
| 565 | ofnode child; |
| 566 | int num = 0; |
| 567 | |
| 568 | ofnode_for_each_subnode(child, parent) |
| 569 | num++; |
| 570 | |
| 571 | return num; |
| 572 | } |
| 573 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 574 | static int decode_timing_property(ofnode node, const char *name, |
| 575 | struct timing_entry *result) |
| 576 | { |
| 577 | int length, ret = 0; |
| 578 | |
| 579 | length = ofnode_read_size(node, name); |
| 580 | if (length < 0) { |
| 581 | debug("%s: could not find property %s\n", |
| 582 | ofnode_get_name(node), name); |
| 583 | return length; |
| 584 | } |
| 585 | |
| 586 | if (length == sizeof(u32)) { |
| 587 | result->typ = ofnode_read_u32_default(node, name, 0); |
| 588 | result->min = result->typ; |
| 589 | result->max = result->typ; |
| 590 | } else { |
| 591 | ret = ofnode_read_u32_array(node, name, &result->min, 3); |
| 592 | } |
| 593 | |
| 594 | return ret; |
| 595 | } |
| 596 | |
| 597 | int ofnode_decode_display_timing(ofnode parent, int index, |
| 598 | struct display_timing *dt) |
| 599 | { |
| 600 | int i; |
| 601 | ofnode timings, node; |
| 602 | u32 val = 0; |
| 603 | int ret = 0; |
| 604 | |
| 605 | timings = ofnode_find_subnode(parent, "display-timings"); |
| 606 | if (!ofnode_valid(timings)) |
| 607 | return -EINVAL; |
| 608 | |
Simon Glass | 2852976 | 2017-08-05 15:45:54 -0600 | [diff] [blame] | 609 | i = 0; |
| 610 | ofnode_for_each_subnode(node, timings) { |
| 611 | if (i++ == index) |
| 612 | break; |
| 613 | } |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 614 | |
| 615 | if (!ofnode_valid(node)) |
| 616 | return -EINVAL; |
| 617 | |
| 618 | memset(dt, 0, sizeof(*dt)); |
| 619 | |
| 620 | ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch); |
| 621 | ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch); |
| 622 | ret |= decode_timing_property(node, "hactive", &dt->hactive); |
| 623 | ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len); |
| 624 | ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch); |
| 625 | ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch); |
| 626 | ret |= decode_timing_property(node, "vactive", &dt->vactive); |
| 627 | ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len); |
| 628 | ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock); |
| 629 | |
| 630 | dt->flags = 0; |
| 631 | val = ofnode_read_u32_default(node, "vsync-active", -1); |
| 632 | if (val != -1) { |
| 633 | dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH : |
| 634 | DISPLAY_FLAGS_VSYNC_LOW; |
| 635 | } |
| 636 | val = ofnode_read_u32_default(node, "hsync-active", -1); |
| 637 | if (val != -1) { |
| 638 | dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH : |
| 639 | DISPLAY_FLAGS_HSYNC_LOW; |
| 640 | } |
| 641 | val = ofnode_read_u32_default(node, "de-active", -1); |
| 642 | if (val != -1) { |
| 643 | dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH : |
| 644 | DISPLAY_FLAGS_DE_LOW; |
| 645 | } |
| 646 | val = ofnode_read_u32_default(node, "pixelclk-active", -1); |
| 647 | if (val != -1) { |
| 648 | dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE : |
| 649 | DISPLAY_FLAGS_PIXDATA_NEGEDGE; |
| 650 | } |
| 651 | |
| 652 | if (ofnode_read_bool(node, "interlaced")) |
| 653 | dt->flags |= DISPLAY_FLAGS_INTERLACED; |
| 654 | if (ofnode_read_bool(node, "doublescan")) |
| 655 | dt->flags |= DISPLAY_FLAGS_DOUBLESCAN; |
| 656 | if (ofnode_read_bool(node, "doubleclk")) |
| 657 | dt->flags |= DISPLAY_FLAGS_DOUBLECLK; |
| 658 | |
| 659 | return ret; |
| 660 | } |
| 661 | |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 662 | const void *ofnode_get_property(ofnode node, const char *propname, int *lenp) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 663 | { |
Masahiro Yamada | 5052f1b | 2017-06-22 16:54:04 +0900 | [diff] [blame] | 664 | if (ofnode_is_np(node)) |
| 665 | return of_get_property(ofnode_to_np(node), propname, lenp); |
| 666 | else |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 667 | return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), |
| 668 | propname, lenp); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 669 | } |
| 670 | |
Patrick Delaunay | caee155 | 2020-01-13 11:34:56 +0100 | [diff] [blame] | 671 | int ofnode_get_first_property(ofnode node, struct ofprop *prop) |
| 672 | { |
| 673 | prop->node = node; |
| 674 | |
| 675 | if (ofnode_is_np(node)) { |
| 676 | prop->prop = of_get_first_property(ofnode_to_np(prop->node)); |
| 677 | if (!prop->prop) |
| 678 | return -FDT_ERR_NOTFOUND; |
| 679 | } else { |
| 680 | prop->offset = |
| 681 | fdt_first_property_offset(gd->fdt_blob, |
| 682 | ofnode_to_offset(prop->node)); |
| 683 | if (prop->offset < 0) |
| 684 | return prop->offset; |
| 685 | } |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | int ofnode_get_next_property(struct ofprop *prop) |
| 691 | { |
| 692 | if (ofnode_is_np(prop->node)) { |
| 693 | prop->prop = of_get_next_property(ofnode_to_np(prop->node), |
| 694 | prop->prop); |
| 695 | if (!prop->prop) |
| 696 | return -FDT_ERR_NOTFOUND; |
| 697 | } else { |
| 698 | prop->offset = fdt_next_property_offset(gd->fdt_blob, |
| 699 | prop->offset); |
| 700 | if (prop->offset < 0) |
| 701 | return prop->offset; |
| 702 | } |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | const void *ofnode_get_property_by_prop(const struct ofprop *prop, |
| 708 | const char **propname, int *lenp) |
| 709 | { |
| 710 | if (ofnode_is_np(prop->node)) |
| 711 | return of_get_property_by_prop(ofnode_to_np(prop->node), |
| 712 | prop->prop, propname, lenp); |
| 713 | else |
| 714 | return fdt_getprop_by_offset(gd->fdt_blob, |
| 715 | prop->offset, |
| 716 | propname, lenp); |
| 717 | } |
| 718 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 719 | bool ofnode_is_available(ofnode node) |
| 720 | { |
| 721 | if (ofnode_is_np(node)) |
| 722 | return of_device_is_available(ofnode_to_np(node)); |
| 723 | else |
| 724 | return fdtdec_get_is_enabled(gd->fdt_blob, |
| 725 | ofnode_to_offset(node)); |
| 726 | } |
| 727 | |
| 728 | fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property, |
| 729 | fdt_size_t *sizep) |
| 730 | { |
| 731 | if (ofnode_is_np(node)) { |
| 732 | int na, ns; |
| 733 | int psize; |
| 734 | const struct device_node *np = ofnode_to_np(node); |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame] | 735 | const __be32 *prop = of_get_property(np, property, &psize); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 736 | |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame] | 737 | if (!prop) |
| 738 | return FDT_ADDR_T_NONE; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 739 | na = of_n_addr_cells(np); |
Marek Vasut | 1638c17 | 2018-10-01 12:37:19 +0200 | [diff] [blame] | 740 | ns = of_n_size_cells(np); |
Simon Glass | a67cc63 | 2017-05-18 20:09:27 -0600 | [diff] [blame] | 741 | *sizep = of_read_number(prop + na, ns); |
Marek Vasut | a9dac49 | 2018-10-01 12:37:20 +0200 | [diff] [blame] | 742 | |
Dario Binacchi | f8fc703 | 2021-05-01 17:05:26 +0200 | [diff] [blame] | 743 | if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0) |
Marek Vasut | a9dac49 | 2018-10-01 12:37:20 +0200 | [diff] [blame] | 744 | return of_translate_address(np, prop); |
| 745 | else |
| 746 | return of_read_number(prop, na); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 747 | } else { |
| 748 | return fdtdec_get_addr_size(gd->fdt_blob, |
| 749 | ofnode_to_offset(node), property, |
| 750 | sizep); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname, |
| 755 | size_t sz) |
| 756 | { |
| 757 | if (ofnode_is_np(node)) { |
| 758 | const struct device_node *np = ofnode_to_np(node); |
| 759 | int psize; |
| 760 | const __be32 *prop = of_get_property(np, propname, &psize); |
| 761 | |
| 762 | if (!prop || sz != psize) |
| 763 | return NULL; |
| 764 | return (uint8_t *)prop; |
| 765 | |
| 766 | } else { |
| 767 | return fdtdec_locate_byte_array(gd->fdt_blob, |
| 768 | ofnode_to_offset(node), propname, sz); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type, |
| 773 | const char *propname, struct fdt_pci_addr *addr) |
| 774 | { |
Masahiro Yamada | 5c5991e | 2017-06-22 17:57:50 +0900 | [diff] [blame] | 775 | const fdt32_t *cell; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 776 | int len; |
| 777 | int ret = -ENOENT; |
| 778 | |
| 779 | debug("%s: %s: ", __func__, propname); |
| 780 | |
| 781 | /* |
| 782 | * If we follow the pci bus bindings strictly, we should check |
| 783 | * the value of the node's parent node's #address-cells and |
| 784 | * #size-cells. They need to be 3 and 2 accordingly. However, |
| 785 | * for simplicity we skip the check here. |
| 786 | */ |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 787 | cell = ofnode_get_property(node, propname, &len); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 788 | if (!cell) |
| 789 | goto fail; |
| 790 | |
| 791 | if ((len % FDT_PCI_REG_SIZE) == 0) { |
| 792 | int num = len / FDT_PCI_REG_SIZE; |
| 793 | int i; |
| 794 | |
| 795 | for (i = 0; i < num; i++) { |
| 796 | debug("pci address #%d: %08lx %08lx %08lx\n", i, |
| 797 | (ulong)fdt32_to_cpu(cell[0]), |
| 798 | (ulong)fdt32_to_cpu(cell[1]), |
| 799 | (ulong)fdt32_to_cpu(cell[2])); |
| 800 | if ((fdt32_to_cpu(*cell) & type) == type) { |
| 801 | addr->phys_hi = fdt32_to_cpu(cell[0]); |
| 802 | addr->phys_mid = fdt32_to_cpu(cell[1]); |
Simon Glass | dfd4315 | 2019-09-25 08:55:46 -0600 | [diff] [blame] | 803 | addr->phys_lo = fdt32_to_cpu(cell[2]); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 804 | break; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 805 | } |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 806 | |
| 807 | cell += (FDT_PCI_ADDR_CELLS + |
| 808 | FDT_PCI_SIZE_CELLS); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | if (i == num) { |
| 812 | ret = -ENXIO; |
| 813 | goto fail; |
| 814 | } |
| 815 | |
| 816 | return 0; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 817 | } |
| 818 | |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 819 | ret = -EINVAL; |
| 820 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 821 | fail: |
| 822 | debug("(not found)\n"); |
| 823 | return ret; |
| 824 | } |
| 825 | |
Bin Meng | fa15771 | 2018-08-03 01:14:35 -0700 | [diff] [blame] | 826 | int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device) |
| 827 | { |
| 828 | const char *list, *end; |
| 829 | int len; |
| 830 | |
| 831 | list = ofnode_get_property(node, "compatible", &len); |
| 832 | if (!list) |
| 833 | return -ENOENT; |
| 834 | |
| 835 | end = list + len; |
| 836 | while (list < end) { |
| 837 | len = strlen(list); |
| 838 | if (len >= strlen("pciVVVV,DDDD")) { |
| 839 | char *s = strstr(list, "pci"); |
| 840 | |
| 841 | /* |
| 842 | * check if the string is something like pciVVVV,DDDD.RR |
| 843 | * or just pciVVVV,DDDD |
| 844 | */ |
| 845 | if (s && s[7] == ',' && |
| 846 | (s[12] == '.' || s[12] == 0)) { |
| 847 | s += 3; |
| 848 | *vendor = simple_strtol(s, NULL, 16); |
| 849 | |
| 850 | s += 5; |
| 851 | *device = simple_strtol(s, NULL, 16); |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | } |
| 856 | list += (len + 1); |
| 857 | } |
| 858 | |
| 859 | return -ENOENT; |
| 860 | } |
| 861 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 862 | int ofnode_read_addr_cells(ofnode node) |
| 863 | { |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 864 | if (ofnode_is_np(node)) { |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 865 | return of_n_addr_cells(ofnode_to_np(node)); |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 866 | } else { |
| 867 | int parent = fdt_parent_offset(gd->fdt_blob, |
| 868 | ofnode_to_offset(node)); |
| 869 | |
| 870 | return fdt_address_cells(gd->fdt_blob, parent); |
| 871 | } |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | int ofnode_read_size_cells(ofnode node) |
| 875 | { |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 876 | if (ofnode_is_np(node)) { |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 877 | return of_n_size_cells(ofnode_to_np(node)); |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 878 | } else { |
| 879 | int parent = fdt_parent_offset(gd->fdt_blob, |
| 880 | ofnode_to_offset(node)); |
| 881 | |
| 882 | return fdt_size_cells(gd->fdt_blob, parent); |
| 883 | } |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | int ofnode_read_simple_addr_cells(ofnode node) |
| 887 | { |
| 888 | if (ofnode_is_np(node)) |
| 889 | return of_simple_addr_cells(ofnode_to_np(node)); |
| 890 | else |
| 891 | return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 892 | } |
| 893 | |
| 894 | int ofnode_read_simple_size_cells(ofnode node) |
| 895 | { |
| 896 | if (ofnode_is_np(node)) |
| 897 | return of_simple_size_cells(ofnode_to_np(node)); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 898 | else |
| 899 | return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); |
| 900 | } |
| 901 | |
| 902 | bool ofnode_pre_reloc(ofnode node) |
| 903 | { |
Patrick Delaunay | 0b025b8 | 2019-02-11 12:49:57 +0100 | [diff] [blame] | 904 | #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD) |
| 905 | /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass |
| 906 | * had property dm-pre-reloc or u-boot,dm-spl/tpl. |
| 907 | * They are removed in final dtb (fdtgrep 2nd pass) |
| 908 | */ |
| 909 | return true; |
| 910 | #else |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 911 | if (ofnode_read_bool(node, "u-boot,dm-pre-reloc")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 912 | return true; |
Simon Glass | 23f2284 | 2018-10-01 12:22:18 -0600 | [diff] [blame] | 913 | if (ofnode_read_bool(node, "u-boot,dm-pre-proper")) |
| 914 | return true; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 915 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 916 | /* |
| 917 | * In regular builds individual spl and tpl handling both |
| 918 | * count as handled pre-relocation for later second init. |
| 919 | */ |
Masahiro Yamada | 6a61dd9 | 2017-06-22 16:54:03 +0900 | [diff] [blame] | 920 | if (ofnode_read_bool(node, "u-boot,dm-spl") || |
| 921 | ofnode_read_bool(node, "u-boot,dm-tpl")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 922 | return true; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 923 | |
| 924 | return false; |
Patrick Delaunay | 0b025b8 | 2019-02-11 12:49:57 +0100 | [diff] [blame] | 925 | #endif |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 926 | } |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 927 | |
| 928 | int ofnode_read_resource(ofnode node, uint index, struct resource *res) |
| 929 | { |
| 930 | if (ofnode_is_np(node)) { |
| 931 | return of_address_to_resource(ofnode_to_np(node), index, res); |
| 932 | } else { |
| 933 | struct fdt_resource fres; |
| 934 | int ret; |
| 935 | |
| 936 | ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node), |
| 937 | "reg", index, &fres); |
| 938 | if (ret < 0) |
| 939 | return -EINVAL; |
| 940 | memset(res, '\0', sizeof(*res)); |
| 941 | res->start = fres.start; |
| 942 | res->end = fres.end; |
| 943 | |
| 944 | return 0; |
| 945 | } |
| 946 | } |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 947 | |
| 948 | int ofnode_read_resource_byname(ofnode node, const char *name, |
| 949 | struct resource *res) |
| 950 | { |
| 951 | int index; |
| 952 | |
| 953 | index = ofnode_stringlist_search(node, "reg-names", name); |
| 954 | if (index < 0) |
| 955 | return index; |
| 956 | |
| 957 | return ofnode_read_resource(node, index, res); |
| 958 | } |
Mario Six | aefac06 | 2018-01-15 11:07:19 +0100 | [diff] [blame] | 959 | |
| 960 | u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr) |
| 961 | { |
| 962 | if (ofnode_is_np(node)) |
| 963 | return of_translate_address(ofnode_to_np(node), in_addr); |
| 964 | else |
| 965 | return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr); |
| 966 | } |
Masahiro Yamada | 9349bcc | 2018-04-19 12:14:02 +0900 | [diff] [blame] | 967 | |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 968 | u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr) |
| 969 | { |
| 970 | if (ofnode_is_np(node)) |
| 971 | return of_translate_dma_address(ofnode_to_np(node), in_addr); |
| 972 | else |
| 973 | return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr); |
| 974 | } |
| 975 | |
Nicolas Saenz Julienne | 50d2fa4 | 2021-01-12 13:55:22 +0100 | [diff] [blame] | 976 | int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus, u64 *size) |
| 977 | { |
| 978 | if (ofnode_is_np(node)) |
| 979 | return of_get_dma_range(ofnode_to_np(node), cpu, bus, size); |
| 980 | else |
| 981 | return fdt_get_dma_range(gd->fdt_blob, ofnode_to_offset(node), |
| 982 | cpu, bus, size); |
| 983 | } |
| 984 | |
Masahiro Yamada | 9349bcc | 2018-04-19 12:14:02 +0900 | [diff] [blame] | 985 | int ofnode_device_is_compatible(ofnode node, const char *compat) |
| 986 | { |
| 987 | if (ofnode_is_np(node)) |
| 988 | return of_device_is_compatible(ofnode_to_np(node), compat, |
| 989 | NULL, NULL); |
| 990 | else |
| 991 | return !fdt_node_check_compatible(gd->fdt_blob, |
| 992 | ofnode_to_offset(node), |
| 993 | compat); |
| 994 | } |
Simon Glass | 954eeae | 2018-06-11 13:07:13 -0600 | [diff] [blame] | 995 | |
| 996 | ofnode ofnode_by_compatible(ofnode from, const char *compat) |
| 997 | { |
| 998 | if (of_live_active()) { |
| 999 | return np_to_ofnode(of_find_compatible_node( |
| 1000 | (struct device_node *)ofnode_to_np(from), NULL, |
| 1001 | compat)); |
| 1002 | } else { |
| 1003 | return offset_to_ofnode(fdt_node_offset_by_compatible( |
| 1004 | gd->fdt_blob, ofnode_to_offset(from), compat)); |
| 1005 | } |
| 1006 | } |
Jens Wiklander | 7b68dad | 2018-08-20 11:09:58 +0200 | [diff] [blame] | 1007 | |
| 1008 | ofnode ofnode_by_prop_value(ofnode from, const char *propname, |
| 1009 | const void *propval, int proplen) |
| 1010 | { |
| 1011 | if (of_live_active()) { |
| 1012 | return np_to_ofnode(of_find_node_by_prop_value( |
| 1013 | (struct device_node *)ofnode_to_np(from), propname, |
| 1014 | propval, proplen)); |
| 1015 | } else { |
| 1016 | return offset_to_ofnode(fdt_node_offset_by_prop_value( |
| 1017 | gd->fdt_blob, ofnode_to_offset(from), |
| 1018 | propname, propval, proplen)); |
| 1019 | } |
| 1020 | } |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1021 | |
| 1022 | int ofnode_write_prop(ofnode node, const char *propname, int len, |
| 1023 | const void *value) |
| 1024 | { |
| 1025 | const struct device_node *np = ofnode_to_np(node); |
| 1026 | struct property *pp; |
| 1027 | struct property *pp_last = NULL; |
| 1028 | struct property *new; |
| 1029 | |
| 1030 | if (!of_live_active()) |
| 1031 | return -ENOSYS; |
| 1032 | |
| 1033 | if (!np) |
| 1034 | return -EINVAL; |
| 1035 | |
| 1036 | for (pp = np->properties; pp; pp = pp->next) { |
| 1037 | if (strcmp(pp->name, propname) == 0) { |
| 1038 | /* Property exists -> change value */ |
| 1039 | pp->value = (void *)value; |
| 1040 | pp->length = len; |
| 1041 | return 0; |
| 1042 | } |
| 1043 | pp_last = pp; |
| 1044 | } |
| 1045 | |
| 1046 | if (!pp_last) |
| 1047 | return -ENOENT; |
| 1048 | |
| 1049 | /* Property does not exist -> append new property */ |
| 1050 | new = malloc(sizeof(struct property)); |
| 1051 | if (!new) |
| 1052 | return -ENOMEM; |
| 1053 | |
| 1054 | new->name = strdup(propname); |
Mario Six | 95e885d | 2018-10-04 09:22:24 +0200 | [diff] [blame] | 1055 | if (!new->name) { |
| 1056 | free(new); |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1057 | return -ENOMEM; |
Mario Six | 95e885d | 2018-10-04 09:22:24 +0200 | [diff] [blame] | 1058 | } |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1059 | |
| 1060 | new->value = (void *)value; |
| 1061 | new->length = len; |
| 1062 | new->next = NULL; |
| 1063 | |
| 1064 | pp_last->next = new; |
| 1065 | |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | int ofnode_write_string(ofnode node, const char *propname, const char *value) |
| 1070 | { |
| 1071 | if (!of_live_active()) |
| 1072 | return -ENOSYS; |
| 1073 | |
| 1074 | assert(ofnode_valid(node)); |
| 1075 | |
| 1076 | debug("%s: %s = %s", __func__, propname, value); |
| 1077 | |
| 1078 | return ofnode_write_prop(node, propname, strlen(value) + 1, value); |
| 1079 | } |
| 1080 | |
| 1081 | int ofnode_set_enabled(ofnode node, bool value) |
| 1082 | { |
| 1083 | if (!of_live_active()) |
| 1084 | return -ENOSYS; |
| 1085 | |
| 1086 | assert(ofnode_valid(node)); |
| 1087 | |
| 1088 | if (value) |
| 1089 | return ofnode_write_string(node, "status", "okay"); |
| 1090 | else |
Bin Meng | a9274aa | 2019-07-05 09:23:17 -0700 | [diff] [blame] | 1091 | return ofnode_write_string(node, "status", "disabled"); |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1092 | } |