New upstream version 18.08
[deb_dpdk.git] / drivers / bus / dpaa / include / of.h
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2010-2016 Freescale Semiconductor, Inc.
4  * Copyright 2017 NXP
5  *
6  */
7
8 #ifndef __OF_H
9 #define __OF_H
10
11 #include <compat.h>
12
13 #ifndef OF_INIT_DEFAULT_PATH
14 #define OF_INIT_DEFAULT_PATH "/proc/device-tree"
15 #endif
16
17 #define OF_DEFAULT_NA 1
18 #define OF_DEFAULT_NS 1
19
20 #define OF_FILE_BUF_MAX 256
21
22 /**
23  * Layout of Device Tree:
24  * dt_dir
25  *  |- dt_dir
26  *  |   |- dt_dir
27  *  |   |  |- dt_dir
28  *  |   |  |  |- dt_file
29  *  |   |  |  ``- dt_file
30  *  |   |  ``- dt_file
31  *  |   `-dt_file`
32  *  ``- dt_file
33  *
34  *  +------------------+
35  *  |dt_dir            |
36  *  |+----------------+|
37  *  ||dt_node         ||
38  *  ||+--------------+||
39  *  |||device_node   |||
40  *  ||+--------------+||
41  *  || list_dt_nodes  ||
42  *  |+----------------+|
43  *  | list of subdir   |
44  *  | list of files    |
45  *  +------------------+
46  */
47
48 /**
49  * Device description on of a device node in device tree.
50  */
51 struct device_node {
52         char name[NAME_MAX];
53         char full_name[PATH_MAX];
54 };
55
56 /**
57  * List of device nodes available in a device tree layout
58  */
59 struct dt_node {
60         struct device_node node; /**< Property of node */
61         int is_file; /**< FALSE==dir, TRUE==file */
62         struct list_head list; /**< Nodes within a parent subdir */
63 };
64
65 /**
66  * Types we use to represent directories and files
67  */
68 struct dt_file;
69 struct dt_dir {
70         struct dt_node node;
71         struct list_head subdirs;
72         struct list_head files;
73         struct list_head linear;
74         struct dt_dir *parent;
75         struct dt_file *compatible;
76         struct dt_file *status;
77         struct dt_file *lphandle;
78         struct dt_file *a_cells;
79         struct dt_file *s_cells;
80         struct dt_file *reg;
81 };
82
83 struct dt_file {
84         struct dt_node node;
85         struct dt_dir *parent;
86         ssize_t len;
87         uint64_t buf[OF_FILE_BUF_MAX >> 3];
88 };
89
90 const struct device_node *of_find_compatible_node(
91                                         const struct device_node *from,
92                                         const char *type __always_unused,
93                                         const char *compatible)
94         __attribute__((nonnull(3)));
95
96 #define for_each_compatible_node(dev_node, type, compatible) \
97         for (dev_node = of_find_compatible_node(NULL, type, compatible); \
98                 dev_node != NULL; \
99                 dev_node = of_find_compatible_node(dev_node, type, compatible))
100
101 const void *of_get_property(const struct device_node *from, const char *name,
102                             size_t *lenp) __attribute__((nonnull(2)));
103 bool of_device_is_available(const struct device_node *dev_node);
104
105 const struct device_node *of_find_node_by_phandle(phandle ph);
106
107 const struct device_node *of_get_parent(const struct device_node *dev_node);
108
109 const struct device_node *of_get_next_child(const struct device_node *dev_node,
110                                             const struct device_node *prev);
111
112 const void *of_get_mac_address(const struct device_node *np);
113
114 #define for_each_child_node(parent, child) \
115         for (child = of_get_next_child(parent, NULL); child != NULL; \
116                         child = of_get_next_child(parent, child))
117
118 uint32_t of_n_addr_cells(const struct device_node *dev_node);
119 uint32_t of_n_size_cells(const struct device_node *dev_node);
120
121 const uint32_t *of_get_address(const struct device_node *dev_node, size_t idx,
122                                uint64_t *size, uint32_t *flags);
123
124 uint64_t of_translate_address(const struct device_node *dev_node,
125                               const u32 *addr) __attribute__((nonnull));
126
127 bool of_device_is_compatible(const struct device_node *dev_node,
128                              const char *compatible);
129
130 /* of_init() must be called prior to initialisation or use of any driver
131  * subsystem that is device-tree-dependent. Eg. Qman/Bman, config layers, etc.
132  * The path should usually be "/proc/device-tree".
133  */
134 int of_init_path(const char *dt_path);
135
136 /* of_finish() allows a controlled tear-down of the device-tree layer, eg. if a
137  * full reload is desired without a process exit.
138  */
139 void of_finish(void);
140
141 /* Use of this wrapper is recommended. */
142 static inline int of_init(void)
143 {
144         return of_init_path(OF_INIT_DEFAULT_PATH);
145 }
146
147 /* Read a numeric property according to its size and return it as a 64-bit
148  * value.
149  */
150 static inline uint64_t of_read_number(const __be32 *cell, int size)
151 {
152         uint64_t r = 0;
153
154         while (size--)
155                 r = (r << 32) | be32toh(*(cell++));
156         return r;
157 }
158
159 #endif  /*  __OF_H */