blob: 35b625e7b1f9961a6e9c27ed3d6424146f1e9850 [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
11#include <common.h>
12#include <errno.h>
Simon Glass5f3c9e02020-03-28 14:03:47 -060013#include <log.h>
Simon Glassb38ea5a2014-11-10 17:16:47 -070014#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>
Anatolij Gustschin98381da2019-09-27 13:48:15 +053020#include <power-domain.h>
Simon Glassb38ea5a2014-11-10 17:16:47 -070021
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020022int device_chld_unbind(struct udevice *dev, struct driver *drv)
Simon Glassb38ea5a2014-11-10 17:16:47 -070023{
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) {
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020030 if (drv && (pos->driver != drv))
31 continue;
32
Simon Glassb38ea5a2014-11-10 17:16:47 -070033 ret = device_unbind(pos);
Simon Glass5f3c9e02020-03-28 14:03:47 -060034 if (ret && !saved_ret) {
35 log_warning("device '%s' failed to unbind\n",
36 pos->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -070037 saved_ret = ret;
Simon Glass5f3c9e02020-03-28 14:03:47 -060038 }
Simon Glassb38ea5a2014-11-10 17:16:47 -070039 }
40
Simon Glass5f3c9e02020-03-28 14:03:47 -060041 return log_ret(saved_ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070042}
43
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020044int device_chld_remove(struct udevice *dev, struct driver *drv,
45 uint flags)
Simon Glassb38ea5a2014-11-10 17:16:47 -070046{
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) {
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020053 if (drv && (pos->driver != drv))
54 continue;
55
Stefan Roese80b5bc92017-03-20 12:51:48 +010056 ret = device_remove(pos, flags);
Simon Glassb38ea5a2014-11-10 17:16:47 -070057 if (ret)
58 return ret;
59 }
60
61 return 0;
62}
63
64int device_unbind(struct udevice *dev)
65{
Simon Glassa626dff2015-03-25 12:21:54 -060066 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -070067 int ret;
68
69 if (!dev)
Simon Glass5f3c9e02020-03-28 14:03:47 -060070 return log_msg_ret("dev", -EINVAL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070071
Simon Glass6211d762020-12-19 10:40:10 -070072 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass5f3c9e02020-03-28 14:03:47 -060073 return log_msg_ret("active", -EINVAL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070074
Simon Glass6211d762020-12-19 10:40:10 -070075 if (!(dev_get_flags(dev) & DM_FLAG_BOUND))
Simon Glass5f3c9e02020-03-28 14:03:47 -060076 return log_msg_ret("not-bound", -EINVAL);
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +090077
Simon Glassb38ea5a2014-11-10 17:16:47 -070078 drv = dev->driver;
79 assert(drv);
80
81 if (drv->unbind) {
82 ret = drv->unbind(dev);
83 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -060084 return log_msg_ret("unbind", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070085 }
86
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020087 ret = device_chld_unbind(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070088 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -060089 return log_msg_ret("child unbind", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070090
Simon Glass6211d762020-12-19 10:40:10 -070091 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -070092 free(dev_get_plat(dev));
93 dev_set_plat(dev, NULL);
Simon Glassba750492015-01-25 08:27:00 -070094 }
Simon Glass6211d762020-12-19 10:40:10 -070095 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -070096 free(dev_get_uclass_plat(dev));
Simon Glass2543b162020-12-22 19:30:29 -070097 dev_set_uclass_plat(dev, NULL);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020098 }
Simon Glass6211d762020-12-19 10:40:10 -070099 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -0700100 free(dev_get_parent_plat(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700101 dev_set_parent_plat(dev, NULL);
Simon Glass11b61732015-01-25 08:27:01 -0700102 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700103 ret = uclass_unbind_device(dev);
104 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600105 return log_msg_ret("uc", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700106
107 if (dev->parent)
108 list_del(&dev->sibling_node);
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900109
110 devres_release_all(dev);
111
Simon Glass6211d762020-12-19 10:40:10 -0700112 if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
Simon Glass7760ba22016-05-01 13:52:23 -0600113 free((char *)dev->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700114 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 */
123void device_free(struct udevice *dev)
124{
125 int size;
126
Simon Glass8a2b47f2020-12-03 16:55:17 -0700127 if (dev->driver->priv_auto) {
Simon Glass95588622020-12-22 19:30:28 -0700128 free(dev_get_priv(dev));
129 dev_set_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700130 }
Simon Glass8a2b47f2020-12-03 16:55:17 -0700131 size = dev->uclass->uc_drv->per_device_auto;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700132 if (size) {
Simon Glass95588622020-12-22 19:30:28 -0700133 free(dev_get_uclass_priv(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700134 dev_set_uclass_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700135 }
136 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700137 size = dev->parent->driver->per_child_auto;
Simon Glassc23b4282015-01-25 08:27:06 -0700138 if (!size) {
139 size = dev->parent->uclass->uc_drv->
Simon Glass8a2b47f2020-12-03 16:55:17 -0700140 per_child_auto;
Simon Glassc23b4282015-01-25 08:27:06 -0700141 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700142 if (size) {
Simon Glass95588622020-12-22 19:30:28 -0700143 free(dev_get_parent_priv(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700144 dev_set_parent_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700145 }
146 }
Simon Glass6211d762020-12-19 10:40:10 -0700147 dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID);
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900148
149 devres_release_probe(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700150}
151
Stefan Roese07456762017-04-24 09:48:02 +0200152static bool flags_remove(uint flags, uint drv_flags)
153{
154 if ((flags & DM_REMOVE_NORMAL) ||
Marek Vasut386a68c2020-11-15 21:22:53 +0100155 (flags && (drv_flags & (DM_FLAG_ACTIVE_DMA | DM_FLAG_OS_PREPARE))))
Stefan Roese07456762017-04-24 09:48:02 +0200156 return true;
157
158 return false;
159}
160
Stefan Roese80b5bc92017-03-20 12:51:48 +0100161int device_remove(struct udevice *dev, uint flags)
Simon Glassb38ea5a2014-11-10 17:16:47 -0700162{
Simon Glassa626dff2015-03-25 12:21:54 -0600163 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700164 int ret;
165
166 if (!dev)
167 return -EINVAL;
168
Simon Glass6211d762020-12-19 10:40:10 -0700169 if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
Simon Glassb38ea5a2014-11-10 17:16:47 -0700170 return 0;
171
172 drv = dev->driver;
173 assert(drv);
174
Simon Glass6dd0d1d2021-01-24 14:32:44 -0700175 ret = device_chld_remove(dev, NULL, flags);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700176 if (ret)
177 return ret;
178
Simon Glass6dd0d1d2021-01-24 14:32:44 -0700179 ret = uclass_pre_remove_device(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700180 if (ret)
Simon Glass6dd0d1d2021-01-24 14:32:44 -0700181 return ret;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700182
Stefan Roese505045d2017-03-27 10:58:53 +0200183 /*
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 */
Stefan Roese07456762017-04-24 09:48:02 +0200187 if (drv->remove && flags_remove(flags, drv->flags)) {
Simon Glassb38ea5a2014-11-10 17:16:47 -0700188 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
Simon Glass42697f52020-03-28 14:03:48 -0600201 if (!(flags & DM_REMOVE_NO_PD) &&
202 !(drv->flags &
Simon Glass12810652021-01-24 14:32:42 -0700203 (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) &&
Anatolij Gustschincfb28f12020-02-17 12:36:43 +0100204 dev != gd->cur_serial_dev)
Anatolij Gustschin98381da2019-09-27 13:48:15 +0530205 dev_power_domain_off(dev);
206
Stefan Roese07456762017-04-24 09:48:02 +0200207 if (flags_remove(flags, drv->flags)) {
Stefan Roese505045d2017-03-27 10:58:53 +0200208 device_free(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700209
Simon Glass6211d762020-12-19 10:40:10 -0700210 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Stefan Roese505045d2017-03-27 10:58:53 +0200211 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700212
213 return ret;
214
215err_remove:
216 /* We can't put the children back */
217 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
218 __func__, dev->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700219
220 return ret;
221}