xref: /optee_os/core/include/kernel/dt.h (revision a9a8e483b6ff7f6e40c5ed95310a18e0bd1993c3)
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 struct dt_driver {
63 	const char *name;
64 	const struct dt_device_match *match_table; /* null-terminated */
65 	const void *driver;
66 };
67 
68 #define __dt_driver __section(".rodata.dtdrv" __SECTION_FLAGS_RODATA)
69 
70 /*
71  * Find a driver that is suitable for the given DT node, that is, with
72  * a matching "compatible" property.
73  *
74  * @fdt: pointer to the device tree
75  * @offs: node offset
76  */
77 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs);
78 
79 const struct dt_driver *__dt_driver_start(void);
80 
81 const struct dt_driver *__dt_driver_end(void);
82 
83 /*
84  * Map a device into secure or non-secure memory and return the base VA and
85  * the mapping size. The mapping is done with type MEM_AREA_IO_SEC or
86  * MEM_AREA_IO_NSEC, depending on the device status.
87  * If the mapping already exists, the function simply returns the @vbase and
88  * @size information.
89  *
90  * @offs is the offset of the node that describes the device in @fdt.
91  * @base receives the base virtual address corresponding to the base physical
92  * address of the "reg" property
93  * @size receives the size of the mapping
94  *
95  * Returns 0 on success or -1 in case of error.
96  */
97 int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size);
98 
99 /*
100  * Check whether the node at @offs contains the property with propname or not.
101  *
102  * @offs is the offset of the node that describes the device in @fdt.
103  * @propname is the property that need to check
104  *
105  * Returns true on success or false if no propname.
106  */
107 bool dt_have_prop(const void *fdt, int offs, const char *propname);
108 
109 /*
110  * Modify or add "status" property to "disabled"
111  *
112  * @fdt reference to the Device Tree
113  * @node is the node offset to modify
114  *
115  * Returns 0 on success or -1 on failure
116  */
117 int dt_disable_status(void *fdt, int node);
118 
119 /*
120  * Force secure-status = "okay" and status="disabled" for the target node.
121  *
122  * @fdt reference to the Device Tree
123  * @node is the node offset to modify
124  *
125  * Returns 0 on success or -1 on failure
126  */
127 int dt_enable_secure_status(void *fdt, int node);
128 
129 /*
130  * FDT manipulation functions, not provided by <libfdt.h>
131  */
132 
133 /*
134  * Return the base address for the "reg" property of the specified node or
135  * (paddr_t)-1 in case of error
136  */
137 paddr_t _fdt_reg_base_address(const void *fdt, int offs);
138 
139 /*
140  * Return the reg size for the reg property of the specified node or -1 in case
141  * of error
142  */
143 ssize_t _fdt_reg_size(const void *fdt, int offs);
144 
145 /*
146  * Read the status and secure-status properties into a bitfield.
147  * @status is set to DT_STATUS_DISABLED or a combination of DT_STATUS_OK_NSEC
148  * and DT_STATUS_OK_SEC.
149  */
150 int _fdt_get_status(const void *fdt, int offs);
151 
152 /*
153  * fdt_fill_device_info - Get generic device info from a node
154  *
155  * This function fills the generic information from a given node.
156  * Currently supports a single base register, a single clock,
157  * a single reset ID line and a single interrupt ID.
158  * Default DT_INFO_* macros are used when the relate property is not found.
159  */
160 void _fdt_fill_device_info(const void *fdt, struct dt_node_info *info,
161 			   int node);
162 
163 #else /* !CFG_DT */
164 
165 static inline const struct dt_driver *__dt_driver_start(void)
166 {
167 	return NULL;
168 }
169 
170 static inline const struct dt_driver *__dt_driver_end(void)
171 {
172 	return NULL;
173 }
174 
175 static inline const struct dt_driver *dt_find_compatible_driver(
176 					const void *fdt __unused,
177 					int offs __unused)
178 {
179 	return NULL;
180 }
181 
182 static inline int dt_map_dev(const void *fdt __unused, int offs __unused,
183 			     vaddr_t *vbase __unused, size_t *size __unused)
184 {
185 	return -1;
186 }
187 
188 static inline paddr_t _fdt_reg_base_address(const void *fdt __unused,
189 					    int offs __unused)
190 {
191 	return (paddr_t)-1;
192 }
193 
194 static inline ssize_t _fdt_reg_size(const void *fdt __unused,
195 				    int offs __unused)
196 {
197 	return -1;
198 }
199 
200 static inline int _fdt_get_status(const void *fdt __unused, int offs __unused)
201 {
202 	return -1;
203 }
204 
205 __noreturn
206 static inline void _fdt_fill_device_info(const void *fdt __unused,
207 					 struct dt_node_info *info __unused,
208 					 int node __unused)
209 {
210 	panic();
211 }
212 #endif /* !CFG_DT */
213 
214 #define for_each_dt_driver(drv) \
215 	for (drv = __dt_driver_start(); drv < __dt_driver_end(); drv++)
216 
217 #endif /* KERNEL_DT_H */
218