blob: 616dcf0785921bf11496729aaec331eb18e72bad [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb38ea5a2014-11-10 17:16:47 -07002/*
3 * Device manager
4 *
5 * Copyright (c) 2014 Google, Inc
6 *
7 * (C) Copyright 2012
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassb38ea5a2014-11-10 17:16:47 -07009 */
10
Simon Glassdf34d0b2021-01-24 14:32:45 -070011#define LOG_CATEGORY LOGC_DM
12
Simon Glassb38ea5a2014-11-10 17:16:47 -070013#include <common.h>
14#include <errno.h>
Simon Glass5f3c9e02020-03-28 14:03:47 -060015#include <log.h>
Simon Glassb38ea5a2014-11-10 17:16:47 -070016#include <malloc.h>
17#include <dm/device.h>
18#include <dm/device-internal.h>
19#include <dm/uclass.h>
20#include <dm/uclass-internal.h>
21#include <dm/util.h>
Anatolij Gustschin98381da2019-09-27 13:48:15 +053022#include <power-domain.h>
Simon Glassb38ea5a2014-11-10 17:16:47 -070023
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020024int device_chld_unbind(struct udevice *dev, struct driver *drv)
Simon Glassb38ea5a2014-11-10 17:16:47 -070025{
26 struct udevice *pos, *n;
27 int ret, saved_ret = 0;
28
29 assert(dev);
30
31 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020032 if (drv && (pos->driver != drv))
33 continue;
34
Simon Glassb38ea5a2014-11-10 17:16:47 -070035 ret = device_unbind(pos);
Simon Glass5f3c9e02020-03-28 14:03:47 -060036 if (ret && !saved_ret) {
37 log_warning("device '%s' failed to unbind\n",
38 pos->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -070039 saved_ret = ret;
Simon Glass5f3c9e02020-03-28 14:03:47 -060040 }
Simon Glassb38ea5a2014-11-10 17:16:47 -070041 }
42
Simon Glass5f3c9e02020-03-28 14:03:47 -060043 return log_ret(saved_ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070044}
45
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020046int device_chld_remove(struct udevice *dev, struct driver *drv,
47 uint flags)
Simon Glassb38ea5a2014-11-10 17:16:47 -070048{
49 struct udevice *pos, *n;
Marek Vasutabbdbbd2021-01-24 14:32:46 -070050 int result = 0;
Simon Glassb38ea5a2014-11-10 17:16:47 -070051
52 assert(dev);
53
54 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
Marek Vasutabbdbbd2021-01-24 14:32:46 -070055 int ret;
56
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020057 if (drv && (pos->driver != drv))
58 continue;
59
Stefan Roese80b5bc92017-03-20 12:51:48 +010060 ret = device_remove(pos, flags);
Marek Vasutabbdbbd2021-01-24 14:32:46 -070061 if (ret == -EPROBE_DEFER)
62 result = ret;
63 else if (ret && ret != -EKEYREJECTED)
Simon Glassb38ea5a2014-11-10 17:16:47 -070064 return ret;
65 }
66
Marek Vasutabbdbbd2021-01-24 14:32:46 -070067 return result;
Simon Glassb38ea5a2014-11-10 17:16:47 -070068}
69
70int device_unbind(struct udevice *dev)
71{
Simon Glassa626dff2015-03-25 12:21:54 -060072 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -070073 int ret;
74
75 if (!dev)
Simon Glass5f3c9e02020-03-28 14:03:47 -060076 return log_msg_ret("dev", -EINVAL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070077
Simon Glass6211d762020-12-19 10:40:10 -070078 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass5f3c9e02020-03-28 14:03:47 -060079 return log_msg_ret("active", -EINVAL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070080
Simon Glass6211d762020-12-19 10:40:10 -070081 if (!(dev_get_flags(dev) & DM_FLAG_BOUND))
Simon Glass5f3c9e02020-03-28 14:03:47 -060082 return log_msg_ret("not-bound", -EINVAL);
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +090083
Simon Glassb38ea5a2014-11-10 17:16:47 -070084 drv = dev->driver;
85 assert(drv);
86
87 if (drv->unbind) {
88 ret = drv->unbind(dev);
89 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -060090 return log_msg_ret("unbind", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070091 }
92
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020093 ret = device_chld_unbind(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070094 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -060095 return log_msg_ret("child unbind", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070096
Simon Glass6211d762020-12-19 10:40:10 -070097 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -070098 free(dev_get_plat(dev));
99 dev_set_plat(dev, NULL);
Simon Glassba750492015-01-25 08:27:00 -0700100 }
Simon Glass6211d762020-12-19 10:40:10 -0700101 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -0700102 free(dev_get_uclass_plat(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700103 dev_set_uclass_plat(dev, NULL);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200104 }
Simon Glass6211d762020-12-19 10:40:10 -0700105 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -0700106 free(dev_get_parent_plat(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700107 dev_set_parent_plat(dev, NULL);
Simon Glass11b61732015-01-25 08:27:01 -0700108 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700109 ret = uclass_unbind_device(dev);
110 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600111 return log_msg_ret("uc", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700112
113 if (dev->parent)
114 list_del(&dev->sibling_node);
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900115
116 devres_release_all(dev);
117
Simon Glass6211d762020-12-19 10:40:10 -0700118 if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
Simon Glass7760ba22016-05-01 13:52:23 -0600119 free((char *)dev->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700120 free(dev);
121
122 return 0;
123}
124
125/**
126 * device_free() - Free memory buffers allocated by a device
127 * @dev: Device that is to be started
128 */
129void device_free(struct udevice *dev)
130{
131 int size;
132
Simon Glass8a2b47f2020-12-03 16:55:17 -0700133 if (dev->driver->priv_auto) {
Simon Glass95588622020-12-22 19:30:28 -0700134 free(dev_get_priv(dev));
135 dev_set_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700136 }
Simon Glass8a2b47f2020-12-03 16:55:17 -0700137 size = dev->uclass->uc_drv->per_device_auto;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700138 if (size) {
Simon Glass95588622020-12-22 19:30:28 -0700139 free(dev_get_uclass_priv(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700140 dev_set_uclass_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700141 }
142 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700143 size = dev->parent->driver->per_child_auto;
Simon Glassc23b4282015-01-25 08:27:06 -0700144 if (!size) {
145 size = dev->parent->uclass->uc_drv->
Simon Glass8a2b47f2020-12-03 16:55:17 -0700146 per_child_auto;
Simon Glassc23b4282015-01-25 08:27:06 -0700147 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700148 if (size) {
Simon Glass95588622020-12-22 19:30:28 -0700149 free(dev_get_parent_priv(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700150 dev_set_parent_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700151 }
152 }
Simon Glass6211d762020-12-19 10:40:10 -0700153 dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID);
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900154
155 devres_release_probe(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700156}
157
Simon Glassdf34d0b2021-01-24 14:32:45 -0700158/**
159 * flags_remove() - Figure out whether to remove a device
160 *
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700161 * If this is called with @flags == DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_DMA,
162 * then it returns 0 (=go head and remove) if the device is not matked vital
163 * but is marked DM_REMOVE_ACTIVE_DMA.
164 *
165 * If this is called with @flags == DM_REMOVE_ACTIVE_DMA,
166 * then it returns 0 (=go head and remove) if the device is marked
167 * DM_REMOVE_ACTIVE_DMA, regardless of whether it is marked vital.
168 *
Simon Glassdf34d0b2021-01-24 14:32:45 -0700169 * @flags: Flags passed to device_remove()
170 * @drv_flags: Driver flags
171 * @return 0 if the device should be removed,
172 * -EKEYREJECTED if @flags includes a flag in DM_REMOVE_ACTIVE_ALL but
173 * @drv_flags does not (indicates that this device has nothing to do for
174 * DMA shutdown or OS prepare)
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700175 * -EPROBE_DEFER if @flags is DM_REMOVE_NON_VITAL but @drv_flags contains
176 * DM_FLAG_VITAL (indicates the device is vital and should not be removed)
Simon Glassdf34d0b2021-01-24 14:32:45 -0700177 */
178static int flags_remove(uint flags, uint drv_flags)
Stefan Roese07456762017-04-24 09:48:02 +0200179{
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700180 if (!(flags & DM_REMOVE_NORMAL)) {
181 bool vital_match;
182 bool active_match;
183
184 active_match = !(flags & DM_REMOVE_ACTIVE_ALL) ||
185 (drv_flags & flags);
186 vital_match = !(flags & DM_REMOVE_NON_VITAL) ||
187 !(drv_flags & DM_FLAG_VITAL);
188 if (!vital_match)
189 return -EPROBE_DEFER;
190 if (!active_match)
191 return -EKEYREJECTED;
192 }
Stefan Roese07456762017-04-24 09:48:02 +0200193
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700194 return 0;
Stefan Roese07456762017-04-24 09:48:02 +0200195}
196
Stefan Roese80b5bc92017-03-20 12:51:48 +0100197int device_remove(struct udevice *dev, uint flags)
Simon Glassb38ea5a2014-11-10 17:16:47 -0700198{
Simon Glassa626dff2015-03-25 12:21:54 -0600199 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700200 int ret;
201
202 if (!dev)
203 return -EINVAL;
204
Simon Glass6211d762020-12-19 10:40:10 -0700205 if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
Simon Glassb38ea5a2014-11-10 17:16:47 -0700206 return 0;
207
Simon Glassdf34d0b2021-01-24 14:32:45 -0700208 /*
209 * If the child returns EKEYREJECTED, continue. It just means that it
210 * didn't match the flags.
211 */
212 ret = device_chld_remove(dev, NULL, flags);
213 if (ret && ret != -EKEYREJECTED)
214 return ret;
215
216 /*
217 * Remove the device if called with the "normal" remove flag set,
218 * or if the remove flag matches any of the drivers remove flags
219 */
Simon Glassb38ea5a2014-11-10 17:16:47 -0700220 drv = dev->driver;
221 assert(drv);
Simon Glassdf34d0b2021-01-24 14:32:45 -0700222 ret = flags_remove(flags, drv->flags);
223 if (ret) {
224 log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n",
225 dev->name, flags, drv->flags, ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700226 return ret;
Simon Glassdf34d0b2021-01-24 14:32:45 -0700227 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700228
Simon Glass6dd0d1d2021-01-24 14:32:44 -0700229 ret = uclass_pre_remove_device(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700230 if (ret)
Simon Glass6dd0d1d2021-01-24 14:32:44 -0700231 return ret;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700232
Simon Glassdf34d0b2021-01-24 14:32:45 -0700233 if (drv->remove) {
Simon Glassb38ea5a2014-11-10 17:16:47 -0700234 ret = drv->remove(dev);
235 if (ret)
236 goto err_remove;
237 }
238
239 if (dev->parent && dev->parent->driver->child_post_remove) {
240 ret = dev->parent->driver->child_post_remove(dev);
241 if (ret) {
242 dm_warn("%s: Device '%s' failed child_post_remove()",
243 __func__, dev->name);
244 }
245 }
246
Simon Glass42697f52020-03-28 14:03:48 -0600247 if (!(flags & DM_REMOVE_NO_PD) &&
248 !(drv->flags &
Simon Glass12810652021-01-24 14:32:42 -0700249 (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) &&
Anatolij Gustschincfb28f12020-02-17 12:36:43 +0100250 dev != gd->cur_serial_dev)
Anatolij Gustschin98381da2019-09-27 13:48:15 +0530251 dev_power_domain_off(dev);
252
Simon Glassdf34d0b2021-01-24 14:32:45 -0700253 device_free(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700254
Simon Glassdf34d0b2021-01-24 14:32:45 -0700255 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700256
Simon Glassdf34d0b2021-01-24 14:32:45 -0700257 return 0;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700258
259err_remove:
260 /* We can't put the children back */
261 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
262 __func__, dev->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700263
264 return ret;
265}