2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright 2010-2016 Freescale Semiconductor, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * * Neither the name of the above-listed copyright holders nor the
18 * names of any contributors may be used to endorse or promote products
19 * derived from this software without specific prior written permission.
23 * ALTERNATIVELY, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") as published by the Free Software
25 * Foundation, either version 2 of that License or (at your option) any
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
46 #ifndef OF_INIT_DEFAULT_PATH
47 #define OF_INIT_DEFAULT_PATH "/proc/device-tree"
50 #define OF_DEFAULT_NA 1
51 #define OF_DEFAULT_NS 1
53 #define OF_FILE_BUF_MAX 256
56 * Layout of Device Tree:
67 * +------------------+
69 * |+----------------+|
71 * ||+--------------+||
73 * ||+--------------+||
75 * |+----------------+|
78 * +------------------+
82 * Device description on of a device node in device tree.
86 char full_name[PATH_MAX];
90 * List of device nodes available in a device tree layout
93 struct device_node node; /**< Property of node */
94 int is_file; /**< FALSE==dir, TRUE==file */
95 struct list_head list; /**< Nodes within a parent subdir */
99 * Types we use to represent directories and files
104 struct list_head subdirs;
105 struct list_head files;
106 struct list_head linear;
107 struct dt_dir *parent;
108 struct dt_file *compatible;
109 struct dt_file *status;
110 struct dt_file *lphandle;
111 struct dt_file *a_cells;
112 struct dt_file *s_cells;
118 struct dt_dir *parent;
120 uint64_t buf[OF_FILE_BUF_MAX >> 3];
123 const struct device_node *of_find_compatible_node(
124 const struct device_node *from,
125 const char *type __always_unused,
126 const char *compatible)
127 __attribute__((nonnull(3)));
129 #define for_each_compatible_node(dev_node, type, compatible) \
130 for (dev_node = of_find_compatible_node(NULL, type, compatible); \
132 dev_node = of_find_compatible_node(dev_node, type, compatible))
134 const void *of_get_property(const struct device_node *from, const char *name,
135 size_t *lenp) __attribute__((nonnull(2)));
136 bool of_device_is_available(const struct device_node *dev_node);
138 const struct device_node *of_find_node_by_phandle(phandle ph);
140 const struct device_node *of_get_parent(const struct device_node *dev_node);
142 const struct device_node *of_get_next_child(const struct device_node *dev_node,
143 const struct device_node *prev);
145 #define for_each_child_node(parent, child) \
146 for (child = of_get_next_child(parent, NULL); child != NULL; \
147 child = of_get_next_child(parent, child))
149 uint32_t of_n_addr_cells(const struct device_node *dev_node);
150 uint32_t of_n_size_cells(const struct device_node *dev_node);
152 const uint32_t *of_get_address(const struct device_node *dev_node, size_t idx,
153 uint64_t *size, uint32_t *flags);
155 uint64_t of_translate_address(const struct device_node *dev_node,
156 const u32 *addr) __attribute__((nonnull));
158 bool of_device_is_compatible(const struct device_node *dev_node,
159 const char *compatible);
161 /* of_init() must be called prior to initialisation or use of any driver
162 * subsystem that is device-tree-dependent. Eg. Qman/Bman, config layers, etc.
163 * The path should usually be "/proc/device-tree".
165 int of_init_path(const char *dt_path);
167 /* of_finish() allows a controlled tear-down of the device-tree layer, eg. if a
168 * full reload is desired without a process exit.
170 void of_finish(void);
172 /* Use of this wrapper is recommended. */
173 static inline int of_init(void)
175 return of_init_path(OF_INIT_DEFAULT_PATH);
178 /* Read a numeric property according to its size and return it as a 64-bit
181 static inline uint64_t of_read_number(const __be32 *cell, int size)
186 r = (r << 32) | be32toh(*(cell++));