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