blob: efdb0f29058f641837d936389563ceb47543d980 [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
72 if (dev->flags & 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
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +090075 if (!(dev->flags & 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 Glassba750492015-01-25 08:27:00 -070091 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
92 free(dev->platdata);
93 dev->platdata = NULL;
94 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020095 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
96 free(dev->uclass_platdata);
97 dev->uclass_platdata = NULL;
98 }
Simon Glass11b61732015-01-25 08:27:01 -070099 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
100 free(dev->parent_platdata);
101 dev->parent_platdata = NULL;
102 }
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 Glass2d4c7dc2016-07-04 11:58:15 -0600112 if (dev->flags & 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
127 if (dev->driver->priv_auto_alloc_size) {
128 free(dev->priv);
129 dev->priv = NULL;
130 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700131 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;
Simon Glassc23b4282015-01-25 08:27:06 -0700138 if (!size) {
139 size = dev->parent->uclass->uc_drv->
140 per_child_auto_alloc_size;
141 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700142 if (size) {
143 free(dev->parent_priv);
144 dev->parent_priv = NULL;
145 }
146 }
Simon Glassd3c9d302019-12-29 21:19:21 -0700147 dev->flags &= ~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) ||
155 (flags & (drv_flags & (DM_FLAG_ACTIVE_DMA | DM_FLAG_OS_PREPARE))))
156 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
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
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +0200179 ret = device_chld_remove(dev, NULL, flags);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700180 if (ret)
181 goto err;
182
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 &
Anatolij Gustschincfb28f12020-02-17 12:36:43 +0100203 (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_REMOVE_WITH_PD_ON)) &&
204 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
Stefan Roese505045d2017-03-27 10:58:53 +0200210 dev->seq = -1;
211 dev->flags &= ~DM_FLAG_ACTIVATED;
212 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700213
214 return ret;
215
216err_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);
220err:
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}