Merge tag 'u-boot-atmel-fixes-2021.01-b' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / drivers / core / device-remove.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Device manager
4  *
5  * Copyright (c) 2014 Google, Inc
6  *
7  * (C) Copyright 2012
8  * Pavel Herrmann <morpheus.ibis@gmail.com>
9  */
10
11 #include <common.h>
12 #include <errno.h>
13 #include <log.h>
14 #include <malloc.h>
15 #include <dm/device.h>
16 #include <dm/device-internal.h>
17 #include <dm/uclass.h>
18 #include <dm/uclass-internal.h>
19 #include <dm/util.h>
20 #include <power-domain.h>
21
22 int device_chld_unbind(struct udevice *dev, struct driver *drv)
23 {
24         struct udevice *pos, *n;
25         int ret, saved_ret = 0;
26
27         assert(dev);
28
29         list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
30                 if (drv && (pos->driver != drv))
31                         continue;
32
33                 ret = device_unbind(pos);
34                 if (ret && !saved_ret) {
35                         log_warning("device '%s' failed to unbind\n",
36                                     pos->name);
37                         saved_ret = ret;
38                 }
39         }
40
41         return log_ret(saved_ret);
42 }
43
44 int device_chld_remove(struct udevice *dev, struct driver *drv,
45                        uint flags)
46 {
47         struct udevice *pos, *n;
48         int ret;
49
50         assert(dev);
51
52         list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
53                 if (drv && (pos->driver != drv))
54                         continue;
55
56                 ret = device_remove(pos, flags);
57                 if (ret)
58                         return ret;
59         }
60
61         return 0;
62 }
63
64 int device_unbind(struct udevice *dev)
65 {
66         const struct driver *drv;
67         int ret;
68
69         if (!dev)
70                 return log_msg_ret("dev", -EINVAL);
71
72         if (dev->flags & DM_FLAG_ACTIVATED)
73                 return log_msg_ret("active", -EINVAL);
74
75         if (!(dev->flags & DM_FLAG_BOUND))
76                 return log_msg_ret("not-bound", -EINVAL);
77
78         drv = dev->driver;
79         assert(drv);
80
81         if (drv->unbind) {
82                 ret = drv->unbind(dev);
83                 if (ret)
84                         return log_msg_ret("unbind", ret);
85         }
86
87         ret = device_chld_unbind(dev, NULL);
88         if (ret)
89                 return log_msg_ret("child unbind", ret);
90
91         if (dev->flags & DM_FLAG_ALLOC_PDATA) {
92                 free(dev->platdata);
93                 dev->platdata = NULL;
94         }
95         if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
96                 free(dev->uclass_platdata);
97                 dev->uclass_platdata = NULL;
98         }
99         if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
100                 free(dev->parent_platdata);
101                 dev->parent_platdata = NULL;
102         }
103         ret = uclass_unbind_device(dev);
104         if (ret)
105                 return log_msg_ret("uc", ret);
106
107         if (dev->parent)
108                 list_del(&dev->sibling_node);
109
110         devres_release_all(dev);
111
112         if (dev->flags & DM_FLAG_NAME_ALLOCED)
113                 free((char *)dev->name);
114         free(dev);
115
116         return 0;
117 }
118
119 /**
120  * device_free() - Free memory buffers allocated by a device
121  * @dev:        Device that is to be started
122  */
123 void device_free(struct udevice *dev)
124 {
125         int size;
126
127         if (dev->driver->priv_auto_alloc_size) {
128                 free(dev->priv);
129                 dev->priv = NULL;
130         }
131         size = dev->uclass->uc_drv->per_device_auto_alloc_size;
132         if (size) {
133                 free(dev->uclass_priv);
134                 dev->uclass_priv = NULL;
135         }
136         if (dev->parent) {
137                 size = dev->parent->driver->per_child_auto_alloc_size;
138                 if (!size) {
139                         size = dev->parent->uclass->uc_drv->
140                                         per_child_auto_alloc_size;
141                 }
142                 if (size) {
143                         free(dev->parent_priv);
144                         dev->parent_priv = NULL;
145                 }
146         }
147         dev->flags &= ~DM_FLAG_PLATDATA_VALID;
148
149         devres_release_probe(dev);
150 }
151
152 static bool flags_remove(uint flags, uint drv_flags)
153 {
154         if ((flags & DM_REMOVE_NORMAL) ||
155             (flags && (drv_flags & (DM_FLAG_ACTIVE_DMA | DM_FLAG_OS_PREPARE))))
156                 return true;
157
158         return false;
159 }
160
161 int device_remove(struct udevice *dev, uint flags)
162 {
163         const struct driver *drv;
164         int ret;
165
166         if (!dev)
167                 return -EINVAL;
168
169         if (!(dev->flags & DM_FLAG_ACTIVATED))
170                 return 0;
171
172         drv = dev->driver;
173         assert(drv);
174
175         ret = uclass_pre_remove_device(dev);
176         if (ret)
177                 return ret;
178
179         ret = device_chld_remove(dev, NULL, flags);
180         if (ret)
181                 goto err;
182
183         /*
184          * Remove the device if called with the "normal" remove flag set,
185          * or if the remove flag matches any of the drivers remove flags
186          */
187         if (drv->remove && flags_remove(flags, drv->flags)) {
188                 ret = drv->remove(dev);
189                 if (ret)
190                         goto err_remove;
191         }
192
193         if (dev->parent && dev->parent->driver->child_post_remove) {
194                 ret = dev->parent->driver->child_post_remove(dev);
195                 if (ret) {
196                         dm_warn("%s: Device '%s' failed child_post_remove()",
197                                 __func__, dev->name);
198                 }
199         }
200
201         if (!(flags & DM_REMOVE_NO_PD) &&
202             !(drv->flags &
203               (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_REMOVE_WITH_PD_ON)) &&
204             dev != gd->cur_serial_dev)
205                 dev_power_domain_off(dev);
206
207         if (flags_remove(flags, drv->flags)) {
208                 device_free(dev);
209
210                 dev->seq = -1;
211                 dev->flags &= ~DM_FLAG_ACTIVATED;
212         }
213
214         return ret;
215
216 err_remove:
217         /* We can't put the children back */
218         dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
219                 __func__, dev->name);
220 err:
221         ret = uclass_post_probe_device(dev);
222         if (ret) {
223                 dm_warn("%s: Device '%s' failed to post_probe on error path\n",
224                         __func__, dev->name);
225         }
226
227         return ret;
228 }