xref: /optee_os/core/include/kernel/dt_driver.h (revision 19a31ec40245ae01a9adcd206eec2a4bb4479fc9)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2021, Linaro Limited
4  * Copyright (c) 2021, Bootlin
5  * Copyright (c) 2021, STMicroelectronics
6  */
7 
8 #ifndef __KERNEL_DT_DRIVER_H
9 #define __KERNEL_DT_DRIVER_H
10 
11 #include <kernel/dt.h>
12 #include <stdint.h>
13 #include <sys/queue.h>
14 #include <tee_api_types.h>
15 
16 /*
17  * Type indentifiers for registered device drivers consumer can query
18  *
19  * DT_DRIVER_NOTYPE Generic type for when no generic FDT parsing is supported
20  * DT_DRIVER_UART   UART driver currently designed for console means
21  * DT_DRIVER_CLK    Clock controller using generic clock DT bindings
22  * DT_DRIVER_RSTCTRL Reset controller using generic reset DT bindings
23  * DT_DRIVER_I2C    I2C bus controller using generic I2C bus DT bindings
24  * DT_DRIVER_GPIO   GPIO controller using generic GPIO DT bindings
25  * DT_DRIVER_PINCTRL Pin controller using generic reset DT bindings
26  * DT_DRIVER_INTERRUPT Interrupt controller using generic DT bindings
27  * DT_DRIVER_REGULATOR Voltage regulator controller using generic DT bindings
28  * DT_DRIVER_NVMEM NVMEM controller using generic NVMEM DT bindings
29  */
30 enum dt_driver_type {
31 	DT_DRIVER_NOTYPE,
32 	DT_DRIVER_UART,
33 	DT_DRIVER_CLK,
34 	DT_DRIVER_RSTCTRL,
35 	DT_DRIVER_I2C,
36 	DT_DRIVER_GPIO,
37 	DT_DRIVER_PINCTRL,
38 	DT_DRIVER_INTERRUPT,
39 	DT_DRIVER_REGULATOR,
40 	DT_DRIVER_NVMEM,
41 };
42 
43 /*
44  * dt_driver_probe_func - Callback probe function for a driver.
45  *
46  * @fdt: FDT base address
47  * @nodeoffset: Offset of the node in the FDT
48  * @compat_data: Data registered for the compatible that probed the device
49  *
50  * Return TEE_SUCCESS on successful probe,
51  *	TEE_ERROR_DEFER_DRIVER_INIT if probe must be deferred
52  *	TEE_ERROR_ITEM_NOT_FOUND when no driver matched node's compatible string
53  *	Any other TEE_ERROR_* compliant code.
54  */
55 typedef TEE_Result (*dt_driver_probe_func)(const void *fdt, int nodeoffset,
56 					   const void *compat_data);
57 
58 /*
59  * Driver instance registered to be probed on compatible node found in the DT.
60  *
61  * @name: Driver name
62  * @type: Drive type
63  * @match_table: Compatible matching identifiers, null terminated
64  * @driver: Driver private reference or NULL
65  * @probe: Probe callback (see dt_driver_probe_func) or NULL
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 	TEE_Result (*probe)(const void *fdt, int node, const void *compat_data);
73 };
74 
75 #define DEFINE_DT_DRIVER(name) \
76 		SCATTERED_ARRAY_DEFINE_PG_ITEM(dt_drivers, struct dt_driver)
77 
78 #define for_each_dt_driver(drv) \
79 	for (drv = SCATTERED_ARRAY_BEGIN(dt_drivers, struct dt_driver); \
80 	     drv < SCATTERED_ARRAY_END(dt_drivers, struct dt_driver); \
81 	     drv++)
82 
83 /* Opaque reference to DT driver device provider instance */
84 struct dt_driver_provider;
85 
86 /**
87  * struct dt_pargs - Devicetree phandle arguments
88  * @fdt: Device-tree to work on
89  * @phandle_node: Node pointed by the specifier phandle
90  * @args_count: Count of cells for the device
91  * @args: Device consumer specifiers
92  */
93 struct dt_pargs {
94 	const void *fdt;
95 	int phandle_node;
96 	int args_count;
97 	uint32_t args[];
98 };
99 
100 /*
101  * get_of_device_func - Callback function for returning a driver private
102  *	instance based on a FDT phandle with possible arguments and the
103  *	registered dt_driver private data reference.
104  *
105  * @parg: phandle argument(s) referencing the device in the FDT.
106  * @data: driver private data registered in struct dt_driver.
107  * @device_ref: output device reference upon success, e.g. a struct clk
108  *	pointer for a clock driver.
109  *
110  * Return code:
111  * TEE_SUCCESS in case of success
112  * TEE_ERROR_DEFER_DRIVER_INIT if device driver is not yet initialized
113  * Any TEE_Result compliant code in case of error.
114  */
115 typedef TEE_Result (*get_of_device_func)(struct dt_pargs *parg, void *data,
116 					 void *device_ref);
117 
118 /**
119  * dt_driver_register_provider - Register a driver provider
120  *
121  * @fdt: Device tree to work on
122  * @nodeoffset: Node offset in the FDT
123  * @get_of_device: Function to match the devicetree with a device instance
124  * @data: Data which will be passed to the @get_of_device callback
125  * @type: Driver type
126  *
127  * @get_of_device returns a void *. Driver provider is expected to
128  * include a shim helper to cast to device reference into provider driver
129  * target structure reference (e.g (struct clk *) for clock devices).
130  */
131 TEE_Result dt_driver_register_provider(const void *fdt, int nodeoffset,
132 				       get_of_device_func get_of_device,
133 				       void *data, enum dt_driver_type type);
134 
135 /*
136  * dt_driver_device_from_node_idx_prop - Return a device instance based on a
137  *	property name and FDT information
138  *
139  * @prop_name: DT property name, e.g. "clocks" for clock resources
140  * @fdt: FDT base address
141  * @nodeoffset: node offset in the FDT
142  * @prop_idx: index of the phandle data in the property
143  * @type: Driver type
144  * @device_ref: output device opaque reference upon support, for example
145  *	a struct clk pointer for a clock driver.
146 
147  * Return code:
148  * TEE_SUCCESS in case of success,
149  * TEE_ERROR_DEFER_DRIVER_INIT if device driver is not yet initialized
150  * TEE_ERROR_ITEM_NOT_FOUND if @prop_name does not match a property's name
151  *	or @prop_idx does not match any index in @prop_name phandle list
152  * Any TEE_Result compliant code in case of error.
153  */
154 TEE_Result dt_driver_device_from_node_idx_prop(const char *prop_name,
155 					       const void *fdt, int nodeoffset,
156 					       unsigned int prop_idx,
157 					       enum dt_driver_type type,
158 					       void *device_ref);
159 
160 /*
161  * dt_driver_device_from_parent - Return a device instance based on the parent.
162  *	This is mainly used for the devices that are children of a controller
163  *	such as I2C, SPI and so on.
164  *
165  * @fdt: FDT base address
166  * @nodeoffset: node offset in the FDT
167  * @type: Driver type
168  * @device_ref: output device opaque reference upon success, for example
169  *	a struct i2c_dev pointer for a I2C bus driver
170  *
171  * Return code:
172  * TEE_SUCCESS in case of success,
173  * TEE_ERROR_DEFER_DRIVER_INIT if device driver is not yet initialized
174  * Any TEE_Result compliant code in case of error.
175  */
176 TEE_Result dt_driver_device_from_parent(const void *fdt, int nodeoffset,
177 					enum dt_driver_type type,
178 					void *device_ref);
179 
180 /*
181  * dt_driver_device_from_node_idx_prop_phandle() - Same as
182  *	dt_driver_device_from_node_idx_prop() but phandle is not the first
183  *	cells in property @prop_name but is passed as an argument.
184  *
185  * This function is used for DT bindings as "interrupts" property where the
186  * property carries the interrupt information but not the interrupt controller
187  * phandle which is found in a specific property (here "interrupt-parent").
188  */
189 TEE_Result dt_driver_device_from_node_idx_prop_phandle(const char *prop_name,
190 						       const void *fdt,
191 						       int nodeoffs,
192 						       unsigned int prop_index,
193 						       enum dt_driver_type type,
194 						       uint32_t phandle,
195 						       void *device_ref);
196 
197 /*
198  * dt_driver_get_crypto() - Request crypto support for driver initialization
199  *
200  * Return TEE_SUCCESS if cryptography services are initialized, otherwise return
201  * TEE_ERROR_DEFER_DRIVER_INIT.
202  */
203 TEE_Result dt_driver_get_crypto(void);
204 
205 #ifdef CFG_DT
206 /* Inform DT driver probe sequence that core crypto support is initialized */
207 void dt_driver_crypt_init_complete(void);
208 #else
209 static inline void dt_driver_crypt_init_complete(void) {}
210 #endif
211 
212 /*
213  * Return driver provider reference from its node offset value in the FDT
214  */
215 struct dt_driver_provider *
216 dt_driver_get_provider_by_node(int nodeoffset, enum dt_driver_type type);
217 
218 /*
219  * Return driver provider reference from its phandle value in the FDT
220  */
221 struct dt_driver_provider *
222 dt_driver_get_provider_by_phandle(uint32_t phandle, enum dt_driver_type type);
223 
224 /*
225  * Return number cells used for phandle arguments by a driver provider
226  */
227 unsigned int dt_driver_provider_cells(struct dt_driver_provider *prv);
228 
229 /*
230  * Return provider private data registered by dt_driver_register_provider()
231  */
232 void *dt_driver_provider_priv_data(struct dt_driver_provider *prv);
233 
234 /*
235  * dt_driver_probe_device_by_node - Probe matching driver to create a device
236  *	from a FDT node
237  *
238  * @fdt: FDT base address
239  * @nodeoffset: Node byte offset from FDT base
240  * @type: Target driver to match or DT_DRIVER_ANY
241  *
242  * Read the dt_driver database. Compatible list is looked up in the order
243  * of the FDT "compatible" property list. @type can be used to probe only
244  * specific drivers.
245  *
246  */
247 TEE_Result dt_driver_probe_device_by_node(const void *fdt, int nodeoffset,
248 					  enum dt_driver_type type);
249 
250 /*
251  * Get cells count of a device node given its dt_driver type
252  *
253  * @fdt: FDT base address
254  * @nodeoffset: Node offset on the FDT for the device
255  * @type: One of the supported DT_DRIVER_* value.
256  *
257  * Return a positive cell count value (>= 0) or a negative FDT_ error code
258  */
259 int fdt_get_dt_driver_cells(const void *fdt, int nodeoffset,
260 			    enum dt_driver_type type);
261 
262 /*
263  * Called by bus like nodes to propose a node for dt_driver probing
264  *
265  * @fdt: FDT base address
266  * @nodeoffset: Node offset on the FDT for the device
267  */
268 TEE_Result dt_driver_maybe_add_probe_node(const void *fdt, int nodeoffset);
269 
270 #ifdef CFG_DT_DRIVER_EMBEDDED_TEST
271 /*
272  * Return TEE_ERROR_NOT_IMPLEMENTED if test are not implemented
273  * otherwise return TEE_ERROR_GENERIC if some test has failed
274  * otherwise return TEE_SUCCESS (tests succeed or skipped)
275  */
276 TEE_Result dt_driver_test_status(void);
277 #else
278 static inline TEE_Result dt_driver_test_status(void)
279 {
280 	return TEE_ERROR_NOT_IMPLEMENTED;
281 }
282 #endif
283 #endif /* __KERNEL_DT_DRIVER_H */
284