xref: /openbmc/u-boot/include/dm/of.h (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
25e060d8bSSimon Glass /*
35e060d8bSSimon Glass  * Copyright (c) 2017 Google, Inc
45e060d8bSSimon Glass  * Written by Simon Glass <sjg@chromium.org>
55e060d8bSSimon Glass  */
65e060d8bSSimon Glass 
75e060d8bSSimon Glass #ifndef _DM_OF_H
85e060d8bSSimon Glass #define _DM_OF_H
95e060d8bSSimon Glass 
105e060d8bSSimon Glass #include <asm/u-boot.h>
115e060d8bSSimon Glass #include <asm/global_data.h>
125e060d8bSSimon Glass 
135e060d8bSSimon Glass /* integer value within a device tree property which references another node */
145e060d8bSSimon Glass typedef u32 phandle;
155e060d8bSSimon Glass 
165e060d8bSSimon Glass /**
175e060d8bSSimon Glass  * struct property: Device tree property
185e060d8bSSimon Glass  *
195e060d8bSSimon Glass  * @name: Property name
205e060d8bSSimon Glass  * @length: Length of property in bytes
215e060d8bSSimon Glass  * @value: Pointer to property value
225e060d8bSSimon Glass  * @next: Pointer to next property, or NULL if none
235e060d8bSSimon Glass  */
245e060d8bSSimon Glass struct property {
255e060d8bSSimon Glass 	char *name;
265e060d8bSSimon Glass 	int length;
275e060d8bSSimon Glass 	void *value;
285e060d8bSSimon Glass 	struct property *next;
295e060d8bSSimon Glass };
305e060d8bSSimon Glass 
315e060d8bSSimon Glass /**
325e060d8bSSimon Glass  * struct device_node: Device tree node
335e060d8bSSimon Glass  *
345e060d8bSSimon Glass  * @name: Node name
355e060d8bSSimon Glass  * @type: Node type (value of device_type property) or "<NULL>" if none
365e060d8bSSimon Glass  * @phandle: Phandle value of this none, or 0 if none
375e060d8bSSimon Glass  * @full_name: Full path to node, e.g. "/bus@1/spi@1100"
385e060d8bSSimon Glass  * @properties: Pointer to head of list of properties, or NULL if none
395e060d8bSSimon Glass  * @parent: Pointer to parent node, or NULL if this is the root node
405e060d8bSSimon Glass  * @child: Pointer to head of child node list, or NULL if no children
415e060d8bSSimon Glass  * @sibling: Pointer to the next sibling node, or NULL if this is the last
425e060d8bSSimon Glass  */
435e060d8bSSimon Glass struct device_node {
445e060d8bSSimon Glass 	const char *name;
455e060d8bSSimon Glass 	const char *type;
465e060d8bSSimon Glass 	phandle phandle;
475e060d8bSSimon Glass 	const char *full_name;
485e060d8bSSimon Glass 
495e060d8bSSimon Glass 	struct property *properties;
505e060d8bSSimon Glass 	struct device_node *parent;
515e060d8bSSimon Glass 	struct device_node *child;
525e060d8bSSimon Glass 	struct device_node *sibling;
535e060d8bSSimon Glass };
545e060d8bSSimon Glass 
555e060d8bSSimon Glass #define OF_MAX_PHANDLE_ARGS 16
565e060d8bSSimon Glass 
575e060d8bSSimon Glass /**
585e060d8bSSimon Glass  * struct of_phandle_args - structure to hold phandle and arguments
595e060d8bSSimon Glass  *
605e060d8bSSimon Glass  * This is used when decoding a phandle in a device tree property. Typically
615e060d8bSSimon Glass  * these look like this:
625e060d8bSSimon Glass  *
635e060d8bSSimon Glass  * wibble {
645e060d8bSSimon Glass  *    phandle = <5>;
655e060d8bSSimon Glass  * };
665e060d8bSSimon Glass  *
675e060d8bSSimon Glass  * ...
685e060d8bSSimon Glass  * some-prop = <&wibble 1 2 3>
695e060d8bSSimon Glass  *
705e060d8bSSimon Glass  * Here &node is the phandle of the node 'wibble', i.e. 5. There are three
715e060d8bSSimon Glass  * arguments: 1, 2, 3.
725e060d8bSSimon Glass  *
735e060d8bSSimon Glass  * So when decoding the phandle in some-prop, np will point to wibble,
745e060d8bSSimon Glass  * args_count will be 3 and the three arguments will be in args.
755e060d8bSSimon Glass  *
765e060d8bSSimon Glass  * @np: Node that the phandle refers to
775e060d8bSSimon Glass  * @args_count: Number of arguments
785e060d8bSSimon Glass  * @args: Argument values
795e060d8bSSimon Glass  */
805e060d8bSSimon Glass struct of_phandle_args {
815e060d8bSSimon Glass 	struct device_node *np;
825e060d8bSSimon Glass 	int args_count;
835e060d8bSSimon Glass 	uint32_t args[OF_MAX_PHANDLE_ARGS];
845e060d8bSSimon Glass };
855e060d8bSSimon Glass 
865e060d8bSSimon Glass DECLARE_GLOBAL_DATA_PTR;
875e060d8bSSimon Glass 
885e060d8bSSimon Glass /**
895e060d8bSSimon Glass  * of_live_active() - check if livetree is active
905e060d8bSSimon Glass  *
915e060d8bSSimon Glass  * @returns true if livetree is active, false it not
925e060d8bSSimon Glass  */
935e060d8bSSimon Glass #ifdef CONFIG_OF_LIVE
of_live_active(void)945e060d8bSSimon Glass static inline bool of_live_active(void)
955e060d8bSSimon Glass {
965e060d8bSSimon Glass 	return gd->of_root != NULL;
975e060d8bSSimon Glass }
985e060d8bSSimon Glass #else
of_live_active(void)995e060d8bSSimon Glass static inline bool of_live_active(void)
1005e060d8bSSimon Glass {
1015e060d8bSSimon Glass 	return false;
1025e060d8bSSimon Glass }
1035e060d8bSSimon Glass #endif
1045e060d8bSSimon Glass 
105a4b8e372SSimon Glass #define OF_BAD_ADDR	((u64)-1)
106a4b8e372SSimon Glass 
of_node_full_name(const struct device_node * np)107a4b8e372SSimon Glass static inline const char *of_node_full_name(const struct device_node *np)
108a4b8e372SSimon Glass {
109a4b8e372SSimon Glass 	return np ? np->full_name : "<no-node>";
110a4b8e372SSimon Glass }
111a4b8e372SSimon Glass 
112a4b8e372SSimon Glass /* Default #address and #size cells */
113a4b8e372SSimon Glass #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
114a4b8e372SSimon Glass #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
115a4b8e372SSimon Glass #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
116a4b8e372SSimon Glass #endif
117a4b8e372SSimon Glass 
118a4b8e372SSimon Glass /* Default string compare functions */
119a4b8e372SSimon Glass #if !defined(of_compat_cmp)
120a4b8e372SSimon Glass #define of_compat_cmp(s1, s2, l)	strcasecmp((s1), (s2))
121a4b8e372SSimon Glass #define of_prop_cmp(s1, s2)		strcmp((s1), (s2))
122a4b8e372SSimon Glass #define of_node_cmp(s1, s2)		strcasecmp((s1), (s2))
123a4b8e372SSimon Glass #endif
124a4b8e372SSimon Glass 
125a4b8e372SSimon Glass /* Helper to read a big number; size is in cells (not bytes) */
of_read_number(const __be32 * cell,int size)126a4b8e372SSimon Glass static inline u64 of_read_number(const __be32 *cell, int size)
127a4b8e372SSimon Glass {
128a4b8e372SSimon Glass 	u64 r = 0;
129a4b8e372SSimon Glass 	while (size--)
130a4b8e372SSimon Glass 		r = (r << 32) | be32_to_cpu(*(cell++));
131a4b8e372SSimon Glass 	return r;
132a4b8e372SSimon Glass }
133a4b8e372SSimon Glass 
134a4b8e372SSimon Glass /* Like of_read_number, but we want an unsigned long result */
of_read_ulong(const __be32 * cell,int size)135a4b8e372SSimon Glass static inline unsigned long of_read_ulong(const __be32 *cell, int size)
136a4b8e372SSimon Glass {
137a4b8e372SSimon Glass 	/* toss away upper bits if unsigned long is smaller than u64 */
138a4b8e372SSimon Glass 	return of_read_number(cell, size);
139a4b8e372SSimon Glass }
140a4b8e372SSimon Glass 
1415e060d8bSSimon Glass #endif
142