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