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> |
Vignesh R | 0dff370 | 2016-07-06 09:58:55 +0530 | [diff] [blame] | 12 | #include <asm/io.h> |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 13 | #include <clk.h> |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 14 | #include <fdtdec.h> |
Stefan Roese | adc0905 | 2015-09-02 07:41:12 +0200 | [diff] [blame] | 15 | #include <fdt_support.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 16 | #include <malloc.h> |
| 17 | #include <dm/device.h> |
| 18 | #include <dm/device-internal.h> |
| 19 | #include <dm/lists.h> |
Mario Six | 1f1c1ec | 2018-01-15 11:07:20 +0100 | [diff] [blame] | 20 | #include <dm/of_access.h> |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 21 | #include <dm/pinctrl.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 22 | #include <dm/platdata.h> |
Simon Glass | 322b290 | 2017-05-18 20:09:05 -0600 | [diff] [blame] | 23 | #include <dm/read.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 24 | #include <dm/uclass.h> |
| 25 | #include <dm/uclass-internal.h> |
| 26 | #include <dm/util.h> |
| 27 | #include <linux/err.h> |
| 28 | #include <linux/list.h> |
Peng Fan | 6fba32d | 2018-07-27 10:20:38 +0800 | [diff] [blame] | 29 | #include <power-domain.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 30 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 33 | static int device_bind_common(struct udevice *parent, const struct driver *drv, |
| 34 | const char *name, void *platdata, |
Simon Glass | b0cf4b3 | 2017-05-17 17:18:11 -0600 | [diff] [blame] | 35 | ulong driver_data, ofnode node, |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 36 | uint of_platdata_size, struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 37 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 38 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 39 | struct uclass *uc; |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 40 | int size, ret = 0; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 41 | |
Masahiro Yamada | e1cc1f0 | 2015-08-27 12:44:28 +0900 | [diff] [blame] | 42 | if (devp) |
| 43 | *devp = NULL; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 44 | if (!name) |
| 45 | return -EINVAL; |
| 46 | |
| 47 | ret = uclass_get(drv->id, &uc); |
Simon Glass | 43313de | 2015-08-30 16:55:16 -0600 | [diff] [blame] | 48 | if (ret) { |
| 49 | debug("Missing uclass for driver %s\n", drv->name); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 50 | return ret; |
Simon Glass | 43313de | 2015-08-30 16:55:16 -0600 | [diff] [blame] | 51 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 52 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 53 | dev = calloc(1, sizeof(struct udevice)); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 54 | if (!dev) |
| 55 | return -ENOMEM; |
| 56 | |
| 57 | INIT_LIST_HEAD(&dev->sibling_node); |
| 58 | INIT_LIST_HEAD(&dev->child_head); |
| 59 | INIT_LIST_HEAD(&dev->uclass_node); |
Masahiro Yamada | 029bfca | 2015-07-25 21:52:37 +0900 | [diff] [blame] | 60 | #ifdef CONFIG_DEVRES |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 61 | INIT_LIST_HEAD(&dev->devres_head); |
Masahiro Yamada | 029bfca | 2015-07-25 21:52:37 +0900 | [diff] [blame] | 62 | #endif |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 63 | dev->platdata = platdata; |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 64 | dev->driver_data = driver_data; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 65 | dev->name = name; |
Simon Glass | b0cf4b3 | 2017-05-17 17:18:11 -0600 | [diff] [blame] | 66 | dev->node = node; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 67 | dev->parent = parent; |
| 68 | dev->driver = drv; |
| 69 | dev->uclass = uc; |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 70 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 71 | dev->seq = -1; |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 72 | dev->req_seq = -1; |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 73 | if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) && |
| 74 | (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) { |
Simon Glass | 1bd3deb | 2015-02-27 22:06:30 -0700 | [diff] [blame] | 75 | /* |
Stefan Roese | 9d4ce6b | 2016-03-16 09:58:01 +0100 | [diff] [blame] | 76 | * Some devices, such as a SPI bus, I2C bus and serial ports |
| 77 | * are numbered using aliases. |
| 78 | * |
| 79 | * This is just a 'requested' sequence, and will be |
| 80 | * resolved (and ->seq updated) when the device is probed. |
| 81 | */ |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 82 | if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 83 | if (uc->uc_drv->name && ofnode_valid(node)) |
Simon Glass | 322b290 | 2017-05-18 20:09:05 -0600 | [diff] [blame] | 84 | dev_read_alias_seq(dev, &dev->req_seq); |
Thomas Fitzsimmons | f327ecb | 2019-09-06 07:51:18 -0400 | [diff] [blame] | 85 | #if CONFIG_IS_ENABLED(OF_PRIOR_STAGE) |
| 86 | if (dev->req_seq == -1) |
| 87 | dev->req_seq = |
| 88 | uclass_find_next_free_req_seq(drv->id); |
| 89 | #endif |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 90 | } else { |
| 91 | dev->req_seq = uclass_find_next_free_req_seq(drv->id); |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 92 | } |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 93 | } |
Simon Glass | 1bd3deb | 2015-02-27 22:06:30 -0700 | [diff] [blame] | 94 | |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 95 | if (drv->platdata_auto_alloc_size) { |
| 96 | bool alloc = !platdata; |
| 97 | |
| 98 | if (CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 99 | if (of_platdata_size) { |
| 100 | dev->flags |= DM_FLAG_OF_PLATDATA; |
| 101 | if (of_platdata_size < |
| 102 | drv->platdata_auto_alloc_size) |
| 103 | alloc = true; |
| 104 | } |
| 105 | } |
| 106 | if (alloc) { |
| 107 | dev->flags |= DM_FLAG_ALLOC_PDATA; |
| 108 | dev->platdata = calloc(1, |
| 109 | drv->platdata_auto_alloc_size); |
| 110 | if (!dev->platdata) { |
| 111 | ret = -ENOMEM; |
| 112 | goto fail_alloc1; |
| 113 | } |
| 114 | if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) { |
| 115 | memcpy(dev->platdata, platdata, |
| 116 | of_platdata_size); |
| 117 | } |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 120 | |
| 121 | size = uc->uc_drv->per_device_platdata_auto_alloc_size; |
| 122 | if (size) { |
| 123 | dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA; |
| 124 | dev->uclass_platdata = calloc(1, size); |
| 125 | if (!dev->uclass_platdata) { |
| 126 | ret = -ENOMEM; |
| 127 | goto fail_alloc2; |
| 128 | } |
| 129 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 130 | |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 131 | if (parent) { |
| 132 | size = parent->driver->per_child_platdata_auto_alloc_size; |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 133 | if (!size) { |
| 134 | size = parent->uclass->uc_drv-> |
| 135 | per_child_platdata_auto_alloc_size; |
| 136 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 137 | if (size) { |
| 138 | dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA; |
| 139 | dev->parent_platdata = calloc(1, size); |
| 140 | if (!dev->parent_platdata) { |
| 141 | ret = -ENOMEM; |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 142 | goto fail_alloc3; |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 146 | |
| 147 | /* put dev into parent's successor list */ |
| 148 | if (parent) |
| 149 | list_add_tail(&dev->sibling_node, &parent->child_head); |
| 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 | 6142d75 | 2016-01-25 14:58:42 -0700 | [diff] [blame] | 313 | int device_probe(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; |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 318 | int seq; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 319 | |
| 320 | if (!dev) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | if (dev->flags & DM_FLAG_ACTIVATED) |
| 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 | |
| 348 | /* Ensure all parents are probed */ |
| 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 | } |
| 362 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 363 | ret = device_probe(dev->parent); |
| 364 | if (ret) |
| 365 | goto fail; |
Bin Meng | 82cdd78 | 2015-08-24 01:14:02 -0700 | [diff] [blame] | 366 | |
| 367 | /* |
| 368 | * The device might have already been probed during |
| 369 | * the call to device_probe() on its parent device |
| 370 | * (e.g. PCI bridge devices). Test the flags again |
| 371 | * so that we don't mess up the device. |
| 372 | */ |
| 373 | if (dev->flags & DM_FLAG_ACTIVATED) |
| 374 | return 0; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 375 | } |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 376 | |
| 377 | seq = uclass_resolve_seq(dev); |
| 378 | if (seq < 0) { |
| 379 | ret = seq; |
| 380 | goto fail; |
| 381 | } |
| 382 | dev->seq = seq; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 383 | |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 384 | dev->flags |= DM_FLAG_ACTIVATED; |
| 385 | |
Simon Glass | bb53bda | 2015-09-12 08:45:19 -0600 | [diff] [blame] | 386 | /* |
| 387 | * Process pinctrl for everything except the root device, and |
Simon Glass | 27ce962 | 2016-01-21 19:43:25 -0700 | [diff] [blame] | 388 | * continue regardless of the result of pinctrl. Don't process pinctrl |
| 389 | * settings for pinctrl devices since the device may not yet be |
| 390 | * probed. |
Simon Glass | bb53bda | 2015-09-12 08:45:19 -0600 | [diff] [blame] | 391 | */ |
Simon Glass | 27ce962 | 2016-01-21 19:43:25 -0700 | [diff] [blame] | 392 | if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL) |
Simon Glass | bb53bda | 2015-09-12 08:45:19 -0600 | [diff] [blame] | 393 | pinctrl_select_state(dev, "default"); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 394 | |
Anatolij Gustschin | 63a3467 | 2019-07-14 21:11:01 +0200 | [diff] [blame] | 395 | if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent && |
Lokesh Vutla | c10e52b | 2019-09-27 13:48:12 +0530 | [diff] [blame^] | 396 | (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) && |
| 397 | !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) { |
Peng Fan | d220111 | 2019-09-17 09:29:22 +0000 | [diff] [blame] | 398 | ret = dev_power_domain_on(dev); |
| 399 | if (ret) |
| 400 | goto fail; |
Peng Fan | 6fba32d | 2018-07-27 10:20:38 +0800 | [diff] [blame] | 401 | } |
| 402 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 403 | ret = uclass_pre_probe_device(dev); |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 404 | if (ret) |
| 405 | goto fail; |
| 406 | |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 407 | if (dev->parent && dev->parent->driver->child_pre_probe) { |
| 408 | ret = dev->parent->driver->child_pre_probe(dev); |
| 409 | if (ret) |
| 410 | goto fail; |
| 411 | } |
| 412 | |
Simon Glass | a91e48b | 2019-09-25 08:55:53 -0600 | [diff] [blame] | 413 | if (drv->ofdata_to_platdata && |
| 414 | (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 415 | ret = drv->ofdata_to_platdata(dev); |
| 416 | if (ret) |
| 417 | goto fail; |
| 418 | } |
| 419 | |
Bin Meng | 513cf40 | 2019-07-05 09:23:16 -0700 | [diff] [blame] | 420 | /* Only handle devices that have a valid ofnode */ |
| 421 | if (dev_of_valid(dev)) { |
| 422 | /* |
| 423 | * Process 'assigned-{clocks/clock-parents/clock-rates}' |
| 424 | * properties |
| 425 | */ |
| 426 | ret = clk_set_defaults(dev); |
| 427 | if (ret) |
| 428 | goto fail; |
| 429 | } |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 430 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 431 | if (drv->probe) { |
| 432 | ret = drv->probe(dev); |
Simon Glass | d02009d | 2015-03-05 12:25:21 -0700 | [diff] [blame] | 433 | if (ret) { |
| 434 | dev->flags &= ~DM_FLAG_ACTIVATED; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 435 | goto fail; |
Simon Glass | d02009d | 2015-03-05 12:25:21 -0700 | [diff] [blame] | 436 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 439 | ret = uclass_post_probe_device(dev); |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 440 | if (ret) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 441 | goto fail_uclass; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 442 | |
Peng Fan | 7cf58dd | 2016-03-12 13:17:38 +0800 | [diff] [blame] | 443 | if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL) |
| 444 | pinctrl_select_state(dev, "default"); |
| 445 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 446 | return 0; |
| 447 | fail_uclass: |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 448 | if (device_remove(dev, DM_REMOVE_NORMAL)) { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 449 | dm_warn("%s: Device '%s' failed to remove on error path\n", |
| 450 | __func__, dev->name); |
| 451 | } |
| 452 | fail: |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 453 | dev->flags &= ~DM_FLAG_ACTIVATED; |
| 454 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 455 | dev->seq = -1; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 456 | device_free(dev); |
| 457 | |
| 458 | return ret; |
| 459 | } |
| 460 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 461 | void *dev_get_platdata(const struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 462 | { |
| 463 | if (!dev) { |
Simon Glass | df1e072 | 2014-12-10 08:55:56 -0700 | [diff] [blame] | 464 | dm_warn("%s: null device\n", __func__); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | return dev->platdata; |
| 469 | } |
| 470 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 471 | void *dev_get_parent_platdata(const struct udevice *dev) |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 472 | { |
| 473 | if (!dev) { |
Simon Glass | cdfc505 | 2015-07-06 16:47:40 -0600 | [diff] [blame] | 474 | dm_warn("%s: null device\n", __func__); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 475 | return NULL; |
| 476 | } |
| 477 | |
| 478 | return dev->parent_platdata; |
| 479 | } |
| 480 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 481 | void *dev_get_uclass_platdata(const struct udevice *dev) |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 482 | { |
| 483 | if (!dev) { |
Simon Glass | cdfc505 | 2015-07-06 16:47:40 -0600 | [diff] [blame] | 484 | dm_warn("%s: null device\n", __func__); |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | return dev->uclass_platdata; |
| 489 | } |
| 490 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 491 | void *dev_get_priv(const struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 492 | { |
| 493 | if (!dev) { |
Simon Glass | df1e072 | 2014-12-10 08:55:56 -0700 | [diff] [blame] | 494 | dm_warn("%s: null device\n", __func__); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 495 | return NULL; |
| 496 | } |
| 497 | |
| 498 | return dev->priv; |
| 499 | } |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 500 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 501 | void *dev_get_uclass_priv(const struct udevice *dev) |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 502 | { |
| 503 | if (!dev) { |
| 504 | dm_warn("%s: null device\n", __func__); |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | return dev->uclass_priv; |
| 509 | } |
| 510 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 511 | void *dev_get_parent_priv(const struct udevice *dev) |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 512 | { |
| 513 | if (!dev) { |
Simon Glass | df1e072 | 2014-12-10 08:55:56 -0700 | [diff] [blame] | 514 | dm_warn("%s: null device\n", __func__); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 515 | return NULL; |
| 516 | } |
| 517 | |
| 518 | return dev->parent_priv; |
| 519 | } |
| 520 | |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 521 | static int device_get_device_tail(struct udevice *dev, int ret, |
| 522 | struct udevice **devp) |
| 523 | { |
| 524 | if (ret) |
| 525 | return ret; |
| 526 | |
| 527 | ret = device_probe(dev); |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | |
| 531 | *devp = dev; |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 536 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 537 | /** |
| 538 | * device_find_by_ofnode() - Return device associated with given ofnode |
| 539 | * |
| 540 | * The returned device is *not* activated. |
| 541 | * |
| 542 | * @node: The ofnode for which a associated device should be looked up |
| 543 | * @devp: Pointer to structure to hold the found device |
| 544 | * Return: 0 if OK, -ve on error |
| 545 | */ |
| 546 | static int device_find_by_ofnode(ofnode node, struct udevice **devp) |
| 547 | { |
| 548 | struct uclass *uc; |
| 549 | struct udevice *dev; |
| 550 | int ret; |
| 551 | |
| 552 | list_for_each_entry(uc, &gd->uclass_root, sibling_node) { |
| 553 | ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, |
| 554 | &dev); |
| 555 | if (!ret || dev) { |
| 556 | *devp = dev; |
| 557 | return 0; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | return -ENODEV; |
| 562 | } |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 563 | #endif |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 564 | |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 565 | int device_get_child(struct udevice *parent, int index, struct udevice **devp) |
| 566 | { |
| 567 | struct udevice *dev; |
| 568 | |
| 569 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 570 | if (!index--) |
| 571 | return device_get_device_tail(dev, 0, devp); |
| 572 | } |
| 573 | |
| 574 | return -ENODEV; |
| 575 | } |
| 576 | |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 577 | int device_get_child_count(struct udevice *parent) |
| 578 | { |
| 579 | struct udevice *dev; |
| 580 | int count = 0; |
| 581 | |
| 582 | list_for_each_entry(dev, &parent->child_head, sibling_node) |
| 583 | count++; |
| 584 | |
| 585 | return count; |
| 586 | } |
| 587 | |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 588 | int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq, |
| 589 | bool find_req_seq, struct udevice **devp) |
| 590 | { |
| 591 | struct udevice *dev; |
| 592 | |
| 593 | *devp = NULL; |
| 594 | if (seq_or_req_seq == -1) |
| 595 | return -ENODEV; |
| 596 | |
| 597 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 598 | if ((find_req_seq ? dev->req_seq : dev->seq) == |
| 599 | seq_or_req_seq) { |
| 600 | *devp = dev; |
| 601 | return 0; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | return -ENODEV; |
| 606 | } |
| 607 | |
| 608 | int device_get_child_by_seq(struct udevice *parent, int seq, |
| 609 | struct udevice **devp) |
| 610 | { |
| 611 | struct udevice *dev; |
| 612 | int ret; |
| 613 | |
| 614 | *devp = NULL; |
| 615 | ret = device_find_child_by_seq(parent, seq, false, &dev); |
| 616 | if (ret == -ENODEV) { |
| 617 | /* |
| 618 | * We didn't find it in probed devices. See if there is one |
| 619 | * that will request this seq if probed. |
| 620 | */ |
| 621 | ret = device_find_child_by_seq(parent, seq, true, &dev); |
| 622 | } |
| 623 | return device_get_device_tail(dev, ret, devp); |
| 624 | } |
| 625 | |
| 626 | int device_find_child_by_of_offset(struct udevice *parent, int of_offset, |
| 627 | struct udevice **devp) |
| 628 | { |
| 629 | struct udevice *dev; |
| 630 | |
| 631 | *devp = NULL; |
| 632 | |
| 633 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 634 | if (dev_of_offset(dev) == of_offset) { |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 635 | *devp = dev; |
| 636 | return 0; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | return -ENODEV; |
| 641 | } |
| 642 | |
Simon Glass | 861bc9f | 2015-06-23 15:38:38 -0600 | [diff] [blame] | 643 | int device_get_child_by_of_offset(struct udevice *parent, int node, |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 644 | struct udevice **devp) |
| 645 | { |
| 646 | struct udevice *dev; |
| 647 | int ret; |
| 648 | |
| 649 | *devp = NULL; |
Simon Glass | 861bc9f | 2015-06-23 15:38:38 -0600 | [diff] [blame] | 650 | ret = device_find_child_by_of_offset(parent, node, &dev); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 651 | return device_get_device_tail(dev, ret, devp); |
| 652 | } |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 653 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 654 | static struct udevice *_device_find_global_by_ofnode(struct udevice *parent, |
| 655 | ofnode ofnode) |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 656 | { |
| 657 | struct udevice *dev, *found; |
| 658 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 659 | if (ofnode_equal(dev_ofnode(parent), ofnode)) |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 660 | return parent; |
| 661 | |
| 662 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 663 | found = _device_find_global_by_ofnode(dev, ofnode); |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 664 | if (found) |
| 665 | return found; |
| 666 | } |
| 667 | |
| 668 | return NULL; |
| 669 | } |
| 670 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 671 | int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp) |
| 672 | { |
| 673 | *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode); |
| 674 | |
| 675 | return *devp ? 0 : -ENOENT; |
| 676 | } |
| 677 | |
| 678 | int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp) |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 679 | { |
| 680 | struct udevice *dev; |
| 681 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 682 | dev = _device_find_global_by_ofnode(gd->dm_root, ofnode); |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 683 | return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp); |
| 684 | } |
| 685 | |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 686 | int device_find_first_child(struct udevice *parent, struct udevice **devp) |
| 687 | { |
| 688 | if (list_empty(&parent->child_head)) { |
| 689 | *devp = NULL; |
| 690 | } else { |
| 691 | *devp = list_first_entry(&parent->child_head, struct udevice, |
| 692 | sibling_node); |
| 693 | } |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | int device_find_next_child(struct udevice **devp) |
| 699 | { |
| 700 | struct udevice *dev = *devp; |
| 701 | struct udevice *parent = dev->parent; |
| 702 | |
| 703 | if (list_is_last(&dev->sibling_node, &parent->child_head)) { |
| 704 | *devp = NULL; |
| 705 | } else { |
| 706 | *devp = list_entry(dev->sibling_node.next, struct udevice, |
| 707 | sibling_node); |
| 708 | } |
| 709 | |
| 710 | return 0; |
| 711 | } |
Simon Glass | 70c3a0e | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 712 | |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 713 | int device_find_first_inactive_child(struct udevice *parent, |
| 714 | enum uclass_id uclass_id, |
| 715 | struct udevice **devp) |
| 716 | { |
| 717 | struct udevice *dev; |
| 718 | |
| 719 | *devp = NULL; |
| 720 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 721 | if (!device_active(dev) && |
| 722 | device_get_uclass_id(dev) == uclass_id) { |
| 723 | *devp = dev; |
| 724 | return 0; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | return -ENODEV; |
| 729 | } |
| 730 | |
Simon Glass | a11341a | 2018-11-18 08:14:31 -0700 | [diff] [blame] | 731 | int device_find_first_child_by_uclass(struct udevice *parent, |
| 732 | enum uclass_id uclass_id, |
| 733 | struct udevice **devp) |
| 734 | { |
| 735 | struct udevice *dev; |
| 736 | |
| 737 | *devp = NULL; |
| 738 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 739 | if (device_get_uclass_id(dev) == uclass_id) { |
| 740 | *devp = dev; |
| 741 | return 0; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | return -ENODEV; |
| 746 | } |
| 747 | |
| 748 | int device_find_child_by_name(struct udevice *parent, const char *name, |
| 749 | struct udevice **devp) |
| 750 | { |
| 751 | struct udevice *dev; |
| 752 | |
| 753 | *devp = NULL; |
| 754 | |
| 755 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 756 | if (!strcmp(dev->name, name)) { |
| 757 | *devp = dev; |
| 758 | return 0; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | return -ENODEV; |
| 763 | } |
| 764 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 765 | struct udevice *dev_get_parent(const struct udevice *child) |
Simon Glass | 43f488a | 2014-11-11 10:46:19 -0700 | [diff] [blame] | 766 | { |
| 767 | return child->parent; |
| 768 | } |
| 769 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 770 | ulong dev_get_driver_data(const struct udevice *dev) |
Simon Glass | 70c3a0e | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 771 | { |
Simon Glass | 46227bd | 2015-03-25 12:21:55 -0600 | [diff] [blame] | 772 | return dev->driver_data; |
Simon Glass | 70c3a0e | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 773 | } |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 774 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 775 | const void *dev_get_driver_ops(const struct udevice *dev) |
Przemyslaw Marczak | d3ef0d7 | 2015-04-15 13:07:24 +0200 | [diff] [blame] | 776 | { |
| 777 | if (!dev || !dev->driver->ops) |
| 778 | return NULL; |
| 779 | |
| 780 | return dev->driver->ops; |
| 781 | } |
| 782 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 783 | enum uclass_id device_get_uclass_id(const struct udevice *dev) |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 784 | { |
| 785 | return dev->uclass->uc_drv->id; |
| 786 | } |
Peng Fan | 99b7235 | 2015-02-10 14:46:32 +0800 | [diff] [blame] | 787 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 788 | const char *dev_get_uclass_name(const struct udevice *dev) |
Przemyslaw Marczak | 5ed2e42 | 2015-04-15 13:07:25 +0200 | [diff] [blame] | 789 | { |
| 790 | if (!dev) |
| 791 | return NULL; |
| 792 | |
| 793 | return dev->uclass->uc_drv->name; |
| 794 | } |
| 795 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 796 | bool device_has_children(const struct udevice *dev) |
Simon Glass | 40f829a | 2015-03-25 12:21:57 -0600 | [diff] [blame] | 797 | { |
| 798 | return !list_empty(&dev->child_head); |
| 799 | } |
| 800 | |
| 801 | bool device_has_active_children(struct udevice *dev) |
| 802 | { |
| 803 | struct udevice *child; |
| 804 | |
| 805 | for (device_find_first_child(dev, &child); |
| 806 | child; |
| 807 | device_find_next_child(&child)) { |
| 808 | if (device_active(child)) |
| 809 | return true; |
| 810 | } |
| 811 | |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | bool device_is_last_sibling(struct udevice *dev) |
| 816 | { |
| 817 | struct udevice *parent = dev->parent; |
| 818 | |
| 819 | if (!parent) |
| 820 | return false; |
| 821 | return list_is_last(&dev->sibling_node, &parent->child_head); |
| 822 | } |
Simon Glass | e3b23e2 | 2015-07-30 13:40:39 -0600 | [diff] [blame] | 823 | |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 824 | void device_set_name_alloced(struct udevice *dev) |
| 825 | { |
Simon Glass | 2d4c7dc | 2016-07-04 11:58:15 -0600 | [diff] [blame] | 826 | dev->flags |= DM_FLAG_NAME_ALLOCED; |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 827 | } |
| 828 | |
Simon Glass | e3b23e2 | 2015-07-30 13:40:39 -0600 | [diff] [blame] | 829 | int device_set_name(struct udevice *dev, const char *name) |
| 830 | { |
| 831 | name = strdup(name); |
| 832 | if (!name) |
| 833 | return -ENOMEM; |
| 834 | dev->name = name; |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 835 | device_set_name_alloced(dev); |
Simon Glass | e3b23e2 | 2015-07-30 13:40:39 -0600 | [diff] [blame] | 836 | |
| 837 | return 0; |
| 838 | } |
Mugunthan V N | 4666bd9 | 2016-04-28 15:36:02 +0530 | [diff] [blame] | 839 | |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 840 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 54cbcc8 | 2017-05-18 20:08:57 -0600 | [diff] [blame] | 841 | bool device_is_compatible(struct udevice *dev, const char *compat) |
Mugunthan V N | 4666bd9 | 2016-04-28 15:36:02 +0530 | [diff] [blame] | 842 | { |
Masahiro Yamada | 9349bcc | 2018-04-19 12:14:02 +0900 | [diff] [blame] | 843 | return ofnode_device_is_compatible(dev_ofnode(dev), compat); |
Mugunthan V N | 4666bd9 | 2016-04-28 15:36:02 +0530 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | bool of_machine_is_compatible(const char *compat) |
| 847 | { |
| 848 | const void *fdt = gd->fdt_blob; |
| 849 | |
| 850 | return !fdt_node_check_compatible(fdt, 0, compat); |
| 851 | } |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 852 | |
| 853 | int dev_disable_by_path(const char *path) |
| 854 | { |
| 855 | struct uclass *uc; |
| 856 | ofnode node = ofnode_path(path); |
| 857 | struct udevice *dev; |
| 858 | int ret = 1; |
| 859 | |
| 860 | if (!of_live_active()) |
| 861 | return -ENOSYS; |
| 862 | |
| 863 | list_for_each_entry(uc, &gd->uclass_root, sibling_node) { |
| 864 | ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev); |
| 865 | if (!ret) |
| 866 | break; |
| 867 | } |
| 868 | |
| 869 | if (ret) |
| 870 | return ret; |
| 871 | |
| 872 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
| 873 | if (ret) |
| 874 | return ret; |
| 875 | |
| 876 | ret = device_unbind(dev); |
| 877 | if (ret) |
| 878 | return ret; |
| 879 | |
| 880 | return ofnode_set_enabled(node, false); |
| 881 | } |
| 882 | |
| 883 | int dev_enable_by_path(const char *path) |
| 884 | { |
| 885 | ofnode node = ofnode_path(path); |
| 886 | ofnode pnode = ofnode_get_parent(node); |
| 887 | struct udevice *parent; |
| 888 | int ret = 1; |
| 889 | |
| 890 | if (!of_live_active()) |
| 891 | return -ENOSYS; |
| 892 | |
| 893 | ret = device_find_by_ofnode(pnode, &parent); |
| 894 | if (ret) |
| 895 | return ret; |
| 896 | |
| 897 | ret = ofnode_set_enabled(node, true); |
| 898 | if (ret) |
| 899 | return ret; |
| 900 | |
Bin Meng | 9a9b074 | 2018-10-10 22:06:58 -0700 | [diff] [blame] | 901 | return lists_bind_fdt(parent, node, NULL, false); |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 902 | } |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 903 | #endif |