2 * Copyright (c) 2017 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <fdt_support.h>
13 #include <dm/of_access.h>
14 #include <dm/of_addr.h>
15 #include <dm/ofnode.h>
16 #include <linux/err.h>
18 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
20 assert(ofnode_valid(node));
21 debug("%s: %s: ", __func__, propname);
23 if (ofnode_is_np(node)) {
24 return of_read_u32(ofnode_to_np(node), propname, outp);
29 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
31 if (!cell || len < sizeof(int)) {
32 debug("(not found)\n");
35 *outp = fdt32_to_cpu(cell[0]);
37 debug("%#x (%d)\n", *outp, *outp);
42 int ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
44 assert(ofnode_valid(node));
45 ofnode_read_u32(node, propname, &def);
50 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
52 assert(ofnode_valid(node));
53 ofnode_read_u32(node, propname, (u32 *)&def);
58 bool ofnode_read_bool(ofnode node, const char *propname)
62 assert(ofnode_valid(node));
63 debug("%s: %s: ", __func__, propname);
65 if (ofnode_is_np(node)) {
66 val = !!of_find_property(ofnode_to_np(node), propname, NULL);
68 val = !!fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
71 debug("%s\n", val ? "true" : "false");
76 const char *ofnode_read_string(ofnode node, const char *propname)
78 const char *str = NULL;
81 assert(ofnode_valid(node));
82 debug("%s: %s: ", __func__, propname);
84 if (ofnode_is_np(node)) {
85 struct property *prop = of_find_property(
86 ofnode_to_np(node), propname, NULL);
93 str = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
97 debug("<not found>\n");
100 if (strnlen(str, len) >= len) {
101 debug("<invalid>\n");
109 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
113 assert(ofnode_valid(node));
114 debug("%s: %s: ", __func__, subnode_name);
116 if (ofnode_is_np(node)) {
117 const struct device_node *np = ofnode_to_np(node);
119 for (np = np->child; np; np = np->sibling) {
120 if (!strcmp(subnode_name, np->name))
123 subnode = np_to_ofnode(np);
125 int ooffset = fdt_subnode_offset(gd->fdt_blob,
126 ofnode_to_offset(node), subnode_name);
127 subnode = offset_to_ofnode(ooffset);
129 debug("%s\n", ofnode_valid(subnode) ?
130 ofnode_get_name(subnode) : "<none>");
135 int ofnode_read_u32_array(ofnode node, const char *propname,
136 u32 *out_values, size_t sz)
138 assert(ofnode_valid(node));
139 debug("%s: %s: ", __func__, propname);
141 if (ofnode_is_np(node)) {
142 return of_read_u32_array(ofnode_to_np(node), propname,
145 return fdtdec_get_int_array(gd->fdt_blob,
146 ofnode_to_offset(node), propname,
151 ofnode ofnode_first_subnode(ofnode node)
153 assert(ofnode_valid(node));
154 if (ofnode_is_np(node))
155 return np_to_ofnode(node.np->child);
157 return offset_to_ofnode(
158 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
161 ofnode ofnode_next_subnode(ofnode node)
163 assert(ofnode_valid(node));
164 if (ofnode_is_np(node))
165 return np_to_ofnode(node.np->sibling);
167 return offset_to_ofnode(
168 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
171 const char *ofnode_get_name(ofnode node)
173 assert(ofnode_valid(node));
174 if (ofnode_is_np(node))
175 return strrchr(node.np->full_name, '/') + 1;
177 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
180 int ofnode_read_size(ofnode node, const char *propname)
184 if (ofnode_is_np(node)) {
185 struct property *prop = of_find_property(
186 ofnode_to_np(node), propname, NULL);
191 if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
199 fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
201 if (ofnode_is_np(node)) {
202 const __be32 *prop_val;
206 prop_val = of_get_address(
207 (struct device_node *)ofnode_to_np(node), index,
210 return FDT_ADDR_T_NONE;
211 return be32_to_cpup(prop_val);
213 return fdt_get_base_address(gd->fdt_blob,
214 ofnode_to_offset(node));
217 return FDT_ADDR_T_NONE;
220 fdt_addr_t ofnode_get_addr(ofnode node)
222 return ofnode_get_addr_index(node, 0);
225 int ofnode_stringlist_search(ofnode node, const char *property,
228 if (ofnode_is_np(node)) {
229 return of_property_match_string(ofnode_to_np(node),
234 ret = fdt_stringlist_search(gd->fdt_blob,
235 ofnode_to_offset(node), property,
237 if (ret == -FDT_ERR_NOTFOUND)
246 int ofnode_read_string_index(ofnode node, const char *property, int index,
249 if (ofnode_is_np(node)) {
250 return of_property_read_string_index(ofnode_to_np(node),
251 property, index, outp);
255 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
256 property, index, &len);
263 int ofnode_read_string_count(ofnode node, const char *property)
265 if (ofnode_is_np(node)) {
266 return of_property_count_strings(ofnode_to_np(node), property);
268 return fdt_stringlist_count(gd->fdt_blob,
269 ofnode_to_offset(node), property);
273 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
274 struct ofnode_phandle_args *out)
276 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
277 out->node = offset_to_ofnode(in->node);
278 out->args_count = in->args_count;
279 memcpy(out->args, in->args, sizeof(out->args));
282 static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
283 struct ofnode_phandle_args *out)
285 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
286 out->node = np_to_ofnode(in->np);
287 out->args_count = in->args_count;
288 memcpy(out->args, in->args, sizeof(out->args));
291 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
292 const char *cells_name, int cell_count,
294 struct ofnode_phandle_args *out_args)
296 if (ofnode_is_np(node)) {
297 struct of_phandle_args args;
300 ret = of_parse_phandle_with_args(ofnode_to_np(node),
301 list_name, cells_name, index, &args);
304 ofnode_from_of_phandle_args(&args, out_args);
306 struct fdtdec_phandle_args args;
309 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
310 ofnode_to_offset(node), list_name, cells_name,
311 cell_count, index, &args);
314 ofnode_from_fdtdec_phandle_args(&args, out_args);
320 ofnode ofnode_path(const char *path)
322 if (of_live_active())
323 return np_to_ofnode(of_find_node_by_path(path));
325 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
328 const char *ofnode_get_chosen_prop(const char *name)
332 chosen_node = ofnode_path("/chosen");
334 return ofnode_read_string(chosen_node, name);
337 ofnode ofnode_get_chosen_node(const char *name)
341 prop = ofnode_get_chosen_prop(name);
343 return ofnode_null();
345 return ofnode_path(prop);
348 static int decode_timing_property(ofnode node, const char *name,
349 struct timing_entry *result)
353 length = ofnode_read_size(node, name);
355 debug("%s: could not find property %s\n",
356 ofnode_get_name(node), name);
360 if (length == sizeof(u32)) {
361 result->typ = ofnode_read_u32_default(node, name, 0);
362 result->min = result->typ;
363 result->max = result->typ;
365 ret = ofnode_read_u32_array(node, name, &result->min, 3);
371 int ofnode_decode_display_timing(ofnode parent, int index,
372 struct display_timing *dt)
375 ofnode timings, node;
379 timings = ofnode_find_subnode(parent, "display-timings");
380 if (!ofnode_valid(timings))
383 for (i = 0, node = ofnode_first_subnode(timings);
384 ofnode_valid(node) && i != index;
385 node = ofnode_first_subnode(node))
388 if (!ofnode_valid(node))
391 memset(dt, 0, sizeof(*dt));
393 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
394 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
395 ret |= decode_timing_property(node, "hactive", &dt->hactive);
396 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
397 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
398 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
399 ret |= decode_timing_property(node, "vactive", &dt->vactive);
400 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
401 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
404 val = ofnode_read_u32_default(node, "vsync-active", -1);
406 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
407 DISPLAY_FLAGS_VSYNC_LOW;
409 val = ofnode_read_u32_default(node, "hsync-active", -1);
411 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
412 DISPLAY_FLAGS_HSYNC_LOW;
414 val = ofnode_read_u32_default(node, "de-active", -1);
416 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
417 DISPLAY_FLAGS_DE_LOW;
419 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
421 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
422 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
425 if (ofnode_read_bool(node, "interlaced"))
426 dt->flags |= DISPLAY_FLAGS_INTERLACED;
427 if (ofnode_read_bool(node, "doublescan"))
428 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
429 if (ofnode_read_bool(node, "doubleclk"))
430 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
435 const u32 *ofnode_read_prop(ofnode node, const char *propname, int *lenp)
437 if (ofnode_is_np(node)) {
438 struct property *prop;
440 prop = of_find_property(ofnode_to_np(node), propname, lenp);
445 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
450 bool ofnode_is_available(ofnode node)
452 if (ofnode_is_np(node))
453 return of_device_is_available(ofnode_to_np(node));
455 return fdtdec_get_is_enabled(gd->fdt_blob,
456 ofnode_to_offset(node));
459 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
462 if (ofnode_is_np(node)) {
465 const struct device_node *np = ofnode_to_np(node);
466 const __be32 *prop = of_get_property(np, "reg", &psize);
468 na = of_n_addr_cells(np);
469 ns = of_n_addr_cells(np);
470 *sizep = of_read_number(prop + na, ns);
471 return of_read_number(prop, na);
473 return fdtdec_get_addr_size(gd->fdt_blob,
474 ofnode_to_offset(node), property,
479 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
482 if (ofnode_is_np(node)) {
483 const struct device_node *np = ofnode_to_np(node);
485 const __be32 *prop = of_get_property(np, propname, &psize);
487 if (!prop || sz != psize)
489 return (uint8_t *)prop;
492 return fdtdec_locate_byte_array(gd->fdt_blob,
493 ofnode_to_offset(node), propname, sz);
497 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
498 const char *propname, struct fdt_pci_addr *addr)
504 debug("%s: %s: ", __func__, propname);
507 * If we follow the pci bus bindings strictly, we should check
508 * the value of the node's parent node's #address-cells and
509 * #size-cells. They need to be 3 and 2 accordingly. However,
510 * for simplicity we skip the check here.
512 cell = ofnode_read_prop(node, propname, &len);
516 if ((len % FDT_PCI_REG_SIZE) == 0) {
517 int num = len / FDT_PCI_REG_SIZE;
520 for (i = 0; i < num; i++) {
521 debug("pci address #%d: %08lx %08lx %08lx\n", i,
522 (ulong)fdt32_to_cpu(cell[0]),
523 (ulong)fdt32_to_cpu(cell[1]),
524 (ulong)fdt32_to_cpu(cell[2]));
525 if ((fdt32_to_cpu(*cell) & type) == type) {
526 addr->phys_hi = fdt32_to_cpu(cell[0]);
527 addr->phys_mid = fdt32_to_cpu(cell[1]);
528 addr->phys_lo = fdt32_to_cpu(cell[1]);
531 cell += (FDT_PCI_ADDR_CELLS +
547 debug("(not found)\n");
551 int ofnode_read_addr_cells(ofnode node)
553 if (ofnode_is_np(node))
554 return of_n_addr_cells(ofnode_to_np(node));
555 else /* NOTE: this call should walk up the parent stack */
556 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
559 int ofnode_read_size_cells(ofnode node)
561 if (ofnode_is_np(node))
562 return of_n_size_cells(ofnode_to_np(node));
563 else /* NOTE: this call should walk up the parent stack */
564 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
567 int ofnode_read_simple_addr_cells(ofnode node)
569 if (ofnode_is_np(node))
570 return of_simple_addr_cells(ofnode_to_np(node));
572 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
575 int ofnode_read_simple_size_cells(ofnode node)
577 if (ofnode_is_np(node))
578 return of_simple_size_cells(ofnode_to_np(node));
580 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
583 bool ofnode_pre_reloc(ofnode node)
585 if (ofnode_read_prop(node, "u-boot,dm-pre-reloc", NULL))
588 #ifdef CONFIG_TPL_BUILD
589 if (ofnode_read_prop(node, "u-boot,dm-tpl", NULL))
591 #elif defined(CONFIG_SPL_BUILD)
592 if (ofnode_read_prop(node, "u-boot,dm-spl", NULL))
596 * In regular builds individual spl and tpl handling both
597 * count as handled pre-relocation for later second init.
599 if (ofnode_read_prop(node, "u-boot,dm-spl", NULL) ||
600 ofnode_read_prop(node, "u-boot,dm-tpl", NULL))