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