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