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>
17 #include <linux/ioport.h>
19 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
21 assert(ofnode_valid(node));
22 debug("%s: %s: ", __func__, propname);
24 if (ofnode_is_np(node)) {
25 return of_read_u32(ofnode_to_np(node), propname, outp);
30 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
32 if (!cell || len < sizeof(int)) {
33 debug("(not found)\n");
36 *outp = fdt32_to_cpu(cell[0]);
38 debug("%#x (%d)\n", *outp, *outp);
43 int ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
45 assert(ofnode_valid(node));
46 ofnode_read_u32(node, propname, &def);
51 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
53 assert(ofnode_valid(node));
54 ofnode_read_u32(node, propname, (u32 *)&def);
59 bool ofnode_read_bool(ofnode node, const char *propname)
63 assert(ofnode_valid(node));
64 debug("%s: %s: ", __func__, propname);
66 prop = ofnode_get_property(node, propname, NULL);
68 debug("%s\n", prop ? "true" : "false");
70 return prop ? true : false;
73 const char *ofnode_read_string(ofnode node, const char *propname)
75 const char *str = NULL;
78 assert(ofnode_valid(node));
79 debug("%s: %s: ", __func__, propname);
81 if (ofnode_is_np(node)) {
82 struct property *prop = of_find_property(
83 ofnode_to_np(node), propname, NULL);
90 str = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
94 debug("<not found>\n");
97 if (strnlen(str, len) >= len) {
106 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
110 assert(ofnode_valid(node));
111 debug("%s: %s: ", __func__, subnode_name);
113 if (ofnode_is_np(node)) {
114 const struct device_node *np = ofnode_to_np(node);
116 for (np = np->child; np; np = np->sibling) {
117 if (!strcmp(subnode_name, np->name))
120 subnode = np_to_ofnode(np);
122 int ooffset = fdt_subnode_offset(gd->fdt_blob,
123 ofnode_to_offset(node), subnode_name);
124 subnode = offset_to_ofnode(ooffset);
126 debug("%s\n", ofnode_valid(subnode) ?
127 ofnode_get_name(subnode) : "<none>");
132 int ofnode_read_u32_array(ofnode node, const char *propname,
133 u32 *out_values, size_t sz)
135 assert(ofnode_valid(node));
136 debug("%s: %s: ", __func__, propname);
138 if (ofnode_is_np(node)) {
139 return of_read_u32_array(ofnode_to_np(node), propname,
142 return fdtdec_get_int_array(gd->fdt_blob,
143 ofnode_to_offset(node), propname,
148 ofnode ofnode_first_subnode(ofnode node)
150 assert(ofnode_valid(node));
151 if (ofnode_is_np(node))
152 return np_to_ofnode(node.np->child);
154 return offset_to_ofnode(
155 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
158 ofnode ofnode_next_subnode(ofnode node)
160 assert(ofnode_valid(node));
161 if (ofnode_is_np(node))
162 return np_to_ofnode(node.np->sibling);
164 return offset_to_ofnode(
165 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
168 const char *ofnode_get_name(ofnode node)
170 assert(ofnode_valid(node));
171 if (ofnode_is_np(node))
172 return strrchr(node.np->full_name, '/') + 1;
174 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
177 int ofnode_read_size(ofnode node, const char *propname)
181 if (ofnode_is_np(node)) {
182 struct property *prop = of_find_property(
183 ofnode_to_np(node), propname, NULL);
188 if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
196 fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
198 if (ofnode_is_np(node)) {
199 const __be32 *prop_val;
204 prop_val = of_get_address(ofnode_to_np(node), index, &size,
207 return FDT_ADDR_T_NONE;
208 na = of_n_addr_cells(ofnode_to_np(node));
209 return of_read_number(prop_val, na);
211 return fdt_get_base_address(gd->fdt_blob,
212 ofnode_to_offset(node));
215 return FDT_ADDR_T_NONE;
218 fdt_addr_t ofnode_get_addr(ofnode node)
220 return ofnode_get_addr_index(node, 0);
223 int ofnode_stringlist_search(ofnode node, const char *property,
226 if (ofnode_is_np(node)) {
227 return of_property_match_string(ofnode_to_np(node),
232 ret = fdt_stringlist_search(gd->fdt_blob,
233 ofnode_to_offset(node), property,
235 if (ret == -FDT_ERR_NOTFOUND)
244 int ofnode_read_string_index(ofnode node, const char *property, int index,
247 if (ofnode_is_np(node)) {
248 return of_property_read_string_index(ofnode_to_np(node),
249 property, index, outp);
253 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
254 property, index, &len);
261 int ofnode_read_string_count(ofnode node, const char *property)
263 if (ofnode_is_np(node)) {
264 return of_property_count_strings(ofnode_to_np(node), property);
266 return fdt_stringlist_count(gd->fdt_blob,
267 ofnode_to_offset(node), property);
271 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
272 struct ofnode_phandle_args *out)
274 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
275 out->node = offset_to_ofnode(in->node);
276 out->args_count = in->args_count;
277 memcpy(out->args, in->args, sizeof(out->args));
280 static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
281 struct ofnode_phandle_args *out)
283 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
284 out->node = np_to_ofnode(in->np);
285 out->args_count = in->args_count;
286 memcpy(out->args, in->args, sizeof(out->args));
289 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
290 const char *cells_name, int cell_count,
292 struct ofnode_phandle_args *out_args)
294 if (ofnode_is_np(node)) {
295 struct of_phandle_args args;
298 ret = of_parse_phandle_with_args(ofnode_to_np(node),
299 list_name, cells_name, index, &args);
302 ofnode_from_of_phandle_args(&args, out_args);
304 struct fdtdec_phandle_args args;
307 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
308 ofnode_to_offset(node), list_name, cells_name,
309 cell_count, index, &args);
312 ofnode_from_fdtdec_phandle_args(&args, out_args);
318 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
319 const char *cells_name)
321 if (ofnode_is_np(node))
322 return of_count_phandle_with_args(ofnode_to_np(node),
323 list_name, cells_name);
325 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
326 ofnode_to_offset(node), list_name, cells_name,
330 ofnode ofnode_path(const char *path)
332 if (of_live_active())
333 return np_to_ofnode(of_find_node_by_path(path));
335 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
338 const char *ofnode_get_chosen_prop(const char *name)
342 chosen_node = ofnode_path("/chosen");
344 return ofnode_read_string(chosen_node, name);
347 ofnode ofnode_get_chosen_node(const char *name)
351 prop = ofnode_get_chosen_prop(name);
353 return ofnode_null();
355 return ofnode_path(prop);
358 static int decode_timing_property(ofnode node, const char *name,
359 struct timing_entry *result)
363 length = ofnode_read_size(node, name);
365 debug("%s: could not find property %s\n",
366 ofnode_get_name(node), name);
370 if (length == sizeof(u32)) {
371 result->typ = ofnode_read_u32_default(node, name, 0);
372 result->min = result->typ;
373 result->max = result->typ;
375 ret = ofnode_read_u32_array(node, name, &result->min, 3);
381 int ofnode_decode_display_timing(ofnode parent, int index,
382 struct display_timing *dt)
385 ofnode timings, node;
389 timings = ofnode_find_subnode(parent, "display-timings");
390 if (!ofnode_valid(timings))
393 for (i = 0, node = ofnode_first_subnode(timings);
394 ofnode_valid(node) && i != index;
395 node = ofnode_first_subnode(node))
398 if (!ofnode_valid(node))
401 memset(dt, 0, sizeof(*dt));
403 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
404 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
405 ret |= decode_timing_property(node, "hactive", &dt->hactive);
406 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
407 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
408 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
409 ret |= decode_timing_property(node, "vactive", &dt->vactive);
410 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
411 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
414 val = ofnode_read_u32_default(node, "vsync-active", -1);
416 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
417 DISPLAY_FLAGS_VSYNC_LOW;
419 val = ofnode_read_u32_default(node, "hsync-active", -1);
421 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
422 DISPLAY_FLAGS_HSYNC_LOW;
424 val = ofnode_read_u32_default(node, "de-active", -1);
426 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
427 DISPLAY_FLAGS_DE_LOW;
429 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
431 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
432 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
435 if (ofnode_read_bool(node, "interlaced"))
436 dt->flags |= DISPLAY_FLAGS_INTERLACED;
437 if (ofnode_read_bool(node, "doublescan"))
438 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
439 if (ofnode_read_bool(node, "doubleclk"))
440 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
445 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
447 if (ofnode_is_np(node))
448 return of_get_property(ofnode_to_np(node), propname, lenp);
450 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
454 bool ofnode_is_available(ofnode node)
456 if (ofnode_is_np(node))
457 return of_device_is_available(ofnode_to_np(node));
459 return fdtdec_get_is_enabled(gd->fdt_blob,
460 ofnode_to_offset(node));
463 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
466 if (ofnode_is_np(node)) {
469 const struct device_node *np = ofnode_to_np(node);
470 const __be32 *prop = of_get_property(np, "reg", &psize);
472 na = of_n_addr_cells(np);
473 ns = of_n_addr_cells(np);
474 *sizep = of_read_number(prop + na, ns);
475 return of_read_number(prop, na);
477 return fdtdec_get_addr_size(gd->fdt_blob,
478 ofnode_to_offset(node), property,
483 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
486 if (ofnode_is_np(node)) {
487 const struct device_node *np = ofnode_to_np(node);
489 const __be32 *prop = of_get_property(np, propname, &psize);
491 if (!prop || sz != psize)
493 return (uint8_t *)prop;
496 return fdtdec_locate_byte_array(gd->fdt_blob,
497 ofnode_to_offset(node), propname, sz);
501 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
502 const char *propname, struct fdt_pci_addr *addr)
508 debug("%s: %s: ", __func__, propname);
511 * If we follow the pci bus bindings strictly, we should check
512 * the value of the node's parent node's #address-cells and
513 * #size-cells. They need to be 3 and 2 accordingly. However,
514 * for simplicity we skip the check here.
516 cell = ofnode_get_property(node, propname, &len);
520 if ((len % FDT_PCI_REG_SIZE) == 0) {
521 int num = len / FDT_PCI_REG_SIZE;
524 for (i = 0; i < num; i++) {
525 debug("pci address #%d: %08lx %08lx %08lx\n", i,
526 (ulong)fdt32_to_cpu(cell[0]),
527 (ulong)fdt32_to_cpu(cell[1]),
528 (ulong)fdt32_to_cpu(cell[2]));
529 if ((fdt32_to_cpu(*cell) & type) == type) {
530 addr->phys_hi = fdt32_to_cpu(cell[0]);
531 addr->phys_mid = fdt32_to_cpu(cell[1]);
532 addr->phys_lo = fdt32_to_cpu(cell[1]);
535 cell += (FDT_PCI_ADDR_CELLS +
551 debug("(not found)\n");
555 int ofnode_read_addr_cells(ofnode node)
557 if (ofnode_is_np(node))
558 return of_n_addr_cells(ofnode_to_np(node));
559 else /* NOTE: this call should walk up the parent stack */
560 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
563 int ofnode_read_size_cells(ofnode node)
565 if (ofnode_is_np(node))
566 return of_n_size_cells(ofnode_to_np(node));
567 else /* NOTE: this call should walk up the parent stack */
568 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
571 int ofnode_read_simple_addr_cells(ofnode node)
573 if (ofnode_is_np(node))
574 return of_simple_addr_cells(ofnode_to_np(node));
576 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
579 int ofnode_read_simple_size_cells(ofnode node)
581 if (ofnode_is_np(node))
582 return of_simple_size_cells(ofnode_to_np(node));
584 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
587 bool ofnode_pre_reloc(ofnode node)
589 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
592 #ifdef CONFIG_TPL_BUILD
593 if (ofnode_read_bool(node, "u-boot,dm-tpl"))
595 #elif defined(CONFIG_SPL_BUILD)
596 if (ofnode_read_bool(node, "u-boot,dm-spl"))
600 * In regular builds individual spl and tpl handling both
601 * count as handled pre-relocation for later second init.
603 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
604 ofnode_read_bool(node, "u-boot,dm-tpl"))
611 int ofnode_read_resource(ofnode node, uint index, struct resource *res)
613 if (ofnode_is_np(node)) {
614 return of_address_to_resource(ofnode_to_np(node), index, res);
616 struct fdt_resource fres;
619 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
620 "reg", index, &fres);
623 memset(res, '\0', sizeof(*res));
624 res->start = fres.start;
631 int ofnode_read_resource_byname(ofnode node, const char *name,
632 struct resource *res)
636 index = ofnode_stringlist_search(node, "reg-names", name);
640 return ofnode_read_resource(node, index, res);