blob: 11d3959d20f4b23eb23fd9d720c821b9c24e4b48 [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 Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Simon Glassb38ea5a2014-11-10 17:16:47 -070024
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020025int device_chld_unbind(struct udevice *dev, struct driver *drv)
Simon Glassb38ea5a2014-11-10 17:16:47 -070026{
27 struct udevice *pos, *n;
28 int ret, saved_ret = 0;
29
30 assert(dev);
31
32 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020033 if (drv && (pos->driver != drv))
34 continue;
35
Simon Glassb38ea5a2014-11-10 17:16:47 -070036 ret = device_unbind(pos);
Simon Glass5f3c9e02020-03-28 14:03:47 -060037 if (ret && !saved_ret) {
38 log_warning("device '%s' failed to unbind\n",
39 pos->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -070040 saved_ret = ret;
Simon Glass5f3c9e02020-03-28 14:03:47 -060041 }
Simon Glassb38ea5a2014-11-10 17:16:47 -070042 }
43
Simon Glass5f3c9e02020-03-28 14:03:47 -060044 return log_ret(saved_ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070045}
46
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020047int device_chld_remove(struct udevice *dev, struct driver *drv,
48 uint flags)
Simon Glassb38ea5a2014-11-10 17:16:47 -070049{
50 struct udevice *pos, *n;
Marek Vasutabbdbbd2021-01-24 14:32:46 -070051 int result = 0;
Simon Glassb38ea5a2014-11-10 17:16:47 -070052
53 assert(dev);
54
55 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
Marek Vasutabbdbbd2021-01-24 14:32:46 -070056 int ret;
57
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020058 if (drv && (pos->driver != drv))
59 continue;
60
Stefan Roese80b5bc92017-03-20 12:51:48 +010061 ret = device_remove(pos, flags);
Marek Vasutabbdbbd2021-01-24 14:32:46 -070062 if (ret == -EPROBE_DEFER)
63 result = ret;
64 else if (ret && ret != -EKEYREJECTED)
Simon Glassb38ea5a2014-11-10 17:16:47 -070065 return ret;
66 }
67
Marek Vasutabbdbbd2021-01-24 14:32:46 -070068 return result;
Simon Glassb38ea5a2014-11-10 17:16:47 -070069}
70
71int device_unbind(struct udevice *dev)
72{
Simon Glassa626dff2015-03-25 12:21:54 -060073 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -070074 int ret;
75
76 if (!dev)
Simon Glass5f3c9e02020-03-28 14:03:47 -060077 return log_msg_ret("dev", -EINVAL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070078
Simon Glass6211d762020-12-19 10:40:10 -070079 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass5f3c9e02020-03-28 14:03:47 -060080 return log_msg_ret("active", -EINVAL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070081
Simon Glass6211d762020-12-19 10:40:10 -070082 if (!(dev_get_flags(dev) & DM_FLAG_BOUND))
Simon Glass5f3c9e02020-03-28 14:03:47 -060083 return log_msg_ret("not-bound", -EINVAL);
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +090084
Simon Glassb38ea5a2014-11-10 17:16:47 -070085 drv = dev->driver;
86 assert(drv);
87
88 if (drv->unbind) {
89 ret = drv->unbind(dev);
90 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -060091 return log_msg_ret("unbind", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070092 }
93
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020094 ret = device_chld_unbind(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070095 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -060096 return log_msg_ret("child unbind", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070097
Simon Glass6211d762020-12-19 10:40:10 -070098 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -070099 free(dev_get_plat(dev));
100 dev_set_plat(dev, NULL);
Simon Glassba750492015-01-25 08:27:00 -0700101 }
Simon Glass6211d762020-12-19 10:40:10 -0700102 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -0700103 free(dev_get_uclass_plat(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700104 dev_set_uclass_plat(dev, NULL);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200105 }
Simon Glass6211d762020-12-19 10:40:10 -0700106 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -0700107 free(dev_get_parent_plat(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700108 dev_set_parent_plat(dev, NULL);
Simon Glass11b61732015-01-25 08:27:01 -0700109 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700110 ret = uclass_unbind_device(dev);
111 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600112 return log_msg_ret("uc", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700113
114 if (dev->parent)
115 list_del(&dev->sibling_node);
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900116
117 devres_release_all(dev);
118
Simon Glass6211d762020-12-19 10:40:10 -0700119 if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
Simon Glass7760ba22016-05-01 13:52:23 -0600120 free((char *)dev->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700121 free(dev);
122
123 return 0;
124}
125
126/**
127 * device_free() - Free memory buffers allocated by a device
128 * @dev: Device that is to be started
129 */
130void device_free(struct udevice *dev)
131{
132 int size;
133
Simon Glass8a2b47f2020-12-03 16:55:17 -0700134 if (dev->driver->priv_auto) {
Simon Glass95588622020-12-22 19:30:28 -0700135 free(dev_get_priv(dev));
136 dev_set_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700137 }
Simon Glass8a2b47f2020-12-03 16:55:17 -0700138 size = dev->uclass->uc_drv->per_device_auto;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700139 if (size) {
Simon Glass95588622020-12-22 19:30:28 -0700140 free(dev_get_uclass_priv(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700141 dev_set_uclass_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700142 }
143 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700144 size = dev->parent->driver->per_child_auto;
Simon Glassc23b4282015-01-25 08:27:06 -0700145 if (!size) {
146 size = dev->parent->uclass->uc_drv->
Simon Glass8a2b47f2020-12-03 16:55:17 -0700147 per_child_auto;
Simon Glassc23b4282015-01-25 08:27:06 -0700148 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700149 if (size) {
Simon Glass95588622020-12-22 19:30:28 -0700150 free(dev_get_parent_priv(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700151 dev_set_parent_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700152 }
153 }
Simon Glass6211d762020-12-19 10:40:10 -0700154 dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID);
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900155
156 devres_release_probe(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700157}
158
Simon Glassdf34d0b2021-01-24 14:32:45 -0700159/**
160 * flags_remove() - Figure out whether to remove a device
161 *
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700162 * 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.
165 *
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.
169 *
Simon Glassdf34d0b2021-01-24 14:32:45 -0700170 * @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)
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700176 * -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)
Simon Glassdf34d0b2021-01-24 14:32:45 -0700178 */
179static int flags_remove(uint flags, uint drv_flags)
Stefan Roese07456762017-04-24 09:48:02 +0200180{
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700181 if (!(flags & DM_REMOVE_NORMAL)) {
182 bool vital_match;
183 bool active_match;
184
185 active_match = !(flags & DM_REMOVE_ACTIVE_ALL) ||
186 (drv_flags & flags);
187 vital_match = !(flags & DM_REMOVE_NON_VITAL) ||
188 !(drv_flags & DM_FLAG_VITAL);
189 if (!vital_match)
190 return -EPROBE_DEFER;
191 if (!active_match)
192 return -EKEYREJECTED;
193 }
Stefan Roese07456762017-04-24 09:48:02 +0200194
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700195 return 0;
Stefan Roese07456762017-04-24 09:48:02 +0200196}
197
Stefan Roese80b5bc92017-03-20 12:51:48 +0100198int device_remove(struct udevice *dev, uint flags)
Simon Glassb38ea5a2014-11-10 17:16:47 -0700199{
Simon Glassa626dff2015-03-25 12:21:54 -0600200 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700201 int ret;
202
203 if (!dev)
204 return -EINVAL;
205
Simon Glass6211d762020-12-19 10:40:10 -0700206 if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
Simon Glassb38ea5a2014-11-10 17:16:47 -0700207 return 0;
208
Simon Glassdf34d0b2021-01-24 14:32:45 -0700209 /*
210 * If the child returns EKEYREJECTED, continue. It just means that it
211 * didn't match the flags.
212 */
213 ret = device_chld_remove(dev, NULL, flags);
214 if (ret && ret != -EKEYREJECTED)
215 return ret;
216
217 /*
218 * Remove the device if called with the "normal" remove flag set,
219 * or if the remove flag matches any of the drivers remove flags
220 */
Simon Glassb38ea5a2014-11-10 17:16:47 -0700221 drv = dev->driver;
222 assert(drv);
Simon Glassdf34d0b2021-01-24 14:32:45 -0700223 ret = flags_remove(flags, drv->flags);
224 if (ret) {
225 log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n",
226 dev->name, flags, drv->flags, ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700227 return ret;
Simon Glassdf34d0b2021-01-24 14:32:45 -0700228 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700229
Simon Glass6dd0d1d2021-01-24 14:32:44 -0700230 ret = uclass_pre_remove_device(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700231 if (ret)
Simon Glass6dd0d1d2021-01-24 14:32:44 -0700232 return ret;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700233
Simon Glassdf34d0b2021-01-24 14:32:45 -0700234 if (drv->remove) {
Simon Glassb38ea5a2014-11-10 17:16:47 -0700235 ret = drv->remove(dev);
236 if (ret)
237 goto err_remove;
238 }
239
240 if (dev->parent && dev->parent->driver->child_post_remove) {
241 ret = dev->parent->driver->child_post_remove(dev);
242 if (ret) {
243 dm_warn("%s: Device '%s' failed child_post_remove()",
244 __func__, dev->name);
245 }
246 }
247
Simon Glass42697f52020-03-28 14:03:48 -0600248 if (!(flags & DM_REMOVE_NO_PD) &&
249 !(drv->flags &
Simon Glass12810652021-01-24 14:32:42 -0700250 (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) &&
Anatolij Gustschincfb28f12020-02-17 12:36:43 +0100251 dev != gd->cur_serial_dev)
Anatolij Gustschin98381da2019-09-27 13:48:15 +0530252 dev_power_domain_off(dev);
253
Simon Glassdf34d0b2021-01-24 14:32:45 -0700254 device_free(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700255
Simon Glassdf34d0b2021-01-24 14:32:45 -0700256 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700257
Simon Glassdf34d0b2021-01-24 14:32:45 -0700258 return 0;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700259
260err_remove:
261 /* We can't put the children back */
262 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
263 __func__, dev->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700264
265 return ret;
266}