blob: e8b33c158d93e7b4d5191d98c9a11bfcc99139a1 [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 Glass4984de22017-05-17 17:18:10 -060018/**
19 * ofnode - reference to a device tree node
20 *
21 * This union can hold either a straightforward pointer to a struct device_node
22 * in the live device tree, or an offset within the flat device tree. In the
23 * latter case, the pointer value is just the integer offset within the flat DT.
24 *
25 * Thus we can reference nodes in both the live tree (once available) and the
26 * flat tree (until then). Functions are available to translate between an
27 * ofnode and either an offset or a struct device_node *.
28 *
29 * The reference can also hold a null offset, in which case the pointer value
Simon Glass9e512042017-05-18 20:08:58 -060030 * here is NULL. This corresponds to a struct device_node * value of
Simon Glass4984de22017-05-17 17:18:10 -060031 * NULL, or an offset of -1.
32 *
33 * There is no ambiguity as to whether ofnode holds an offset or a node
34 * pointer: when the live tree is active it holds a node pointer, otherwise it
35 * holds an offset. The value itself does not need to be unique and in theory
36 * the same value could point to a valid device node or a valid offset. We
37 * could arrange for a unique value to be used (e.g. by making the pointer
38 * point to an offset within the flat device tree in the case of an offset) but
39 * this increases code size slightly due to the subtraction. Since it offers no
40 * real benefit, the approach described here seems best.
41 *
42 * For now these points use constant types, since we don't allow writing
43 * the DT.
44 *
45 * @np: Pointer to device node, used for live tree
46 * @flat_ptr: Pointer into flat device tree, used for flat tree. Note that this
47 * is not a really a pointer to a node: it is an offset value. See above.
48 */
49typedef union ofnode_union {
50 const struct device_node *np; /* will be used for future live tree */
51 long of_offset;
52} ofnode;
53
Simon Glass9e512042017-05-18 20:08:58 -060054struct ofnode_phandle_args {
55 ofnode node;
56 int args_count;
57 uint32_t args[OF_MAX_PHANDLE_ARGS];
58};
59
60/**
61 * _ofnode_to_np() - convert an ofnode to a live DT node pointer
62 *
63 * This cannot be called if the reference contains an offset.
64 *
65 * @node: Reference containing struct device_node * (possibly invalid)
66 * @return pointer to device node (can be NULL)
67 */
68static inline const struct device_node *ofnode_to_np(ofnode node)
69{
70#ifdef OF_CHECKS
71 if (!of_live_active())
72 return NULL;
73#endif
74 return node.np;
75}
76
Simon Glass4984de22017-05-17 17:18:10 -060077/**
78 * ofnode_to_offset() - convert an ofnode to a flat DT offset
79 *
80 * This cannot be called if the reference contains a node pointer.
81 *
82 * @node: Reference containing offset (possibly invalid)
83 * @return DT offset (can be -1)
84 */
85static inline int ofnode_to_offset(ofnode node)
86{
Simon Glass9e512042017-05-18 20:08:58 -060087#ifdef OF_CHECKS
88 if (of_live_active())
89 return -1;
90#endif
Simon Glass4984de22017-05-17 17:18:10 -060091 return node.of_offset;
92}
93
94/**
95 * ofnode_valid() - check if an ofnode is valid
96 *
97 * @return true if the reference contains a valid ofnode, false if it is NULL
98 */
99static inline bool ofnode_valid(ofnode node)
100{
Simon Glass9e512042017-05-18 20:08:58 -0600101 if (of_live_active())
102 return node.np != NULL;
103 else
104 return node.of_offset != -1;
Simon Glass4984de22017-05-17 17:18:10 -0600105}
106
107/**
108 * offset_to_ofnode() - convert a DT offset to an ofnode
109 *
110 * @of_offset: DT offset (either valid, or -1)
111 * @return reference to the associated DT offset
112 */
113static inline ofnode offset_to_ofnode(int of_offset)
114{
115 ofnode node;
116
Simon Glass9e512042017-05-18 20:08:58 -0600117 if (of_live_active())
118 node.np = NULL;
119 else
120 node.of_offset = of_offset;
Simon Glass4984de22017-05-17 17:18:10 -0600121
122 return node;
123}
124
125/**
Simon Glass9e512042017-05-18 20:08:58 -0600126 * np_to_ofnode() - convert a node pointer to an ofnode
127 *
128 * @np: Live node pointer (can be NULL)
129 * @return reference to the associated node pointer
130 */
131static inline ofnode np_to_ofnode(const struct device_node *np)
132{
133 ofnode node;
134
135 node.np = np;
136
137 return node;
138}
139
140/**
141 * ofnode_is_np() - check if a reference is a node pointer
142 *
143 * This function associated that if there is a valid live tree then all
144 * references will use it. This is because using the flat DT when the live tree
145 * is valid is not permitted.
146 *
147 * @node: reference to check (possibly invalid)
148 * @return true if the reference is a live node pointer, false if it is a DT
149 * offset
150 */
151static inline bool ofnode_is_np(ofnode node)
152{
153#ifdef OF_CHECKS
154 /*
155 * Check our assumption that flat tree offsets are not used when a
156 * live tree is in use.
157 */
158 assert(!ofnode_valid(node) ||
159 (of_live_active() ? _ofnode_to_np(node)
160 : _ofnode_to_np(node)));
161#endif
162 return of_live_active() && ofnode_valid(node);
163}
164
165/**
Simon Glass4984de22017-05-17 17:18:10 -0600166 * ofnode_equal() - check if two references are equal
167 *
168 * @return true if equal, else false
169 */
170static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
171{
172 /* We only need to compare the contents */
173 return ref1.of_offset == ref2.of_offset;
174}
175
Simon Glass9e512042017-05-18 20:08:58 -0600176/**
177 * ofnode_null() - Obtain a null ofnode
178 *
179 * This returns an ofnode which points to no node. It works both with the flat
180 * tree and livetree.
181 */
182static inline ofnode ofnode_null(void)
183{
184 ofnode node;
185
186 if (of_live_active())
187 node.np = NULL;
188 else
189 node.of_offset = -1;
190
191 return node;
192}
193
194/**
195 * ofnode_read_u32() - Read a 32-bit integer from a property
196 *
197 * @ref: valid node reference to read property from
198 * @propname: name of the property to read from
199 * @outp: place to put value (if found)
200 * @return 0 if OK, -ve on error
201 */
202int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
203
204/**
205 * ofnode_read_s32() - Read a 32-bit integer from a property
206 *
207 * @ref: valid node reference to read property from
208 * @propname: name of the property to read from
209 * @outp: place to put value (if found)
210 * @return 0 if OK, -ve on error
211 */
212static inline int ofnode_read_s32(ofnode node, const char *propname,
213 s32 *out_value)
214{
215 return ofnode_read_u32(node, propname, (u32 *)out_value);
216}
217
218/**
219 * ofnode_read_u32_default() - Read a 32-bit integer from a property
220 *
221 * @ref: valid node reference to read property from
222 * @propname: name of the property to read from
223 * @def: default value to return if the property has no value
224 * @return property value, or @def if not found
225 */
226int ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
227
228/**
229 * ofnode_read_s32_default() - Read a 32-bit integer from a property
230 *
231 * @ref: valid node reference to read property from
232 * @propname: name of the property to read from
233 * @def: default value to return if the property has no value
234 * @return property value, or @def if not found
235 */
236int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
237
238/**
239 * ofnode_read_string() - Read a string from a property
240 *
241 * @ref: valid node reference to read property from
242 * @propname: name of the property to read
243 * @return string from property value, or NULL if there is no such property
244 */
245const char *ofnode_read_string(ofnode node, const char *propname);
246
247/**
248 * ofnode_read_u32_array - Find and read an array of 32 bit integers
249 *
250 * @node: valid node reference to read property from
251 * @propname: name of the property to read
252 * @out_values: pointer to return value, modified only if return value is 0
253 * @sz: number of array elements to read
254 *
255 * Search for a property in a device node and read 32-bit value(s) from
256 * it. Returns 0 on success, -EINVAL if the property does not exist,
257 * -ENODATA if property does not have a value, and -EOVERFLOW if the
258 * property data isn't large enough.
259 *
260 * The out_values is modified only if a valid u32 value can be decoded.
261 */
262int ofnode_read_u32_array(ofnode node, const char *propname,
263 u32 *out_values, size_t sz);
264
265/**
266 * ofnode_read_bool() - read a boolean value from a property
267 *
268 * @node: valid node reference to read property from
269 * @propname: name of property to read
270 * @return true if property is present (meaning true), false if not present
271 */
272bool ofnode_read_bool(ofnode node, const char *propname);
273
274/**
275 * ofnode_find_subnode() - find a named subnode of a parent node
276 *
277 * @node: valid reference to parent node
278 * @subnode_name: name of subnode to find
279 * @return reference to subnode (which can be invalid if there is no such
280 * subnode)
281 */
282ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
283
284/**
285 * ofnode_first_subnode() - find the first subnode of a parent node
286 *
287 * @node: valid reference to a valid parent node
288 * @return reference to the first subnode (which can be invalid if the parent
289 * node has no subnodes)
290 */
291ofnode ofnode_first_subnode(ofnode node);
292
293/**
294 * ofnode_next_subnode() - find the next sibling of a subnode
295 *
296 * @node: valid reference to previous node (sibling)
297 * @return reference to the next subnode (which can be invalid if the node
298 * has no more siblings)
299 */
300ofnode ofnode_next_subnode(ofnode node);
301
302/**
303 * ofnode_get_name() - get the name of a node
304 *
305 * @node: valid node to look up
306 * @return name or node
307 */
308const char *ofnode_get_name(ofnode node);
309
310/**
311 * ofnode_read_size() - read the size of a property
312 *
313 * @node: node to check
314 * @propname: property to check
315 * @return size of property if present, or -EINVAL if not
316 */
317int ofnode_read_size(ofnode node, const char *propname);
318
319/**
320 * ofnode_stringlist_search() - find a string in a string list and return index
321 *
322 * Note that it is possible for this function to succeed on property values
323 * that are not NUL-terminated. That's because the function will stop after
324 * finding the first occurrence of @string. This can for example happen with
325 * small-valued cell properties, such as #address-cells, when searching for
326 * the empty string.
327 *
328 * @node: node to check
329 * @propname: name of the property containing the string list
330 * @string: string to look up in the string list
331 *
332 * @return:
333 * the index of the string in the list of strings
334 * -ENODATA if the property is not found
335 * -EINVAL on some other error
336 */
337int ofnode_stringlist_search(ofnode node, const char *propname,
338 const char *string);
339
340/**
341 * fdt_stringlist_get() - obtain the string at a given index in a string list
342 *
343 * Note that this will successfully extract strings from properties with
344 * non-NUL-terminated values. For example on small-valued cell properties
345 * this function will return the empty string.
346 *
347 * If non-NULL, the length of the string (on success) or a negative error-code
348 * (on failure) will be stored in the integer pointer to by lenp.
349 *
350 * @node: node to check
351 * @propname: name of the property containing the string list
352 * @index: index of the string to return
353 * @lenp: return location for the string length or an error code on failure
354 *
355 * @return:
356 * length of string, if found or -ve error value if not found
357 */
358int ofnode_read_string_index(ofnode node, const char *propname, int index,
359 const char **outp);
360
361/**
362 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
363 *
364 * This function is useful to parse lists of phandles and their arguments.
365 * Returns 0 on success and fills out_args, on error returns appropriate
366 * errno value.
367 *
368 * Caller is responsible to call of_node_put() on the returned out_args->np
369 * pointer.
370 *
371 * Example:
372 *
373 * phandle1: node1 {
374 * #list-cells = <2>;
375 * }
376 *
377 * phandle2: node2 {
378 * #list-cells = <1>;
379 * }
380 *
381 * node3 {
382 * list = <&phandle1 1 2 &phandle2 3>;
383 * }
384 *
385 * To get a device_node of the `node2' node you may call this:
386 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
387 *
388 * @node: device tree node containing a list
389 * @list_name: property name that contains a list
390 * @cells_name: property name that specifies phandles' arguments count
391 * @cells_count: Cell count to use if @cells_name is NULL
392 * @index: index of a phandle to parse out
393 * @out_args: optional pointer to output arguments structure (will be filled)
394 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
395 * @list_name does not exist, -EINVAL if a phandle was not found,
396 * @cells_name could not be found, the arguments were truncated or there
397 * were too many arguments.
398 */
399int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
400 const char *cells_name, int cell_count,
401 int index,
402 struct ofnode_phandle_args *out_args);
403
404/**
405 * ofnode_path() - find a node by full path
406 *
407 * @path: Full path to node, e.g. "/bus/spi@1"
408 * @return reference to the node found. Use ofnode_valid() to check if it exists
409 */
410ofnode ofnode_path(const char *path);
411
412/**
413 * ofnode_get_chosen_prop() - get the value of a chosen property
414 *
415 * This looks for a property within the /chosen node and returns its value
416 *
417 * @propname: Property name to look for
418 */
419const char *ofnode_get_chosen_prop(const char *propname);
420
421/**
422 * ofnode_get_chosen_node() - get the chosen node
423 *
424 * @return the chosen node if present, else ofnode_null()
425 */
426ofnode ofnode_get_chosen_node(const char *name);
427
428struct display_timing;
429/**
430 * ofnode_decode_display_timing() - decode display timings
431 *
432 * Decode display timings from the supplied 'display-timings' node.
433 * See doc/device-tree-bindings/video/display-timing.txt for binding
434 * information.
435 *
436 * @node 'display-timing' node containing the timing subnodes
437 * @index Index number to read (0=first timing subnode)
438 * @config Place to put timings
439 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
440 */
441int ofnode_decode_display_timing(ofnode node, int index,
442 struct display_timing *config);
443
444/**
445 * ofnode_read_prop()- - read a node property
446 *
447 * @node: node to read
448 * @propname: property to read
449 * @lenp: place to put length on success
450 * @return pointer to property, or NULL if not found
451 */
452const u32 *ofnode_read_prop(ofnode node, const char *propname, int *lenp);
453
454/**
455 * ofnode_is_available() - check if a node is marked available
456 *
457 * @node: node to check
458 * @return true if node's 'status' property is "okay" (or is missing)
459 */
460bool ofnode_is_available(ofnode node);
461
462/**
463 * ofnode_get_addr_size() - get address and size from a property
464 *
465 * This does no address translation. It simply reads an property that contains
466 * an address and a size value, one after the other.
467 *
468 * @node: node to read from
469 * @propname: property to read
470 * @sizep: place to put size value (on success)
471 * @return address value, or FDT_ADDR_T_NONE on error
472 */
473phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
474 phys_size_t *sizep);
475
476/**
477 * ofnode_read_u8_array_ptr() - find an 8-bit array
478 *
479 * Look up a property in a node and return a pointer to its contents as a
480 * byte array of given length. The property must have at least enough data
481 * for the array (count bytes). It may have more, but this will be ignored.
482 * The data is not copied.
483 *
484 * @node node to examine
485 * @propname name of property to find
486 * @sz number of array elements
487 * @return pointer to byte array if found, or NULL if the property is not
488 * found or there is not enough data
489 */
490const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
491 size_t sz);
492
493/**
494 * ofnode_read_pci_addr() - look up a PCI address
495 *
496 * Look at an address property in a node and return the PCI address which
497 * corresponds to the given type in the form of fdt_pci_addr.
498 * The property must hold one fdt_pci_addr with a lengh.
499 *
500 * @node node to examine
501 * @type pci address type (FDT_PCI_SPACE_xxx)
502 * @propname name of property to find
503 * @addr returns pci address in the form of fdt_pci_addr
504 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
505 * format of the property was invalid, -ENXIO if the requested
506 * address type was not found
507 */
508int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
509 const char *propname, struct fdt_pci_addr *addr);
510
511/**
512 * ofnode_read_addr_cells() - Get the number of address cells for a node
513 *
514 * This walks back up the tree to find the closest #address-cells property
515 * which controls the given node.
516 *
517 * @node: Node to check
518 * @return number of address cells this node uses
519 */
520int ofnode_read_addr_cells(ofnode node);
521
522/**
523 * ofnode_read_size_cells() - Get the number of size cells for a node
524 *
525 * This walks back up the tree to find the closest #size-cells property
526 * which controls the given node.
527 *
528 * @node: Node to check
529 * @return number of size cells this node uses
530 */
531int ofnode_read_size_cells(ofnode node);
532
533/**
534 * ofnode_pre_reloc() - check if a node should be bound before relocation
535 *
536 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
537 * via special device tree properties.
538 *
539 * Before relocation this function can be used to check if nodes are required
540 * in either SPL or TPL stages.
541 *
542 * After relocation and jumping into the real U-Boot binary it is possible to
543 * determine if a node was bound in one of SPL/TPL stages.
544 *
545 * There are 3 settings currently in use
546 * -
547 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
548 * Existing platforms only use it to indicate nodes needed in
549 * SPL. Should probably be replaced by u-boot,dm-spl for
550 * new platforms.
551 *
552 * @node: node to check
553 * @eturns true if node is needed in SPL/TL, false otherwise
554 */
555bool ofnode_pre_reloc(ofnode node);
556
Simon Glass4984de22017-05-17 17:18:10 -0600557#endif