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