xref: /optee_os/core/include/kernel/dt.h (revision c44d734b6366cbf4d12610310e809872db65f89d)
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 enum dt_driver_type {
66 	DT_DRIVER_NOTYPE,
67 	DT_DRIVER_UART,
68 	DT_DRIVER_CLK,
69 	DT_DRIVER_RSTCTRL,
70 };
71 
72 /*
73  * dt_driver_probe_func - Callback probe function for a driver.
74  *
75  * @fdt: FDT base address
76  * @nodeoffset: Offset of the node in the FDT
77  * @compat_data: Data registered for the compatible that probed the device
78  *
79  * Return TEE_SUCCESS on successful probe,
80  *	TEE_ERROR_DEFER_DRIVER_INIT if probe must be deferred
81  *	TEE_ERROR_ITEM_NOT_FOUND when no driver matched node's compatible string
82  *	Any other TEE_ERROR_* compliant code.
83  */
84 typedef TEE_Result (*dt_driver_probe_func)(const void *fdt, int nodeoffset,
85 					   const void *compat_data);
86 
87 #if defined(CFG_DT)
88 /*
89  * Driver instance registered to be probed on compatible node found in the DT.
90  *
91  * @name: Driver name
92  * @type: Drive type
93  * @match_table: Compatible matching identifiers, null terminated
94  * @driver: Driver private reference or NULL
95  * @probe: Probe callback (see dt_driver_probe_func) or NULL
96  */
97 struct dt_driver {
98 	const char *name;
99 	enum dt_driver_type type;
100 	const struct dt_device_match *match_table; /* null-terminated */
101 	const void *driver;
102 	TEE_Result (*probe)(const void *fdt, int node, const void *compat_data);
103 };
104 
105 #define DEFINE_DT_DRIVER(name) \
106 		SCATTERED_ARRAY_DEFINE_PG_ITEM(dt_drivers, struct dt_driver)
107 
108 /*
109  * Find a driver that is suitable for the given DT node, that is, with
110  * a matching "compatible" property.
111  *
112  * @fdt: pointer to the device tree
113  * @offs: node offset
114  */
115 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs);
116 
117 /*
118  * Map a device into secure or non-secure memory and return the base VA and
119  * the mapping size. The mapping is done with type MEM_AREA_IO_SEC or
120  * MEM_AREA_IO_NSEC, depending on the device status.
121  * If the mapping already exists, the function simply returns the @vbase and
122  * @size information.
123  *
124  * @offs is the offset of the node that describes the device in @fdt.
125  * @base receives the base virtual address corresponding to the base physical
126  * address of the "reg" property
127  * @size receives the size of the mapping
128  *
129  * Returns 0 on success or -1 in case of error.
130  */
131 int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size);
132 
133 /*
134  * Check whether the node at @offs contains the property with propname or not.
135  *
136  * @offs is the offset of the node that describes the device in @fdt.
137  * @propname is the property that need to check
138  *
139  * Returns true on success or false if no propname.
140  */
141 bool dt_have_prop(const void *fdt, int offs, const char *propname);
142 
143 /*
144  * Modify or add "status" property to "disabled"
145  *
146  * @fdt reference to the Device Tree
147  * @node is the node offset to modify
148  *
149  * Returns 0 on success or -1 on failure
150  */
151 int dt_disable_status(void *fdt, int node);
152 
153 /*
154  * Force secure-status = "okay" and status="disabled" for the target node.
155  *
156  * @fdt reference to the Device Tree
157  * @node is the node offset to modify
158  *
159  * Returns 0 on success or -1 on failure
160  */
161 int dt_enable_secure_status(void *fdt, int node);
162 
163 /*
164  * FDT manipulation functions, not provided by <libfdt.h>
165  */
166 
167 /*
168  * Return the base address for the "reg" property of the specified node or
169  * (paddr_t)-1 in case of error
170  */
171 paddr_t _fdt_reg_base_address(const void *fdt, int offs);
172 
173 /*
174  * Return the reg size for the reg property of the specified node or -1 in case
175  * of error
176  */
177 size_t _fdt_reg_size(const void *fdt, int offs);
178 
179 /*
180  * Read the status and secure-status properties into a bitfield.
181  * Return -1 on failure, DT_STATUS_DISABLED if the node is disabled,
182  * otherwise return a combination of DT_STATUS_OK_NSEC and DT_STATUS_OK_SEC.
183  */
184 int _fdt_get_status(const void *fdt, int offs);
185 
186 /*
187  * fdt_fill_device_info - Get generic device info from a node
188  *
189  * This function fills the generic information from a given node.
190  * Currently supports a single base register, a single clock,
191  * a single reset ID line and a single interrupt ID.
192  * Default DT_INFO_* macros are used when the relate property is not found.
193  */
194 void _fdt_fill_device_info(const void *fdt, struct dt_node_info *info,
195 			   int node);
196 /*
197  * Read cells from a given property of the given node. Any number of 32-bit
198  * cells of the property can be read. Returns 0 on success, or a negative
199  * FDT error value otherwise.
200  */
201 int _fdt_read_uint32_array(const void *fdt, int node, const char *prop_name,
202 			   uint32_t *array, size_t count);
203 
204 /*
205  * Read one cell from a given property of the given node.
206  * Returns 0 on success, or a negative FDT error value otherwise.
207  */
208 int _fdt_read_uint32(const void *fdt, int node, const char *prop_name,
209 		     uint32_t *value);
210 
211 /*
212  * Read one cell from a property of a cell or default to a given value
213  * Returns the 32bit cell value or @dflt_value on failure.
214  */
215 uint32_t _fdt_read_uint32_default(const void *fdt, int node,
216 				  const char *prop_name, uint32_t dflt_value);
217 
218 /*
219  * Check whether the node at @node has a reference name.
220  *
221  * @node is the offset of the node that describes the device in @fdt.
222  *
223  * Returns true on success or false if no property
224  */
225 bool _fdt_check_node(const void *fdt, int node);
226 
227 #else /* !CFG_DT */
228 
229 static inline const struct dt_driver *dt_find_compatible_driver(
230 					const void *fdt __unused,
231 					int offs __unused)
232 {
233 	return NULL;
234 }
235 
236 static inline int dt_map_dev(const void *fdt __unused, int offs __unused,
237 			     vaddr_t *vbase __unused, size_t *size __unused)
238 {
239 	return -1;
240 }
241 
242 static inline paddr_t _fdt_reg_base_address(const void *fdt __unused,
243 					    int offs __unused)
244 {
245 	return (paddr_t)-1;
246 }
247 
248 static inline size_t _fdt_reg_size(const void *fdt __unused,
249 				   int offs __unused)
250 {
251 	return (size_t)-1;
252 }
253 
254 static inline int _fdt_get_status(const void *fdt __unused, int offs __unused)
255 {
256 	return -1;
257 }
258 
259 __noreturn
260 static inline void _fdt_fill_device_info(const void *fdt __unused,
261 					 struct dt_node_info *info __unused,
262 					 int node __unused)
263 {
264 	panic();
265 }
266 
267 static inline int _fdt_read_uint32_array(const void *fdt __unused,
268 					 int node __unused,
269 					 const char *prop_name __unused,
270 					 uint32_t *array __unused,
271 					 size_t count __unused)
272 {
273 	return -1;
274 }
275 
276 static inline int _fdt_read_uint32(const void *fdt __unused,
277 				   int node __unused,
278 				   const char *prop_name __unused,
279 				   uint32_t *value __unused)
280 {
281 	return -1;
282 }
283 
284 static inline uint32_t _fdt_read_uint32_default(const void *fdt __unused,
285 						int node __unused,
286 						const char *prop_name __unused,
287 						uint32_t dflt_value __unused)
288 {
289 	return dflt_value;
290 }
291 
292 #endif /* !CFG_DT */
293 
294 #define for_each_dt_driver(drv) \
295 	for (drv = SCATTERED_ARRAY_BEGIN(dt_drivers, struct dt_driver); \
296 	     drv < SCATTERED_ARRAY_END(dt_drivers, struct dt_driver); \
297 	     drv++)
298 
299 #endif /* KERNEL_DT_H */
300