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