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