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