Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Device manager |
| 4 | * |
| 5 | * Copyright (c) 2014 Google, Inc |
| 6 | * |
| 7 | * (C) Copyright 2012 |
| 8 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <errno.h> |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 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> |
Anatolij Gustschin | 98381da | 2019-09-27 13:48:15 +0530 | [diff] [blame] | 20 | #include <power-domain.h> |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 21 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 22 | int device_chld_unbind(struct udevice *dev, struct driver *drv) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 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) { |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 30 | if (drv && (pos->driver != drv)) |
| 31 | continue; |
| 32 | |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 33 | ret = device_unbind(pos); |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 34 | if (ret && !saved_ret) { |
| 35 | log_warning("device '%s' failed to unbind\n", |
| 36 | pos->name); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 37 | saved_ret = ret; |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 38 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 41 | return log_ret(saved_ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 44 | int device_chld_remove(struct udevice *dev, struct driver *drv, |
| 45 | uint flags) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 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) { |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 53 | if (drv && (pos->driver != drv)) |
| 54 | continue; |
| 55 | |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 56 | ret = device_remove(pos, flags); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 57 | if (ret) |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | int device_unbind(struct udevice *dev) |
| 65 | { |
Simon Glass | a626dff | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 66 | const struct driver *drv; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 67 | int ret; |
| 68 | |
| 69 | if (!dev) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 70 | return log_msg_ret("dev", -EINVAL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 71 | |
| 72 | if (dev->flags & DM_FLAG_ACTIVATED) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 73 | return log_msg_ret("active", -EINVAL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 74 | |
Masahiro Yamada | bdbb5dd | 2015-07-25 21:52:34 +0900 | [diff] [blame] | 75 | if (!(dev->flags & DM_FLAG_BOUND)) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 76 | return log_msg_ret("not-bound", -EINVAL); |
Masahiro Yamada | bdbb5dd | 2015-07-25 21:52:34 +0900 | [diff] [blame] | 77 | |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 78 | drv = dev->driver; |
| 79 | assert(drv); |
| 80 | |
| 81 | if (drv->unbind) { |
| 82 | ret = drv->unbind(dev); |
| 83 | if (ret) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 84 | return log_msg_ret("unbind", ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 87 | ret = device_chld_unbind(dev, NULL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 88 | if (ret) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 89 | return log_msg_ret("child unbind", ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 90 | |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 91 | if (dev->flags & DM_FLAG_ALLOC_PDATA) { |
| 92 | free(dev->platdata); |
| 93 | dev->platdata = NULL; |
| 94 | } |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 95 | if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) { |
| 96 | free(dev->uclass_platdata); |
| 97 | dev->uclass_platdata = NULL; |
| 98 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 99 | if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) { |
| 100 | free(dev->parent_platdata); |
| 101 | dev->parent_platdata = NULL; |
| 102 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 103 | ret = uclass_unbind_device(dev); |
| 104 | if (ret) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 105 | return log_msg_ret("uc", ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 106 | |
| 107 | if (dev->parent) |
| 108 | list_del(&dev->sibling_node); |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 109 | |
| 110 | devres_release_all(dev); |
| 111 | |
Simon Glass | 2d4c7dc | 2016-07-04 11:58:15 -0600 | [diff] [blame] | 112 | if (dev->flags & DM_FLAG_NAME_ALLOCED) |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 113 | free((char *)dev->name); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 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 | |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame^] | 127 | if (dev->driver->priv_auto) { |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 128 | free(dev->priv); |
| 129 | dev->priv = NULL; |
| 130 | } |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame^] | 131 | size = dev->uclass->uc_drv->per_device_auto; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 132 | if (size) { |
| 133 | free(dev->uclass_priv); |
| 134 | dev->uclass_priv = NULL; |
| 135 | } |
| 136 | if (dev->parent) { |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame^] | 137 | size = dev->parent->driver->per_child_auto; |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 138 | if (!size) { |
| 139 | size = dev->parent->uclass->uc_drv-> |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame^] | 140 | per_child_auto; |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 141 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 142 | if (size) { |
| 143 | free(dev->parent_priv); |
| 144 | dev->parent_priv = NULL; |
| 145 | } |
| 146 | } |
Simon Glass | d3c9d30 | 2019-12-29 21:19:21 -0700 | [diff] [blame] | 147 | dev->flags &= ~DM_FLAG_PLATDATA_VALID; |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 148 | |
| 149 | devres_release_probe(dev); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Stefan Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 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 | |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 161 | int device_remove(struct udevice *dev, uint flags) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 162 | { |
Simon Glass | a626dff | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 163 | const struct driver *drv; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 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 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 179 | ret = device_chld_remove(dev, NULL, flags); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 180 | if (ret) |
| 181 | goto err; |
| 182 | |
Stefan Roese | 505045d | 2017-03-27 10:58:53 +0200 | [diff] [blame] | 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 | */ |
Stefan Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 187 | if (drv->remove && flags_remove(flags, drv->flags)) { |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 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 | |
Simon Glass | 42697f5 | 2020-03-28 14:03:48 -0600 | [diff] [blame] | 201 | if (!(flags & DM_REMOVE_NO_PD) && |
| 202 | !(drv->flags & |
Anatolij Gustschin | cfb28f1 | 2020-02-17 12:36:43 +0100 | [diff] [blame] | 203 | (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_REMOVE_WITH_PD_ON)) && |
| 204 | dev != gd->cur_serial_dev) |
Anatolij Gustschin | 98381da | 2019-09-27 13:48:15 +0530 | [diff] [blame] | 205 | dev_power_domain_off(dev); |
| 206 | |
Stefan Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 207 | if (flags_remove(flags, drv->flags)) { |
Stefan Roese | 505045d | 2017-03-27 10:58:53 +0200 | [diff] [blame] | 208 | device_free(dev); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 209 | |
Stefan Roese | 505045d | 2017-03-27 10:58:53 +0200 | [diff] [blame] | 210 | dev->seq = -1; |
| 211 | dev->flags &= ~DM_FLAG_ACTIVATED; |
| 212 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 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 | } |