xref: /OK3568_Linux_fs/u-boot/include/dm/of.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2017 Google, Inc
3*4882a593Smuzhiyun  * Written by Simon Glass <sjg@chromium.org>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifndef _DM_OF_H
9*4882a593Smuzhiyun #define _DM_OF_H
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <asm/u-boot.h>
12*4882a593Smuzhiyun #include <asm/global_data.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /* integer value within a device tree property which references another node */
15*4882a593Smuzhiyun typedef u32 phandle;
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * struct property: Device tree property
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * @name: Property name
21*4882a593Smuzhiyun  * @length: Length of property in bytes
22*4882a593Smuzhiyun  * @value: Pointer to property value
23*4882a593Smuzhiyun  * @next: Pointer to next property, or NULL if none
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun struct property {
26*4882a593Smuzhiyun 	char *name;
27*4882a593Smuzhiyun 	int length;
28*4882a593Smuzhiyun 	void *value;
29*4882a593Smuzhiyun 	struct property *next;
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /**
33*4882a593Smuzhiyun  * struct device_node: Device tree node
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * @name: Node name
36*4882a593Smuzhiyun  * @type: Node type (value of device_type property) or "<NULL>" if none
37*4882a593Smuzhiyun  * @phandle: Phandle value of this none, or 0 if none
38*4882a593Smuzhiyun  * @full_name: Full path to node, e.g. "/bus@1/spi@1100"
39*4882a593Smuzhiyun  * @properties: Pointer to head of list of properties, or NULL if none
40*4882a593Smuzhiyun  * @parent: Pointer to parent node, or NULL if this is the root node
41*4882a593Smuzhiyun  * @child: Pointer to head of child node list, or NULL if no children
42*4882a593Smuzhiyun  * @sibling: Pointer to the next sibling node, or NULL if this is the last
43*4882a593Smuzhiyun  */
44*4882a593Smuzhiyun struct device_node {
45*4882a593Smuzhiyun 	const char *name;
46*4882a593Smuzhiyun 	const char *type;
47*4882a593Smuzhiyun 	phandle phandle;
48*4882a593Smuzhiyun 	const char *full_name;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	struct property *properties;
51*4882a593Smuzhiyun 	struct device_node *parent;
52*4882a593Smuzhiyun 	struct device_node *child;
53*4882a593Smuzhiyun 	struct device_node *sibling;
54*4882a593Smuzhiyun };
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define OF_MAX_PHANDLE_ARGS 16
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /**
59*4882a593Smuzhiyun  * struct of_phandle_args - structure to hold phandle and arguments
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * This is used when decoding a phandle in a device tree property. Typically
62*4882a593Smuzhiyun  * these look like this:
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * wibble {
65*4882a593Smuzhiyun  *    phandle = <5>;
66*4882a593Smuzhiyun  * };
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * ...
69*4882a593Smuzhiyun  * some-prop = <&wibble 1 2 3>
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * Here &node is the phandle of the node 'wibble', i.e. 5. There are three
72*4882a593Smuzhiyun  * arguments: 1, 2, 3.
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * So when decoding the phandle in some-prop, np will point to wibble,
75*4882a593Smuzhiyun  * args_count will be 3 and the three arguments will be in args.
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * @np: Node that the phandle refers to
78*4882a593Smuzhiyun  * @args_count: Number of arguments
79*4882a593Smuzhiyun  * @args: Argument values
80*4882a593Smuzhiyun  */
81*4882a593Smuzhiyun struct of_phandle_args {
82*4882a593Smuzhiyun 	struct device_node *np;
83*4882a593Smuzhiyun 	int args_count;
84*4882a593Smuzhiyun 	uint32_t args[OF_MAX_PHANDLE_ARGS];
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /**
90*4882a593Smuzhiyun  * of_live_active() - check if livetree is active
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  * @returns true if livetree is active, false it not
93*4882a593Smuzhiyun  */
94*4882a593Smuzhiyun #ifdef CONFIG_OF_LIVE
of_live_active(void)95*4882a593Smuzhiyun static inline bool of_live_active(void)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	return gd->of_root != NULL;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun #else
of_live_active(void)100*4882a593Smuzhiyun static inline bool of_live_active(void)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	return false;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun #endif
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun #define OF_BAD_ADDR	((u64)-1)
107*4882a593Smuzhiyun 
of_node_full_name(const struct device_node * np)108*4882a593Smuzhiyun static inline const char *of_node_full_name(const struct device_node *np)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	return np ? np->full_name : "<no-node>";
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /* Default #address and #size cells */
114*4882a593Smuzhiyun #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
115*4882a593Smuzhiyun #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
116*4882a593Smuzhiyun #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
117*4882a593Smuzhiyun #endif
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /* Default string compare functions */
120*4882a593Smuzhiyun #if !defined(of_compat_cmp)
121*4882a593Smuzhiyun #define of_compat_cmp(s1, s2, l)	strcasecmp((s1), (s2))
122*4882a593Smuzhiyun #define of_prop_cmp(s1, s2)		strcmp((s1), (s2))
123*4882a593Smuzhiyun #define of_node_cmp(s1, s2)		strcasecmp((s1), (s2))
124*4882a593Smuzhiyun #endif
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /* Helper to read a big number; size is in cells (not bytes) */
of_read_number(const __be32 * cell,int size)127*4882a593Smuzhiyun static inline u64 of_read_number(const __be32 *cell, int size)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	u64 r = 0;
130*4882a593Smuzhiyun 	while (size--)
131*4882a593Smuzhiyun 		r = (r << 32) | be32_to_cpu(*(cell++));
132*4882a593Smuzhiyun 	return r;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun /* Like of_read_number, but we want an unsigned long result */
of_read_ulong(const __be32 * cell,int size)136*4882a593Smuzhiyun static inline unsigned long of_read_ulong(const __be32 *cell, int size)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	/* toss away upper bits if unsigned long is smaller than u64 */
139*4882a593Smuzhiyun 	return of_read_number(cell, size);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun #endif
143