New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / bus / dpaa / include / of.h
1 /*-
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.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2010-2016 Freescale Semiconductor, Inc.
8  * Copyright 2017 NXP.
9  *
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.
20  *
21  *   GPL LICENSE SUMMARY
22  *
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
26  * later version.
27  *
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.
39  */
40
41 #ifndef __OF_H
42 #define __OF_H
43
44 #include <compat.h>
45
46 #ifndef OF_INIT_DEFAULT_PATH
47 #define OF_INIT_DEFAULT_PATH "/proc/device-tree"
48 #endif
49
50 #define OF_DEFAULT_NA 1
51 #define OF_DEFAULT_NS 1
52
53 #define OF_FILE_BUF_MAX 256
54
55 /**
56  * Layout of Device Tree:
57  * dt_dir
58  *  |- dt_dir
59  *  |   |- dt_dir
60  *  |   |  |- dt_dir
61  *  |   |  |  |- dt_file
62  *  |   |  |  ``- dt_file
63  *  |   |  ``- dt_file
64  *  |   `-dt_file`
65  *  ``- dt_file
66  *
67  *  +------------------+
68  *  |dt_dir            |
69  *  |+----------------+|
70  *  ||dt_node         ||
71  *  ||+--------------+||
72  *  |||device_node   |||
73  *  ||+--------------+||
74  *  || list_dt_nodes  ||
75  *  |+----------------+|
76  *  | list of subdir   |
77  *  | list of files    |
78  *  +------------------+
79  */
80
81 /**
82  * Device description on of a device node in device tree.
83  */
84 struct device_node {
85         char name[NAME_MAX];
86         char full_name[PATH_MAX];
87 };
88
89 /**
90  * List of device nodes available in a device tree layout
91  */
92 struct dt_node {
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 */
96 };
97
98 /**
99  * Types we use to represent directories and files
100  */
101 struct dt_file;
102 struct dt_dir {
103         struct dt_node node;
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;
113         struct dt_file *reg;
114 };
115
116 struct dt_file {
117         struct dt_node node;
118         struct dt_dir *parent;
119         ssize_t len;
120         uint64_t buf[OF_FILE_BUF_MAX >> 3];
121 };
122
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)));
128
129 #define for_each_compatible_node(dev_node, type, compatible) \
130         for (dev_node = of_find_compatible_node(NULL, type, compatible); \
131                 dev_node != NULL; \
132                 dev_node = of_find_compatible_node(dev_node, type, compatible))
133
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);
137
138 const struct device_node *of_find_node_by_phandle(phandle ph);
139
140 const struct device_node *of_get_parent(const struct device_node *dev_node);
141
142 const struct device_node *of_get_next_child(const struct device_node *dev_node,
143                                             const struct device_node *prev);
144
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))
148
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);
151
152 const uint32_t *of_get_address(const struct device_node *dev_node, size_t idx,
153                                uint64_t *size, uint32_t *flags);
154
155 uint64_t of_translate_address(const struct device_node *dev_node,
156                               const u32 *addr) __attribute__((nonnull));
157
158 bool of_device_is_compatible(const struct device_node *dev_node,
159                              const char *compatible);
160
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".
164  */
165 int of_init_path(const char *dt_path);
166
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.
169  */
170 void of_finish(void);
171
172 /* Use of this wrapper is recommended. */
173 static inline int of_init(void)
174 {
175         return of_init_path(OF_INIT_DEFAULT_PATH);
176 }
177
178 /* Read a numeric property according to its size and return it as a 64-bit
179  * value.
180  */
181 static inline uint64_t of_read_number(const __be32 *cell, int size)
182 {
183         uint64_t r = 0;
184
185         while (size--)
186                 r = (r << 32) | be32toh(*(cell++));
187         return r;
188 }
189
190 #endif  /*  __OF_H */