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