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