xref: /rk3399_rockchip-uboot/include/fdtdec.h (revision 3ddecfc74086aa185a2f671cc07cb826b72d35f0)
1b5220bc6SSimon Glass /*
2b5220bc6SSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
3b5220bc6SSimon Glass  * See file CREDITS for list of people who contributed to this
4b5220bc6SSimon Glass  * project.
5b5220bc6SSimon Glass  *
6b5220bc6SSimon Glass  * This program is free software; you can redistribute it and/or
7b5220bc6SSimon Glass  * modify it under the terms of the GNU General Public License as
8b5220bc6SSimon Glass  * published by the Free Software Foundation; either version 2 of
9b5220bc6SSimon Glass  * the License, or (at your option) any later version.
10b5220bc6SSimon Glass  *
11b5220bc6SSimon Glass  * This program is distributed in the hope that it will be useful,
12b5220bc6SSimon Glass  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13b5220bc6SSimon Glass  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14b5220bc6SSimon Glass  * GNU General Public License for more details.
15b5220bc6SSimon Glass  *
16b5220bc6SSimon Glass  * You should have received a copy of the GNU General Public License
17b5220bc6SSimon Glass  * along with this program; if not, write to the Free Software
18b5220bc6SSimon Glass  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19b5220bc6SSimon Glass  * MA 02111-1307 USA
20b5220bc6SSimon Glass  */
21b5220bc6SSimon Glass 
22b5220bc6SSimon Glass 
23b5220bc6SSimon Glass /*
24b5220bc6SSimon Glass  * This file contains convenience functions for decoding useful and
25b5220bc6SSimon Glass  * enlightening information from FDTs. It is intended to be used by device
26b5220bc6SSimon Glass  * drivers and board-specific code within U-Boot. It aims to reduce the
27b5220bc6SSimon Glass  * amount of FDT munging required within U-Boot itself, so that driver code
28b5220bc6SSimon Glass  * changes to support FDT are minimized.
29b5220bc6SSimon Glass  */
30b5220bc6SSimon Glass 
31b5220bc6SSimon Glass #include <libfdt.h>
32b5220bc6SSimon Glass 
33b5220bc6SSimon Glass /*
34b5220bc6SSimon Glass  * A typedef for a physical address. Note that fdt data is always big
35b5220bc6SSimon Glass  * endian even on a litle endian machine.
36b5220bc6SSimon Glass  */
37b5220bc6SSimon Glass #ifdef CONFIG_PHYS_64BIT
38b5220bc6SSimon Glass typedef u64 fdt_addr_t;
39b5220bc6SSimon Glass #define FDT_ADDR_T_NONE (-1ULL)
40b5220bc6SSimon Glass #define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
41b5220bc6SSimon Glass #else
42b5220bc6SSimon Glass typedef u32 fdt_addr_t;
43b5220bc6SSimon Glass #define FDT_ADDR_T_NONE (-1U)
44b5220bc6SSimon Glass #define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
45b5220bc6SSimon Glass #endif
46b5220bc6SSimon Glass 
47b5220bc6SSimon Glass /* Information obtained about memory from the FDT */
48b5220bc6SSimon Glass struct fdt_memory {
49b5220bc6SSimon Glass 	fdt_addr_t start;
50b5220bc6SSimon Glass 	fdt_addr_t end;
51b5220bc6SSimon Glass };
52b5220bc6SSimon Glass 
53b5220bc6SSimon Glass /**
54b5220bc6SSimon Glass  * Compat types that we know about and for which we might have drivers.
55b5220bc6SSimon Glass  * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
56b5220bc6SSimon Glass  * within drivers.
57b5220bc6SSimon Glass  */
58b5220bc6SSimon Glass enum fdt_compat_id {
59b5220bc6SSimon Glass 	COMPAT_UNKNOWN,
6087f938c9SSimon Glass 	COMPAT_NVIDIA_TEGRA20_USB,	/* Tegra2 USB port */
6196a78ac0SYen Lin 	COMPAT_NVIDIA_TEGRA20_I2C,	/* Tegra2 i2c */
6296a78ac0SYen Lin 	COMPAT_NVIDIA_TEGRA20_DVC,	/* Tegra2 dvc (really just i2c) */
63b5220bc6SSimon Glass 
64b5220bc6SSimon Glass 	COMPAT_COUNT,
65b5220bc6SSimon Glass };
66b5220bc6SSimon Glass 
67ed3ee5cdSSimon Glass /* GPIOs are numbered from 0 */
68ed3ee5cdSSimon Glass enum {
69ed3ee5cdSSimon Glass 	FDT_GPIO_NONE = -1U,	/* an invalid GPIO used to end our list */
70ed3ee5cdSSimon Glass 
71ed3ee5cdSSimon Glass 	FDT_GPIO_ACTIVE_LOW = 1 << 0,	/* input is active low (else high) */
72ed3ee5cdSSimon Glass };
73ed3ee5cdSSimon Glass 
74ed3ee5cdSSimon Glass /* This is the state of a GPIO pin as defined by the fdt */
75ed3ee5cdSSimon Glass struct fdt_gpio_state {
76ed3ee5cdSSimon Glass 	const char *name;	/* name of the fdt property defining this */
77ed3ee5cdSSimon Glass 	uint gpio;		/* GPIO number, or FDT_GPIO_NONE if none */
78ed3ee5cdSSimon Glass 	u8 flags;		/* FDT_GPIO_... flags */
79ed3ee5cdSSimon Glass };
80ed3ee5cdSSimon Glass 
81ed3ee5cdSSimon Glass /* This tells us whether a fdt_gpio_state record is valid or not */
82ed3ee5cdSSimon Glass #define fdt_gpio_isvalid(x) ((x)->gpio != FDT_GPIO_NONE)
83ed3ee5cdSSimon Glass 
84b5220bc6SSimon Glass /**
85b5220bc6SSimon Glass  * Find the next numbered alias for a peripheral. This is used to enumerate
86b5220bc6SSimon Glass  * all the peripherals of a certain type.
87b5220bc6SSimon Glass  *
88b5220bc6SSimon Glass  * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
89b5220bc6SSimon Glass  * this function will return a pointer to the node the alias points to, and
90b5220bc6SSimon Glass  * then update *upto to 1. Next time you call this function, the next node
91b5220bc6SSimon Glass  * will be returned.
92b5220bc6SSimon Glass  *
93b5220bc6SSimon Glass  * All nodes returned will match the compatible ID, as it is assumed that
94b5220bc6SSimon Glass  * all peripherals use the same driver.
95b5220bc6SSimon Glass  *
96b5220bc6SSimon Glass  * @param blob		FDT blob to use
97b5220bc6SSimon Glass  * @param name		Root name of alias to search for
98b5220bc6SSimon Glass  * @param id		Compatible ID to look for
99b5220bc6SSimon Glass  * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
100b5220bc6SSimon Glass  */
101b5220bc6SSimon Glass int fdtdec_next_alias(const void *blob, const char *name,
102b5220bc6SSimon Glass 		enum fdt_compat_id id, int *upto);
103b5220bc6SSimon Glass 
104b5220bc6SSimon Glass /**
105f88fe2deSSimon Glass  * Find the next compatible node for a peripheral.
106f88fe2deSSimon Glass  *
107f88fe2deSSimon Glass  * Do the first call with node = 0. This function will return a pointer to
108f88fe2deSSimon Glass  * the next compatible node. Next time you call this function, pass the
109f88fe2deSSimon Glass  * value returned, and the next node will be provided.
110f88fe2deSSimon Glass  *
111f88fe2deSSimon Glass  * @param blob		FDT blob to use
112f88fe2deSSimon Glass  * @param node		Start node for search
113f88fe2deSSimon Glass  * @param id		Compatible ID to look for (enum fdt_compat_id)
114f88fe2deSSimon Glass  * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
115f88fe2deSSimon Glass  */
116f88fe2deSSimon Glass int fdtdec_next_compatible(const void *blob, int node,
117f88fe2deSSimon Glass 		enum fdt_compat_id id);
118f88fe2deSSimon Glass 
119f88fe2deSSimon Glass /**
120*3ddecfc7SSimon Glass  * Find the next compatible subnode for a peripheral.
121*3ddecfc7SSimon Glass  *
122*3ddecfc7SSimon Glass  * Do the first call with node set to the parent and depth = 0. This
123*3ddecfc7SSimon Glass  * function will return the offset of the next compatible node. Next time
124*3ddecfc7SSimon Glass  * you call this function, pass the node value returned last time, with
125*3ddecfc7SSimon Glass  * depth unchanged, and the next node will be provided.
126*3ddecfc7SSimon Glass  *
127*3ddecfc7SSimon Glass  * @param blob		FDT blob to use
128*3ddecfc7SSimon Glass  * @param node		Start node for search
129*3ddecfc7SSimon Glass  * @param id		Compatible ID to look for (enum fdt_compat_id)
130*3ddecfc7SSimon Glass  * @param depthp	Current depth (set to 0 before first call)
131*3ddecfc7SSimon Glass  * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
132*3ddecfc7SSimon Glass  */
133*3ddecfc7SSimon Glass int fdtdec_next_compatible_subnode(const void *blob, int node,
134*3ddecfc7SSimon Glass 		enum fdt_compat_id id, int *depthp);
135*3ddecfc7SSimon Glass 
136*3ddecfc7SSimon Glass /**
137b5220bc6SSimon Glass  * Look up an address property in a node and return it as an address.
138b5220bc6SSimon Glass  * The property must hold either one address with no trailing data or
139b5220bc6SSimon Glass  * one address with a length. This is only tested on 32-bit machines.
140b5220bc6SSimon Glass  *
141b5220bc6SSimon Glass  * @param blob	FDT blob
142b5220bc6SSimon Glass  * @param node	node to examine
143b5220bc6SSimon Glass  * @param prop_name	name of property to find
144b5220bc6SSimon Glass  * @return address, if found, or FDT_ADDR_T_NONE if not
145b5220bc6SSimon Glass  */
146b5220bc6SSimon Glass fdt_addr_t fdtdec_get_addr(const void *blob, int node,
147b5220bc6SSimon Glass 		const char *prop_name);
148b5220bc6SSimon Glass 
149b5220bc6SSimon Glass /**
150b5220bc6SSimon Glass  * Look up a 32-bit integer property in a node and return it. The property
151b5220bc6SSimon Glass  * must have at least 4 bytes of data. The value of the first cell is
152b5220bc6SSimon Glass  * returned.
153b5220bc6SSimon Glass  *
154b5220bc6SSimon Glass  * @param blob	FDT blob
155b5220bc6SSimon Glass  * @param node	node to examine
156b5220bc6SSimon Glass  * @param prop_name	name of property to find
157b5220bc6SSimon Glass  * @param default_val	default value to return if the property is not found
158b5220bc6SSimon Glass  * @return integer value, if found, or default_val if not
159b5220bc6SSimon Glass  */
160b5220bc6SSimon Glass s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
161b5220bc6SSimon Glass 		s32 default_val);
162b5220bc6SSimon Glass 
163b5220bc6SSimon Glass /**
164b5220bc6SSimon Glass  * Checks whether a node is enabled.
165b5220bc6SSimon Glass  * This looks for a 'status' property. If this exists, then returns 1 if
166b5220bc6SSimon Glass  * the status is 'ok' and 0 otherwise. If there is no status property,
167f88fe2deSSimon Glass  * it returns 1 on the assumption that anything mentioned should be enabled
168f88fe2deSSimon Glass  * by default.
169b5220bc6SSimon Glass  *
170b5220bc6SSimon Glass  * @param blob	FDT blob
171b5220bc6SSimon Glass  * @param node	node to examine
172f88fe2deSSimon Glass  * @return integer value 0 (not enabled) or 1 (enabled)
173b5220bc6SSimon Glass  */
174f88fe2deSSimon Glass int fdtdec_get_is_enabled(const void *blob, int node);
175b5220bc6SSimon Glass 
176b5220bc6SSimon Glass /**
1779a263e55SSimon Glass  * Make sure we have a valid fdt available to control U-Boot.
1789a263e55SSimon Glass  *
1799a263e55SSimon Glass  * If not, a message is printed to the console if the console is ready.
1809a263e55SSimon Glass  *
1819a263e55SSimon Glass  * @return 0 if all ok, -1 if not
1829a263e55SSimon Glass  */
1839a263e55SSimon Glass int fdtdec_prepare_fdt(void);
1849a263e55SSimon Glass 
1859a263e55SSimon Glass /**
1869a263e55SSimon Glass  * Checks that we have a valid fdt available to control U-Boot.
1879a263e55SSimon Glass 
1889a263e55SSimon Glass  * However, if not then for the moment nothing is done, since this function
1899a263e55SSimon Glass  * is called too early to panic().
1909a263e55SSimon Glass  *
1919a263e55SSimon Glass  * @returns 0
192b5220bc6SSimon Glass  */
193b5220bc6SSimon Glass int fdtdec_check_fdt(void);
194a53f4a29SSimon Glass 
195a53f4a29SSimon Glass /**
196a53f4a29SSimon Glass  * Find the nodes for a peripheral and return a list of them in the correct
197a53f4a29SSimon Glass  * order. This is used to enumerate all the peripherals of a certain type.
198a53f4a29SSimon Glass  *
199a53f4a29SSimon Glass  * To use this, optionally set up a /aliases node with alias properties for
200a53f4a29SSimon Glass  * a peripheral. For example, for usb you could have:
201a53f4a29SSimon Glass  *
202a53f4a29SSimon Glass  * aliases {
203a53f4a29SSimon Glass  *		usb0 = "/ehci@c5008000";
204a53f4a29SSimon Glass  *		usb1 = "/ehci@c5000000";
205a53f4a29SSimon Glass  * };
206a53f4a29SSimon Glass  *
207a53f4a29SSimon Glass  * Pass "usb" as the name to this function and will return a list of two
208a53f4a29SSimon Glass  * nodes offsets: /ehci@c5008000 and ehci@c5000000.
209a53f4a29SSimon Glass  *
210a53f4a29SSimon Glass  * All nodes returned will match the compatible ID, as it is assumed that
211a53f4a29SSimon Glass  * all peripherals use the same driver.
212a53f4a29SSimon Glass  *
213a53f4a29SSimon Glass  * If no alias node is found, then the node list will be returned in the
214a53f4a29SSimon Glass  * order found in the fdt. If the aliases mention a node which doesn't
215a53f4a29SSimon Glass  * exist, then this will be ignored. If nodes are found with no aliases,
216a53f4a29SSimon Glass  * they will be added in any order.
217a53f4a29SSimon Glass  *
218a53f4a29SSimon Glass  * If there is a gap in the aliases, then this function return a 0 node at
219a53f4a29SSimon Glass  * that position. The return value will also count these gaps.
220a53f4a29SSimon Glass  *
221a53f4a29SSimon Glass  * This function checks node properties and will not return nodes which are
222a53f4a29SSimon Glass  * marked disabled (status = "disabled").
223a53f4a29SSimon Glass  *
224a53f4a29SSimon Glass  * @param blob		FDT blob to use
225a53f4a29SSimon Glass  * @param name		Root name of alias to search for
226a53f4a29SSimon Glass  * @param id		Compatible ID to look for
227a53f4a29SSimon Glass  * @param node_list	Place to put list of found nodes
228a53f4a29SSimon Glass  * @param maxcount	Maximum number of nodes to find
229a53f4a29SSimon Glass  * @return number of nodes found on success, FTD_ERR_... on error
230a53f4a29SSimon Glass  */
231a53f4a29SSimon Glass int fdtdec_find_aliases_for_id(const void *blob, const char *name,
232a53f4a29SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount);
233a53f4a29SSimon Glass 
234a53f4a29SSimon Glass /*
235c6782270SSimon Glass  * This function is similar to fdtdec_find_aliases_for_id() except that it
236c6782270SSimon Glass  * adds to the node_list that is passed in. Any 0 elements are considered
237c6782270SSimon Glass  * available for allocation - others are considered already used and are
238c6782270SSimon Glass  * skipped.
239c6782270SSimon Glass  *
240c6782270SSimon Glass  * You can use this by calling fdtdec_find_aliases_for_id() with an
241c6782270SSimon Glass  * uninitialised array, then setting the elements that are returned to -1,
242c6782270SSimon Glass  * say, then calling this function, perhaps with a different compat id.
243c6782270SSimon Glass  * Any elements you get back that are >0 are new nodes added by the call
244c6782270SSimon Glass  * to this function.
245c6782270SSimon Glass  *
246c6782270SSimon Glass  * Note that if you have some nodes with aliases and some without, you are
247c6782270SSimon Glass  * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
248c6782270SSimon Glass  * one compat_id may fill in positions for which you have aliases defined
249c6782270SSimon Glass  * for another compat_id. When you later call *this* function with the second
250c6782270SSimon Glass  * compat_id, the alias positions may already be used. A debug warning may
251c6782270SSimon Glass  * be generated in this case, but it is safest to define aliases for all
252c6782270SSimon Glass  * nodes when you care about the ordering.
253c6782270SSimon Glass  */
254c6782270SSimon Glass int fdtdec_add_aliases_for_id(const void *blob, const char *name,
255c6782270SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount);
256c6782270SSimon Glass 
257c6782270SSimon Glass /*
258a53f4a29SSimon Glass  * Get the name for a compatible ID
259a53f4a29SSimon Glass  *
260a53f4a29SSimon Glass  * @param id		Compatible ID to look for
261a53f4a29SSimon Glass  * @return compatible string for that id
262a53f4a29SSimon Glass  */
263a53f4a29SSimon Glass const char *fdtdec_get_compatible(enum fdt_compat_id id);
264d17da655SSimon Glass 
265d17da655SSimon Glass /* Look up a phandle and follow it to its node. Then return the offset
266d17da655SSimon Glass  * of that node.
267d17da655SSimon Glass  *
268d17da655SSimon Glass  * @param blob		FDT blob
269d17da655SSimon Glass  * @param node		node to examine
270d17da655SSimon Glass  * @param prop_name	name of property to find
271d17da655SSimon Glass  * @return node offset if found, -ve error code on error
272d17da655SSimon Glass  */
273d17da655SSimon Glass int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
274d17da655SSimon Glass 
275d17da655SSimon Glass /**
276d17da655SSimon Glass  * Look up a property in a node and return its contents in an integer
277d17da655SSimon Glass  * array of given length. The property must have at least enough data for
278d17da655SSimon Glass  * the array (4*count bytes). It may have more, but this will be ignored.
279d17da655SSimon Glass  *
280d17da655SSimon Glass  * @param blob		FDT blob
281d17da655SSimon Glass  * @param node		node to examine
282d17da655SSimon Glass  * @param prop_name	name of property to find
283d17da655SSimon Glass  * @param array		array to fill with data
284d17da655SSimon Glass  * @param count		number of array elements
285d17da655SSimon Glass  * @return 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
286d17da655SSimon Glass  *		or -FDT_ERR_BADLAYOUT if not enough data
287d17da655SSimon Glass  */
288d17da655SSimon Glass int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
289d17da655SSimon Glass 		u32 *array, int count);
290d17da655SSimon Glass 
291d17da655SSimon Glass /**
29296875e7dSSimon Glass  * Look up a property in a node and return a pointer to its contents as a
29396875e7dSSimon Glass  * unsigned int array of given length. The property must have at least enough
29496875e7dSSimon Glass  * data for the array ('count' cells). It may have more, but this will be
29596875e7dSSimon Glass  * ignored. The data is not copied.
29696875e7dSSimon Glass  *
29796875e7dSSimon Glass  * Note that you must access elements of the array with fdt32_to_cpu(),
29896875e7dSSimon Glass  * since the elements will be big endian even on a little endian machine.
29996875e7dSSimon Glass  *
30096875e7dSSimon Glass  * @param blob		FDT blob
30196875e7dSSimon Glass  * @param node		node to examine
30296875e7dSSimon Glass  * @param prop_name	name of property to find
30396875e7dSSimon Glass  * @param count		number of array elements
30496875e7dSSimon Glass  * @return pointer to array if found, or NULL if the property is not
30596875e7dSSimon Glass  *		found or there is not enough data
30696875e7dSSimon Glass  */
30796875e7dSSimon Glass const u32 *fdtdec_locate_array(const void *blob, int node,
30896875e7dSSimon Glass 			       const char *prop_name, int count);
30996875e7dSSimon Glass 
31096875e7dSSimon Glass /**
311d17da655SSimon Glass  * Look up a boolean property in a node and return it.
312d17da655SSimon Glass  *
313d17da655SSimon Glass  * A boolean properly is true if present in the device tree and false if not
314d17da655SSimon Glass  * present, regardless of its value.
315d17da655SSimon Glass  *
316d17da655SSimon Glass  * @param blob	FDT blob
317d17da655SSimon Glass  * @param node	node to examine
318d17da655SSimon Glass  * @param prop_name	name of property to find
319d17da655SSimon Glass  * @return 1 if the properly is present; 0 if it isn't present
320d17da655SSimon Glass  */
321d17da655SSimon Glass int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
322ed3ee5cdSSimon Glass 
323ed3ee5cdSSimon Glass /**
324ed3ee5cdSSimon Glass  * Decode a single GPIOs from an FDT.
325ed3ee5cdSSimon Glass  *
326ed3ee5cdSSimon Glass  * If the property is not found, then the GPIO structure will still be
327ed3ee5cdSSimon Glass  * initialised, with gpio set to FDT_GPIO_NONE. This makes it easy to
328ed3ee5cdSSimon Glass  * provide optional GPIOs.
329ed3ee5cdSSimon Glass  *
330ed3ee5cdSSimon Glass  * @param blob		FDT blob to use
331ed3ee5cdSSimon Glass  * @param node		Node to look at
332ed3ee5cdSSimon Glass  * @param prop_name	Node property name
333ed3ee5cdSSimon Glass  * @param gpio		gpio elements to fill from FDT
334ed3ee5cdSSimon Glass  * @return 0 if ok, -FDT_ERR_NOTFOUND if the property is missing.
335ed3ee5cdSSimon Glass  */
336ed3ee5cdSSimon Glass int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
337ed3ee5cdSSimon Glass 		struct fdt_gpio_state *gpio);
338ed3ee5cdSSimon Glass 
339ed3ee5cdSSimon Glass /**
340ed3ee5cdSSimon Glass  * Set up a GPIO pin according to the provided gpio information. At present this
341ed3ee5cdSSimon Glass  * just requests the GPIO.
342ed3ee5cdSSimon Glass  *
343ed3ee5cdSSimon Glass  * If the gpio is FDT_GPIO_NONE, no action is taken. This makes it easy to
344ed3ee5cdSSimon Glass  * deal with optional GPIOs.
345ed3ee5cdSSimon Glass  *
346ed3ee5cdSSimon Glass  * @param gpio		GPIO info to use for set up
347ed3ee5cdSSimon Glass  * @return 0 if all ok or gpio was FDT_GPIO_NONE; -1 on error
348ed3ee5cdSSimon Glass  */
349ed3ee5cdSSimon Glass int fdtdec_setup_gpio(struct fdt_gpio_state *gpio);
350