blob: b513b6861a676b25eca0e302fde856d01126ee88 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass6494d702014-02-26 15:59:18 -07002/*
3 * Copyright (C) 2013 Google, Inc
4 *
5 * (C) Copyright 2012
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
7 * Marek Vasut <marex@denx.de>
Simon Glass6494d702014-02-26 15:59:18 -07008 */
9
10#ifndef _DM_DEVICE_INTERNAL_H
11#define _DM_DEVICE_INTERNAL_H
12
Simon Glass396e3432017-05-18 20:09:05 -060013#include <dm/ofnode.h>
14
15struct device_node;
Heiko Schocher54c5d082014-05-22 12:43:05 +020016struct udevice;
Simon Glass6494d702014-02-26 15:59:18 -070017
18/**
Simon Glasse80be742020-11-28 17:50:06 -070019 * device_bind() - Create a device and bind it to a driver
Simon Glass6494d702014-02-26 15:59:18 -070020 *
21 * Called to set up a new device attached to a driver. The device will either
Simon Glasscaa4daa2020-12-03 16:55:18 -070022 * have plat, or a device tree node which can be used to create the
23 * plat.
Simon Glass6494d702014-02-26 15:59:18 -070024 *
25 * Once bound a device exists but is not yet active until device_probe() is
26 * called.
27 *
28 * @parent: Pointer to device's parent, under which this driver will exist
29 * @drv: Device's driver
30 * @name: Name of device (e.g. device tree node name)
Simon Glasscaa4daa2020-12-03 16:55:18 -070031 * @plat: Pointer to data for this device - the structure is device-
Simon Glass6494d702014-02-26 15:59:18 -070032 * specific but may include the device's I/O address, etc.. This is NULL for
33 * devices which use device tree.
Simon Glasse80be742020-11-28 17:50:06 -070034 * @ofnode: Devicetree node for this device. This is ofnode_null() for
35 * devices which don't use devicetree or don't have a node.
Masahiro Yamadae6cabe42015-08-27 12:44:28 +090036 * @devp: if non-NULL, returns a pointer to the bound device
Simon Glass6494d702014-02-26 15:59:18 -070037 * @return 0 if OK, -ve on error
38 */
Simon Glass734206d2020-11-28 17:50:01 -070039int device_bind(struct udevice *parent, const struct driver *drv,
Simon Glasscaa4daa2020-12-03 16:55:18 -070040 const char *name, void *plat, ofnode node,
Simon Glass734206d2020-11-28 17:50:01 -070041 struct udevice **devp);
Simon Glassd677b002018-06-11 13:07:15 -060042
Simon Glass6494d702014-02-26 15:59:18 -070043/**
Stephen Warrendaac3bf2016-05-11 15:26:24 -060044 * device_bind_with_driver_data() - Create a device and bind it to a driver
45 *
46 * Called to set up a new device attached to a driver, in the case where the
47 * driver was matched to the device by means of a match table that provides
48 * driver_data.
49 *
50 * Once bound a device exists but is not yet active until device_probe() is
51 * called.
52 *
53 * @parent: Pointer to device's parent, under which this driver will exist
54 * @drv: Device's driver
55 * @name: Name of device (e.g. device tree node name)
56 * @driver_data: The driver_data field from the driver's match table.
Simon Glass396e3432017-05-18 20:09:05 -060057 * @node: Device tree node for this device. This is invalid for devices which
58 * don't use device tree.
Stephen Warrendaac3bf2016-05-11 15:26:24 -060059 * @devp: if non-NULL, returns a pointer to the bound device
60 * @return 0 if OK, -ve on error
61 */
62int device_bind_with_driver_data(struct udevice *parent,
63 const struct driver *drv, const char *name,
Simon Glass396e3432017-05-18 20:09:05 -060064 ulong driver_data, ofnode node,
Stephen Warrendaac3bf2016-05-11 15:26:24 -060065 struct udevice **devp);
Stephen Warrendaac3bf2016-05-11 15:26:24 -060066/**
Simon Glass6494d702014-02-26 15:59:18 -070067 * device_bind_by_name: Create a device and bind it to a driver
68 *
69 * This is a helper function used to bind devices which do not use device
70 * tree.
71 *
72 * @parent: Pointer to device's parent
Bin Meng6244fc62018-10-10 22:06:59 -070073 * @pre_reloc_only: If true, bind the driver only if its DM_FLAG_PRE_RELOC flag
74 * is set. If false bind the driver always.
Simon Glasscaa4daa2020-12-03 16:55:18 -070075 * @info: Name and plat for this device
Masahiro Yamadae6cabe42015-08-27 12:44:28 +090076 * @devp: if non-NULL, returns a pointer to the bound device
Simon Glass6494d702014-02-26 15:59:18 -070077 * @return 0 if OK, -ve on error
78 */
Simon Glass00606d72014-07-23 06:55:03 -060079int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Simon Glassa294ead2020-10-03 11:31:33 -060080 const struct driver_info *info, struct udevice **devp);
Simon Glass6494d702014-02-26 15:59:18 -070081
82/**
Claudiu Bezneacfecbaf2020-09-07 17:46:33 +030083 * device_reparent: reparent the device to a new parent
84 *
85 * @dev: pointer to device to be reparented
86 * @new_parent: pointer to new parent device
87 * @return 0 if OK, -ve on error
88 */
89int device_reparent(struct udevice *dev, struct udevice *new_parent);
90
91/**
Simon Glassd1998a92020-12-03 16:55:21 -070092 * device_of_to_plat() - Read platform data for a device
Simon Glassbcd90cb2019-12-29 21:19:20 -070093 *
94 * Read platform data for a device (typically from the device tree) so that
95 * the information needed to probe the device is present.
96 *
97 * This may cause some others devices to be probed if this one depends on them,
98 * e.g. a GPIO line will cause a GPIO device to be probed.
99 *
100 * All private data associated with the device is allocated.
101 *
102 * @dev: Pointer to device to process
103 * @return 0 if OK, -ve on error
104 */
Simon Glassd1998a92020-12-03 16:55:21 -0700105int device_of_to_plat(struct udevice *dev);
Simon Glassbcd90cb2019-12-29 21:19:20 -0700106
107/**
Simon Glass6494d702014-02-26 15:59:18 -0700108 * device_probe() - Probe a device, activating it
109 *
110 * Activate a device so that it is ready for use. All its parents are probed
111 * first.
112 *
113 * @dev: Pointer to device to probe
114 * @return 0 if OK, -ve on error
115 */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200116int device_probe(struct udevice *dev);
Simon Glass6494d702014-02-26 15:59:18 -0700117
118/**
119 * device_remove() - Remove a device, de-activating it
120 *
121 * De-activate a device so that it is no longer ready for use. All its
122 * children are deactivated first.
123 *
124 * @dev: Pointer to device to remove
Simon Glasse88afcc2017-07-29 11:35:00 -0600125 * @flags: Flags for selective device removal (DM_REMOVE_...)
Simon Glassc51d2e72021-01-24 14:32:45 -0700126 * @return 0 if OK, -EKEYREJECTED if not removed due to flags, other -ve on
127 * error (such an error here is normally a very bad thing)
Simon Glass6494d702014-02-26 15:59:18 -0700128 */
Masahiro Yamada0a5804b2015-08-12 07:31:52 +0900129#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Stefan Roese706865a2017-03-20 12:51:48 +0100130int device_remove(struct udevice *dev, uint flags);
Simon Glass3ac435d2014-11-10 17:16:47 -0700131#else
Stefan Roese706865a2017-03-20 12:51:48 +0100132static inline int device_remove(struct udevice *dev, uint flags) { return 0; }
Simon Glass3ac435d2014-11-10 17:16:47 -0700133#endif
Simon Glass6494d702014-02-26 15:59:18 -0700134
135/**
136 * device_unbind() - Unbind a device, destroying it
137 *
138 * Unbind a device and remove all memory used by it
139 *
140 * @dev: Pointer to device to unbind
141 * @return 0 if OK, -ve on error
142 */
Masahiro Yamada0a5804b2015-08-12 07:31:52 +0900143#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocher54c5d082014-05-22 12:43:05 +0200144int device_unbind(struct udevice *dev);
Marek Vasut66c03152015-02-18 22:36:18 +0100145#else
146static inline int device_unbind(struct udevice *dev) { return 0; }
147#endif
Simon Glass6494d702014-02-26 15:59:18 -0700148
Masahiro Yamada0a5804b2015-08-12 07:31:52 +0900149#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Simon Glass3ac435d2014-11-10 17:16:47 -0700150void device_free(struct udevice *dev);
151#else
152static inline void device_free(struct udevice *dev) {}
153#endif
154
Simon Glassf3301772015-07-07 20:53:44 -0600155/**
Jean-Jacques Hiblot3be9bcb2018-08-09 16:17:45 +0200156 * device_chld_unbind() - Unbind all device's children from the device if bound
157 * to drv
158 *
159 * On error, the function continues to unbind all children, and reports the
160 * first error.
161 *
162 * @dev: The device that is to be stripped of its children
163 * @drv: The targeted driver
164 * @return 0 on success, -ve on error
165 */
166#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
167int device_chld_unbind(struct udevice *dev, struct driver *drv);
168#else
169static inline int device_chld_unbind(struct udevice *dev, struct driver *drv)
170{
171 return 0;
172}
173#endif
174
175/**
176 * device_chld_remove() - Stop all device's children
Simon Glassc51d2e72021-01-24 14:32:45 -0700177 *
178 * This continues through all children recursively stopping part-way through if
179 * an error occurs. Return values of -EKEYREJECTED are ignored and processing
180 * continues, since they just indicate that the child did not elect to be
181 * removed based on the value of @flags.
182 *
Jean-Jacques Hiblot3be9bcb2018-08-09 16:17:45 +0200183 * @dev: The device whose children are to be removed
184 * @drv: The targeted driver
185 * @flags: Flag, if this functions is called in the pre-OS stage
186 * @return 0 on success, -ve on error
187 */
188#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
189int device_chld_remove(struct udevice *dev, struct driver *drv,
190 uint flags);
191#else
192static inline int device_chld_remove(struct udevice *dev, struct driver *drv,
193 uint flags)
194{
195 return 0;
196}
197#endif
198
199/**
Simon Glass12559f52020-12-22 19:30:27 -0700200 * dev_set_priv() - Set the private data for a device
201 *
202 * This is normally handled by driver model, which automatically allocates
203 * private data when an 'auto' size if provided by the driver.
204 *
205 * Use this function to override normal operation for special situations, such
206 * as needing to allocate a variable amount of data.
207 *
208 * @dev Device to check
209 * @priv New private-data pointer
210 */
211void dev_set_priv(struct udevice *dev, void *priv);
212
213/**
214 * dev_set_parent_priv() - Set the parent-private data for a device
215 *
216 * This is normally handled by driver model, which automatically allocates
217 * parent-private data when an 'auto' size if provided by the driver.
218 *
219 * Use this function to override normal operation for special situations, such
220 * as needing to allocate a variable amount of data.
221 *
222 * @dev: Device to update
223 * @parent_priv: New parent-private data
224 */
225void dev_set_parent_priv(struct udevice *dev, void *parent_priv);
226
227/**
228 * dev_set_uclass_priv() - Set the uclass private data for a device
229 *
230 * This is normally handled by driver model, which automatically allocates
231 * uclass-private data when an 'auto' size if provided by the driver.
232 *
233 * Use this function to override normal operation for special situations, such
234 * as needing to allocate a variable amount of data.
235 *
236 * @dev: Device to update
237 * @uclass_priv: New uclass private data
238 */
239void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv);
240
241/**
242 * dev_set_plat() - Set the platform data for a device
243 *
244 * This is normally handled by driver model, which automatically allocates
245 * platform data when an 'auto' size if provided by the driver.
246 *
247 * Use this function to override normal operation for special situations, such
248 * as needing to allocate a variable amount of data.
249 *
250 * @dev Device to check
251 * @plat New platform-data pointer
252 */
253void dev_set_plat(struct udevice *dev, void *priv);
254
255/**
256 * dev_set_parent_plat() - Set the parent platform data for a device
257 *
258 * This is normally handled by driver model, which automatically allocates
259 * parent platform data when an 'auto' size if provided by the driver.
260 *
261 * Use this function to override normal operation for special situations, such
262 * as needing to allocate a variable amount of data.
263 *
264 * @dev: Device to update
265 * @parent_plat: New parent platform data
266 */
267void dev_set_parent_plat(struct udevice *dev, void *parent_plat);
268
269/**
270 * dev_set_uclass_plat() - Set the uclass platform data for a device
271 *
272 * This is normally handled by driver model, which automatically allocates
273 * uclass platform data when an 'auto' size if provided by the driver.
274 *
275 * Use this function to override normal operation for special situations, such
276 * as needing to allocate a variable amount of data.
277 *
278 * @dev: Device to update
279 * @uclass_plat: New uclass platform data
280 */
281void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat);
282
283/**
Simon Glassf3301772015-07-07 20:53:44 -0600284 * simple_bus_translate() - translate a bus address to a system address
285 *
286 * This handles the 'ranges' property in a simple bus. It translates the
287 * device address @addr to a system address using this property.
288 *
289 * @dev: Simple bus device (parent of target device)
290 * @addr: Address to translate
291 * @return new address
292 */
293fdt_addr_t simple_bus_translate(struct udevice *dev, fdt_addr_t addr);
294
Simon Glass89876a52014-06-11 23:29:49 -0600295/* Cast away any volatile pointer */
296#define DM_ROOT_NON_CONST (((gd_t *)gd)->dm_root)
297#define DM_UCLASS_ROOT_NON_CONST (((gd_t *)gd)->uclass_root)
Simon Glass8a715532020-12-19 10:40:17 -0700298#define DM_UCLASS_ROOT_S_NON_CONST (((gd_t *)gd)->uclass_root_s)
Simon Glass89876a52014-06-11 23:29:49 -0600299
Masahiro Yamada608f26c2015-07-25 21:52:35 +0900300/* device resource management */
Masahiro Yamadae2282d72015-07-25 21:52:37 +0900301#ifdef CONFIG_DEVRES
302
Masahiro Yamada608f26c2015-07-25 21:52:35 +0900303/**
304 * devres_release_probe - Release managed resources allocated after probing
305 * @dev: Device to release resources for
306 *
307 * Release all resources allocated for @dev when it was probed or later.
308 * This function is called on driver removal.
309 */
310void devres_release_probe(struct udevice *dev);
311
312/**
313 * devres_release_all - Release all managed resources
314 * @dev: Device to release resources for
315 *
316 * Release all resources associated with @dev. This function is
317 * called on driver unbinding.
318 */
319void devres_release_all(struct udevice *dev);
320
Masahiro Yamadae2282d72015-07-25 21:52:37 +0900321#else /* ! CONFIG_DEVRES */
322
323static inline void devres_release_probe(struct udevice *dev)
324{
325}
326
327static inline void devres_release_all(struct udevice *dev)
328{
329}
330
331#endif /* ! CONFIG_DEVRES */
Simon Glass6494d702014-02-26 15:59:18 -0700332#endif