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