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