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