xref: /optee_os/core/include/kernel/dt.h (revision 9e3c57c88b0cdd41de57107725621c8c0857a838)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2016-2021, Linaro Limited
4  */
5 
6 #ifndef KERNEL_DT_H
7 #define KERNEL_DT_H
8 
9 #include <compiler.h>
10 #include <kernel/interrupt.h>
11 #include <kernel/panic.h>
12 #include <scattered_array.h>
13 #include <stdint.h>
14 #include <tee_api_types.h>
15 #include <types_ext.h>
16 #include <util.h>
17 
18 /*
19  * Bitfield to reflect status and secure-status values ("okay", "disabled"
20  * or not present)
21  */
22 #define DT_STATUS_DISABLED		U(0)
23 #define DT_STATUS_OK_NSEC		BIT(0)
24 #define DT_STATUS_OK_SEC		BIT(1)
25 
26 #define DT_INFO_INVALID_REG		((paddr_t)-1)
27 #define DT_INFO_INVALID_REG_SIZE	((size_t)-1)
28 #define DT_INFO_INVALID_CLOCK		-1
29 #define DT_INFO_INVALID_RESET		-1
30 #define DT_INFO_INVALID_INTERRUPT	-1
31 
32 /*
33  * @status: Bit mask for DT_STATUS_*
34  * @reg: Device register physical base address or DT_INFO_INVALID_REG
35  * @reg_size: Device register size or DT_INFO_INVALID_REG_SIZE
36  * @clock: Device identifier (positive value) or DT_INFO_INVALID_CLOCK
37  * @reset: Device reset identifier (positive value) or DT_INFO_INVALID_CLOCK
38  * @interrupt: Device interrupt identifier (positive value) or
39  * DT_INFO_INVALID_INTERRUPT
40  * @type: IRQ_TYPE_* value parsed from interrupts properties or IRQ_TYPE_NONE if
41  * not present
42  * @prio: interrupt priority parsed from interrupts properties or 0 if not
43  * present
44  */
45 struct dt_node_info {
46 	unsigned int status;
47 	paddr_t reg;
48 	size_t reg_size;
49 	int clock;
50 	int reset;
51 	int interrupt;
52 	uint32_t type;
53 	uint32_t prio;
54 };
55 
56 /*
57  * DT-aware drivers
58  */
59 
60 struct dt_device_match {
61 	const char *compatible;
62 	const void *compat_data;
63 };
64 
65 /*
66  * DT_MAP_AUTO: Uses status properties from device tree to determine mapping.
67  * DT_MAP_SECURE: Force mapping for device to be secure.
68  * DT_MAP_NON_SECURE: Force mapping for device to be non-secure.
69  */
70 enum dt_map_dev_directive {
71 	DT_MAP_AUTO,
72 	DT_MAP_SECURE,
73 	DT_MAP_NON_SECURE
74 };
75 
76 #ifdef CFG_DT
77 /*
78  * Find a driver that is suitable for the given DT node, that is, with
79  * a matching "compatible" property.
80  *
81  * @fdt: pointer to the device tree
82  * @offs: node offset
83  */
84 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs);
85 
86 /*
87  * Map a device into secure or non-secure memory and return the base VA and
88  * the mapping size. The mapping is done with type MEM_AREA_IO_SEC or
89  * MEM_AREA_IO_NSEC, depending on the device status.
90  * If the mapping already exists, the function simply returns the @vbase and
91  * @size information.
92  *
93  * @offs is the offset of the node that describes the device in @fdt.
94  * @base receives the base virtual address corresponding to the base physical
95  * address of the "reg" property
96  * @size receives the size of the mapping
97  * @mapping what kind of mapping is done for memory.
98  *
99  * Returns 0 on success or -1 in case of error.
100  */
101 int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size,
102 	       enum dt_map_dev_directive mapping);
103 
104 /*
105  * Check whether the node at @offs contains the property with propname or not.
106  *
107  * @offs is the offset of the node that describes the device in @fdt.
108  * @propname is the property that need to check
109  *
110  * Returns true on success or false if no propname.
111  */
112 bool dt_have_prop(const void *fdt, int offs, const char *propname);
113 
114 /*
115  * Modify or add "status" property to "disabled"
116  *
117  * @fdt reference to the Device Tree
118  * @node is the node offset to modify
119  *
120  * Returns 0 on success or -1 on failure
121  */
122 int dt_disable_status(void *fdt, int node);
123 
124 /*
125  * Force secure-status = "okay" and status="disabled" for the target node.
126  *
127  * @fdt reference to the Device Tree
128  * @node is the node offset to modify
129  *
130  * Returns 0 on success or -1 on failure
131  */
132 int dt_enable_secure_status(void *fdt, int node);
133 
134 /*
135  * FDT manipulation functions, not provided by <libfdt.h>
136  */
137 
138 /*
139  * Return the base address for the "reg" property of the specified node or
140  * (paddr_t)-1 in case of error
141  */
142 paddr_t fdt_reg_base_address(const void *fdt, int offs);
143 
144 /*
145  * Return the reg size for the reg property of the specified node or -1 in case
146  * of error
147  */
148 size_t fdt_reg_size(const void *fdt, int offs);
149 
150 /*
151  * Read the status and secure-status properties into a bitfield.
152  * Return -1 on failure, DT_STATUS_DISABLED if the node is disabled,
153  * otherwise return a combination of DT_STATUS_OK_NSEC and DT_STATUS_OK_SEC.
154  */
155 int fdt_get_status(const void *fdt, int offs);
156 
157 /*
158  * fdt_fill_device_info - Get generic device info from a node
159  *
160  * This function fills the generic information from a given node.
161  * Currently supports a single base register, a single clock,
162  * a single reset ID line and a single interrupt ID.
163  * Default DT_INFO_* macros are used when the relate property is not found.
164  */
165 void fdt_fill_device_info(const void *fdt, struct dt_node_info *info,
166 			  int node);
167 /*
168  * Read cells from a given property of the given node. Any number of 32-bit
169  * cells of the property can be read. Returns 0 on success, or a negative
170  * FDT error value otherwise.
171  */
172 int fdt_read_uint32_array(const void *fdt, int node, const char *prop_name,
173 			  uint32_t *array, size_t count);
174 
175 /*
176  * Read one cell from a given multi-value property of the given node.
177  * Returns 0 on success, or a negative FDT error value otherwise.
178  */
179 int fdt_read_uint32_index(const void *fdt, int node, const char *prop_name,
180 			  int index, uint32_t *value);
181 
182 /*
183  * Read one cell from a given property of the given node.
184  * Returns 0 on success, or a negative FDT error value otherwise.
185  */
186 int fdt_read_uint32(const void *fdt, int node, const char *prop_name,
187 		    uint32_t *value);
188 
189 /*
190  * Read one cell from a property of a cell or default to a given value
191  * Returns the 32bit cell value or @dflt_value on failure.
192  */
193 uint32_t fdt_read_uint32_default(const void *fdt, int node,
194 				 const char *prop_name, uint32_t dflt_value);
195 
196 /*
197  * This function fills reg node info (base & size) with an index.
198  *
199  * Returns 0 on success and a negative FDT error code on failure.
200  */
201 int fdt_get_reg_props_by_index(const void *fdt, int node, int index,
202 			       paddr_t *base, size_t *size);
203 
204 /*
205  * This function fills reg node info (base & size) with an index found by
206  * checking the reg-names node.
207  *
208  * Returns 0 on success and a negative FDT error code on failure.
209  */
210 int fdt_get_reg_props_by_name(const void *fdt, int node, const char *name,
211 			      paddr_t *base, size_t *size);
212 
213 #else /* !CFG_DT */
214 
215 static inline const struct dt_driver *dt_find_compatible_driver(
216 					const void *fdt __unused,
217 					int offs __unused)
218 {
219 	return NULL;
220 }
221 
222 static inline int dt_map_dev(const void *fdt __unused, int offs __unused,
223 			     vaddr_t *vbase __unused, size_t *size __unused,
224 			     enum dt_map_dev_directive mapping __unused)
225 {
226 	return -1;
227 }
228 
229 static inline paddr_t fdt_reg_base_address(const void *fdt __unused,
230 					   int offs __unused)
231 {
232 	return (paddr_t)-1;
233 }
234 
235 static inline size_t fdt_reg_size(const void *fdt __unused,
236 				  int offs __unused)
237 {
238 	return (size_t)-1;
239 }
240 
241 static inline int fdt_get_status(const void *fdt __unused, int offs __unused)
242 {
243 	return -1;
244 }
245 
246 __noreturn
247 static inline void fdt_fill_device_info(const void *fdt __unused,
248 					struct dt_node_info *info __unused,
249 					int node __unused)
250 {
251 	panic();
252 }
253 
254 static inline int fdt_read_uint32_array(const void *fdt __unused,
255 					int node __unused,
256 					const char *prop_name __unused,
257 					uint32_t *array __unused,
258 					size_t count __unused)
259 {
260 	return -1;
261 }
262 
263 static inline int fdt_read_uint32(const void *fdt __unused,
264 				  int node __unused,
265 				  const char *prop_name __unused,
266 				  uint32_t *value __unused)
267 {
268 	return -1;
269 }
270 
271 static inline uint32_t fdt_read_uint32_default(const void *fdt __unused,
272 					       int node __unused,
273 					       const char *prop_name __unused,
274 					       uint32_t dflt_value __unused)
275 {
276 	return dflt_value;
277 }
278 
279 static inline int fdt_read_uint32_index(const void *fdt __unused,
280 					int node __unused,
281 					const char *prop_name __unused,
282 					int index __unused,
283 					uint32_t *value __unused)
284 {
285 	return -1;
286 }
287 
288 static inline int fdt_get_reg_props_by_index(const void *fdt __unused,
289 					     int node __unused,
290 					     int index __unused,
291 					     paddr_t *base __unused,
292 					     size_t *size __unused)
293 {
294 	return -1;
295 }
296 
297 static inline int fdt_get_reg_props_by_name(const void *fdt __unused,
298 					    int node __unused,
299 					    const char *name __unused,
300 					    paddr_t *base __unused,
301 					    size_t *size __unused)
302 {
303 	return -1;
304 }
305 
306 #endif /* !CFG_DT */
307 #endif /* KERNEL_DT_H */
308