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