blob: 79374b8f91aa8030cb5f1d3ad595290a14c5f818 [file] [log] [blame]
Simon Glass4984de22017-05-17 17:18:10 -06001/*
2 * Copyright (c) 2017 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#ifndef _DM_OFNODE_H
9#define _DM_OFNODE_H
10
Simon Glass9e512042017-05-18 20:08:58 -060011/* TODO(sjg@chromium.org): Drop fdtdec.h include */
12#include <fdtdec.h>
13#include <dm/of.h>
14
15/* Enable checks to protect against invalid calls */
16#undef OF_CHECKS
17
Simon Glassdcf98852017-07-25 08:29:55 -060018struct resource;
19
Simon Glass4984de22017-05-17 17:18:10 -060020/**
21 * ofnode - reference to a device tree node
22 *
23 * This union can hold either a straightforward pointer to a struct device_node
24 * in the live device tree, or an offset within the flat device tree. In the
25 * latter case, the pointer value is just the integer offset within the flat DT.
26 *
27 * Thus we can reference nodes in both the live tree (once available) and the
28 * flat tree (until then). Functions are available to translate between an
29 * ofnode and either an offset or a struct device_node *.
30 *
31 * The reference can also hold a null offset, in which case the pointer value
Simon Glass9e512042017-05-18 20:08:58 -060032 * here is NULL. This corresponds to a struct device_node * value of
Simon Glass4984de22017-05-17 17:18:10 -060033 * NULL, or an offset of -1.
34 *
35 * There is no ambiguity as to whether ofnode holds an offset or a node
36 * pointer: when the live tree is active it holds a node pointer, otherwise it
37 * holds an offset. The value itself does not need to be unique and in theory
38 * the same value could point to a valid device node or a valid offset. We
39 * could arrange for a unique value to be used (e.g. by making the pointer
40 * point to an offset within the flat device tree in the case of an offset) but
41 * this increases code size slightly due to the subtraction. Since it offers no
42 * real benefit, the approach described here seems best.
43 *
44 * For now these points use constant types, since we don't allow writing
45 * the DT.
46 *
47 * @np: Pointer to device node, used for live tree
48 * @flat_ptr: Pointer into flat device tree, used for flat tree. Note that this
49 * is not a really a pointer to a node: it is an offset value. See above.
50 */
51typedef union ofnode_union {
52 const struct device_node *np; /* will be used for future live tree */
53 long of_offset;
54} ofnode;
55
Simon Glass9e512042017-05-18 20:08:58 -060056struct ofnode_phandle_args {
57 ofnode node;
58 int args_count;
59 uint32_t args[OF_MAX_PHANDLE_ARGS];
60};
61
62/**
63 * _ofnode_to_np() - convert an ofnode to a live DT node pointer
64 *
65 * This cannot be called if the reference contains an offset.
66 *
67 * @node: Reference containing struct device_node * (possibly invalid)
68 * @return pointer to device node (can be NULL)
69 */
70static inline const struct device_node *ofnode_to_np(ofnode node)
71{
72#ifdef OF_CHECKS
73 if (!of_live_active())
74 return NULL;
75#endif
76 return node.np;
77}
78
Simon Glass4984de22017-05-17 17:18:10 -060079/**
80 * ofnode_to_offset() - convert an ofnode to a flat DT offset
81 *
82 * This cannot be called if the reference contains a node pointer.
83 *
84 * @node: Reference containing offset (possibly invalid)
85 * @return DT offset (can be -1)
86 */
87static inline int ofnode_to_offset(ofnode node)
88{
Simon Glass9e512042017-05-18 20:08:58 -060089#ifdef OF_CHECKS
90 if (of_live_active())
91 return -1;
92#endif
Simon Glass4984de22017-05-17 17:18:10 -060093 return node.of_offset;
94}
95
96/**
97 * ofnode_valid() - check if an ofnode is valid
98 *
99 * @return true if the reference contains a valid ofnode, false if it is NULL
100 */
101static inline bool ofnode_valid(ofnode node)
102{
Simon Glass9e512042017-05-18 20:08:58 -0600103 if (of_live_active())
104 return node.np != NULL;
105 else
106 return node.of_offset != -1;
Simon Glass4984de22017-05-17 17:18:10 -0600107}
108
109/**
110 * offset_to_ofnode() - convert a DT offset to an ofnode
111 *
112 * @of_offset: DT offset (either valid, or -1)
113 * @return reference to the associated DT offset
114 */
115static inline ofnode offset_to_ofnode(int of_offset)
116{
117 ofnode node;
118
Simon Glass9e512042017-05-18 20:08:58 -0600119 if (of_live_active())
120 node.np = NULL;
121 else
122 node.of_offset = of_offset;
Simon Glass4984de22017-05-17 17:18:10 -0600123
124 return node;
125}
126
127/**
Simon Glass9e512042017-05-18 20:08:58 -0600128 * np_to_ofnode() - convert a node pointer to an ofnode
129 *
130 * @np: Live node pointer (can be NULL)
131 * @return reference to the associated node pointer
132 */
133static inline ofnode np_to_ofnode(const struct device_node *np)
134{
135 ofnode node;
136
137 node.np = np;
138
139 return node;
140}
141
142/**
143 * ofnode_is_np() - check if a reference is a node pointer
144 *
145 * This function associated that if there is a valid live tree then all
146 * references will use it. This is because using the flat DT when the live tree
147 * is valid is not permitted.
148 *
149 * @node: reference to check (possibly invalid)
150 * @return true if the reference is a live node pointer, false if it is a DT
151 * offset
152 */
153static inline bool ofnode_is_np(ofnode node)
154{
155#ifdef OF_CHECKS
156 /*
157 * Check our assumption that flat tree offsets are not used when a
158 * live tree is in use.
159 */
160 assert(!ofnode_valid(node) ||
161 (of_live_active() ? _ofnode_to_np(node)
162 : _ofnode_to_np(node)));
163#endif
164 return of_live_active() && ofnode_valid(node);
165}
166
167/**
Simon Glass4984de22017-05-17 17:18:10 -0600168 * ofnode_equal() - check if two references are equal
169 *
170 * @return true if equal, else false
171 */
172static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
173{
174 /* We only need to compare the contents */
175 return ref1.of_offset == ref2.of_offset;
176}
177
Simon Glass9e512042017-05-18 20:08:58 -0600178/**
179 * ofnode_null() - Obtain a null ofnode
180 *
181 * This returns an ofnode which points to no node. It works both with the flat
182 * tree and livetree.
183 */
184static inline ofnode ofnode_null(void)
185{
186 ofnode node;
187
188 if (of_live_active())
189 node.np = NULL;
190 else
191 node.of_offset = -1;
192
193 return node;
194}
195
196/**
197 * ofnode_read_u32() - Read a 32-bit integer from a property
198 *
199 * @ref: valid node reference to read property from
200 * @propname: name of the property to read from
201 * @outp: place to put value (if found)
202 * @return 0 if OK, -ve on error
203 */
204int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
205
206/**
207 * ofnode_read_s32() - Read a 32-bit integer from a property
208 *
209 * @ref: valid node reference to read property from
210 * @propname: name of the property to read from
211 * @outp: place to put value (if found)
212 * @return 0 if OK, -ve on error
213 */
214static inline int ofnode_read_s32(ofnode node, const char *propname,
215 s32 *out_value)
216{
217 return ofnode_read_u32(node, propname, (u32 *)out_value);
218}
219
220/**
221 * ofnode_read_u32_default() - Read a 32-bit integer from a property
222 *
223 * @ref: valid node reference to read property from
224 * @propname: name of the property to read from
225 * @def: default value to return if the property has no value
226 * @return property value, or @def if not found
227 */
228int ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
229
230/**
231 * ofnode_read_s32_default() - Read a 32-bit integer from a property
232 *
233 * @ref: valid node reference to read property from
234 * @propname: name of the property to read from
235 * @def: default value to return if the property has no value
236 * @return property value, or @def if not found
237 */
238int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
239
240/**
241 * ofnode_read_string() - Read a string from a property
242 *
243 * @ref: valid node reference to read property from
244 * @propname: name of the property to read
245 * @return string from property value, or NULL if there is no such property
246 */
247const char *ofnode_read_string(ofnode node, const char *propname);
248
249/**
Simon Glassbed77492017-05-18 20:09:01 -0600250 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glass9e512042017-05-18 20:08:58 -0600251 *
252 * @node: valid node reference to read property from
253 * @propname: name of the property to read
254 * @out_values: pointer to return value, modified only if return value is 0
255 * @sz: number of array elements to read
256 *
257 * Search for a property in a device node and read 32-bit value(s) from
258 * it. Returns 0 on success, -EINVAL if the property does not exist,
259 * -ENODATA if property does not have a value, and -EOVERFLOW if the
260 * property data isn't large enough.
261 *
262 * The out_values is modified only if a valid u32 value can be decoded.
263 */
264int ofnode_read_u32_array(ofnode node, const char *propname,
265 u32 *out_values, size_t sz);
266
267/**
268 * ofnode_read_bool() - read a boolean value from a property
269 *
270 * @node: valid node reference to read property from
271 * @propname: name of property to read
272 * @return true if property is present (meaning true), false if not present
273 */
274bool ofnode_read_bool(ofnode node, const char *propname);
275
276/**
277 * ofnode_find_subnode() - find a named subnode of a parent node
278 *
279 * @node: valid reference to parent node
280 * @subnode_name: name of subnode to find
281 * @return reference to subnode (which can be invalid if there is no such
282 * subnode)
283 */
284ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
285
286/**
287 * ofnode_first_subnode() - find the first subnode of a parent node
288 *
289 * @node: valid reference to a valid parent node
290 * @return reference to the first subnode (which can be invalid if the parent
291 * node has no subnodes)
292 */
293ofnode ofnode_first_subnode(ofnode node);
294
295/**
296 * ofnode_next_subnode() - find the next sibling of a subnode
297 *
298 * @node: valid reference to previous node (sibling)
299 * @return reference to the next subnode (which can be invalid if the node
300 * has no more siblings)
301 */
302ofnode ofnode_next_subnode(ofnode node);
303
304/**
305 * ofnode_get_name() - get the name of a node
306 *
307 * @node: valid node to look up
308 * @return name or node
309 */
310const char *ofnode_get_name(ofnode node);
311
312/**
313 * ofnode_read_size() - read the size of a property
314 *
315 * @node: node to check
316 * @propname: property to check
317 * @return size of property if present, or -EINVAL if not
318 */
319int ofnode_read_size(ofnode node, const char *propname);
320
321/**
Simon Glassbed77492017-05-18 20:09:01 -0600322 * ofnode_get_addr_index() - get an address from a node
323 *
324 * This reads the register address from a node
325 *
326 * @node: node to read from
327 * @index: Index of address to read (0 for first)
328 * @return address, or FDT_ADDR_T_NONE if not present or invalid
329 */
330phys_addr_t ofnode_get_addr_index(ofnode node, int index);
331
332/**
333 * ofnode_get_addr() - get an address from a node
334 *
335 * This reads the register address from a node
336 *
337 * @node: node to read from
338 * @return address, or FDT_ADDR_T_NONE if not present or invalid
339 */
340phys_addr_t ofnode_get_addr(ofnode node);
341
342/**
Simon Glass9e512042017-05-18 20:08:58 -0600343 * ofnode_stringlist_search() - find a string in a string list and return index
344 *
345 * Note that it is possible for this function to succeed on property values
346 * that are not NUL-terminated. That's because the function will stop after
347 * finding the first occurrence of @string. This can for example happen with
348 * small-valued cell properties, such as #address-cells, when searching for
349 * the empty string.
350 *
351 * @node: node to check
352 * @propname: name of the property containing the string list
353 * @string: string to look up in the string list
354 *
355 * @return:
356 * the index of the string in the list of strings
357 * -ENODATA if the property is not found
358 * -EINVAL on some other error
359 */
360int ofnode_stringlist_search(ofnode node, const char *propname,
361 const char *string);
362
363/**
Simon Glass8c293d62017-06-12 06:21:28 -0600364 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glass9e512042017-05-18 20:08:58 -0600365 *
366 * Note that this will successfully extract strings from properties with
367 * non-NUL-terminated values. For example on small-valued cell properties
368 * this function will return the empty string.
369 *
370 * If non-NULL, the length of the string (on success) or a negative error-code
371 * (on failure) will be stored in the integer pointer to by lenp.
372 *
373 * @node: node to check
374 * @propname: name of the property containing the string list
375 * @index: index of the string to return
376 * @lenp: return location for the string length or an error code on failure
377 *
378 * @return:
379 * length of string, if found or -ve error value if not found
380 */
381int ofnode_read_string_index(ofnode node, const char *propname, int index,
382 const char **outp);
383
384/**
Simon Glass8c293d62017-06-12 06:21:28 -0600385 * ofnode_read_string_count() - find the number of strings in a string list
386 *
387 * @node: node to check
388 * @propname: name of the property containing the string list
389 * @return:
390 * number of strings in the list, or -ve error value if not found
391 */
392int ofnode_read_string_count(ofnode node, const char *property);
393
394/**
Simon Glass9e512042017-05-18 20:08:58 -0600395 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
396 *
397 * This function is useful to parse lists of phandles and their arguments.
398 * Returns 0 on success and fills out_args, on error returns appropriate
399 * errno value.
400 *
401 * Caller is responsible to call of_node_put() on the returned out_args->np
402 * pointer.
403 *
404 * Example:
405 *
406 * phandle1: node1 {
407 * #list-cells = <2>;
408 * }
409 *
410 * phandle2: node2 {
411 * #list-cells = <1>;
412 * }
413 *
414 * node3 {
415 * list = <&phandle1 1 2 &phandle2 3>;
416 * }
417 *
418 * To get a device_node of the `node2' node you may call this:
419 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
420 *
421 * @node: device tree node containing a list
422 * @list_name: property name that contains a list
423 * @cells_name: property name that specifies phandles' arguments count
424 * @cells_count: Cell count to use if @cells_name is NULL
425 * @index: index of a phandle to parse out
426 * @out_args: optional pointer to output arguments structure (will be filled)
427 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
428 * @list_name does not exist, -EINVAL if a phandle was not found,
429 * @cells_name could not be found, the arguments were truncated or there
430 * were too many arguments.
431 */
432int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
433 const char *cells_name, int cell_count,
434 int index,
435 struct ofnode_phandle_args *out_args);
436
437/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200438 * ofnode_count_phandle_with_args() - Count number of phandle in a list
439 *
440 * This function is useful to count phandles into a list.
441 * Returns number of phandle on success, on error returns appropriate
442 * errno value.
443 *
444 * @node: device tree node containing a list
445 * @list_name: property name that contains a list
446 * @cells_name: property name that specifies phandles' arguments count
447 * @return number of phandle on success, -ENOENT if @list_name does not
448 * exist, -EINVAL if a phandle was not found, @cells_name could not
449 * be found.
450 */
451int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
452 const char *cells_name);
453
454/**
Simon Glass9e512042017-05-18 20:08:58 -0600455 * ofnode_path() - find a node by full path
456 *
457 * @path: Full path to node, e.g. "/bus/spi@1"
458 * @return reference to the node found. Use ofnode_valid() to check if it exists
459 */
460ofnode ofnode_path(const char *path);
461
462/**
463 * ofnode_get_chosen_prop() - get the value of a chosen property
464 *
465 * This looks for a property within the /chosen node and returns its value
466 *
467 * @propname: Property name to look for
468 */
469const char *ofnode_get_chosen_prop(const char *propname);
470
471/**
472 * ofnode_get_chosen_node() - get the chosen node
473 *
474 * @return the chosen node if present, else ofnode_null()
475 */
476ofnode ofnode_get_chosen_node(const char *name);
477
478struct display_timing;
479/**
480 * ofnode_decode_display_timing() - decode display timings
481 *
482 * Decode display timings from the supplied 'display-timings' node.
483 * See doc/device-tree-bindings/video/display-timing.txt for binding
484 * information.
485 *
486 * @node 'display-timing' node containing the timing subnodes
487 * @index Index number to read (0=first timing subnode)
488 * @config Place to put timings
489 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
490 */
491int ofnode_decode_display_timing(ofnode node, int index,
492 struct display_timing *config);
493
494/**
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900495 * ofnode_get_property()- - get a pointer to the value of a node property
Simon Glass9e512042017-05-18 20:08:58 -0600496 *
497 * @node: node to read
498 * @propname: property to read
499 * @lenp: place to put length on success
500 * @return pointer to property, or NULL if not found
501 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900502const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glass9e512042017-05-18 20:08:58 -0600503
504/**
505 * ofnode_is_available() - check if a node is marked available
506 *
507 * @node: node to check
508 * @return true if node's 'status' property is "okay" (or is missing)
509 */
510bool ofnode_is_available(ofnode node);
511
512/**
513 * ofnode_get_addr_size() - get address and size from a property
514 *
515 * This does no address translation. It simply reads an property that contains
516 * an address and a size value, one after the other.
517 *
518 * @node: node to read from
519 * @propname: property to read
520 * @sizep: place to put size value (on success)
521 * @return address value, or FDT_ADDR_T_NONE on error
522 */
523phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
524 phys_size_t *sizep);
525
526/**
527 * ofnode_read_u8_array_ptr() - find an 8-bit array
528 *
529 * Look up a property in a node and return a pointer to its contents as a
530 * byte array of given length. The property must have at least enough data
531 * for the array (count bytes). It may have more, but this will be ignored.
532 * The data is not copied.
533 *
534 * @node node to examine
535 * @propname name of property to find
536 * @sz number of array elements
537 * @return pointer to byte array if found, or NULL if the property is not
538 * found or there is not enough data
539 */
540const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
541 size_t sz);
542
543/**
544 * ofnode_read_pci_addr() - look up a PCI address
545 *
546 * Look at an address property in a node and return the PCI address which
547 * corresponds to the given type in the form of fdt_pci_addr.
548 * The property must hold one fdt_pci_addr with a lengh.
549 *
550 * @node node to examine
551 * @type pci address type (FDT_PCI_SPACE_xxx)
552 * @propname name of property to find
553 * @addr returns pci address in the form of fdt_pci_addr
554 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
555 * format of the property was invalid, -ENXIO if the requested
556 * address type was not found
557 */
558int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
559 const char *propname, struct fdt_pci_addr *addr);
560
561/**
562 * ofnode_read_addr_cells() - Get the number of address cells for a node
563 *
564 * This walks back up the tree to find the closest #address-cells property
565 * which controls the given node.
566 *
567 * @node: Node to check
568 * @return number of address cells this node uses
569 */
570int ofnode_read_addr_cells(ofnode node);
571
572/**
573 * ofnode_read_size_cells() - Get the number of size cells for a node
574 *
575 * This walks back up the tree to find the closest #size-cells property
576 * which controls the given node.
577 *
578 * @node: Node to check
579 * @return number of size cells this node uses
580 */
581int ofnode_read_size_cells(ofnode node);
582
583/**
Simon Glass878d68c2017-06-12 06:21:31 -0600584 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
585 *
586 * This function matches fdt_address_cells().
587 *
588 * @np: Node pointer to check
589 * @return value of #address-cells property in this node, or 2 if none
590 */
591int ofnode_read_simple_addr_cells(ofnode node);
592
593/**
594 * ofnode_read_simple_size_cells() - Get the size cells property in a node
595 *
596 * This function matches fdt_size_cells().
597 *
598 * @np: Node pointer to check
599 * @return value of #size-cells property in this node, or 2 if none
600 */
601int ofnode_read_simple_size_cells(ofnode node);
602
603/**
Simon Glass9e512042017-05-18 20:08:58 -0600604 * ofnode_pre_reloc() - check if a node should be bound before relocation
605 *
606 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
607 * via special device tree properties.
608 *
609 * Before relocation this function can be used to check if nodes are required
610 * in either SPL or TPL stages.
611 *
612 * After relocation and jumping into the real U-Boot binary it is possible to
613 * determine if a node was bound in one of SPL/TPL stages.
614 *
615 * There are 3 settings currently in use
616 * -
617 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
618 * Existing platforms only use it to indicate nodes needed in
619 * SPL. Should probably be replaced by u-boot,dm-spl for
620 * new platforms.
621 *
622 * @node: node to check
623 * @eturns true if node is needed in SPL/TL, false otherwise
624 */
625bool ofnode_pre_reloc(ofnode node);
626
Simon Glassdcf98852017-07-25 08:29:55 -0600627int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900628int ofnode_read_resource_byname(ofnode node, const char *name,
629 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -0600630
Simon Glass3991f422017-08-05 15:45:54 -0600631/**
632 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
633 *
634 * @node: child node (ofnode, lvalue)
635 * @parent: parent node (ofnode)
636 *
637 * This is a wrapper around a for loop and is used like so:
638 *
639 * ofnode node;
640 *
641 * ofnode_for_each_subnode(node, parent) {
642 * Use node
643 * ...
644 * }
645 *
646 * Note that this is implemented as a macro and @node is used as
647 * iterator in the loop. The parent variable can be a constant or even a
648 * literal.
649 */
650#define ofnode_for_each_subnode(node, parent) \
651 for (node = ofnode_first_subnode(parent); \
652 ofnode_valid(node); \
653 node = ofnode_next_subnode(node))
654
Simon Glass4984de22017-05-17 17:18:10 -0600655#endif