Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Device manager |
| 4 | * |
| 5 | * Copyright (c) 2013 Google, Inc |
| 6 | * |
| 7 | * (C) Copyright 2012 |
| 8 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 12 | #include <cpu_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Vignesh R | 0dff370 | 2016-07-06 09:58:55 +0530 | [diff] [blame] | 14 | #include <asm/io.h> |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 15 | #include <clk.h> |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 16 | #include <fdtdec.h> |
Stefan Roese | adc0905 | 2015-09-02 07:41:12 +0200 | [diff] [blame] | 17 | #include <fdt_support.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 18 | #include <malloc.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 19 | #include <asm/cache.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 20 | #include <dm/device.h> |
| 21 | #include <dm/device-internal.h> |
| 22 | #include <dm/lists.h> |
Mario Six | 1f1c1ec | 2018-01-15 11:07:20 +0100 | [diff] [blame] | 23 | #include <dm/of_access.h> |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 24 | #include <dm/pinctrl.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 25 | #include <dm/platdata.h> |
Simon Glass | 322b290 | 2017-05-18 20:09:05 -0600 | [diff] [blame] | 26 | #include <dm/read.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 27 | #include <dm/uclass.h> |
| 28 | #include <dm/uclass-internal.h> |
| 29 | #include <dm/util.h> |
| 30 | #include <linux/err.h> |
| 31 | #include <linux/list.h> |
Peng Fan | 6fba32d | 2018-07-27 10:20:38 +0800 | [diff] [blame] | 32 | #include <power-domain.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 33 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 36 | static int device_bind_common(struct udevice *parent, const struct driver *drv, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 37 | const char *name, void *plat, |
Simon Glass | b0cf4b3 | 2017-05-17 17:18:11 -0600 | [diff] [blame] | 38 | ulong driver_data, ofnode node, |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 39 | uint of_platdata_size, struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 40 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 41 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 42 | struct uclass *uc; |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 43 | int size, ret = 0; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 44 | |
Masahiro Yamada | e1cc1f0 | 2015-08-27 12:44:28 +0900 | [diff] [blame] | 45 | if (devp) |
| 46 | *devp = NULL; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 47 | if (!name) |
| 48 | return -EINVAL; |
| 49 | |
| 50 | ret = uclass_get(drv->id, &uc); |
Simon Glass | 43313de | 2015-08-30 16:55:16 -0600 | [diff] [blame] | 51 | if (ret) { |
| 52 | debug("Missing uclass for driver %s\n", drv->name); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 53 | return ret; |
Simon Glass | 43313de | 2015-08-30 16:55:16 -0600 | [diff] [blame] | 54 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 55 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 56 | dev = calloc(1, sizeof(struct udevice)); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 57 | if (!dev) |
| 58 | return -ENOMEM; |
| 59 | |
| 60 | INIT_LIST_HEAD(&dev->sibling_node); |
| 61 | INIT_LIST_HEAD(&dev->child_head); |
| 62 | INIT_LIST_HEAD(&dev->uclass_node); |
Masahiro Yamada | 029bfca | 2015-07-25 21:52:37 +0900 | [diff] [blame] | 63 | #ifdef CONFIG_DEVRES |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 64 | INIT_LIST_HEAD(&dev->devres_head); |
Masahiro Yamada | 029bfca | 2015-07-25 21:52:37 +0900 | [diff] [blame] | 65 | #endif |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 66 | dev->plat = plat; |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 67 | dev->driver_data = driver_data; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 68 | dev->name = name; |
Simon Glass | b0cf4b3 | 2017-05-17 17:18:11 -0600 | [diff] [blame] | 69 | dev->node = node; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 70 | dev->parent = parent; |
| 71 | dev->driver = drv; |
| 72 | dev->uclass = uc; |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 73 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 74 | dev->seq = -1; |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 75 | dev->req_seq = -1; |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 76 | if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) && |
| 77 | (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) { |
Simon Glass | 1bd3deb | 2015-02-27 22:06:30 -0700 | [diff] [blame] | 78 | /* |
Stefan Roese | 9d4ce6b | 2016-03-16 09:58:01 +0100 | [diff] [blame] | 79 | * Some devices, such as a SPI bus, I2C bus and serial ports |
| 80 | * are numbered using aliases. |
| 81 | * |
| 82 | * This is just a 'requested' sequence, and will be |
| 83 | * resolved (and ->seq updated) when the device is probed. |
| 84 | */ |
Simon Glass | e422f8c | 2020-07-09 18:39:24 -0600 | [diff] [blame] | 85 | if (CONFIG_IS_ENABLED(OF_CONTROL) && |
| 86 | !CONFIG_IS_ENABLED(OF_PLATDATA)) { |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 87 | if (uc->uc_drv->name && ofnode_valid(node)) |
Simon Glass | 322b290 | 2017-05-18 20:09:05 -0600 | [diff] [blame] | 88 | dev_read_alias_seq(dev, &dev->req_seq); |
Thomas Fitzsimmons | f327ecb | 2019-09-06 07:51:18 -0400 | [diff] [blame] | 89 | #if CONFIG_IS_ENABLED(OF_PRIOR_STAGE) |
| 90 | if (dev->req_seq == -1) |
| 91 | dev->req_seq = |
| 92 | uclass_find_next_free_req_seq(drv->id); |
| 93 | #endif |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 94 | } else { |
| 95 | dev->req_seq = uclass_find_next_free_req_seq(drv->id); |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 96 | } |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 97 | } |
Simon Glass | 1bd3deb | 2015-02-27 22:06:30 -0700 | [diff] [blame] | 98 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 99 | if (drv->plat_auto) { |
| 100 | bool alloc = !plat; |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 101 | |
| 102 | if (CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 103 | if (of_platdata_size) { |
| 104 | dev->flags |= DM_FLAG_OF_PLATDATA; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 105 | if (of_platdata_size < drv->plat_auto) |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 106 | alloc = true; |
| 107 | } |
| 108 | } |
| 109 | if (alloc) { |
| 110 | dev->flags |= DM_FLAG_ALLOC_PDATA; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 111 | dev->plat = calloc(1, drv->plat_auto); |
| 112 | if (!dev->plat) { |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 113 | ret = -ENOMEM; |
| 114 | goto fail_alloc1; |
| 115 | } |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 116 | if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat) { |
| 117 | memcpy(dev->plat, plat, |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 118 | of_platdata_size); |
| 119 | } |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 122 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 123 | size = uc->uc_drv->per_device_plat_auto; |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 124 | if (size) { |
| 125 | dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 126 | dev->uclass_plat = calloc(1, size); |
| 127 | if (!dev->uclass_plat) { |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 128 | ret = -ENOMEM; |
| 129 | goto fail_alloc2; |
| 130 | } |
| 131 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 132 | |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 133 | if (parent) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 134 | size = parent->driver->per_child_plat_auto; |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 135 | if (!size) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 136 | size = parent->uclass->uc_drv->per_child_plat_auto; |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 137 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 138 | if (size) { |
| 139 | dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 140 | dev->parent_plat = calloc(1, size); |
| 141 | if (!dev->parent_plat) { |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 142 | ret = -ENOMEM; |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 143 | goto fail_alloc3; |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
Heinrich Schuchardt | 5246b37 | 2020-02-15 21:38:48 +0100 | [diff] [blame] | 146 | /* put dev into parent's successor list */ |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 147 | list_add_tail(&dev->sibling_node, &parent->child_head); |
Heinrich Schuchardt | 5246b37 | 2020-02-15 21:38:48 +0100 | [diff] [blame] | 148 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 149 | |
| 150 | ret = uclass_bind_device(dev); |
| 151 | if (ret) |
Simon Glass | 4ebe01b | 2015-01-25 08:26:59 -0700 | [diff] [blame] | 152 | goto fail_uclass_bind; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 153 | |
| 154 | /* if we fail to bind we remove device from successors and free it */ |
| 155 | if (drv->bind) { |
| 156 | ret = drv->bind(dev); |
Simon Glass | 4ebe01b | 2015-01-25 08:26:59 -0700 | [diff] [blame] | 157 | if (ret) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 158 | goto fail_bind; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 159 | } |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 160 | if (parent && parent->driver->child_post_bind) { |
| 161 | ret = parent->driver->child_post_bind(dev); |
| 162 | if (ret) |
| 163 | goto fail_child_post_bind; |
| 164 | } |
Simon Glass | 286863d | 2016-01-05 09:30:59 -0700 | [diff] [blame] | 165 | if (uc->uc_drv->post_bind) { |
| 166 | ret = uc->uc_drv->post_bind(dev); |
| 167 | if (ret) |
| 168 | goto fail_uclass_post_bind; |
| 169 | } |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 170 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 171 | if (parent) |
Masahiro Yamada | f70f39f | 2017-09-29 12:31:20 +0900 | [diff] [blame] | 172 | pr_debug("Bound device %s to %s\n", dev->name, parent->name); |
Masahiro Yamada | e1cc1f0 | 2015-08-27 12:44:28 +0900 | [diff] [blame] | 173 | if (devp) |
| 174 | *devp = dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 175 | |
Masahiro Yamada | bdbb5dd | 2015-07-25 21:52:34 +0900 | [diff] [blame] | 176 | dev->flags |= DM_FLAG_BOUND; |
| 177 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 178 | return 0; |
| 179 | |
Simon Glass | 286863d | 2016-01-05 09:30:59 -0700 | [diff] [blame] | 180 | fail_uclass_post_bind: |
| 181 | /* There is no child unbind() method, so no clean-up required */ |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 182 | fail_child_post_bind: |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 183 | if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { |
Simon Glass | e70a4f4 | 2015-02-27 22:06:33 -0700 | [diff] [blame] | 184 | if (drv->unbind && drv->unbind(dev)) { |
| 185 | dm_warn("unbind() method failed on dev '%s' on error path\n", |
| 186 | dev->name); |
| 187 | } |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 190 | fail_bind: |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 191 | if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { |
Simon Glass | e70a4f4 | 2015-02-27 22:06:33 -0700 | [diff] [blame] | 192 | if (uclass_unbind_device(dev)) { |
| 193 | dm_warn("Failed to unbind dev '%s' on error path\n", |
| 194 | dev->name); |
| 195 | } |
Simon Glass | 4ebe01b | 2015-01-25 08:26:59 -0700 | [diff] [blame] | 196 | } |
| 197 | fail_uclass_bind: |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 198 | if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { |
Simon Glass | e70a4f4 | 2015-02-27 22:06:33 -0700 | [diff] [blame] | 199 | list_del(&dev->sibling_node); |
| 200 | if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 201 | free(dev->parent_plat); |
| 202 | dev->parent_plat = NULL; |
Simon Glass | e70a4f4 | 2015-02-27 22:06:33 -0700 | [diff] [blame] | 203 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 204 | } |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 205 | fail_alloc3: |
| 206 | if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 207 | free(dev->uclass_plat); |
| 208 | dev->uclass_plat = NULL; |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 209 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 210 | fail_alloc2: |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 211 | if (dev->flags & DM_FLAG_ALLOC_PDATA) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 212 | free(dev->plat); |
| 213 | dev->plat = NULL; |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 214 | } |
| 215 | fail_alloc1: |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 216 | devres_release_all(dev); |
| 217 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 218 | free(dev); |
Simon Glass | 4ebe01b | 2015-01-25 08:26:59 -0700 | [diff] [blame] | 219 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 220 | return ret; |
| 221 | } |
| 222 | |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 223 | int device_bind_with_driver_data(struct udevice *parent, |
| 224 | const struct driver *drv, const char *name, |
Simon Glass | 322b290 | 2017-05-18 20:09:05 -0600 | [diff] [blame] | 225 | ulong driver_data, ofnode node, |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 226 | struct udevice **devp) |
| 227 | { |
Simon Glass | 322b290 | 2017-05-18 20:09:05 -0600 | [diff] [blame] | 228 | return device_bind_common(parent, drv, name, NULL, driver_data, node, |
| 229 | 0, devp); |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 230 | } |
| 231 | |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 232 | int device_bind(struct udevice *parent, const struct driver *drv, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 233 | const char *name, void *plat, ofnode node, |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 234 | struct udevice **devp) |
Simon Glass | 9eb151e | 2018-06-11 13:07:15 -0600 | [diff] [blame] | 235 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 236 | return device_bind_common(parent, drv, name, plat, 0, node, 0, |
Simon Glass | 9eb151e | 2018-06-11 13:07:15 -0600 | [diff] [blame] | 237 | devp); |
| 238 | } |
| 239 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 240 | int device_bind_by_name(struct udevice *parent, bool pre_reloc_only, |
Simon Glass | cfd6a00 | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 241 | const struct driver_info *info, struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 242 | { |
| 243 | struct driver *drv; |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 244 | uint platdata_size = 0; |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 245 | int ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 246 | |
| 247 | drv = lists_driver_lookup_name(info->name); |
| 248 | if (!drv) |
| 249 | return -ENOENT; |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 250 | if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC)) |
| 251 | return -EPERM; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 252 | |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 253 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 254 | platdata_size = info->platdata_size; |
| 255 | #endif |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 256 | ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0, |
| 257 | ofnode_null(), platdata_size, devp); |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 258 | if (ret) |
| 259 | return ret; |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 260 | |
| 261 | return ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 264 | int device_reparent(struct udevice *dev, struct udevice *new_parent) |
| 265 | { |
| 266 | struct udevice *pos, *n; |
| 267 | |
| 268 | assert(dev); |
| 269 | assert(new_parent); |
| 270 | |
| 271 | list_for_each_entry_safe(pos, n, &dev->parent->child_head, |
| 272 | sibling_node) { |
| 273 | if (pos->driver != dev->driver) |
| 274 | continue; |
| 275 | |
| 276 | list_del(&dev->sibling_node); |
| 277 | list_add_tail(&dev->sibling_node, &new_parent->child_head); |
| 278 | dev->parent = new_parent; |
| 279 | |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
Simon Glass | 825d3f9 | 2015-03-25 12:21:53 -0600 | [diff] [blame] | 286 | static void *alloc_priv(int size, uint flags) |
| 287 | { |
| 288 | void *priv; |
| 289 | |
| 290 | if (flags & DM_FLAG_ALLOC_PRIV_DMA) { |
Faiz Abbas | 03020a9 | 2017-09-19 16:53:50 +0530 | [diff] [blame] | 291 | size = ROUND(size, ARCH_DMA_MINALIGN); |
Simon Glass | 825d3f9 | 2015-03-25 12:21:53 -0600 | [diff] [blame] | 292 | priv = memalign(ARCH_DMA_MINALIGN, size); |
Simon Glass | 27bc9b1 | 2017-04-04 13:00:19 -0600 | [diff] [blame] | 293 | if (priv) { |
Simon Glass | 825d3f9 | 2015-03-25 12:21:53 -0600 | [diff] [blame] | 294 | memset(priv, '\0', size); |
Simon Glass | 27bc9b1 | 2017-04-04 13:00:19 -0600 | [diff] [blame] | 295 | |
| 296 | /* |
| 297 | * Ensure that the zero bytes are flushed to memory. |
| 298 | * This prevents problems if the driver uses this as |
| 299 | * both an input and an output buffer: |
| 300 | * |
| 301 | * 1. Zeroes written to buffer (here) and sit in the |
| 302 | * cache |
| 303 | * 2. Driver issues a read command to DMA |
| 304 | * 3. CPU runs out of cache space and evicts some cache |
| 305 | * data in the buffer, writing zeroes to RAM from |
| 306 | * the memset() above |
| 307 | * 4. DMA completes |
| 308 | * 5. Buffer now has some DMA data and some zeroes |
| 309 | * 6. Data being read is now incorrect |
| 310 | * |
| 311 | * To prevent this, ensure that the cache is clean |
| 312 | * within this range at the start. The driver can then |
| 313 | * use normal flush-after-write, invalidate-before-read |
| 314 | * procedures. |
| 315 | * |
| 316 | * TODO(sjg@chromium.org): Drop this microblaze |
| 317 | * exception. |
| 318 | */ |
| 319 | #ifndef CONFIG_MICROBLAZE |
| 320 | flush_dcache_range((ulong)priv, (ulong)priv + size); |
| 321 | #endif |
| 322 | } |
Simon Glass | 825d3f9 | 2015-03-25 12:21:53 -0600 | [diff] [blame] | 323 | } else { |
| 324 | priv = calloc(1, size); |
| 325 | } |
| 326 | |
| 327 | return priv; |
| 328 | } |
| 329 | |
Simon Glass | 4a26385 | 2019-12-29 21:19:20 -0700 | [diff] [blame] | 330 | int device_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 331 | { |
Simon Glass | a626dff | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 332 | const struct driver *drv; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 333 | int size = 0; |
| 334 | int ret; |
| 335 | |
| 336 | if (!dev) |
| 337 | return -EINVAL; |
| 338 | |
Simon Glass | d3c9d30 | 2019-12-29 21:19:21 -0700 | [diff] [blame] | 339 | if (dev->flags & DM_FLAG_PLATDATA_VALID) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 340 | return 0; |
| 341 | |
Simon Glass | 44c4789 | 2020-04-05 15:38:19 -0600 | [diff] [blame] | 342 | /* Ensure all parents have ofdata */ |
| 343 | if (dev->parent) { |
| 344 | ret = device_ofdata_to_platdata(dev->parent); |
| 345 | if (ret) |
| 346 | goto fail; |
| 347 | |
| 348 | /* |
| 349 | * The device might have already been probed during |
| 350 | * the call to device_probe() on its parent device |
| 351 | * (e.g. PCI bridge devices). Test the flags again |
| 352 | * so that we don't mess up the device. |
| 353 | */ |
| 354 | if (dev->flags & DM_FLAG_PLATDATA_VALID) |
| 355 | return 0; |
| 356 | } |
| 357 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 358 | drv = dev->driver; |
| 359 | assert(drv); |
| 360 | |
Bin Meng | 82cdd78 | 2015-08-24 01:14:02 -0700 | [diff] [blame] | 361 | /* Allocate private data if requested and not reentered */ |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 362 | if (drv->priv_auto && !dev->priv) { |
| 363 | dev->priv = alloc_priv(drv->priv_auto, drv->flags); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 364 | if (!dev->priv) { |
| 365 | ret = -ENOMEM; |
| 366 | goto fail; |
| 367 | } |
| 368 | } |
Bin Meng | 82cdd78 | 2015-08-24 01:14:02 -0700 | [diff] [blame] | 369 | /* Allocate private data if requested and not reentered */ |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 370 | size = dev->uclass->uc_drv->per_device_auto; |
Bin Meng | 82cdd78 | 2015-08-24 01:14:02 -0700 | [diff] [blame] | 371 | if (size && !dev->uclass_priv) { |
Simon Glass | 7ebd13d | 2018-10-01 12:22:05 -0600 | [diff] [blame] | 372 | dev->uclass_priv = alloc_priv(size, |
| 373 | dev->uclass->uc_drv->flags); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 374 | if (!dev->uclass_priv) { |
| 375 | ret = -ENOMEM; |
| 376 | goto fail; |
| 377 | } |
| 378 | } |
| 379 | |
Simon Glass | a8d8f16 | 2019-12-29 21:19:18 -0700 | [diff] [blame] | 380 | /* Allocate parent data for this child */ |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 381 | if (dev->parent) { |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 382 | size = dev->parent->driver->per_child_auto; |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 383 | if (!size) { |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 384 | size = dev->parent->uclass->uc_drv->per_child_auto; |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 385 | } |
Bin Meng | 82cdd78 | 2015-08-24 01:14:02 -0700 | [diff] [blame] | 386 | if (size && !dev->parent_priv) { |
Simon Glass | 825d3f9 | 2015-03-25 12:21:53 -0600 | [diff] [blame] | 387 | dev->parent_priv = alloc_priv(size, drv->flags); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 388 | if (!dev->parent_priv) { |
| 389 | ret = -ENOMEM; |
| 390 | goto fail; |
| 391 | } |
| 392 | } |
Simon Glass | a8d8f16 | 2019-12-29 21:19:18 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | if (drv->ofdata_to_platdata && |
| 396 | (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) { |
| 397 | ret = drv->ofdata_to_platdata(dev); |
| 398 | if (ret) |
| 399 | goto fail; |
| 400 | } |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 401 | |
Simon Glass | d3c9d30 | 2019-12-29 21:19:21 -0700 | [diff] [blame] | 402 | dev->flags |= DM_FLAG_PLATDATA_VALID; |
| 403 | |
Simon Glass | 4a26385 | 2019-12-29 21:19:20 -0700 | [diff] [blame] | 404 | return 0; |
| 405 | fail: |
| 406 | device_free(dev); |
| 407 | |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | int device_probe(struct udevice *dev) |
| 412 | { |
| 413 | const struct driver *drv; |
| 414 | int ret; |
| 415 | int seq; |
| 416 | |
| 417 | if (!dev) |
| 418 | return -EINVAL; |
| 419 | |
| 420 | if (dev->flags & DM_FLAG_ACTIVATED) |
| 421 | return 0; |
| 422 | |
| 423 | drv = dev->driver; |
| 424 | assert(drv); |
| 425 | |
| 426 | ret = device_ofdata_to_platdata(dev); |
| 427 | if (ret) |
| 428 | goto fail; |
| 429 | |
Simon Glass | a8d8f16 | 2019-12-29 21:19:18 -0700 | [diff] [blame] | 430 | /* Ensure all parents are probed */ |
| 431 | if (dev->parent) { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 432 | ret = device_probe(dev->parent); |
| 433 | if (ret) |
| 434 | goto fail; |
Bin Meng | 82cdd78 | 2015-08-24 01:14:02 -0700 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * The device might have already been probed during |
| 438 | * the call to device_probe() on its parent device |
| 439 | * (e.g. PCI bridge devices). Test the flags again |
| 440 | * so that we don't mess up the device. |
| 441 | */ |
| 442 | if (dev->flags & DM_FLAG_ACTIVATED) |
| 443 | return 0; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 444 | } |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 445 | |
| 446 | seq = uclass_resolve_seq(dev); |
| 447 | if (seq < 0) { |
| 448 | ret = seq; |
| 449 | goto fail; |
| 450 | } |
| 451 | dev->seq = seq; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 452 | |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 453 | dev->flags |= DM_FLAG_ACTIVATED; |
| 454 | |
Simon Glass | bb53bda | 2015-09-12 08:45:19 -0600 | [diff] [blame] | 455 | /* |
| 456 | * Process pinctrl for everything except the root device, and |
Simon Glass | 27ce962 | 2016-01-21 19:43:25 -0700 | [diff] [blame] | 457 | * continue regardless of the result of pinctrl. Don't process pinctrl |
| 458 | * settings for pinctrl devices since the device may not yet be |
| 459 | * probed. |
Simon Glass | bb53bda | 2015-09-12 08:45:19 -0600 | [diff] [blame] | 460 | */ |
Simon Glass | 27ce962 | 2016-01-21 19:43:25 -0700 | [diff] [blame] | 461 | if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL) |
Simon Glass | bb53bda | 2015-09-12 08:45:19 -0600 | [diff] [blame] | 462 | pinctrl_select_state(dev, "default"); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 463 | |
Anatolij Gustschin | 63a3467 | 2019-07-14 21:11:01 +0200 | [diff] [blame] | 464 | if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent && |
Lokesh Vutla | c10e52b | 2019-09-27 13:48:12 +0530 | [diff] [blame] | 465 | (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) && |
| 466 | !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) { |
Peng Fan | d220111 | 2019-09-17 09:29:22 +0000 | [diff] [blame] | 467 | ret = dev_power_domain_on(dev); |
| 468 | if (ret) |
| 469 | goto fail; |
Peng Fan | 6fba32d | 2018-07-27 10:20:38 +0800 | [diff] [blame] | 470 | } |
| 471 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 472 | ret = uclass_pre_probe_device(dev); |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 473 | if (ret) |
| 474 | goto fail; |
| 475 | |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 476 | if (dev->parent && dev->parent->driver->child_pre_probe) { |
| 477 | ret = dev->parent->driver->child_pre_probe(dev); |
| 478 | if (ret) |
| 479 | goto fail; |
| 480 | } |
| 481 | |
Bin Meng | 513cf40 | 2019-07-05 09:23:16 -0700 | [diff] [blame] | 482 | /* Only handle devices that have a valid ofnode */ |
| 483 | if (dev_of_valid(dev)) { |
| 484 | /* |
| 485 | * Process 'assigned-{clocks/clock-parents/clock-rates}' |
| 486 | * properties |
| 487 | */ |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 488 | ret = clk_set_defaults(dev, 0); |
Bin Meng | 513cf40 | 2019-07-05 09:23:16 -0700 | [diff] [blame] | 489 | if (ret) |
| 490 | goto fail; |
| 491 | } |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 492 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 493 | if (drv->probe) { |
| 494 | ret = drv->probe(dev); |
Simon Glass | 9046dec | 2019-12-29 21:19:16 -0700 | [diff] [blame] | 495 | if (ret) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 496 | goto fail; |
| 497 | } |
| 498 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 499 | ret = uclass_post_probe_device(dev); |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 500 | if (ret) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 501 | goto fail_uclass; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 502 | |
Peng Fan | 7cf58dd | 2016-03-12 13:17:38 +0800 | [diff] [blame] | 503 | if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL) |
| 504 | pinctrl_select_state(dev, "default"); |
| 505 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 506 | return 0; |
| 507 | fail_uclass: |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 508 | if (device_remove(dev, DM_REMOVE_NORMAL)) { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 509 | dm_warn("%s: Device '%s' failed to remove on error path\n", |
| 510 | __func__, dev->name); |
| 511 | } |
| 512 | fail: |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 513 | dev->flags &= ~DM_FLAG_ACTIVATED; |
| 514 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 515 | dev->seq = -1; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 516 | device_free(dev); |
| 517 | |
| 518 | return ret; |
| 519 | } |
| 520 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 521 | void *dev_get_platdata(const struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 522 | { |
| 523 | if (!dev) { |
Simon Glass | df1e072 | 2014-12-10 08:55:56 -0700 | [diff] [blame] | 524 | dm_warn("%s: null device\n", __func__); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 525 | return NULL; |
| 526 | } |
| 527 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 528 | return dev->plat; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 531 | void *dev_get_parent_plat(const struct udevice *dev) |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 532 | { |
| 533 | if (!dev) { |
Simon Glass | cdfc505 | 2015-07-06 16:47:40 -0600 | [diff] [blame] | 534 | dm_warn("%s: null device\n", __func__); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 535 | return NULL; |
| 536 | } |
| 537 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 538 | return dev->parent_plat; |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 541 | void *dev_get_uclass_plat(const struct udevice *dev) |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 542 | { |
| 543 | if (!dev) { |
Simon Glass | cdfc505 | 2015-07-06 16:47:40 -0600 | [diff] [blame] | 544 | dm_warn("%s: null device\n", __func__); |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 545 | return NULL; |
| 546 | } |
| 547 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame^] | 548 | return dev->uclass_plat; |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 549 | } |
| 550 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 551 | void *dev_get_priv(const struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 552 | { |
| 553 | if (!dev) { |
Simon Glass | df1e072 | 2014-12-10 08:55:56 -0700 | [diff] [blame] | 554 | dm_warn("%s: null device\n", __func__); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 555 | return NULL; |
| 556 | } |
| 557 | |
| 558 | return dev->priv; |
| 559 | } |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 560 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 561 | void *dev_get_uclass_priv(const struct udevice *dev) |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 562 | { |
| 563 | if (!dev) { |
| 564 | dm_warn("%s: null device\n", __func__); |
| 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | return dev->uclass_priv; |
| 569 | } |
| 570 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 571 | void *dev_get_parent_priv(const struct udevice *dev) |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 572 | { |
| 573 | if (!dev) { |
Simon Glass | df1e072 | 2014-12-10 08:55:56 -0700 | [diff] [blame] | 574 | dm_warn("%s: null device\n", __func__); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | return dev->parent_priv; |
| 579 | } |
| 580 | |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 581 | static int device_get_device_tail(struct udevice *dev, int ret, |
| 582 | struct udevice **devp) |
| 583 | { |
| 584 | if (ret) |
| 585 | return ret; |
| 586 | |
| 587 | ret = device_probe(dev); |
| 588 | if (ret) |
| 589 | return ret; |
| 590 | |
| 591 | *devp = dev; |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 596 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 597 | /** |
| 598 | * device_find_by_ofnode() - Return device associated with given ofnode |
| 599 | * |
| 600 | * The returned device is *not* activated. |
| 601 | * |
| 602 | * @node: The ofnode for which a associated device should be looked up |
| 603 | * @devp: Pointer to structure to hold the found device |
| 604 | * Return: 0 if OK, -ve on error |
| 605 | */ |
| 606 | static int device_find_by_ofnode(ofnode node, struct udevice **devp) |
| 607 | { |
| 608 | struct uclass *uc; |
| 609 | struct udevice *dev; |
| 610 | int ret; |
| 611 | |
| 612 | list_for_each_entry(uc, &gd->uclass_root, sibling_node) { |
| 613 | ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, |
| 614 | &dev); |
| 615 | if (!ret || dev) { |
| 616 | *devp = dev; |
| 617 | return 0; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | return -ENODEV; |
| 622 | } |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 623 | #endif |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 624 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 625 | int device_get_child(const struct udevice *parent, int index, |
| 626 | struct udevice **devp) |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 627 | { |
| 628 | struct udevice *dev; |
| 629 | |
| 630 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 631 | if (!index--) |
| 632 | return device_get_device_tail(dev, 0, devp); |
| 633 | } |
| 634 | |
| 635 | return -ENODEV; |
| 636 | } |
| 637 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 638 | int device_get_child_count(const struct udevice *parent) |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 639 | { |
| 640 | struct udevice *dev; |
| 641 | int count = 0; |
| 642 | |
| 643 | list_for_each_entry(dev, &parent->child_head, sibling_node) |
| 644 | count++; |
| 645 | |
| 646 | return count; |
| 647 | } |
| 648 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 649 | int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq, |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 650 | bool find_req_seq, struct udevice **devp) |
| 651 | { |
| 652 | struct udevice *dev; |
| 653 | |
| 654 | *devp = NULL; |
| 655 | if (seq_or_req_seq == -1) |
| 656 | return -ENODEV; |
| 657 | |
| 658 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 659 | if ((find_req_seq ? dev->req_seq : dev->seq) == |
| 660 | seq_or_req_seq) { |
| 661 | *devp = dev; |
| 662 | return 0; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | return -ENODEV; |
| 667 | } |
| 668 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 669 | int device_get_child_by_seq(const struct udevice *parent, int seq, |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 670 | struct udevice **devp) |
| 671 | { |
| 672 | struct udevice *dev; |
| 673 | int ret; |
| 674 | |
| 675 | *devp = NULL; |
| 676 | ret = device_find_child_by_seq(parent, seq, false, &dev); |
| 677 | if (ret == -ENODEV) { |
| 678 | /* |
| 679 | * We didn't find it in probed devices. See if there is one |
| 680 | * that will request this seq if probed. |
| 681 | */ |
| 682 | ret = device_find_child_by_seq(parent, seq, true, &dev); |
| 683 | } |
| 684 | return device_get_device_tail(dev, ret, devp); |
| 685 | } |
| 686 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 687 | int device_find_child_by_of_offset(const struct udevice *parent, int of_offset, |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 688 | struct udevice **devp) |
| 689 | { |
| 690 | struct udevice *dev; |
| 691 | |
| 692 | *devp = NULL; |
| 693 | |
| 694 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 695 | if (dev_of_offset(dev) == of_offset) { |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 696 | *devp = dev; |
| 697 | return 0; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | return -ENODEV; |
| 702 | } |
| 703 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 704 | int device_get_child_by_of_offset(const struct udevice *parent, int node, |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 705 | struct udevice **devp) |
| 706 | { |
| 707 | struct udevice *dev; |
| 708 | int ret; |
| 709 | |
| 710 | *devp = NULL; |
Simon Glass | 861bc9f | 2015-06-23 15:38:38 -0600 | [diff] [blame] | 711 | ret = device_find_child_by_of_offset(parent, node, &dev); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 712 | return device_get_device_tail(dev, ret, devp); |
| 713 | } |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 714 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 715 | static struct udevice *_device_find_global_by_ofnode(struct udevice *parent, |
| 716 | ofnode ofnode) |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 717 | { |
| 718 | struct udevice *dev, *found; |
| 719 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 720 | if (ofnode_equal(dev_ofnode(parent), ofnode)) |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 721 | return parent; |
| 722 | |
| 723 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 724 | found = _device_find_global_by_ofnode(dev, ofnode); |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 725 | if (found) |
| 726 | return found; |
| 727 | } |
| 728 | |
| 729 | return NULL; |
| 730 | } |
| 731 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 732 | int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp) |
| 733 | { |
| 734 | *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode); |
| 735 | |
| 736 | return *devp ? 0 : -ENOENT; |
| 737 | } |
| 738 | |
| 739 | int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp) |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 740 | { |
| 741 | struct udevice *dev; |
| 742 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 743 | dev = _device_find_global_by_ofnode(gd->dm_root, ofnode); |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 744 | return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp); |
| 745 | } |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 746 | |
| 747 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 748 | int device_get_by_driver_info(const struct driver_info *info, |
| 749 | struct udevice **devp) |
| 750 | { |
Simon Glass | cfd6a00 | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 751 | struct driver_info *info_base = |
| 752 | ll_entry_start(struct driver_info, driver_info); |
| 753 | int idx = info - info_base; |
| 754 | struct driver_rt *drt = gd_dm_driver_rt() + idx; |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 755 | struct udevice *dev; |
| 756 | |
Simon Glass | cfd6a00 | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 757 | dev = drt->dev; |
Simon Glass | d825ea7 | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 758 | *devp = NULL; |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 759 | |
| 760 | return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp); |
| 761 | } |
Simon Glass | 5792f4b | 2020-10-03 11:31:40 -0600 | [diff] [blame] | 762 | |
| 763 | int device_get_by_driver_info_idx(uint idx, struct udevice **devp) |
| 764 | { |
| 765 | struct driver_rt *drt = gd_dm_driver_rt() + idx; |
| 766 | struct udevice *dev; |
| 767 | |
| 768 | dev = drt->dev; |
| 769 | *devp = NULL; |
| 770 | |
| 771 | return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp); |
| 772 | } |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 773 | #endif |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 774 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 775 | int device_find_first_child(const struct udevice *parent, struct udevice **devp) |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 776 | { |
| 777 | if (list_empty(&parent->child_head)) { |
| 778 | *devp = NULL; |
| 779 | } else { |
| 780 | *devp = list_first_entry(&parent->child_head, struct udevice, |
| 781 | sibling_node); |
| 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | int device_find_next_child(struct udevice **devp) |
| 788 | { |
| 789 | struct udevice *dev = *devp; |
| 790 | struct udevice *parent = dev->parent; |
| 791 | |
| 792 | if (list_is_last(&dev->sibling_node, &parent->child_head)) { |
| 793 | *devp = NULL; |
| 794 | } else { |
| 795 | *devp = list_entry(dev->sibling_node.next, struct udevice, |
| 796 | sibling_node); |
| 797 | } |
| 798 | |
| 799 | return 0; |
| 800 | } |
Simon Glass | 70c3a0e | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 801 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 802 | int device_find_first_inactive_child(const struct udevice *parent, |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 803 | enum uclass_id uclass_id, |
| 804 | struct udevice **devp) |
| 805 | { |
| 806 | struct udevice *dev; |
| 807 | |
| 808 | *devp = NULL; |
| 809 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 810 | if (!device_active(dev) && |
| 811 | device_get_uclass_id(dev) == uclass_id) { |
| 812 | *devp = dev; |
| 813 | return 0; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | return -ENODEV; |
| 818 | } |
| 819 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 820 | int device_find_first_child_by_uclass(const struct udevice *parent, |
Simon Glass | a11341a | 2018-11-18 08:14:31 -0700 | [diff] [blame] | 821 | enum uclass_id uclass_id, |
| 822 | struct udevice **devp) |
| 823 | { |
| 824 | struct udevice *dev; |
| 825 | |
| 826 | *devp = NULL; |
| 827 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 828 | if (device_get_uclass_id(dev) == uclass_id) { |
| 829 | *devp = dev; |
| 830 | return 0; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | return -ENODEV; |
| 835 | } |
| 836 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 837 | int device_find_child_by_name(const struct udevice *parent, const char *name, |
Simon Glass | a11341a | 2018-11-18 08:14:31 -0700 | [diff] [blame] | 838 | struct udevice **devp) |
| 839 | { |
| 840 | struct udevice *dev; |
| 841 | |
| 842 | *devp = NULL; |
| 843 | |
| 844 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 845 | if (!strcmp(dev->name, name)) { |
| 846 | *devp = dev; |
| 847 | return 0; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | return -ENODEV; |
| 852 | } |
| 853 | |
Simon Glass | 76db9b7 | 2020-01-27 08:49:48 -0700 | [diff] [blame] | 854 | int device_first_child_err(struct udevice *parent, struct udevice **devp) |
| 855 | { |
| 856 | struct udevice *dev; |
| 857 | |
| 858 | device_find_first_child(parent, &dev); |
| 859 | if (!dev) |
| 860 | return -ENODEV; |
| 861 | |
| 862 | return device_get_device_tail(dev, 0, devp); |
| 863 | } |
| 864 | |
| 865 | int device_next_child_err(struct udevice **devp) |
| 866 | { |
| 867 | struct udevice *dev = *devp; |
| 868 | |
| 869 | device_find_next_child(&dev); |
| 870 | if (!dev) |
| 871 | return -ENODEV; |
| 872 | |
| 873 | return device_get_device_tail(dev, 0, devp); |
| 874 | } |
| 875 | |
Simon Glass | 000676b | 2020-01-27 08:49:47 -0700 | [diff] [blame] | 876 | int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp) |
| 877 | { |
| 878 | struct udevice *dev; |
| 879 | int ret; |
| 880 | |
| 881 | device_find_first_child(parent, &dev); |
| 882 | if (!dev) |
| 883 | return -ENODEV; |
| 884 | |
| 885 | ret = device_ofdata_to_platdata(dev); |
| 886 | if (ret) |
| 887 | return ret; |
| 888 | |
| 889 | *devp = dev; |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | int device_next_child_ofdata_err(struct udevice **devp) |
| 895 | { |
| 896 | struct udevice *dev = *devp; |
| 897 | int ret; |
| 898 | |
| 899 | device_find_next_child(&dev); |
| 900 | if (!dev) |
| 901 | return -ENODEV; |
| 902 | |
| 903 | ret = device_ofdata_to_platdata(dev); |
| 904 | if (ret) |
| 905 | return ret; |
| 906 | |
| 907 | *devp = dev; |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 912 | struct udevice *dev_get_parent(const struct udevice *child) |
Simon Glass | 43f488a | 2014-11-11 10:46:19 -0700 | [diff] [blame] | 913 | { |
| 914 | return child->parent; |
| 915 | } |
| 916 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 917 | ulong dev_get_driver_data(const struct udevice *dev) |
Simon Glass | 70c3a0e | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 918 | { |
Simon Glass | 46227bd | 2015-03-25 12:21:55 -0600 | [diff] [blame] | 919 | return dev->driver_data; |
Simon Glass | 70c3a0e | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 920 | } |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 921 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 922 | const void *dev_get_driver_ops(const struct udevice *dev) |
Przemyslaw Marczak | d3ef0d7 | 2015-04-15 13:07:24 +0200 | [diff] [blame] | 923 | { |
| 924 | if (!dev || !dev->driver->ops) |
| 925 | return NULL; |
| 926 | |
| 927 | return dev->driver->ops; |
| 928 | } |
| 929 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 930 | enum uclass_id device_get_uclass_id(const struct udevice *dev) |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 931 | { |
| 932 | return dev->uclass->uc_drv->id; |
| 933 | } |
Peng Fan | 99b7235 | 2015-02-10 14:46:32 +0800 | [diff] [blame] | 934 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 935 | const char *dev_get_uclass_name(const struct udevice *dev) |
Przemyslaw Marczak | 5ed2e42 | 2015-04-15 13:07:25 +0200 | [diff] [blame] | 936 | { |
| 937 | if (!dev) |
| 938 | return NULL; |
| 939 | |
| 940 | return dev->uclass->uc_drv->name; |
| 941 | } |
| 942 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 943 | bool device_has_children(const struct udevice *dev) |
Simon Glass | 40f829a | 2015-03-25 12:21:57 -0600 | [diff] [blame] | 944 | { |
| 945 | return !list_empty(&dev->child_head); |
| 946 | } |
| 947 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 948 | bool device_has_active_children(const struct udevice *dev) |
Simon Glass | 40f829a | 2015-03-25 12:21:57 -0600 | [diff] [blame] | 949 | { |
| 950 | struct udevice *child; |
| 951 | |
| 952 | for (device_find_first_child(dev, &child); |
| 953 | child; |
| 954 | device_find_next_child(&child)) { |
| 955 | if (device_active(child)) |
| 956 | return true; |
| 957 | } |
| 958 | |
| 959 | return false; |
| 960 | } |
| 961 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 962 | bool device_is_last_sibling(const struct udevice *dev) |
Simon Glass | 40f829a | 2015-03-25 12:21:57 -0600 | [diff] [blame] | 963 | { |
| 964 | struct udevice *parent = dev->parent; |
| 965 | |
| 966 | if (!parent) |
| 967 | return false; |
| 968 | return list_is_last(&dev->sibling_node, &parent->child_head); |
| 969 | } |
Simon Glass | e3b23e2 | 2015-07-30 13:40:39 -0600 | [diff] [blame] | 970 | |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 971 | void device_set_name_alloced(struct udevice *dev) |
| 972 | { |
Simon Glass | 2d4c7dc | 2016-07-04 11:58:15 -0600 | [diff] [blame] | 973 | dev->flags |= DM_FLAG_NAME_ALLOCED; |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 974 | } |
| 975 | |
Simon Glass | e3b23e2 | 2015-07-30 13:40:39 -0600 | [diff] [blame] | 976 | int device_set_name(struct udevice *dev, const char *name) |
| 977 | { |
| 978 | name = strdup(name); |
| 979 | if (!name) |
| 980 | return -ENOMEM; |
| 981 | dev->name = name; |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 982 | device_set_name_alloced(dev); |
Simon Glass | e3b23e2 | 2015-07-30 13:40:39 -0600 | [diff] [blame] | 983 | |
| 984 | return 0; |
| 985 | } |
Mugunthan V N | 4666bd9 | 2016-04-28 15:36:02 +0530 | [diff] [blame] | 986 | |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 987 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 988 | bool device_is_compatible(const struct udevice *dev, const char *compat) |
Mugunthan V N | 4666bd9 | 2016-04-28 15:36:02 +0530 | [diff] [blame] | 989 | { |
Masahiro Yamada | 9349bcc | 2018-04-19 12:14:02 +0900 | [diff] [blame] | 990 | return ofnode_device_is_compatible(dev_ofnode(dev), compat); |
Mugunthan V N | 4666bd9 | 2016-04-28 15:36:02 +0530 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | bool of_machine_is_compatible(const char *compat) |
| 994 | { |
| 995 | const void *fdt = gd->fdt_blob; |
| 996 | |
| 997 | return !fdt_node_check_compatible(fdt, 0, compat); |
| 998 | } |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 999 | |
| 1000 | int dev_disable_by_path(const char *path) |
| 1001 | { |
| 1002 | struct uclass *uc; |
| 1003 | ofnode node = ofnode_path(path); |
| 1004 | struct udevice *dev; |
| 1005 | int ret = 1; |
| 1006 | |
| 1007 | if (!of_live_active()) |
| 1008 | return -ENOSYS; |
| 1009 | |
| 1010 | list_for_each_entry(uc, &gd->uclass_root, sibling_node) { |
| 1011 | ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev); |
| 1012 | if (!ret) |
| 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | if (ret) |
| 1017 | return ret; |
| 1018 | |
| 1019 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
| 1020 | if (ret) |
| 1021 | return ret; |
| 1022 | |
| 1023 | ret = device_unbind(dev); |
| 1024 | if (ret) |
| 1025 | return ret; |
| 1026 | |
| 1027 | return ofnode_set_enabled(node, false); |
| 1028 | } |
| 1029 | |
| 1030 | int dev_enable_by_path(const char *path) |
| 1031 | { |
| 1032 | ofnode node = ofnode_path(path); |
| 1033 | ofnode pnode = ofnode_get_parent(node); |
| 1034 | struct udevice *parent; |
| 1035 | int ret = 1; |
| 1036 | |
| 1037 | if (!of_live_active()) |
| 1038 | return -ENOSYS; |
| 1039 | |
| 1040 | ret = device_find_by_ofnode(pnode, &parent); |
| 1041 | if (ret) |
| 1042 | return ret; |
| 1043 | |
| 1044 | ret = ofnode_set_enabled(node, true); |
| 1045 | if (ret) |
| 1046 | return ret; |
| 1047 | |
Bin Meng | 9a9b074 | 2018-10-10 22:06:58 -0700 | [diff] [blame] | 1048 | return lists_bind_fdt(parent, node, NULL, false); |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 1049 | } |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 1050 | #endif |