xref: /optee_os/core/include/kernel/dt.h (revision 5e5887713cbac1009fad29e0e01246ea5bae5639)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2016, 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 <stdint.h>
13 #include <types_ext.h>
14 #include <util.h>
15 
16 /*
17  * Bitfield to reflect status and secure-status values ("okay", "disabled"
18  * or not present)
19  */
20 #define DT_STATUS_DISABLED		U(0)
21 #define DT_STATUS_OK_NSEC		BIT(0)
22 #define DT_STATUS_OK_SEC		BIT(1)
23 
24 #define DT_INFO_INVALID_REG		((paddr_t)-1)
25 #define DT_INFO_INVALID_REG_SIZE	((ssize_t)-1)
26 #define DT_INFO_INVALID_CLOCK		-1
27 #define DT_INFO_INVALID_RESET		-1
28 #define DT_INFO_INVALID_INTERRUPT	-1
29 
30 /*
31  * @status: Bit mask for DT_STATUS_*
32  * @reg: Device register physical base address or DT_INFO_INVALID_REG
33  * @clock: Device identifier (positive value) or DT_INFO_INVALID_CLOCK
34  * @reset: Device reset identifier (positive value) or DT_INFO_INVALID_CLOCK
35  * @interrupt: Device interrupt identifier (positive value) or
36  * DT_INFO_INVALID_INTERRUPT
37  * @type: IRQ_TYPE_* value parsed from interrupts properties or IRQ_TYPE_NONE if
38  * not present
39  * @prio: interrupt priority parsed from interrupts properties or 0 if not
40  * present
41  */
42 struct dt_node_info {
43 	unsigned int status;
44 	paddr_t reg;
45 	int clock;
46 	int reset;
47 	int interrupt;
48 	uint32_t type;
49 	uint32_t prio;
50 };
51 
52 #if defined(CFG_DT)
53 
54 /*
55  * DT-aware drivers
56  */
57 
58 struct dt_device_match {
59 	const char *compatible;
60 };
61 
62 enum dt_driver_type {
63 	DT_DRIVER_NOTYPE,
64 	DT_DRIVER_UART,
65 };
66 
67 struct dt_driver {
68 	const char *name;
69 	enum dt_driver_type type;
70 	const struct dt_device_match *match_table; /* null-terminated */
71 	const void *driver;
72 };
73 
74 #define __dt_driver __section(".rodata.dtdrv" __SECTION_FLAGS_RODATA)
75 
76 /*
77  * Find a driver that is suitable for the given DT node, that is, with
78  * a matching "compatible" property.
79  *
80  * @fdt: pointer to the device tree
81  * @offs: node offset
82  */
83 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs);
84 
85 const struct dt_driver *__dt_driver_start(void);
86 
87 const struct dt_driver *__dt_driver_end(void);
88 
89 /*
90  * Map a device into secure or non-secure memory and return the base VA and
91  * the mapping size. The mapping is done with type MEM_AREA_IO_SEC or
92  * MEM_AREA_IO_NSEC, depending on the device status.
93  * If the mapping already exists, the function simply returns the @vbase and
94  * @size information.
95  *
96  * @offs is the offset of the node that describes the device in @fdt.
97  * @base receives the base virtual address corresponding to the base physical
98  * address of the "reg" property
99  * @size receives the size of the mapping
100  *
101  * Returns 0 on success or -1 in case of error.
102  */
103 int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size);
104 
105 /*
106  * Check whether the node at @offs contains the property with propname or not.
107  *
108  * @offs is the offset of the node that describes the device in @fdt.
109  * @propname is the property that need to check
110  *
111  * Returns true on success or false if no propname.
112  */
113 bool dt_have_prop(const void *fdt, int offs, const char *propname);
114 
115 /*
116  * Modify or add "status" property to "disabled"
117  *
118  * @fdt reference to the Device Tree
119  * @node is the node offset to modify
120  *
121  * Returns 0 on success or -1 on failure
122  */
123 int dt_disable_status(void *fdt, int node);
124 
125 /*
126  * Force secure-status = "okay" and status="disabled" for the target node.
127  *
128  * @fdt reference to the Device Tree
129  * @node is the node offset to modify
130  *
131  * Returns 0 on success or -1 on failure
132  */
133 int dt_enable_secure_status(void *fdt, int node);
134 
135 /*
136  * FDT manipulation functions, not provided by <libfdt.h>
137  */
138 
139 /*
140  * Return the base address for the "reg" property of the specified node or
141  * (paddr_t)-1 in case of error
142  */
143 paddr_t _fdt_reg_base_address(const void *fdt, int offs);
144 
145 /*
146  * Return the reg size for the reg property of the specified node or -1 in case
147  * of error
148  */
149 ssize_t _fdt_reg_size(const void *fdt, int offs);
150 
151 /*
152  * Read the status and secure-status properties into a bitfield.
153  * @status is set to DT_STATUS_DISABLED or a combination of DT_STATUS_OK_NSEC
154  * and DT_STATUS_OK_SEC.
155  */
156 int _fdt_get_status(const void *fdt, int offs);
157 
158 /*
159  * fdt_fill_device_info - Get generic device info from a node
160  *
161  * This function fills the generic information from a given node.
162  * Currently supports a single base register, a single clock,
163  * a single reset ID line and a single interrupt ID.
164  * Default DT_INFO_* macros are used when the relate property is not found.
165  */
166 void _fdt_fill_device_info(const void *fdt, struct dt_node_info *info,
167 			   int node);
168 
169 #else /* !CFG_DT */
170 
171 static inline const struct dt_driver *__dt_driver_start(void)
172 {
173 	return NULL;
174 }
175 
176 static inline const struct dt_driver *__dt_driver_end(void)
177 {
178 	return NULL;
179 }
180 
181 static inline const struct dt_driver *dt_find_compatible_driver(
182 					const void *fdt __unused,
183 					int offs __unused)
184 {
185 	return NULL;
186 }
187 
188 static inline int dt_map_dev(const void *fdt __unused, int offs __unused,
189 			     vaddr_t *vbase __unused, size_t *size __unused)
190 {
191 	return -1;
192 }
193 
194 static inline paddr_t _fdt_reg_base_address(const void *fdt __unused,
195 					    int offs __unused)
196 {
197 	return (paddr_t)-1;
198 }
199 
200 static inline ssize_t _fdt_reg_size(const void *fdt __unused,
201 				    int offs __unused)
202 {
203 	return -1;
204 }
205 
206 static inline int _fdt_get_status(const void *fdt __unused, int offs __unused)
207 {
208 	return -1;
209 }
210 
211 __noreturn
212 static inline void _fdt_fill_device_info(const void *fdt __unused,
213 					 struct dt_node_info *info __unused,
214 					 int node __unused)
215 {
216 	panic();
217 }
218 #endif /* !CFG_DT */
219 
220 #define for_each_dt_driver(drv) \
221 	for (drv = __dt_driver_start(); drv < __dt_driver_end(); drv++)
222 
223 #endif /* KERNEL_DT_H */
224