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