xref: /optee_os/core/include/kernel/dt_driver.h (revision 45f25897f4bcac14dc13e2adee7f1f96f117fcde)
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  * @args_count: Count of cells for the device
23  * @args: Device consumer specifiers
24  */
25 struct dt_driver_phandle_args {
26 	int args_count;
27 	uint32_t args[];
28 };
29 
30 /*
31  * get_of_device_func - Callback function for returning a driver private
32  *	instance based on a FDT phandle with possible arguments and the
33  *	registered dt_driver private data reference.
34  *
35  * @parg: phandle argument(s) referencing the device in the FDT.
36  * @data: driver private data registered in struct dt_driver.
37  * @res: Output result code of the operation: TEE_ERROR_BUSY is target
38  *	device is not yet initialized, otherwise any other compliant code.
39  *
40  * Return a device opaque reference, e.g. a struct clk pointer for a clock
41  * driver, or NULL if not found in which case @res provides the error code.
42  */
43 typedef void *(*get_of_device_func)(struct dt_driver_phandle_args *parg,
44 				    void *data, TEE_Result *res);
45 
46 /**
47  * dt_driver_register_provider - Register a driver provider
48  *
49  * @fdt: Device tree to work on
50  * @nodeoffset: Node offset in the FDT
51  * @get_of_device: Function to match the devicetree with a device instance
52  * @data: Data which will be passed to the @get_of_device callback
53  * @type: Driver type
54  *
55  * @get_of_device returns a void *. Driver provider is expected to
56  * include a shim helper to cast to device reference into provider driver
57  * target structure reference (e.g (struct clk *) for clock devices).
58  */
59 TEE_Result dt_driver_register_provider(const void *fdt, int nodeoffset,
60 				       get_of_device_func get_of_device,
61 				       void *data, enum dt_driver_type type);
62 
63 /*
64  * dt_driver_device_from_node_idx_prop - Return a device instance based on a
65  *	property name and FDT information
66  *
67  * @prop_name: DT property name, e.g. "clocks" for clock resources
68  * @fdt: FDT base address
69  * @nodeoffset: node offset in the FDT
70  * @prop_idx: index of the phandle data in the property
71  * @type: Driver type
72  * @res: Output result code of the operation: TEE_ERROR_BUSY is target
73  *	device is not yet initialized, otherwise any other compliant code.
74  *
75  * Return a device opaque reference, e.g. a struct clk pointer for a clock
76  * driver, or NULL if not found in which case @res provides the error code.
77  */
78 void *dt_driver_device_from_node_idx_prop(const char *prop_name,
79 					  const void *fdt, int nodeoffset,
80 					  unsigned int prop_idx,
81 					  enum dt_driver_type type,
82 					  TEE_Result *res);
83 
84 /*
85  * dt_driver_get_crypto() - Request crypto support for driver initialization
86  *
87  * Return TEE_SUCCESS if cryptography services are initialized, otherwise return
88  * TEE_ERROR_DEFER_DRIVER_INIT.
89  */
90 TEE_Result dt_driver_get_crypto(void);
91 
92 #ifdef CFG_DT
93 /* Inform DT driver probe sequence that core crypto support is initialized */
94 void dt_driver_crypt_init_complete(void);
95 #else
96 static inline void dt_driver_crypt_init_complete(void) {}
97 #endif
98 
99 /*
100  * Return driver provider reference from its node offset value in the FDT
101  */
102 struct dt_driver_provider *
103 dt_driver_get_provider_by_node(int nodeoffset, enum dt_driver_type type);
104 
105 /*
106  * Return driver provider reference from its phandle value in the FDT
107  */
108 struct dt_driver_provider *
109 dt_driver_get_provider_by_phandle(uint32_t phandle, enum dt_driver_type type);
110 
111 /*
112  * Return number cells used for phandle arguments by a driver provider
113  */
114 unsigned int dt_driver_provider_cells(struct dt_driver_provider *prv);
115 
116 /*
117  * dt_driver_probe_device_by_node - Probe matching driver to create a device
118  *	from a FDT node
119  *
120  * @fdt: FDT base address
121  * @nodeoffset: Node byte offset from FDT base
122  * @type: Target driver to match or DT_DRIVER_ANY
123  *
124  * Read the dt_driver database. Compatible list is looked up in the order
125  * of the FDT "compatible" property list. @type can be used to probe only
126  * specific drivers.
127  *
128  */
129 TEE_Result dt_driver_probe_device_by_node(const void *fdt, int nodeoffset,
130 					  enum dt_driver_type type);
131 
132 /*
133  * Get cells count of a device node given its dt_driver type
134  *
135  * @fdt: FDT base address
136  * @nodeoffset: Node offset on the FDT for the device
137  * @type: One of the supported DT_DRIVER_* value.
138  *
139  * Currently supports type DT_DRIVER_CLK.
140  * Return a positive cell count value (>= 0) or a negative FDT_ error code
141  */
142 int fdt_get_dt_driver_cells(const void *fdt, int nodeoffset,
143 			    enum dt_driver_type type);
144 
145 /*
146  * Called by bus like nodes to propose a node for dt_driver probing
147  *
148  * @fdt: FDT base address
149  * @nodeoffset: Node offset on the FDT for the device
150  */
151 TEE_Result dt_driver_maybe_add_probe_node(const void *fdt, int nodeoffset);
152 #endif /* __DT_DRIVER_H */
153