1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2014 Google, Inc
11 #define LOG_CATEGORY LOGC_DM
16 #include <dm/device.h>
17 #include <dm/device-internal.h>
18 #include <dm/uclass.h>
19 #include <dm/uclass-internal.h>
21 #include <power-domain.h>
22 #include <asm/global_data.h>
24 int device_chld_unbind(struct udevice *dev, struct driver *drv)
26 struct udevice *pos, *n;
27 int ret, saved_ret = 0;
31 device_foreach_child_safe(pos, n, dev) {
32 if (drv && (pos->driver != drv))
35 ret = device_unbind(pos);
36 if (ret && !saved_ret) {
37 log_warning("device '%s' failed to unbind\n",
43 return log_ret(saved_ret);
46 int device_chld_remove(struct udevice *dev, struct driver *drv,
49 struct udevice *pos, *n;
54 device_foreach_child_safe(pos, n, dev) {
57 if (drv && (pos->driver != drv))
60 ret = device_remove(pos, flags);
61 if (ret == -EPROBE_DEFER)
63 else if (ret && ret != -EKEYREJECTED)
70 int device_unbind(struct udevice *dev)
72 const struct driver *drv;
76 return log_msg_ret("dev", -EINVAL);
78 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
79 return log_msg_ret("active", -EINVAL);
81 if (!(dev_get_flags(dev) & DM_FLAG_BOUND))
82 return log_msg_ret("not-bound", -EINVAL);
88 ret = drv->unbind(dev);
90 return log_msg_ret("unbind", ret);
93 ret = device_chld_unbind(dev, NULL);
95 return log_msg_ret("child unbind", ret);
97 ret = uclass_pre_unbind_device(dev);
99 return log_msg_ret("uc", ret);
100 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
101 free(dev_get_plat(dev));
102 dev_set_plat(dev, NULL);
104 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
105 free(dev_get_uclass_plat(dev));
106 dev_set_uclass_plat(dev, NULL);
108 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
109 free(dev_get_parent_plat(dev));
110 dev_set_parent_plat(dev, NULL);
112 ret = uclass_unbind_device(dev);
114 return log_msg_ret("uc", ret);
117 list_del(&dev->sibling_node);
119 devres_release_all(dev);
121 if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
122 free((char *)dev->name);
129 * device_free() - Free memory buffers allocated by a device
130 * @dev: Device that is to be started
132 void device_free(struct udevice *dev)
136 if (dev->driver->priv_auto) {
137 free(dev_get_priv(dev));
138 dev_set_priv(dev, NULL);
140 size = dev->uclass->uc_drv->per_device_auto;
142 free(dev_get_uclass_priv(dev));
143 dev_set_uclass_priv(dev, NULL);
146 size = dev->parent->driver->per_child_auto;
148 size = dev->parent->uclass->uc_drv->per_child_auto;
150 free(dev_get_parent_priv(dev));
151 dev_set_parent_priv(dev, NULL);
154 dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID);
156 devres_release_probe(dev);
160 * flags_remove() - Figure out whether to remove a device
162 * If this is called with @flags == DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_DMA,
163 * then it returns 0 (=go head and remove) if the device is not matked vital
164 * but is marked DM_REMOVE_ACTIVE_DMA.
166 * If this is called with @flags == DM_REMOVE_ACTIVE_DMA,
167 * then it returns 0 (=go head and remove) if the device is marked
168 * DM_REMOVE_ACTIVE_DMA, regardless of whether it is marked vital.
170 * @flags: Flags passed to device_remove()
171 * @drv_flags: Driver flags
172 * Return: 0 if the device should be removed,
173 * -EKEYREJECTED if @flags includes a flag in DM_REMOVE_ACTIVE_ALL but
174 * @drv_flags does not (indicates that this device has nothing to do for
175 * DMA shutdown or OS prepare)
176 * -EPROBE_DEFER if @flags is DM_REMOVE_NON_VITAL but @drv_flags contains
177 * DM_FLAG_VITAL (indicates the device is vital and should not be removed)
179 static int flags_remove(uint flags, uint drv_flags)
181 if (!(flags & DM_REMOVE_NORMAL)) {
185 active_match = !(flags & DM_REMOVE_ACTIVE_ALL) ||
187 vital_match = !(flags & DM_REMOVE_NON_VITAL) ||
188 !(drv_flags & DM_FLAG_VITAL);
190 return -EPROBE_DEFER;
192 return -EKEYREJECTED;
198 int device_remove(struct udevice *dev, uint flags)
200 const struct driver *drv;
206 if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
209 ret = device_notify(dev, EVT_DM_PRE_REMOVE);
214 * If the child returns EKEYREJECTED, continue. It just means that it
215 * didn't match the flags.
217 ret = device_chld_remove(dev, NULL, flags);
218 if (ret && ret != -EKEYREJECTED)
222 * Remove the device if called with the "normal" remove flag set,
223 * or if the remove flag matches any of the drivers remove flags
227 ret = flags_remove(flags, drv->flags);
229 log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n",
230 dev->name, flags, drv->flags, ret);
234 ret = uclass_pre_remove_device(dev);
239 ret = drv->remove(dev);
244 if (dev->parent && dev->parent->driver->child_post_remove) {
245 ret = dev->parent->driver->child_post_remove(dev);
247 dm_warn("%s: Device '%s' failed child_post_remove()",
248 __func__, dev->name);
252 if (!(flags & DM_REMOVE_NO_PD) &&
254 (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) &&
255 dev != gd->cur_serial_dev)
256 dev_power_domain_off(dev);
260 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
262 ret = device_notify(dev, EVT_DM_POST_REMOVE);
269 /* We can't put the children back */
270 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
271 __func__, dev->name);