xref: /openbmc/u-boot/include/fdtdec.h (revision 9d86f0c3)
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * See file CREDITS for list of people who contributed to this
4  * project.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA
20  */
21 
22 #ifndef __fdtdec_h
23 #define __fdtdec_h
24 
25 /*
26  * This file contains convenience functions for decoding useful and
27  * enlightening information from FDTs. It is intended to be used by device
28  * drivers and board-specific code within U-Boot. It aims to reduce the
29  * amount of FDT munging required within U-Boot itself, so that driver code
30  * changes to support FDT are minimized.
31  */
32 
33 #include <libfdt.h>
34 
35 /*
36  * A typedef for a physical address. Note that fdt data is always big
37  * endian even on a litle endian machine.
38  */
39 #ifdef CONFIG_PHYS_64BIT
40 typedef u64 fdt_addr_t;
41 #define FDT_ADDR_T_NONE (-1ULL)
42 #define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
43 #define fdt_size_to_cpu(reg) be64_to_cpu(reg)
44 #else
45 typedef u32 fdt_addr_t;
46 #define FDT_ADDR_T_NONE (-1U)
47 #define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
48 #define fdt_size_to_cpu(reg) be32_to_cpu(reg)
49 #endif
50 
51 /* Information obtained about memory from the FDT */
52 struct fdt_memory {
53 	fdt_addr_t start;
54 	fdt_addr_t end;
55 };
56 
57 /**
58  * Compat types that we know about and for which we might have drivers.
59  * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
60  * within drivers.
61  */
62 enum fdt_compat_id {
63 	COMPAT_UNKNOWN,
64 	COMPAT_NVIDIA_TEGRA20_USB,	/* Tegra20 USB port */
65 	COMPAT_NVIDIA_TEGRA20_I2C,	/* Tegra20 i2c */
66 	COMPAT_NVIDIA_TEGRA20_DVC,	/* Tegra20 dvc (really just i2c) */
67 	COMPAT_NVIDIA_TEGRA20_EMC,	/* Tegra20 memory controller */
68 	COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra20 memory timing table */
69 	COMPAT_NVIDIA_TEGRA20_KBC,	/* Tegra20 Keyboard */
70 	COMPAT_NVIDIA_TEGRA20_NAND,	/* Tegra2 NAND controller */
71 	COMPAT_NVIDIA_TEGRA20_PWM,	/* Tegra 2 PWM controller */
72 	COMPAT_NVIDIA_TEGRA20_DC,	/* Tegra 2 Display controller */
73 
74 	COMPAT_COUNT,
75 };
76 
77 /* GPIOs are numbered from 0 */
78 enum {
79 	FDT_GPIO_NONE = -1U,	/* an invalid GPIO used to end our list */
80 
81 	FDT_GPIO_ACTIVE_LOW = 1 << 0,	/* input is active low (else high) */
82 };
83 
84 /* This is the state of a GPIO pin as defined by the fdt */
85 struct fdt_gpio_state {
86 	const char *name;	/* name of the fdt property defining this */
87 	uint gpio;		/* GPIO number, or FDT_GPIO_NONE if none */
88 	u8 flags;		/* FDT_GPIO_... flags */
89 };
90 
91 /* This tells us whether a fdt_gpio_state record is valid or not */
92 #define fdt_gpio_isvalid(x) ((x)->gpio != FDT_GPIO_NONE)
93 
94 /**
95  * Read the GPIO taking into account the polarity of the pin.
96  *
97  * @param gpio		pointer to the decoded gpio
98  * @return value of the gpio if successful, < 0 if unsuccessful
99  */
100 int fdtdec_get_gpio(struct fdt_gpio_state *gpio);
101 
102 /**
103  * Write the GPIO taking into account the polarity of the pin.
104  *
105  * @param gpio		pointer to the decoded gpio
106  * @return 0 if successful
107  */
108 int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val);
109 
110 /**
111  * Find the next numbered alias for a peripheral. This is used to enumerate
112  * all the peripherals of a certain type.
113  *
114  * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
115  * this function will return a pointer to the node the alias points to, and
116  * then update *upto to 1. Next time you call this function, the next node
117  * will be returned.
118  *
119  * All nodes returned will match the compatible ID, as it is assumed that
120  * all peripherals use the same driver.
121  *
122  * @param blob		FDT blob to use
123  * @param name		Root name of alias to search for
124  * @param id		Compatible ID to look for
125  * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
126  */
127 int fdtdec_next_alias(const void *blob, const char *name,
128 		enum fdt_compat_id id, int *upto);
129 
130 /**
131  * Find the compatible ID for a given node.
132  *
133  * Generally each node has at least one compatible string attached to it.
134  * This function looks through our list of known compatible strings and
135  * returns the corresponding ID which matches the compatible string.
136  *
137  * @param blob		FDT blob to use
138  * @param node		Node containing compatible string to find
139  * @return compatible ID, or COMPAT_UNKNOWN if we cannot find a match
140  */
141 enum fdt_compat_id fdtdec_lookup(const void *blob, int node);
142 
143 /**
144  * Find the next compatible node for a peripheral.
145  *
146  * Do the first call with node = 0. This function will return a pointer to
147  * the next compatible node. Next time you call this function, pass the
148  * value returned, and the next node will be provided.
149  *
150  * @param blob		FDT blob to use
151  * @param node		Start node for search
152  * @param id		Compatible ID to look for (enum fdt_compat_id)
153  * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
154  */
155 int fdtdec_next_compatible(const void *blob, int node,
156 		enum fdt_compat_id id);
157 
158 /**
159  * Find the next compatible subnode for a peripheral.
160  *
161  * Do the first call with node set to the parent and depth = 0. This
162  * function will return the offset of the next compatible node. Next time
163  * you call this function, pass the node value returned last time, with
164  * depth unchanged, and the next node will be provided.
165  *
166  * @param blob		FDT blob to use
167  * @param node		Start node for search
168  * @param id		Compatible ID to look for (enum fdt_compat_id)
169  * @param depthp	Current depth (set to 0 before first call)
170  * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
171  */
172 int fdtdec_next_compatible_subnode(const void *blob, int node,
173 		enum fdt_compat_id id, int *depthp);
174 
175 /**
176  * Look up an address property in a node and return it as an address.
177  * The property must hold either one address with no trailing data or
178  * one address with a length. This is only tested on 32-bit machines.
179  *
180  * @param blob	FDT blob
181  * @param node	node to examine
182  * @param prop_name	name of property to find
183  * @return address, if found, or FDT_ADDR_T_NONE if not
184  */
185 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
186 		const char *prop_name);
187 
188 /**
189  * Look up a 32-bit integer property in a node and return it. The property
190  * must have at least 4 bytes of data. The value of the first cell is
191  * returned.
192  *
193  * @param blob	FDT blob
194  * @param node	node to examine
195  * @param prop_name	name of property to find
196  * @param default_val	default value to return if the property is not found
197  * @return integer value, if found, or default_val if not
198  */
199 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
200 		s32 default_val);
201 
202 /**
203  * Look up a 64-bit integer property in a node and return it. The property
204  * must have at least 8 bytes of data (2 cells). The first two cells are
205  * concatenated to form a 8 bytes value, where the first cell is top half and
206  * the second cell is bottom half.
207  *
208  * @param blob	FDT blob
209  * @param node	node to examine
210  * @param prop_name	name of property to find
211  * @param default_val	default value to return if the property is not found
212  * @return integer value, if found, or default_val if not
213  */
214 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
215 		uint64_t default_val);
216 
217 /**
218  * Checks whether a node is enabled.
219  * This looks for a 'status' property. If this exists, then returns 1 if
220  * the status is 'ok' and 0 otherwise. If there is no status property,
221  * it returns 1 on the assumption that anything mentioned should be enabled
222  * by default.
223  *
224  * @param blob	FDT blob
225  * @param node	node to examine
226  * @return integer value 0 (not enabled) or 1 (enabled)
227  */
228 int fdtdec_get_is_enabled(const void *blob, int node);
229 
230 /**
231  * Make sure we have a valid fdt available to control U-Boot.
232  *
233  * If not, a message is printed to the console if the console is ready.
234  *
235  * @return 0 if all ok, -1 if not
236  */
237 int fdtdec_prepare_fdt(void);
238 
239 /**
240  * Checks that we have a valid fdt available to control U-Boot.
241 
242  * However, if not then for the moment nothing is done, since this function
243  * is called too early to panic().
244  *
245  * @returns 0
246  */
247 int fdtdec_check_fdt(void);
248 
249 /**
250  * Find the nodes for a peripheral and return a list of them in the correct
251  * order. This is used to enumerate all the peripherals of a certain type.
252  *
253  * To use this, optionally set up a /aliases node with alias properties for
254  * a peripheral. For example, for usb you could have:
255  *
256  * aliases {
257  *		usb0 = "/ehci@c5008000";
258  *		usb1 = "/ehci@c5000000";
259  * };
260  *
261  * Pass "usb" as the name to this function and will return a list of two
262  * nodes offsets: /ehci@c5008000 and ehci@c5000000.
263  *
264  * All nodes returned will match the compatible ID, as it is assumed that
265  * all peripherals use the same driver.
266  *
267  * If no alias node is found, then the node list will be returned in the
268  * order found in the fdt. If the aliases mention a node which doesn't
269  * exist, then this will be ignored. If nodes are found with no aliases,
270  * they will be added in any order.
271  *
272  * If there is a gap in the aliases, then this function return a 0 node at
273  * that position. The return value will also count these gaps.
274  *
275  * This function checks node properties and will not return nodes which are
276  * marked disabled (status = "disabled").
277  *
278  * @param blob		FDT blob to use
279  * @param name		Root name of alias to search for
280  * @param id		Compatible ID to look for
281  * @param node_list	Place to put list of found nodes
282  * @param maxcount	Maximum number of nodes to find
283  * @return number of nodes found on success, FTD_ERR_... on error
284  */
285 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
286 			enum fdt_compat_id id, int *node_list, int maxcount);
287 
288 /*
289  * This function is similar to fdtdec_find_aliases_for_id() except that it
290  * adds to the node_list that is passed in. Any 0 elements are considered
291  * available for allocation - others are considered already used and are
292  * skipped.
293  *
294  * You can use this by calling fdtdec_find_aliases_for_id() with an
295  * uninitialised array, then setting the elements that are returned to -1,
296  * say, then calling this function, perhaps with a different compat id.
297  * Any elements you get back that are >0 are new nodes added by the call
298  * to this function.
299  *
300  * Note that if you have some nodes with aliases and some without, you are
301  * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
302  * one compat_id may fill in positions for which you have aliases defined
303  * for another compat_id. When you later call *this* function with the second
304  * compat_id, the alias positions may already be used. A debug warning may
305  * be generated in this case, but it is safest to define aliases for all
306  * nodes when you care about the ordering.
307  */
308 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
309 			enum fdt_compat_id id, int *node_list, int maxcount);
310 
311 /*
312  * Get the name for a compatible ID
313  *
314  * @param id		Compatible ID to look for
315  * @return compatible string for that id
316  */
317 const char *fdtdec_get_compatible(enum fdt_compat_id id);
318 
319 /* Look up a phandle and follow it to its node. Then return the offset
320  * of that node.
321  *
322  * @param blob		FDT blob
323  * @param node		node to examine
324  * @param prop_name	name of property to find
325  * @return node offset if found, -ve error code on error
326  */
327 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
328 
329 /**
330  * Look up a property in a node and return its contents in an integer
331  * array of given length. The property must have at least enough data for
332  * the array (4*count bytes). It may have more, but this will be ignored.
333  *
334  * @param blob		FDT blob
335  * @param node		node to examine
336  * @param prop_name	name of property to find
337  * @param array		array to fill with data
338  * @param count		number of array elements
339  * @return 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
340  *		or -FDT_ERR_BADLAYOUT if not enough data
341  */
342 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
343 		u32 *array, int count);
344 
345 /**
346  * Look up a property in a node and return a pointer to its contents as a
347  * unsigned int array of given length. The property must have at least enough
348  * data for the array ('count' cells). It may have more, but this will be
349  * ignored. The data is not copied.
350  *
351  * Note that you must access elements of the array with fdt32_to_cpu(),
352  * since the elements will be big endian even on a little endian machine.
353  *
354  * @param blob		FDT blob
355  * @param node		node to examine
356  * @param prop_name	name of property to find
357  * @param count		number of array elements
358  * @return pointer to array if found, or NULL if the property is not
359  *		found or there is not enough data
360  */
361 const u32 *fdtdec_locate_array(const void *blob, int node,
362 			       const char *prop_name, int count);
363 
364 /**
365  * Look up a boolean property in a node and return it.
366  *
367  * A boolean properly is true if present in the device tree and false if not
368  * present, regardless of its value.
369  *
370  * @param blob	FDT blob
371  * @param node	node to examine
372  * @param prop_name	name of property to find
373  * @return 1 if the properly is present; 0 if it isn't present
374  */
375 int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
376 
377 /**
378  * Decode a single GPIOs from an FDT.
379  *
380  * If the property is not found, then the GPIO structure will still be
381  * initialised, with gpio set to FDT_GPIO_NONE. This makes it easy to
382  * provide optional GPIOs.
383  *
384  * @param blob		FDT blob to use
385  * @param node		Node to look at
386  * @param prop_name	Node property name
387  * @param gpio		gpio elements to fill from FDT
388  * @return 0 if ok, -FDT_ERR_NOTFOUND if the property is missing.
389  */
390 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
391 		struct fdt_gpio_state *gpio);
392 
393 /**
394  * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
395  * terminating item.
396  *
397  * @param blob         FDT blob to use
398  * @param node         Node to look at
399  * @param prop_name    Node property name
400  * @param gpio         Array of gpio elements to fill from FDT. This will be
401  *                     untouched if either 0 or an error is returned
402  * @param max_count    Maximum number of elements allowed
403  * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
404  * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
405  */
406 int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
407 		struct fdt_gpio_state *gpio, int max_count);
408 
409 /**
410  * Set up a GPIO pin according to the provided gpio information. At present this
411  * just requests the GPIO.
412  *
413  * If the gpio is FDT_GPIO_NONE, no action is taken. This makes it easy to
414  * deal with optional GPIOs.
415  *
416  * @param gpio		GPIO info to use for set up
417  * @return 0 if all ok or gpio was FDT_GPIO_NONE; -1 on error
418  */
419 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio);
420 
421 /**
422  * Look in the FDT for a config item with the given name and return its value
423  * as a 32-bit integer. The property must have at least 4 bytes of data. The
424  * value of the first cell is returned.
425  *
426  * @param blob		FDT blob to use
427  * @param prop_name	Node property name
428  * @param default_val	default value to return if the property is not found
429  * @return integer value, if found, or default_val if not
430  */
431 int fdtdec_get_config_int(const void *blob, const char *prop_name,
432 		int default_val);
433 
434 /**
435  * Look in the FDT for a config item with the given name
436  * and return whether it exists.
437  *
438  * @param blob		FDT blob
439  * @param prop_name	property name to look up
440  * @return 1, if it exists, or 0 if not
441  */
442 int fdtdec_get_config_bool(const void *blob, const char *prop_name);
443 
444 /**
445  * Look in the FDT for a config item with the given name and return its value
446  * as a string.
447  *
448  * @param blob          FDT blob
449  * @param prop_name     property name to look up
450  * @returns property string, NULL on error.
451  */
452 char *fdtdec_get_config_string(const void *blob, const char *prop_name);
453 
454 /*
455  * Look up a property in a node and return its contents in a byte
456  * array of given length. The property must have at least enough data for
457  * the array (count bytes). It may have more, but this will be ignored.
458  *
459  * @param blob		FDT blob
460  * @param node		node to examine
461  * @param prop_name	name of property to find
462  * @param array		array to fill with data
463  * @param count		number of array elements
464  * @return 0 if ok, or -FDT_ERR_MISSING if the property is not found,
465  *		or -FDT_ERR_BADLAYOUT if not enough data
466  */
467 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
468 		u8 *array, int count);
469 
470 /**
471  * Look up a property in a node and return a pointer to its contents as a
472  * byte array of given length. The property must have at least enough data
473  * for the array (count bytes). It may have more, but this will be ignored.
474  * The data is not copied.
475  *
476  * @param blob		FDT blob
477  * @param node		node to examine
478  * @param prop_name	name of property to find
479  * @param count		number of array elements
480  * @return pointer to byte array if found, or NULL if the property is not
481  *		found or there is not enough data
482  */
483 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
484 			     const char *prop_name, int count);
485 
486 /**
487  * Look up a property in a node which contains a memory region address and
488  * size. Then return a pointer to this address.
489  *
490  * The property must hold one address with a length. This is only tested on
491  * 32-bit machines.
492  *
493  * @param blob		FDT blob
494  * @param node		node to examine
495  * @param prop_name	name of property to find
496  * @param ptrp		returns pointer to region, or NULL if no address
497  * @param size		returns size of region
498  * @return 0 if ok, -1 on error (propery not found)
499  */
500 int fdtdec_decode_region(const void *blob, int node,
501 		const char *prop_name, void **ptrp, size_t *size);
502 #endif
503