blob: bf7f261cbce02f36be0a7b39027beb478c5cc633 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdd6ab882014-02-26 15:59:18 -07002/*
3 * Device manager
4 *
5 * Copyright (c) 2013 Google, Inc
6 *
7 * (C) Copyright 2012
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassdd6ab882014-02-26 15:59:18 -07009 */
10
Tom Riniabb9a042024-05-18 20:20:43 -060011#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070012#include <cpu_func.h>
Simon Glass3ee337a2022-03-04 08:43:03 -070013#include <event.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Vignesh R0dff3702016-07-06 09:58:55 +053016#include <asm/io.h>
Philipp Tomsich9cf03b02018-01-08 13:59:18 +010017#include <clk.h>
Simon Glassdb6f0202014-07-23 06:55:12 -060018#include <fdtdec.h>
Stefan Roeseadc09052015-09-02 07:41:12 +020019#include <fdt_support.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070020#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060021#include <asm/cache.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070022#include <dm/device.h>
23#include <dm/device-internal.h>
24#include <dm/lists.h>
Mario Six1f1c1ec2018-01-15 11:07:20 +010025#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090026#include <dm/pinctrl.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070027#include <dm/platdata.h>
Simon Glass322b2902017-05-18 20:09:05 -060028#include <dm/read.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070029#include <dm/uclass.h>
30#include <dm/uclass-internal.h>
31#include <dm/util.h>
Mark Kettenise1311b62021-10-23 16:58:01 +020032#include <iommu.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070033#include <linux/err.h>
34#include <linux/list.h>
Peng Fan6fba32d2018-07-27 10:20:38 +080035#include <power-domain.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060036#include <linux/printk.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070037
Simon Glassdb6f0202014-07-23 06:55:12 -060038DECLARE_GLOBAL_DATA_PTR;
39
Stephen Warren8c93df12016-05-11 15:26:24 -060040static int device_bind_common(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -070041 const char *name, void *plat,
Simon Glassb0cf4b32017-05-17 17:18:11 -060042 ulong driver_data, ofnode node,
Simon Glass39edb952020-12-03 16:55:19 -070043 uint of_plat_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070044{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020045 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070046 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020047 int size, ret = 0;
Simon Glass7272e922020-12-16 21:20:09 -070048 bool auto_seq = true;
Simon Glass2543b162020-12-22 19:30:29 -070049 void *ptr;
Simon Glassdd6ab882014-02-26 15:59:18 -070050
Simon Glass47fd4a22021-03-15 17:25:15 +130051 if (CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND))
52 return -ENOSYS;
53
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090054 if (devp)
55 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070056 if (!name)
57 return -EINVAL;
58
59 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060060 if (ret) {
61 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070062 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060063 }
Simon Glassdd6ab882014-02-26 15:59:18 -070064
Heiko Schocherb74fcb42014-05-22 12:43:05 +020065 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070066 if (!dev)
67 return -ENOMEM;
68
69 INIT_LIST_HEAD(&dev->sibling_node);
70 INIT_LIST_HEAD(&dev->child_head);
71 INIT_LIST_HEAD(&dev->uclass_node);
Simon Glass3996f062022-03-27 14:26:19 -060072#if CONFIG_IS_ENABLED(DEVRES)
Masahiro Yamada8b15b162015-07-25 21:52:35 +090073 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090074#endif
Simon Glass2543b162020-12-22 19:30:29 -070075 dev_set_plat(dev, plat);
Stephen Warren8c93df12016-05-11 15:26:24 -060076 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070077 dev->name = name;
Simon Glassa7ece582020-12-19 10:40:14 -070078 dev_set_ofnode(dev, node);
Simon Glassdd6ab882014-02-26 15:59:18 -070079 dev->parent = parent;
80 dev->driver = drv;
81 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060082
Simon Glass5e349922020-12-19 10:40:09 -070083 dev->seq_ = -1;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010084 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
85 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070086 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010087 * Some devices, such as a SPI bus, I2C bus and serial ports
88 * are numbered using aliases.
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010089 */
Simon Glasse422f8c2020-07-09 18:39:24 -060090 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
91 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass7272e922020-12-16 21:20:09 -070092 if (uc->uc_drv->name && ofnode_valid(node)) {
Simon Glass28823e32021-07-05 16:32:39 -060093 if (!dev_read_alias_seq(dev, &dev->seq_)) {
Simon Glass38040a92020-12-16 21:20:32 -070094 auto_seq = false;
Simon Glass28823e32021-07-05 16:32:39 -060095 log_debug(" - seq=%d\n", dev->seq_);
96 }
Simon Glass7272e922020-12-16 21:20:09 -070097 }
Simon Glass0ccb0972015-01-25 08:27:05 -070098 }
Simon Glassdb6f0202014-07-23 06:55:12 -060099 }
Simon Glass3a9d5f12020-12-16 21:20:17 -0700100 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
Simon Glass5e349922020-12-19 10:40:09 -0700101 dev->seq_ = uclass_find_next_free_seq(uc);
Simon Glass1bd3deb2015-02-27 22:06:30 -0700102
Simon Glasse07434b2021-02-03 21:29:43 -0700103 /* Check if we need to allocate plat */
Simon Glass71fa5b42020-12-03 16:55:18 -0700104 if (drv->plat_auto) {
105 bool alloc = !plat;
Simon Glassafbf9b82016-07-04 11:58:18 -0600106
Simon Glasse07434b2021-02-03 21:29:43 -0700107 /*
108 * For of-platdata, we try use the existing data, but if
109 * plat_auto is larger, we must allocate a new space
110 */
Simon Glassafbf9b82016-07-04 11:58:18 -0600111 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glasse07434b2021-02-03 21:29:43 -0700112 if (of_plat_size)
Simon Glass6211d762020-12-19 10:40:10 -0700113 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
Simon Glasse07434b2021-02-03 21:29:43 -0700114 if (of_plat_size < drv->plat_auto)
115 alloc = true;
Simon Glassafbf9b82016-07-04 11:58:18 -0600116 }
117 if (alloc) {
Simon Glass6211d762020-12-19 10:40:10 -0700118 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700119 ptr = calloc(1, drv->plat_auto);
120 if (!ptr) {
Simon Glassafbf9b82016-07-04 11:58:18 -0600121 ret = -ENOMEM;
122 goto fail_alloc1;
123 }
Simon Glasse07434b2021-02-03 21:29:43 -0700124
125 /*
126 * For of-platdata, copy the old plat into the new
127 * space
128 */
Simon Glass2543b162020-12-22 19:30:29 -0700129 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
130 memcpy(ptr, plat, of_plat_size);
131 dev_set_plat(dev, ptr);
Simon Glassba750492015-01-25 08:27:00 -0700132 }
133 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200134
Simon Glass71fa5b42020-12-03 16:55:18 -0700135 size = uc->uc_drv->per_device_plat_auto;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200136 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700137 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700138 ptr = calloc(1, size);
139 if (!ptr) {
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200140 ret = -ENOMEM;
141 goto fail_alloc2;
142 }
Simon Glass2543b162020-12-22 19:30:29 -0700143 dev_set_uclass_plat(dev, ptr);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200144 }
Simon Glass11b61732015-01-25 08:27:01 -0700145
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200146 if (parent) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700147 size = parent->driver->per_child_plat_auto;
Simon Glassd6206a52021-02-03 21:29:44 -0700148 if (!size)
Simon Glass71fa5b42020-12-03 16:55:18 -0700149 size = parent->uclass->uc_drv->per_child_plat_auto;
Simon Glass11b61732015-01-25 08:27:01 -0700150 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700151 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700152 ptr = calloc(1, size);
153 if (!ptr) {
Simon Glass11b61732015-01-25 08:27:01 -0700154 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200155 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700156 }
Simon Glass2543b162020-12-22 19:30:29 -0700157 dev_set_parent_plat(dev, ptr);
Simon Glass11b61732015-01-25 08:27:01 -0700158 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100159 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700160 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100161 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700162
163 ret = uclass_bind_device(dev);
164 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700165 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700166
167 /* if we fail to bind we remove device from successors and free it */
168 if (drv->bind) {
169 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700170 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700171 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700172 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700173 if (parent && parent->driver->child_post_bind) {
174 ret = parent->driver->child_post_bind(dev);
175 if (ret)
176 goto fail_child_post_bind;
177 }
Simon Glass286863d2016-01-05 09:30:59 -0700178 if (uc->uc_drv->post_bind) {
179 ret = uc->uc_drv->post_bind(dev);
180 if (ret)
181 goto fail_uclass_post_bind;
182 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700183
Simon Glassdd6ab882014-02-26 15:59:18 -0700184 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900185 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900186 if (devp)
187 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700188
Simon Glass6211d762020-12-19 10:40:10 -0700189 dev_or_flags(dev, DM_FLAG_BOUND);
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900190
Simon Glassdd6ab882014-02-26 15:59:18 -0700191 return 0;
192
Simon Glass286863d2016-01-05 09:30:59 -0700193fail_uclass_post_bind:
194 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700195fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900196 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700197 if (drv->unbind && drv->unbind(dev)) {
198 dm_warn("unbind() method failed on dev '%s' on error path\n",
199 dev->name);
200 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700201 }
202
Simon Glassdd6ab882014-02-26 15:59:18 -0700203fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900204 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700205 if (uclass_unbind_device(dev)) {
206 dm_warn("Failed to unbind dev '%s' on error path\n",
207 dev->name);
208 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700209 }
210fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900211 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700212 list_del(&dev->sibling_node);
Simon Glass6211d762020-12-19 10:40:10 -0700213 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass2543b162020-12-22 19:30:29 -0700214 free(dev_get_parent_plat(dev));
215 dev_set_parent_plat(dev, NULL);
Simon Glasse70a4f42015-02-27 22:06:33 -0700216 }
Simon Glass11b61732015-01-25 08:27:01 -0700217 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200218fail_alloc3:
Simon Glassd6206a52021-02-03 21:29:44 -0700219 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
220 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
221 free(dev_get_uclass_plat(dev));
222 dev_set_uclass_plat(dev, NULL);
223 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200224 }
Simon Glass11b61732015-01-25 08:27:01 -0700225fail_alloc2:
Simon Glassd6206a52021-02-03 21:29:44 -0700226 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
227 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
228 free(dev_get_plat(dev));
229 dev_set_plat(dev, NULL);
230 }
Simon Glassba750492015-01-25 08:27:00 -0700231 }
232fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900233 devres_release_all(dev);
234
Simon Glassdd6ab882014-02-26 15:59:18 -0700235 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700236
Simon Glassdd6ab882014-02-26 15:59:18 -0700237 return ret;
238}
239
Stephen Warren8c93df12016-05-11 15:26:24 -0600240int device_bind_with_driver_data(struct udevice *parent,
241 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600242 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600243 struct udevice **devp)
244{
Simon Glass322b2902017-05-18 20:09:05 -0600245 return device_bind_common(parent, drv, name, NULL, driver_data, node,
246 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600247}
248
Simon Glass884870f2020-11-28 17:50:01 -0700249int device_bind(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -0700250 const char *name, void *plat, ofnode node,
Simon Glass884870f2020-11-28 17:50:01 -0700251 struct udevice **devp)
Simon Glass9eb151e2018-06-11 13:07:15 -0600252{
Simon Glass71fa5b42020-12-03 16:55:18 -0700253 return device_bind_common(parent, drv, name, plat, 0, node, 0,
Simon Glass9eb151e2018-06-11 13:07:15 -0600254 devp);
255}
256
Simon Glassfef72b72014-07-23 06:55:03 -0600257int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Simon Glasscfd6a002020-10-03 11:31:33 -0600258 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700259{
260 struct driver *drv;
Simon Glass39edb952020-12-03 16:55:19 -0700261 uint plat_size = 0;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300262 int ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700263
264 drv = lists_driver_lookup_name(info->name);
265 if (!drv)
266 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600267 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
268 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700269
Simon Glassafbf9b82016-07-04 11:58:18 -0600270#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass39edb952020-12-03 16:55:19 -0700271 plat_size = info->plat_size;
Simon Glassafbf9b82016-07-04 11:58:18 -0600272#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700273 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
Simon Glass39edb952020-12-03 16:55:19 -0700274 ofnode_null(), plat_size, devp);
Walter Lozanoffc41b02020-06-25 01:10:11 -0300275 if (ret)
276 return ret;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300277
278 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700279}
280
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300281int device_reparent(struct udevice *dev, struct udevice *new_parent)
282{
283 struct udevice *pos, *n;
284
285 assert(dev);
286 assert(new_parent);
287
Sean Andersonf69a3302022-03-30 12:26:40 -0400288 device_foreach_child_safe(pos, n, dev->parent) {
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300289 if (pos->driver != dev->driver)
290 continue;
291
292 list_del(&dev->sibling_node);
293 list_add_tail(&dev->sibling_node, &new_parent->child_head);
294 dev->parent = new_parent;
295
296 break;
297 }
298
299 return 0;
300}
301
Simon Glass825d3f92015-03-25 12:21:53 -0600302static void *alloc_priv(int size, uint flags)
303{
304 void *priv;
305
306 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530307 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600308 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600309 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600310 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600311
312 /*
313 * Ensure that the zero bytes are flushed to memory.
314 * This prevents problems if the driver uses this as
315 * both an input and an output buffer:
316 *
317 * 1. Zeroes written to buffer (here) and sit in the
318 * cache
319 * 2. Driver issues a read command to DMA
320 * 3. CPU runs out of cache space and evicts some cache
321 * data in the buffer, writing zeroes to RAM from
322 * the memset() above
323 * 4. DMA completes
324 * 5. Buffer now has some DMA data and some zeroes
325 * 6. Data being read is now incorrect
326 *
327 * To prevent this, ensure that the cache is clean
328 * within this range at the start. The driver can then
329 * use normal flush-after-write, invalidate-before-read
330 * procedures.
Simon Glass27bc9b12017-04-04 13:00:19 -0600331 */
Simon Glass27bc9b12017-04-04 13:00:19 -0600332 flush_dcache_range((ulong)priv, (ulong)priv + size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600333 }
Simon Glass825d3f92015-03-25 12:21:53 -0600334 } else {
335 priv = calloc(1, size);
336 }
337
338 return priv;
339}
340
Simon Glassad337e62020-12-19 10:40:08 -0700341/**
342 * device_alloc_priv() - Allocate priv/plat data required by the device
343 *
344 * @dev: Device to process
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100345 * Return: 0 if OK, -ENOMEM if out of memory
Simon Glassad337e62020-12-19 10:40:08 -0700346 */
347static int device_alloc_priv(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700348{
Simon Glassa626dff2015-03-25 12:21:54 -0600349 const struct driver *drv;
Simon Glass2543b162020-12-22 19:30:29 -0700350 void *ptr;
Simon Glassad337e62020-12-19 10:40:08 -0700351 int size;
Simon Glass44c47892020-04-05 15:38:19 -0600352
Simon Glassdd6ab882014-02-26 15:59:18 -0700353 drv = dev->driver;
354 assert(drv);
355
Bin Meng82cdd782015-08-24 01:14:02 -0700356 /* Allocate private data if requested and not reentered */
Simon Glass2543b162020-12-22 19:30:29 -0700357 if (drv->priv_auto && !dev_get_priv(dev)) {
358 ptr = alloc_priv(drv->priv_auto, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700359 if (!ptr)
360 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700361 dev_set_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700362 }
Simon Glassad337e62020-12-19 10:40:08 -0700363
Bin Meng82cdd782015-08-24 01:14:02 -0700364 /* Allocate private data if requested and not reentered */
Simon Glass8a2b47f2020-12-03 16:55:17 -0700365 size = dev->uclass->uc_drv->per_device_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700366 if (size && !dev_get_uclass_priv(dev)) {
367 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700368 if (!ptr)
369 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700370 dev_set_uclass_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700371 }
372
Simon Glassa8d8f162019-12-29 21:19:18 -0700373 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700374 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700375 size = dev->parent->driver->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700376 if (!size)
Simon Glass8a2b47f2020-12-03 16:55:17 -0700377 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700378 if (size && !dev_get_parent_priv(dev)) {
379 ptr = alloc_priv(size, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700380 if (!ptr)
381 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700382 dev_set_parent_priv(dev, ptr);
Simon Glass60d971b2014-07-23 06:55:20 -0600383 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700384 }
385
Simon Glassad337e62020-12-19 10:40:08 -0700386 return 0;
387}
388
389int device_of_to_plat(struct udevice *dev)
390{
391 const struct driver *drv;
392 int ret;
393
394 if (!dev)
395 return -EINVAL;
396
Simon Glass6211d762020-12-19 10:40:10 -0700397 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
Simon Glassad337e62020-12-19 10:40:08 -0700398 return 0;
399
Simon Glass47fd4a22021-03-15 17:25:15 +1300400 /*
401 * This is not needed if binding is disabled, since data is allocated
402 * at build time.
403 */
404 if (!CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND)) {
405 /* Ensure all parents have ofdata */
406 if (dev->parent) {
407 ret = device_of_to_plat(dev->parent);
408 if (ret)
409 goto fail;
410
411 /*
412 * The device might have already been probed during
413 * the call to device_probe() on its parent device
414 * (e.g. PCI bridge devices). Test the flags again
415 * so that we don't mess up the device.
416 */
417 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
418 return 0;
419 }
420
421 ret = device_alloc_priv(dev);
Simon Glassad337e62020-12-19 10:40:08 -0700422 if (ret)
423 goto fail;
Simon Glassad337e62020-12-19 10:40:08 -0700424 }
Simon Glassad337e62020-12-19 10:40:08 -0700425 drv = dev->driver;
426 assert(drv);
427
Simon Glassaad29ae2020-12-03 16:55:21 -0700428 if (drv->of_to_plat &&
Simon Glass07c17772020-12-19 10:40:12 -0700429 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
Simon Glassaad29ae2020-12-03 16:55:21 -0700430 ret = drv->of_to_plat(dev);
Simon Glassa8d8f162019-12-29 21:19:18 -0700431 if (ret)
432 goto fail;
433 }
Simon Glass60d971b2014-07-23 06:55:20 -0600434
Simon Glass6211d762020-12-19 10:40:10 -0700435 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
Simon Glassd3c9d302019-12-29 21:19:21 -0700436
Simon Glass4a263852019-12-29 21:19:20 -0700437 return 0;
438fail:
439 device_free(dev);
440
441 return ret;
442}
443
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100444/**
445 * device_get_dma_constraints() - Populate device's DMA constraints
446 *
447 * Gets a device's DMA constraints from firmware. This information is later
448 * used by drivers to translate physcal addresses to the device's bus address
449 * space. For now only device-tree is supported.
450 *
451 * @dev: Pointer to target device
452 * Return: 0 if OK or if no DMA constraints were found, error otherwise
453 */
454static int device_get_dma_constraints(struct udevice *dev)
455{
456 struct udevice *parent = dev->parent;
457 phys_addr_t cpu = 0;
458 dma_addr_t bus = 0;
459 u64 size = 0;
460 int ret;
461
462 if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
463 return 0;
464
465 /*
466 * We start parsing for dma-ranges from the device's bus node. This is
467 * specially important on nested buses.
468 */
469 ret = dev_get_dma_range(parent, &cpu, &bus, &size);
470 /* Don't return an error if no 'dma-ranges' were found */
471 if (ret && ret != -ENOENT) {
472 dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret);
473 return ret;
474 }
475
476 dev_set_dma_offset(dev, cpu - bus);
477
478 return 0;
479}
480
Simon Glass4a263852019-12-29 21:19:20 -0700481int device_probe(struct udevice *dev)
482{
483 const struct driver *drv;
484 int ret;
Simon Glass4a263852019-12-29 21:19:20 -0700485
486 if (!dev)
487 return -EINVAL;
488
Simon Glass6211d762020-12-19 10:40:10 -0700489 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass4a263852019-12-29 21:19:20 -0700490 return 0;
491
Simon Glass3ee337a2022-03-04 08:43:03 -0700492 ret = device_notify(dev, EVT_DM_PRE_PROBE);
493 if (ret)
494 return ret;
495
Simon Glass4a263852019-12-29 21:19:20 -0700496 drv = dev->driver;
497 assert(drv);
498
Simon Glassaad29ae2020-12-03 16:55:21 -0700499 ret = device_of_to_plat(dev);
Simon Glass4a263852019-12-29 21:19:20 -0700500 if (ret)
501 goto fail;
502
Simon Glassa8d8f162019-12-29 21:19:18 -0700503 /* Ensure all parents are probed */
504 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700505 ret = device_probe(dev->parent);
506 if (ret)
507 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700508
509 /*
510 * The device might have already been probed during
511 * the call to device_probe() on its parent device
512 * (e.g. PCI bridge devices). Test the flags again
513 * so that we don't mess up the device.
514 */
Simon Glass6211d762020-12-19 10:40:10 -0700515 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Bin Meng82cdd782015-08-24 01:14:02 -0700516 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700517 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600518
Simon Glass6211d762020-12-19 10:40:10 -0700519 dev_or_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600520
Michal Simekc5190ed2022-01-07 16:38:09 +0100521 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
522 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
523 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
524 ret = dev_power_domain_on(dev);
525 if (ret)
526 goto fail;
527 }
528
Simon Glassbb53bda2015-09-12 08:45:19 -0600529 /*
530 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700531 * continue regardless of the result of pinctrl. Don't process pinctrl
532 * settings for pinctrl devices since the device may not yet be
533 * probed.
Simon Glass303c3c22021-01-21 13:57:13 -0700534 *
535 * This call can produce some non-intuitive results. For example, on an
536 * x86 device where dev is the main PCI bus, the pinctrl device may be
537 * child or grandchild of that bus, meaning that the child will be
538 * probed here. If the child happens to be the P2SB and the pinctrl
539 * device is a child of that, then both the pinctrl and P2SB will be
540 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
541 * is set just above. However, the PCI bus' probe() method and
542 * associated uclass methods have not yet been called.
Simon Glassbb53bda2015-09-12 08:45:19 -0600543 */
Patrick Delaunay99f0cd22021-11-19 10:02:27 +0100544 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL) {
545 ret = pinctrl_select_state(dev, "default");
546 if (ret && ret != -ENOSYS)
547 log_debug("Device '%s' failed to configure default pinctrl: %d (%s)\n",
548 dev->name, ret, errno_str(ret));
549 }
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900550
Mark Kettenise1311b62021-10-23 16:58:01 +0200551 if (CONFIG_IS_ENABLED(IOMMU) && dev->parent &&
552 (device_get_uclass_id(dev) != UCLASS_IOMMU)) {
553 ret = dev_iommu_enable(dev);
554 if (ret)
555 goto fail;
556 }
557
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100558 ret = device_get_dma_constraints(dev);
559 if (ret)
560 goto fail;
561
Simon Glass9c1f3822015-03-05 12:25:22 -0700562 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700563 if (ret)
564 goto fail;
565
Simon Glassd45560d2014-07-23 06:55:21 -0600566 if (dev->parent && dev->parent->driver->child_pre_probe) {
567 ret = dev->parent->driver->child_pre_probe(dev);
568 if (ret)
569 goto fail;
570 }
571
Bin Meng513cf402019-07-05 09:23:16 -0700572 /* Only handle devices that have a valid ofnode */
Simon Glassf1d50f72020-12-19 10:40:13 -0700573 if (dev_has_ofnode(dev)) {
Bin Meng513cf402019-07-05 09:23:16 -0700574 /*
575 * Process 'assigned-{clocks/clock-parents/clock-rates}'
576 * properties
577 */
Sean Anderson08d531c2021-06-11 00:16:07 -0400578 ret = clk_set_defaults(dev, CLK_DEFAULTS_PRE);
Bin Meng513cf402019-07-05 09:23:16 -0700579 if (ret)
580 goto fail;
581 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100582
Simon Glassdd6ab882014-02-26 15:59:18 -0700583 if (drv->probe) {
584 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700585 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700586 goto fail;
587 }
588
Simon Glassdd6ab882014-02-26 15:59:18 -0700589 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600590 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700591 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700592
Patrick Delaunay99f0cd22021-11-19 10:02:27 +0100593 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL) {
594 ret = pinctrl_select_state(dev, "default");
595 if (ret && ret != -ENOSYS)
596 log_debug("Device '%s' failed to configure default pinctrl: %d (%s)\n",
597 dev->name, ret, errno_str(ret));
598 }
Peng Fan7cf58dd2016-03-12 13:17:38 +0800599
Simon Glass3ee337a2022-03-04 08:43:03 -0700600 ret = device_notify(dev, EVT_DM_POST_PROBE);
601 if (ret)
Simon Glass375067f2023-08-24 13:55:35 -0600602 goto fail_event;
Simon Glass3ee337a2022-03-04 08:43:03 -0700603
Simon Glassdd6ab882014-02-26 15:59:18 -0700604 return 0;
Simon Glass375067f2023-08-24 13:55:35 -0600605fail_event:
Simon Glassdd6ab882014-02-26 15:59:18 -0700606fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100607 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700608 dm_warn("%s: Device '%s' failed to remove on error path\n",
609 __func__, dev->name);
610 }
611fail:
Simon Glass6211d762020-12-19 10:40:10 -0700612 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600613
Simon Glassdd6ab882014-02-26 15:59:18 -0700614 device_free(dev);
615
616 return ret;
617}
618
Simon Glassfa20e932020-12-03 16:55:20 -0700619void *dev_get_plat(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700620{
621 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700622 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700623 return NULL;
624 }
625
Simon Glass781aa3e2021-03-15 17:25:40 +1300626 return dm_priv_to_rw(dev->plat_);
Simon Glassdd6ab882014-02-26 15:59:18 -0700627}
628
Simon Glass71fa5b42020-12-03 16:55:18 -0700629void *dev_get_parent_plat(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700630{
631 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600632 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700633 return NULL;
634 }
635
Simon Glass781aa3e2021-03-15 17:25:40 +1300636 return dm_priv_to_rw(dev->parent_plat_);
Simon Glass11b61732015-01-25 08:27:01 -0700637}
638
Simon Glass71fa5b42020-12-03 16:55:18 -0700639void *dev_get_uclass_plat(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200640{
641 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600642 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200643 return NULL;
644 }
645
Simon Glass781aa3e2021-03-15 17:25:40 +1300646 return dm_priv_to_rw(dev->uclass_plat_);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200647}
648
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600649void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700650{
651 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700652 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700653 return NULL;
654 }
655
Simon Glass781aa3e2021-03-15 17:25:40 +1300656 return dm_priv_to_rw(dev->priv_);
Simon Glassdd6ab882014-02-26 15:59:18 -0700657}
Simon Glass48d4e292014-07-23 06:55:19 -0600658
Simon Glass7c1974b2023-01-15 14:15:42 -0700659/* notrace is needed as this is called by timer_get_rate() */
660notrace void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700661{
662 if (!dev) {
663 dm_warn("%s: null device\n", __func__);
664 return NULL;
665 }
666
Simon Glass781aa3e2021-03-15 17:25:40 +1300667 return dm_priv_to_rw(dev->uclass_priv_);
Simon Glassde0977b2015-03-05 12:25:20 -0700668}
669
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600670void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600671{
672 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700673 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600674 return NULL;
675 }
676
Simon Glass781aa3e2021-03-15 17:25:40 +1300677 return dm_priv_to_rw(dev->parent_priv_);
Simon Glass60d971b2014-07-23 06:55:20 -0600678}
679
Simon Glassd1f12cf2022-05-08 04:39:24 -0600680void *dev_get_attach_ptr(const struct udevice *dev, enum dm_tag_t tag)
681{
682 switch (tag) {
683 case DM_TAG_PLAT:
684 return dev_get_plat(dev);
685 case DM_TAG_PARENT_PLAT:
686 return dev_get_parent_plat(dev);
687 case DM_TAG_UC_PLAT:
688 return dev_get_uclass_plat(dev);
689 case DM_TAG_PRIV:
690 return dev_get_priv(dev);
691 case DM_TAG_PARENT_PRIV:
692 return dev_get_parent_priv(dev);
693 case DM_TAG_UC_PRIV:
694 return dev_get_uclass_priv(dev);
695 default:
696 return NULL;
697 }
698}
699
700int dev_get_attach_size(const struct udevice *dev, enum dm_tag_t tag)
701{
702 const struct udevice *parent = dev_get_parent(dev);
703 const struct uclass *uc = dev->uclass;
704 const struct uclass_driver *uc_drv = uc->uc_drv;
705 const struct driver *parent_drv = NULL;
706 int size = 0;
707
708 if (parent)
709 parent_drv = parent->driver;
710
711 switch (tag) {
712 case DM_TAG_PLAT:
713 size = dev->driver->plat_auto;
714 break;
715 case DM_TAG_PARENT_PLAT:
716 if (parent) {
717 size = parent_drv->per_child_plat_auto;
718 if (!size)
719 size = parent->uclass->uc_drv->per_child_plat_auto;
720 }
721 break;
722 case DM_TAG_UC_PLAT:
723 size = uc_drv->per_device_plat_auto;
724 break;
725 case DM_TAG_PRIV:
726 size = dev->driver->priv_auto;
727 break;
728 case DM_TAG_PARENT_PRIV:
729 if (parent) {
730 size = parent_drv->per_child_auto;
731 if (!size)
732 size = parent->uclass->uc_drv->per_child_auto;
733 }
734 break;
735 case DM_TAG_UC_PRIV:
736 size = uc_drv->per_device_auto;
737 break;
738 default:
739 break;
740 }
741
742 return size;
743}
744
Simon Glass48d4e292014-07-23 06:55:19 -0600745static int device_get_device_tail(struct udevice *dev, int ret,
746 struct udevice **devp)
747{
748 if (ret)
749 return ret;
750
751 ret = device_probe(dev);
752 if (ret)
753 return ret;
754
755 *devp = dev;
756
757 return 0;
758}
759
Simon Glass3580f6d2021-08-07 07:24:03 -0600760#if CONFIG_IS_ENABLED(OF_REAL)
Mario Six3bedfbe2018-06-26 08:46:50 +0200761/**
762 * device_find_by_ofnode() - Return device associated with given ofnode
763 *
764 * The returned device is *not* activated.
765 *
766 * @node: The ofnode for which a associated device should be looked up
767 * @devp: Pointer to structure to hold the found device
768 * Return: 0 if OK, -ve on error
769 */
770static int device_find_by_ofnode(ofnode node, struct udevice **devp)
771{
772 struct uclass *uc;
773 struct udevice *dev;
774 int ret;
775
Simon Glass784cd9e2020-12-19 10:40:17 -0700776 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +0200777 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
778 &dev);
779 if (!ret || dev) {
780 *devp = dev;
781 return 0;
782 }
783 }
784
785 return -ENODEV;
786}
Marek Vasut7f18c342019-08-31 18:03:28 +0200787#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200788
Simon Glass2a580882020-01-27 08:49:36 -0700789int device_get_child(const struct udevice *parent, int index,
790 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600791{
792 struct udevice *dev;
793
Sean Andersonf69a3302022-03-30 12:26:40 -0400794 device_foreach_child(dev, parent) {
Simon Glass48d4e292014-07-23 06:55:19 -0600795 if (!index--)
796 return device_get_device_tail(dev, 0, devp);
797 }
798
799 return -ENODEV;
800}
801
Simon Glass2a580882020-01-27 08:49:36 -0700802int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530803{
804 struct udevice *dev;
805 int count = 0;
806
Sean Andersonf69a3302022-03-30 12:26:40 -0400807 device_foreach_child(dev, parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530808 count++;
809
810 return count;
811}
812
Simon Glass51608c92021-12-16 20:59:32 -0700813int device_get_decendent_count(const struct udevice *parent)
814{
815 const struct udevice *dev;
816 int count = 1;
817
Sean Andersonf69a3302022-03-30 12:26:40 -0400818 device_foreach_child(dev, parent)
Simon Glass51608c92021-12-16 20:59:32 -0700819 count += device_get_decendent_count(dev);
820
821 return count;
822}
823
Simon Glass07e13382020-12-16 21:20:29 -0700824int device_find_child_by_seq(const struct udevice *parent, int seq,
825 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600826{
827 struct udevice *dev;
828
829 *devp = NULL;
Simon Glass48d4e292014-07-23 06:55:19 -0600830
Sean Andersonf69a3302022-03-30 12:26:40 -0400831 device_foreach_child(dev, parent) {
Simon Glass5e349922020-12-19 10:40:09 -0700832 if (dev->seq_ == seq) {
Simon Glass48d4e292014-07-23 06:55:19 -0600833 *devp = dev;
834 return 0;
835 }
836 }
837
838 return -ENODEV;
839}
840
Simon Glass2a580882020-01-27 08:49:36 -0700841int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600842 struct udevice **devp)
843{
844 struct udevice *dev;
845 int ret;
846
847 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700848 ret = device_find_child_by_seq(parent, seq, &dev);
849
Simon Glass48d4e292014-07-23 06:55:19 -0600850 return device_get_device_tail(dev, ret, devp);
851}
852
Simon Glass2a580882020-01-27 08:49:36 -0700853int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600854 struct udevice **devp)
855{
856 struct udevice *dev;
857
858 *devp = NULL;
859
Sean Andersonf69a3302022-03-30 12:26:40 -0400860 device_foreach_child(dev, parent) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700861 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600862 *devp = dev;
863 return 0;
864 }
865 }
866
867 return -ENODEV;
868}
869
Simon Glass2a580882020-01-27 08:49:36 -0700870int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600871 struct udevice **devp)
872{
873 struct udevice *dev;
874 int ret;
875
876 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600877 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600878 return device_get_device_tail(dev, ret, devp);
879}
Simon Glass44da7352014-10-13 23:41:49 -0600880
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200881static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
882 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600883{
884 struct udevice *dev, *found;
885
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200886 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600887 return parent;
888
Sean Andersonf69a3302022-03-30 12:26:40 -0400889 device_foreach_child(dev, parent) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200890 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600891 if (found)
892 return found;
893 }
894
895 return NULL;
896}
897
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200898int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
899{
900 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
901
902 return *devp ? 0 : -ENOENT;
903}
904
905int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600906{
907 struct udevice *dev;
908
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200909 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600910 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
911}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300912
913#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass0000e0d2021-03-15 17:25:28 +1300914int device_get_by_ofplat_idx(uint idx, struct udevice **devp)
Simon Glass5792f4b2020-10-03 11:31:40 -0600915{
Simon Glass5792f4b2020-10-03 11:31:40 -0600916 struct udevice *dev;
917
Simon Glass0000e0d2021-03-15 17:25:28 +1300918 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
919 struct udevice *base = ll_entry_start(struct udevice, udevice);
920
921 dev = base + idx;
922 } else {
923 struct driver_rt *drt = gd_dm_driver_rt() + idx;
924
925 dev = drt->dev;
926 }
Simon Glass5792f4b2020-10-03 11:31:40 -0600927 *devp = NULL;
928
929 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
930}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300931#endif
Simon Glassae2efac2015-06-23 15:38:37 -0600932
Simon Glass2a580882020-01-27 08:49:36 -0700933int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600934{
935 if (list_empty(&parent->child_head)) {
936 *devp = NULL;
937 } else {
938 *devp = list_first_entry(&parent->child_head, struct udevice,
939 sibling_node);
940 }
941
942 return 0;
943}
944
945int device_find_next_child(struct udevice **devp)
946{
947 struct udevice *dev = *devp;
948 struct udevice *parent = dev->parent;
949
950 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
951 *devp = NULL;
952 } else {
953 *devp = list_entry(dev->sibling_node.next, struct udevice,
954 sibling_node);
955 }
956
957 return 0;
958}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700959
Simon Glass2a580882020-01-27 08:49:36 -0700960int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600961 enum uclass_id uclass_id,
962 struct udevice **devp)
963{
964 struct udevice *dev;
965
966 *devp = NULL;
Sean Andersonf69a3302022-03-30 12:26:40 -0400967 device_foreach_child(dev, parent) {
Simon Glassb775e872018-10-01 12:22:07 -0600968 if (!device_active(dev) &&
969 device_get_uclass_id(dev) == uclass_id) {
970 *devp = dev;
971 return 0;
972 }
973 }
974
975 return -ENODEV;
976}
977
Simon Glass2a580882020-01-27 08:49:36 -0700978int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700979 enum uclass_id uclass_id,
980 struct udevice **devp)
981{
982 struct udevice *dev;
983
984 *devp = NULL;
Sean Andersonf69a3302022-03-30 12:26:40 -0400985 device_foreach_child(dev, parent) {
Simon Glassa11341a2018-11-18 08:14:31 -0700986 if (device_get_uclass_id(dev) == uclass_id) {
987 *devp = dev;
988 return 0;
989 }
990 }
991
992 return -ENODEV;
993}
994
Simon Glass4b23e452021-10-23 17:26:08 -0600995int device_find_child_by_namelen(const struct udevice *parent, const char *name,
996 int len, struct udevice **devp)
Simon Glassa11341a2018-11-18 08:14:31 -0700997{
998 struct udevice *dev;
999
1000 *devp = NULL;
1001
Sean Andersonf69a3302022-03-30 12:26:40 -04001002 device_foreach_child(dev, parent) {
Simon Glass4b23e452021-10-23 17:26:08 -06001003 if (!strncmp(dev->name, name, len) &&
1004 strlen(dev->name) == len) {
Simon Glassa11341a2018-11-18 08:14:31 -07001005 *devp = dev;
1006 return 0;
1007 }
1008 }
1009
1010 return -ENODEV;
1011}
1012
Simon Glass4b23e452021-10-23 17:26:08 -06001013int device_find_child_by_name(const struct udevice *parent, const char *name,
1014 struct udevice **devp)
1015{
1016 return device_find_child_by_namelen(parent, name, strlen(name), devp);
1017}
1018
Simon Glass76db9b72020-01-27 08:49:48 -07001019int device_first_child_err(struct udevice *parent, struct udevice **devp)
1020{
1021 struct udevice *dev;
1022
1023 device_find_first_child(parent, &dev);
1024 if (!dev)
1025 return -ENODEV;
1026
1027 return device_get_device_tail(dev, 0, devp);
1028}
1029
1030int device_next_child_err(struct udevice **devp)
1031{
1032 struct udevice *dev = *devp;
1033
1034 device_find_next_child(&dev);
1035 if (!dev)
1036 return -ENODEV;
1037
1038 return device_get_device_tail(dev, 0, devp);
1039}
1040
Simon Glass000676b2020-01-27 08:49:47 -07001041int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
1042{
1043 struct udevice *dev;
1044 int ret;
1045
1046 device_find_first_child(parent, &dev);
1047 if (!dev)
1048 return -ENODEV;
1049
Simon Glassaad29ae2020-12-03 16:55:21 -07001050 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -07001051 if (ret)
1052 return ret;
1053
1054 *devp = dev;
1055
1056 return 0;
1057}
1058
1059int device_next_child_ofdata_err(struct udevice **devp)
1060{
1061 struct udevice *dev = *devp;
1062 int ret;
1063
1064 device_find_next_child(&dev);
1065 if (!dev)
1066 return -ENODEV;
1067
Simon Glassaad29ae2020-12-03 16:55:21 -07001068 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -07001069 if (ret)
1070 return ret;
1071
1072 *devp = dev;
1073
1074 return 0;
1075}
1076
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001077struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -07001078{
1079 return child->parent;
1080}
1081
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001082ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -07001083{
Simon Glass46227bd2015-03-25 12:21:55 -06001084 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -07001085}
Simon Glass98fd5d12015-01-25 08:27:04 -07001086
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001087const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +02001088{
1089 if (!dev || !dev->driver->ops)
1090 return NULL;
1091
1092 return dev->driver->ops;
1093}
1094
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001095enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -07001096{
1097 return dev->uclass->uc_drv->id;
1098}
Peng Fan99b72352015-02-10 14:46:32 +08001099
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001100const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +02001101{
1102 if (!dev)
1103 return NULL;
1104
1105 return dev->uclass->uc_drv->name;
1106}
1107
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001108bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001109{
1110 return !list_empty(&dev->child_head);
1111}
1112
Simon Glass2a580882020-01-27 08:49:36 -07001113bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001114{
1115 struct udevice *child;
1116
1117 for (device_find_first_child(dev, &child);
1118 child;
1119 device_find_next_child(&child)) {
1120 if (device_active(child))
1121 return true;
1122 }
1123
1124 return false;
1125}
1126
Simon Glass2a580882020-01-27 08:49:36 -07001127bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001128{
1129 struct udevice *parent = dev->parent;
1130
1131 if (!parent)
1132 return false;
1133 return list_is_last(&dev->sibling_node, &parent->child_head);
1134}
Simon Glasse3b23e22015-07-30 13:40:39 -06001135
Simon Glass7760ba22016-05-01 13:52:23 -06001136void device_set_name_alloced(struct udevice *dev)
1137{
Simon Glass6211d762020-12-19 10:40:10 -07001138 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
Simon Glass7760ba22016-05-01 13:52:23 -06001139}
1140
Simon Glasse3b23e22015-07-30 13:40:39 -06001141int device_set_name(struct udevice *dev, const char *name)
1142{
1143 name = strdup(name);
1144 if (!name)
1145 return -ENOMEM;
1146 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -06001147 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -06001148
1149 return 0;
1150}
Mugunthan V N4666bd92016-04-28 15:36:02 +05301151
Simon Glassd4f0c942020-12-22 19:30:27 -07001152void dev_set_priv(struct udevice *dev, void *priv)
1153{
Simon Glass541f62c2020-12-22 19:30:30 -07001154 dev->priv_ = priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001155}
1156
1157void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
1158{
Simon Glass541f62c2020-12-22 19:30:30 -07001159 dev->parent_priv_ = parent_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001160}
1161
1162void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
1163{
Simon Glass541f62c2020-12-22 19:30:30 -07001164 dev->uclass_priv_ = uclass_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001165}
1166
1167void dev_set_plat(struct udevice *dev, void *plat)
1168{
Simon Glass541f62c2020-12-22 19:30:30 -07001169 dev->plat_ = plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001170}
1171
1172void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1173{
Simon Glass541f62c2020-12-22 19:30:30 -07001174 dev->parent_plat_ = parent_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001175}
1176
1177void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1178{
Simon Glass541f62c2020-12-22 19:30:30 -07001179 dev->uclass_plat_ = uclass_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001180}
1181
Simon Glass3580f6d2021-08-07 07:24:03 -06001182#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass2a580882020-01-27 08:49:36 -07001183bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +05301184{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001185 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +05301186}
1187
1188bool of_machine_is_compatible(const char *compat)
1189{
Patrick Delaunayc5574ea2022-05-17 14:37:05 +02001190 return ofnode_device_is_compatible(ofnode_root(), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +05301191}
Mario Six3bedfbe2018-06-26 08:46:50 +02001192
1193int dev_disable_by_path(const char *path)
1194{
1195 struct uclass *uc;
1196 ofnode node = ofnode_path(path);
1197 struct udevice *dev;
1198 int ret = 1;
1199
1200 if (!of_live_active())
1201 return -ENOSYS;
1202
Simon Glass784cd9e2020-12-19 10:40:17 -07001203 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +02001204 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1205 if (!ret)
1206 break;
1207 }
1208
1209 if (ret)
1210 return ret;
1211
1212 ret = device_remove(dev, DM_REMOVE_NORMAL);
1213 if (ret)
1214 return ret;
1215
1216 ret = device_unbind(dev);
1217 if (ret)
1218 return ret;
1219
1220 return ofnode_set_enabled(node, false);
1221}
1222
1223int dev_enable_by_path(const char *path)
1224{
1225 ofnode node = ofnode_path(path);
1226 ofnode pnode = ofnode_get_parent(node);
1227 struct udevice *parent;
1228 int ret = 1;
1229
1230 if (!of_live_active())
1231 return -ENOSYS;
1232
1233 ret = device_find_by_ofnode(pnode, &parent);
1234 if (ret)
1235 return ret;
1236
1237 ret = ofnode_set_enabled(node, true);
1238 if (ret)
1239 return ret;
1240
Patrice Chotardcc523cf2021-09-10 16:16:20 +02001241 return lists_bind_fdt(parent, node, NULL, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001242}
Marek Vasut7f18c342019-08-31 18:03:28 +02001243#endif
Simon Glassb5660c42021-03-15 17:25:37 +13001244
1245#if CONFIG_IS_ENABLED(OF_PLATDATA_RT)
1246static struct udevice_rt *dev_get_rt(const struct udevice *dev)
1247{
1248 struct udevice *base = ll_entry_start(struct udevice, udevice);
Simon Glass308d91b2022-03-27 14:26:20 -06001249 uint each_size = dm_udevice_size();
1250 int idx = ((void *)dev - (void *)base) / each_size;
Simon Glassb5660c42021-03-15 17:25:37 +13001251
1252 struct udevice_rt *urt = gd_dm_udevice_rt() + idx;
1253
1254 return urt;
1255}
1256
1257u32 dev_get_flags(const struct udevice *dev)
1258{
1259 const struct udevice_rt *urt = dev_get_rt(dev);
1260
1261 return urt->flags_;
1262}
1263
1264void dev_or_flags(const struct udevice *dev, u32 or)
1265{
1266 struct udevice_rt *urt = dev_get_rt(dev);
1267
1268 urt->flags_ |= or;
1269}
1270
1271void dev_bic_flags(const struct udevice *dev, u32 bic)
1272{
1273 struct udevice_rt *urt = dev_get_rt(dev);
1274
1275 urt->flags_ &= ~bic;
1276}
1277#endif /* OF_PLATDATA_RT */