Prepare v2023.10
[platform/kernel/u-boot.git] / include / dm / device-internal.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (C) 2013 Google, Inc
4  *
5  * (C) Copyright 2012
6  * Pavel Herrmann <morpheus.ibis@gmail.com>
7  * Marek Vasut <marex@denx.de>
8  */
9
10 #ifndef _DM_DEVICE_INTERNAL_H
11 #define _DM_DEVICE_INTERNAL_H
12
13 #include <event.h>
14 #include <linker_lists.h>
15 #include <dm/ofnode.h>
16
17 struct device_node;
18 struct driver_info;
19 struct udevice;
20
21 /*
22  * These two macros DM_DEVICE_INST and DM_DEVICE_REF are only allowed in code
23  * generated by dtoc, because the ordering is important and if other instances
24  * creep in then they may mess up the ordering expected by dtoc.
25  *
26  * It is OK to use them with 'extern' though, since that does not actually
27  * add a new record to the linker_list.
28  */
29
30 /**
31  * DM_DEVICE_INST() - Declare a bound device ready for run-time use
32  *
33  * This adds an actual struct udevice to a list which is found by driver model
34  * on start-up.
35  *
36  * For example:
37  *
38  * extern U_BOOT_DRIVER(sandbox_fixed_clock);
39  * extern DM_UCLASS_INST(clk);
40  *
41  * DM_DEVICE_INST(clk_fixed) = {
42  *      .driver         = DM_DRIVER_REF(sandbox_fixed_clock),
43  *      .name           = "sandbox_fixed_clock",
44  *      .plat_          = &_sandbox_fixed_clock_plat_clk_fixed,
45  *      .uclass         = DM_UCLASS_REF(clk),
46  *      ...
47  *      .seq_           = 0,
48  * };
49  *
50  * @_name: Name of the udevice. This must be a valid C identifier, used by the
51  *      linker_list.
52  */
53 #define DM_DEVICE_INST(_name)                                           \
54         ll_entry_declare(struct udevice, _name, udevice)
55
56 /**
57  * DM_DEVICE_REF() - Get a reference to a device
58  *
59  * This is useful in data structures and code for referencing a udevice at
60  * build time. Before this is used, an extern DM_DEVICE_INST() must have been
61  * declared.
62  *
63  * For example:
64  *
65  * extern DM_DEVICE_INST(clk_fixed);
66  *
67  * struct udevice *devs[] = {
68  *      DM_DEVICE_REF(clk_fixed),
69  * };
70  *
71  * @_name: Name of the udevice. This must be a valid C identifier, used by the
72  *      linker_list
73  * @returns struct udevice * for the device
74  */
75 #define DM_DEVICE_REF(_name)                                            \
76         ll_entry_ref(struct udevice, _name, udevice)
77
78 /**
79  * DM_DEVICE_GET() - Get a pointer to a given device
80  *
81  * This is similar to DM_DEVICE_REF() except that it does not need the extern
82  * declaration before it. However it cannot be used in a data structures, only
83  * in code within a function.
84  *
85  * For example:
86  *
87  * void some_function() {
88  *      struct udevice *dev = DM_DEVICE_GET(clk_fixed);
89  * ...
90  * }
91  */
92 #define DM_DEVICE_GET(__name)                                           \
93         ll_entry_get(struct udevice, __name, udevice)
94
95 /**
96  * device_bind() - Create a device and bind it to a driver
97  *
98  * Called to set up a new device attached to a driver. The device will either
99  * have plat, or a device tree node which can be used to create the
100  * plat.
101  *
102  * Once bound a device exists but is not yet active until device_probe() is
103  * called.
104  *
105  * @parent: Pointer to device's parent, under which this driver will exist
106  * @drv: Device's driver
107  * @name: Name of device (e.g. device tree node name)
108  * @plat: Pointer to data for this device - the structure is device-
109  * specific but may include the device's I/O address, etc.. This is NULL for
110  * devices which use device tree.
111  * @ofnode: Devicetree node for this device. This is ofnode_null() for
112  * devices which don't use devicetree or don't have a node.
113  * @devp: if non-NULL, returns a pointer to the bound device
114  * Return: 0 if OK, -ve on error
115  */
116 int device_bind(struct udevice *parent, const struct driver *drv,
117                 const char *name, void *plat, ofnode node,
118                 struct udevice **devp);
119
120 /**
121  * device_bind_with_driver_data() - Create a device and bind it to a driver
122  *
123  * Called to set up a new device attached to a driver, in the case where the
124  * driver was matched to the device by means of a match table that provides
125  * driver_data.
126  *
127  * Once bound a device exists but is not yet active until device_probe() is
128  * called.
129  *
130  * @parent: Pointer to device's parent, under which this driver will exist
131  * @drv: Device's driver
132  * @name: Name of device (e.g. device tree node name)
133  * @driver_data: The driver_data field from the driver's match table.
134  * @node: Device tree node for this device. This is invalid for devices which
135  * don't use device tree.
136  * @devp: if non-NULL, returns a pointer to the bound device
137  * Return: 0 if OK, -ve on error
138  */
139 int device_bind_with_driver_data(struct udevice *parent,
140                                  const struct driver *drv, const char *name,
141                                  ulong driver_data, ofnode node,
142                                  struct udevice **devp);
143 /**
144  * device_bind_by_name: Create a device and bind it to a driver
145  *
146  * This is a helper function used to bind devices which do not use device
147  * tree.
148  *
149  * @parent: Pointer to device's parent
150  * @pre_reloc_only: If true, bind the driver only if its DM_FLAG_PRE_RELOC flag
151  * is set. If false bind the driver always.
152  * @info: Name and plat for this device
153  * @devp: if non-NULL, returns a pointer to the bound device
154  * Return: 0 if OK, -ve on error
155  */
156 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
157                         const struct driver_info *info, struct udevice **devp);
158
159 /**
160  * device_reparent: reparent the device to a new parent
161  *
162  * @dev: pointer to device to be reparented
163  * @new_parent: pointer to new parent device
164  * Return: 0 if OK, -ve on error
165  */
166 int device_reparent(struct udevice *dev, struct udevice *new_parent);
167
168 /**
169  * device_of_to_plat() - Read platform data for a device
170  *
171  * Read platform data for a device (typically from the device tree) so that
172  * the information needed to probe the device is present.
173  *
174  * This may cause some others devices to be probed if this one depends on them,
175  * e.g. a GPIO line will cause a GPIO device to be probed.
176  *
177  * All private data associated with the device is allocated.
178  *
179  * @dev: Pointer to device to process
180  * Return: 0 if OK, -ve on error
181  */
182 int device_of_to_plat(struct udevice *dev);
183
184 /**
185  * device_probe() - Probe a device, activating it
186  *
187  * Activate a device (if not yet activated) so that it is ready for use.
188  * All its parents are probed first.
189  *
190  * @dev: Pointer to device to probe
191  * Return: 0 if OK, -ve on error
192  */
193 int device_probe(struct udevice *dev);
194
195 /**
196  * device_remove() - Remove a device, de-activating it
197  *
198  * De-activate a device so that it is no longer ready for use. All its
199  * children are deactivated first.
200  *
201  * @dev: Pointer to device to remove
202  * @flags: Flags for selective device removal (DM_REMOVE_...)
203  * Return: 0 if OK, -EKEYREJECTED if not removed due to flags, -EPROBE_DEFER if
204  *      this is a vital device and flags is DM_REMOVE_NON_VITAL, other -ve on
205  *      error (such an error here is normally a very bad thing)
206  */
207 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
208 int device_remove(struct udevice *dev, uint flags);
209 #else
210 static inline int device_remove(struct udevice *dev, uint flags) { return 0; }
211 #endif
212
213 /**
214  * device_unbind() - Unbind a device, destroying it
215  *
216  * Unbind a device and remove all memory used by it
217  *
218  * @dev: Pointer to device to unbind
219  * Return: 0 if OK, -ve on error
220  */
221 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
222 int device_unbind(struct udevice *dev);
223 #else
224 static inline int device_unbind(struct udevice *dev) { return 0; }
225 #endif
226
227 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
228 void device_free(struct udevice *dev);
229 #else
230 static inline void device_free(struct udevice *dev) {}
231 #endif
232
233 /**
234  * device_chld_unbind() - Unbind all device's children from the device if bound
235  *                        to drv
236  *
237  * On error, the function continues to unbind all children, and reports the
238  * first error.
239  *
240  * @dev:        The device that is to be stripped of its children
241  * @drv:        The targeted driver
242  * Return: 0 on success, -ve on error
243  */
244 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
245 int device_chld_unbind(struct udevice *dev, struct driver *drv);
246 #else
247 static inline int device_chld_unbind(struct udevice *dev, struct driver *drv)
248 {
249         return 0;
250 }
251 #endif
252
253 /**
254  * device_chld_remove() - Stop all device's children
255  *
256  * This continues through all children recursively stopping part-way through if
257  * an error occurs. Return values of -EKEYREJECTED are ignored and processing
258  * continues, since they just indicate that the child did not elect to be
259  * removed based on the value of @flags. Return values of -EPROBE_DEFER cause
260  * processing of other children to continue, but the function will return
261  * -EPROBE_DEFER.
262  *
263  * @dev:        The device whose children are to be removed
264  * @drv:        The targeted driver
265  * @flags:      Flag, if this functions is called in the pre-OS stage
266  * Return: 0 on success, -EPROBE_DEFER if any child failed to remove, other
267  *      -ve on error
268  */
269 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
270 int device_chld_remove(struct udevice *dev, struct driver *drv,
271                        uint flags);
272 #else
273 static inline int device_chld_remove(struct udevice *dev, struct driver *drv,
274                                      uint flags)
275 {
276         return 0;
277 }
278 #endif
279
280 /**
281  * dev_set_priv() - Set the private data for a device
282  *
283  * This is normally handled by driver model, which automatically allocates
284  * private data when an 'auto' size if provided by the driver.
285  *
286  * Use this function to override normal operation for special situations, such
287  * as needing to allocate a variable amount of data.
288  *
289  * If OF_PLATDATA_RT is enabled, this function cannot be used out of core driver
290  * model code, since the pointer must be within the gd->dm_priv_base region.
291  *
292  * @dev         Device to check
293  * @priv        New private-data pointer
294  */
295 void dev_set_priv(struct udevice *dev, void *priv);
296
297 /**
298  * dev_set_parent_priv() - Set the parent-private data for a device
299  *
300  * This is normally handled by driver model, which automatically allocates
301  * parent-private data when an 'auto' size if provided by the driver.
302  *
303  * Use this function to override normal operation for special situations, such
304  * as needing to allocate a variable amount of data.
305  *
306  * If OF_PLATDATA_RT is enabled, this function cannot be used out of core driver
307  * model code, since the pointer must be within the gd->dm_priv_base region.
308  *
309  * @dev:        Device to update
310  * @parent_priv: New parent-private data
311  */
312 void dev_set_parent_priv(struct udevice *dev, void *parent_priv);
313
314 /**
315  * dev_set_uclass_priv() - Set the uclass private data for a device
316  *
317  * This is normally handled by driver model, which automatically allocates
318  * uclass-private data when an 'auto' size if provided by the driver.
319  *
320  * Use this function to override normal operation for special situations, such
321  * as needing to allocate a variable amount of data.
322  *
323  * If OF_PLATDATA_RT is enabled, this function cannot be used out of core driver
324  * model code, since the pointer must be within the gd->dm_priv_base region.
325  *
326  * @dev:        Device to update
327  * @uclass_priv: New uclass private data
328  */
329 void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv);
330
331 /**
332  * dev_set_plat() - Set the platform data for a device
333  *
334  * This is normally handled by driver model, which automatically allocates
335  * platform data when an 'auto' size if provided by the driver.
336  *
337  * Use this function to override normal operation for special situations, such
338  * as needing to allocate a variable amount of data.
339  *
340  * If OF_PLATDATA_RT is enabled, this function cannot be used out of core driver
341  * model code, since the pointer must be within the gd->dm_priv_base region.
342  *
343  * @dev         Device to check
344  * @plat        New platform-data pointer
345  */
346 void dev_set_plat(struct udevice *dev, void *priv);
347
348 /**
349  * dev_set_parent_plat() - Set the parent platform data for a device
350  *
351  * This is normally handled by driver model, which automatically allocates
352  * parent platform data when an 'auto' size if provided by the driver.
353  *
354  * Use this function to override normal operation for special situations, such
355  * as needing to allocate a variable amount of data.
356  *
357  * If OF_PLATDATA_RT is enabled, this function cannot be used out of core driver
358  * model code, since the pointer must be within the gd->dm_priv_base region.
359  *
360  * @dev:        Device to update
361  * @parent_plat: New parent platform data
362  */
363 void dev_set_parent_plat(struct udevice *dev, void *parent_plat);
364
365 /**
366  * dev_set_uclass_plat() - Set the uclass platform data for a device
367  *
368  * This is normally handled by driver model, which automatically allocates
369  * uclass platform data when an 'auto' size if provided by the driver.
370  *
371  * Use this function to override normal operation for special situations, such
372  * as needing to allocate a variable amount of data.
373  *
374  * If OF_PLATDATA_RT is enabled, this function cannot be used out of core driver
375  * model code, since the pointer must be within the gd->dm_priv_base region.
376  *
377  * @dev:        Device to update
378  * @uclass_plat: New uclass platform data
379  */
380 void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat);
381
382 /**
383  * simple_bus_translate() - translate a bus address to a system address
384  *
385  * This handles the 'ranges' property in a simple bus. It translates the
386  * device address @addr to a system address using this property.
387  *
388  * @dev:        Simple bus device (parent of target device)
389  * @addr:       Address to translate
390  * Return: new address
391  */
392 fdt_addr_t simple_bus_translate(struct udevice *dev, fdt_addr_t addr);
393
394 /* Cast away any volatile pointer */
395 #define DM_ROOT_NON_CONST               (((gd_t *)gd)->dm_root)
396 #define DM_UCLASS_ROOT_NON_CONST        (((gd_t *)gd)->uclass_root)
397 #define DM_UCLASS_ROOT_S_NON_CONST      (((gd_t *)gd)->uclass_root_s)
398
399 /* device resource management */
400 #if CONFIG_IS_ENABLED(DEVRES)
401
402 /**
403  * devres_release_probe - Release managed resources allocated after probing
404  * @dev: Device to release resources for
405  *
406  * Release all resources allocated for @dev when it was probed or later.
407  * This function is called on driver removal.
408  */
409 void devres_release_probe(struct udevice *dev);
410
411 /**
412  * devres_release_all - Release all managed resources
413  * @dev: Device to release resources for
414  *
415  * Release all resources associated with @dev.  This function is
416  * called on driver unbinding.
417  */
418 void devres_release_all(struct udevice *dev);
419
420 #else /* ! DEVRES */
421
422 static inline void devres_release_probe(struct udevice *dev)
423 {
424 }
425
426 static inline void devres_release_all(struct udevice *dev)
427 {
428 }
429
430 #endif /* DEVRES */
431
432 static inline int device_notify(const struct udevice *dev, enum event_t type)
433 {
434 #if CONFIG_IS_ENABLED(DM_EVENT)
435         return event_notify(type, &dev, sizeof(dev));
436 #else
437         return 0;
438 #endif
439 }
440 #endif