xref: /optee_os/core/include/kernel/dt_driver.h (revision 33cc94dfc6a51df36c10ff290090ea76d677d893)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2021, Linaro Limited
4  * Copyright (c) 2021, Bootlin
5  * Copyright (c) 2021, Linaro Limited
6  * Copyright (c) 2021, STMicroelectronics
7  */
8 
9 #ifndef __DT_DRIVER_H
10 #define __DT_DRIVER_H
11 
12 #include <kernel/dt.h>
13 #include <stdint.h>
14 #include <sys/queue.h>
15 #include <tee_api_types.h>
16 
17 /* Opaque reference to DT driver device provider instance */
18 struct dt_driver_provider;
19 
20 /**
21  * struct dt_driver_phandle_args - Devicetree phandle arguments
22  * @fdt: Device-tree to work on
23  * @phandle_node: Node pointed by the specifier phandle
24  * @args_count: Count of cells for the device
25  * @args: Device consumer specifiers
26  */
27 struct dt_driver_phandle_args {
28 	const void *fdt;
29 	int phandle_node;
30 	int args_count;
31 	uint32_t args[];
32 };
33 
34 /*
35  * get_of_device_func - Callback function for returning a driver private
36  *	instance based on a FDT phandle with possible arguments and the
37  *	registered dt_driver private data reference.
38  *
39  * @parg: phandle argument(s) referencing the device in the FDT.
40  * @data: driver private data registered in struct dt_driver.
41  * @res: Output result code of the operation:
42  *	TEE_SUCCESS in case of success
43  *	TEE_ERROR_DEFER_DRIVER_INIT if device driver is not yet initialized
44  *	Any TEE_Result compliant code in case of error.
45  *
46  * Return a device opaque reference, e.g. a struct clk pointer for a clock
47  * driver, or NULL if not found in which case @res provides the error code.
48  */
49 typedef void *(*get_of_device_func)(struct dt_driver_phandle_args *parg,
50 				    void *data, TEE_Result *res);
51 
52 /**
53  * dt_driver_register_provider - Register a driver provider
54  *
55  * @fdt: Device tree to work on
56  * @nodeoffset: Node offset in the FDT
57  * @get_of_device: Function to match the devicetree with a device instance
58  * @data: Data which will be passed to the @get_of_device callback
59  * @type: Driver type
60  *
61  * @get_of_device returns a void *. Driver provider is expected to
62  * include a shim helper to cast to device reference into provider driver
63  * target structure reference (e.g (struct clk *) for clock devices).
64  */
65 TEE_Result dt_driver_register_provider(const void *fdt, int nodeoffset,
66 				       get_of_device_func get_of_device,
67 				       void *data, enum dt_driver_type type);
68 
69 /*
70  * dt_driver_device_from_node_idx_prop - Return a device instance based on a
71  *	property name and FDT information
72  *
73  * @prop_name: DT property name, e.g. "clocks" for clock resources
74  * @fdt: FDT base address
75  * @nodeoffset: node offset in the FDT
76  * @prop_idx: index of the phandle data in the property
77  * @type: Driver type
78  * @res: Output result code of the operation:
79  *	TEE_SUCCESS in case of success
80  *	TEE_ERROR_DEFER_DRIVER_INIT if device driver is not yet initialized
81  *	TEE_ERROR_ITEM_NOT_FOUND if prop_name does not match a property's name
82  *	Any TEE_Result compliant code in case of error.
83  *
84  * Return a device opaque reference, e.g. a struct clk pointer for a clock
85  * driver, or NULL if not found in which case @res provides the error code.
86  */
87 void *dt_driver_device_from_node_idx_prop(const char *prop_name,
88 					  const void *fdt, int nodeoffset,
89 					  unsigned int prop_idx,
90 					  enum dt_driver_type type,
91 					  TEE_Result *res);
92 
93 /*
94  * dt_driver_get_crypto() - Request crypto support for driver initialization
95  *
96  * Return TEE_SUCCESS if cryptography services are initialized, otherwise return
97  * TEE_ERROR_DEFER_DRIVER_INIT.
98  */
99 TEE_Result dt_driver_get_crypto(void);
100 
101 #ifdef CFG_DT
102 /* Inform DT driver probe sequence that core crypto support is initialized */
103 void dt_driver_crypt_init_complete(void);
104 #else
105 static inline void dt_driver_crypt_init_complete(void) {}
106 #endif
107 
108 /*
109  * Return driver provider reference from its node offset value in the FDT
110  */
111 struct dt_driver_provider *
112 dt_driver_get_provider_by_node(int nodeoffset, enum dt_driver_type type);
113 
114 /*
115  * Return driver provider reference from its phandle value in the FDT
116  */
117 struct dt_driver_provider *
118 dt_driver_get_provider_by_phandle(uint32_t phandle, enum dt_driver_type type);
119 
120 /*
121  * Return number cells used for phandle arguments by a driver provider
122  */
123 unsigned int dt_driver_provider_cells(struct dt_driver_provider *prv);
124 
125 /*
126  * dt_driver_probe_device_by_node - Probe matching driver to create a device
127  *	from a FDT node
128  *
129  * @fdt: FDT base address
130  * @nodeoffset: Node byte offset from FDT base
131  * @type: Target driver to match or DT_DRIVER_ANY
132  *
133  * Read the dt_driver database. Compatible list is looked up in the order
134  * of the FDT "compatible" property list. @type can be used to probe only
135  * specific drivers.
136  *
137  */
138 TEE_Result dt_driver_probe_device_by_node(const void *fdt, int nodeoffset,
139 					  enum dt_driver_type type);
140 
141 /*
142  * Get cells count of a device node given its dt_driver type
143  *
144  * @fdt: FDT base address
145  * @nodeoffset: Node offset on the FDT for the device
146  * @type: One of the supported DT_DRIVER_* value.
147  *
148  * Return a positive cell count value (>= 0) or a negative FDT_ error code
149  */
150 int fdt_get_dt_driver_cells(const void *fdt, int nodeoffset,
151 			    enum dt_driver_type type);
152 
153 /*
154  * Called by bus like nodes to propose a node for dt_driver probing
155  *
156  * @fdt: FDT base address
157  * @nodeoffset: Node offset on the FDT for the device
158  */
159 TEE_Result dt_driver_maybe_add_probe_node(const void *fdt, int nodeoffset);
160 
161 #ifdef CFG_DT_DRIVER_EMBEDDED_TEST
162 /*
163  * Return TEE_ERROR_NOT_IMPLEMENTED if test are not implemented
164  * otherwise return TEE_ERROR_GENERIC if some test has failed
165  * otherwise return TEE_SUCCESS (tests succeed or skipped)
166  */
167 TEE_Result dt_driver_test_status(void);
168 #else
169 static inline TEE_Result dt_driver_test_status(void)
170 {
171 	return TEE_ERROR_NOT_IMPLEMENTED;
172 }
173 #endif
174 
175 #endif /* __DT_DRIVER_H */
176