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