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