blob: 60f8d6700ad417dd03c6d703a826c00c834f5f5d [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
11#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 Glassdd6ab882014-02-26 15:59:18 -070036
Simon Glassdb6f0202014-07-23 06:55:12 -060037DECLARE_GLOBAL_DATA_PTR;
38
Stephen Warren8c93df12016-05-11 15:26:24 -060039static int device_bind_common(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -070040 const char *name, void *plat,
Simon Glassb0cf4b32017-05-17 17:18:11 -060041 ulong driver_data, ofnode node,
Simon Glass39edb952020-12-03 16:55:19 -070042 uint of_plat_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070043{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020044 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070045 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020046 int size, ret = 0;
Simon Glass7272e922020-12-16 21:20:09 -070047 bool auto_seq = true;
Simon Glass2543b162020-12-22 19:30:29 -070048 void *ptr;
Simon Glassdd6ab882014-02-26 15:59:18 -070049
Simon Glass47fd4a22021-03-15 17:25:15 +130050 if (CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND))
51 return -ENOSYS;
52
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090053 if (devp)
54 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070055 if (!name)
56 return -EINVAL;
57
58 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060059 if (ret) {
60 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070061 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060062 }
Simon Glassdd6ab882014-02-26 15:59:18 -070063
Heiko Schocherb74fcb42014-05-22 12:43:05 +020064 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070065 if (!dev)
66 return -ENOMEM;
67
68 INIT_LIST_HEAD(&dev->sibling_node);
69 INIT_LIST_HEAD(&dev->child_head);
70 INIT_LIST_HEAD(&dev->uclass_node);
Simon Glass3996f062022-03-27 14:26:19 -060071#if CONFIG_IS_ENABLED(DEVRES)
Masahiro Yamada8b15b162015-07-25 21:52:35 +090072 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090073#endif
Simon Glass2543b162020-12-22 19:30:29 -070074 dev_set_plat(dev, plat);
Stephen Warren8c93df12016-05-11 15:26:24 -060075 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070076 dev->name = name;
Simon Glassa7ece582020-12-19 10:40:14 -070077 dev_set_ofnode(dev, node);
Simon Glassdd6ab882014-02-26 15:59:18 -070078 dev->parent = parent;
79 dev->driver = drv;
80 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060081
Simon Glass5e349922020-12-19 10:40:09 -070082 dev->seq_ = -1;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010083 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
84 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070085 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010086 * Some devices, such as a SPI bus, I2C bus and serial ports
87 * are numbered using aliases.
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010088 */
Simon Glasse422f8c2020-07-09 18:39:24 -060089 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
90 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass7272e922020-12-16 21:20:09 -070091 if (uc->uc_drv->name && ofnode_valid(node)) {
Simon Glass28823e32021-07-05 16:32:39 -060092 if (!dev_read_alias_seq(dev, &dev->seq_)) {
Simon Glass38040a92020-12-16 21:20:32 -070093 auto_seq = false;
Simon Glass28823e32021-07-05 16:32:39 -060094 log_debug(" - seq=%d\n", dev->seq_);
95 }
Simon Glass7272e922020-12-16 21:20:09 -070096 }
Simon Glass0ccb0972015-01-25 08:27:05 -070097 }
Simon Glassdb6f0202014-07-23 06:55:12 -060098 }
Simon Glass3a9d5f12020-12-16 21:20:17 -070099 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
Simon Glass5e349922020-12-19 10:40:09 -0700100 dev->seq_ = uclass_find_next_free_seq(uc);
Simon Glass1bd3deb2015-02-27 22:06:30 -0700101
Simon Glasse07434b2021-02-03 21:29:43 -0700102 /* Check if we need to allocate plat */
Simon Glass71fa5b42020-12-03 16:55:18 -0700103 if (drv->plat_auto) {
104 bool alloc = !plat;
Simon Glassafbf9b82016-07-04 11:58:18 -0600105
Simon Glasse07434b2021-02-03 21:29:43 -0700106 /*
107 * For of-platdata, we try use the existing data, but if
108 * plat_auto is larger, we must allocate a new space
109 */
Simon Glassafbf9b82016-07-04 11:58:18 -0600110 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glasse07434b2021-02-03 21:29:43 -0700111 if (of_plat_size)
Simon Glass6211d762020-12-19 10:40:10 -0700112 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
Simon Glasse07434b2021-02-03 21:29:43 -0700113 if (of_plat_size < drv->plat_auto)
114 alloc = true;
Simon Glassafbf9b82016-07-04 11:58:18 -0600115 }
116 if (alloc) {
Simon Glass6211d762020-12-19 10:40:10 -0700117 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700118 ptr = calloc(1, drv->plat_auto);
119 if (!ptr) {
Simon Glassafbf9b82016-07-04 11:58:18 -0600120 ret = -ENOMEM;
121 goto fail_alloc1;
122 }
Simon Glasse07434b2021-02-03 21:29:43 -0700123
124 /*
125 * For of-platdata, copy the old plat into the new
126 * space
127 */
Simon Glass2543b162020-12-22 19:30:29 -0700128 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
129 memcpy(ptr, plat, of_plat_size);
130 dev_set_plat(dev, ptr);
Simon Glassba750492015-01-25 08:27:00 -0700131 }
132 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200133
Simon Glass71fa5b42020-12-03 16:55:18 -0700134 size = uc->uc_drv->per_device_plat_auto;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200135 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700136 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700137 ptr = calloc(1, size);
138 if (!ptr) {
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200139 ret = -ENOMEM;
140 goto fail_alloc2;
141 }
Simon Glass2543b162020-12-22 19:30:29 -0700142 dev_set_uclass_plat(dev, ptr);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200143 }
Simon Glass11b61732015-01-25 08:27:01 -0700144
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200145 if (parent) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700146 size = parent->driver->per_child_plat_auto;
Simon Glassd6206a52021-02-03 21:29:44 -0700147 if (!size)
Simon Glass71fa5b42020-12-03 16:55:18 -0700148 size = parent->uclass->uc_drv->per_child_plat_auto;
Simon Glass11b61732015-01-25 08:27:01 -0700149 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700150 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700151 ptr = calloc(1, size);
152 if (!ptr) {
Simon Glass11b61732015-01-25 08:27:01 -0700153 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200154 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700155 }
Simon Glass2543b162020-12-22 19:30:29 -0700156 dev_set_parent_plat(dev, ptr);
Simon Glass11b61732015-01-25 08:27:01 -0700157 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100158 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700159 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100160 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700161
162 ret = uclass_bind_device(dev);
163 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700164 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700165
166 /* if we fail to bind we remove device from successors and free it */
167 if (drv->bind) {
168 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700169 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700170 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700171 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700172 if (parent && parent->driver->child_post_bind) {
173 ret = parent->driver->child_post_bind(dev);
174 if (ret)
175 goto fail_child_post_bind;
176 }
Simon Glass286863d2016-01-05 09:30:59 -0700177 if (uc->uc_drv->post_bind) {
178 ret = uc->uc_drv->post_bind(dev);
179 if (ret)
180 goto fail_uclass_post_bind;
181 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700182
Simon Glassdd6ab882014-02-26 15:59:18 -0700183 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900184 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900185 if (devp)
186 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700187
Simon Glass6211d762020-12-19 10:40:10 -0700188 dev_or_flags(dev, DM_FLAG_BOUND);
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900189
Simon Glassdd6ab882014-02-26 15:59:18 -0700190 return 0;
191
Simon Glass286863d2016-01-05 09:30:59 -0700192fail_uclass_post_bind:
193 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700194fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900195 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700196 if (drv->unbind && drv->unbind(dev)) {
197 dm_warn("unbind() method failed on dev '%s' on error path\n",
198 dev->name);
199 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700200 }
201
Simon Glassdd6ab882014-02-26 15:59:18 -0700202fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900203 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700204 if (uclass_unbind_device(dev)) {
205 dm_warn("Failed to unbind dev '%s' on error path\n",
206 dev->name);
207 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700208 }
209fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900210 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700211 list_del(&dev->sibling_node);
Simon Glass6211d762020-12-19 10:40:10 -0700212 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass2543b162020-12-22 19:30:29 -0700213 free(dev_get_parent_plat(dev));
214 dev_set_parent_plat(dev, NULL);
Simon Glasse70a4f42015-02-27 22:06:33 -0700215 }
Simon Glass11b61732015-01-25 08:27:01 -0700216 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200217fail_alloc3:
Simon Glassd6206a52021-02-03 21:29:44 -0700218 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
219 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
220 free(dev_get_uclass_plat(dev));
221 dev_set_uclass_plat(dev, NULL);
222 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200223 }
Simon Glass11b61732015-01-25 08:27:01 -0700224fail_alloc2:
Simon Glassd6206a52021-02-03 21:29:44 -0700225 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
226 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
227 free(dev_get_plat(dev));
228 dev_set_plat(dev, NULL);
229 }
Simon Glassba750492015-01-25 08:27:00 -0700230 }
231fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900232 devres_release_all(dev);
233
Simon Glassdd6ab882014-02-26 15:59:18 -0700234 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700235
Simon Glassdd6ab882014-02-26 15:59:18 -0700236 return ret;
237}
238
Stephen Warren8c93df12016-05-11 15:26:24 -0600239int device_bind_with_driver_data(struct udevice *parent,
240 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600241 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600242 struct udevice **devp)
243{
Simon Glass322b2902017-05-18 20:09:05 -0600244 return device_bind_common(parent, drv, name, NULL, driver_data, node,
245 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600246}
247
Simon Glass884870f2020-11-28 17:50:01 -0700248int device_bind(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -0700249 const char *name, void *plat, ofnode node,
Simon Glass884870f2020-11-28 17:50:01 -0700250 struct udevice **devp)
Simon Glass9eb151e2018-06-11 13:07:15 -0600251{
Simon Glass71fa5b42020-12-03 16:55:18 -0700252 return device_bind_common(parent, drv, name, plat, 0, node, 0,
Simon Glass9eb151e2018-06-11 13:07:15 -0600253 devp);
254}
255
Simon Glassfef72b72014-07-23 06:55:03 -0600256int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Simon Glasscfd6a002020-10-03 11:31:33 -0600257 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700258{
259 struct driver *drv;
Simon Glass39edb952020-12-03 16:55:19 -0700260 uint plat_size = 0;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300261 int ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700262
263 drv = lists_driver_lookup_name(info->name);
264 if (!drv)
265 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600266 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
267 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700268
Simon Glassafbf9b82016-07-04 11:58:18 -0600269#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass39edb952020-12-03 16:55:19 -0700270 plat_size = info->plat_size;
Simon Glassafbf9b82016-07-04 11:58:18 -0600271#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700272 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
Simon Glass39edb952020-12-03 16:55:19 -0700273 ofnode_null(), plat_size, devp);
Walter Lozanoffc41b02020-06-25 01:10:11 -0300274 if (ret)
275 return ret;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300276
277 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700278}
279
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300280int device_reparent(struct udevice *dev, struct udevice *new_parent)
281{
282 struct udevice *pos, *n;
283
284 assert(dev);
285 assert(new_parent);
286
Sean Andersonf69a3302022-03-30 12:26:40 -0400287 device_foreach_child_safe(pos, n, dev->parent) {
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300288 if (pos->driver != dev->driver)
289 continue;
290
291 list_del(&dev->sibling_node);
292 list_add_tail(&dev->sibling_node, &new_parent->child_head);
293 dev->parent = new_parent;
294
295 break;
296 }
297
298 return 0;
299}
300
Simon Glass825d3f92015-03-25 12:21:53 -0600301static void *alloc_priv(int size, uint flags)
302{
303 void *priv;
304
305 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530306 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600307 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600308 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600309 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600310
311 /*
312 * Ensure that the zero bytes are flushed to memory.
313 * This prevents problems if the driver uses this as
314 * both an input and an output buffer:
315 *
316 * 1. Zeroes written to buffer (here) and sit in the
317 * cache
318 * 2. Driver issues a read command to DMA
319 * 3. CPU runs out of cache space and evicts some cache
320 * data in the buffer, writing zeroes to RAM from
321 * the memset() above
322 * 4. DMA completes
323 * 5. Buffer now has some DMA data and some zeroes
324 * 6. Data being read is now incorrect
325 *
326 * To prevent this, ensure that the cache is clean
327 * within this range at the start. The driver can then
328 * use normal flush-after-write, invalidate-before-read
329 * procedures.
Simon Glass27bc9b12017-04-04 13:00:19 -0600330 */
Simon Glass27bc9b12017-04-04 13:00:19 -0600331 flush_dcache_range((ulong)priv, (ulong)priv + size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600332 }
Simon Glass825d3f92015-03-25 12:21:53 -0600333 } else {
334 priv = calloc(1, size);
335 }
336
337 return priv;
338}
339
Simon Glassad337e62020-12-19 10:40:08 -0700340/**
341 * device_alloc_priv() - Allocate priv/plat data required by the device
342 *
343 * @dev: Device to process
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100344 * Return: 0 if OK, -ENOMEM if out of memory
Simon Glassad337e62020-12-19 10:40:08 -0700345 */
346static int device_alloc_priv(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700347{
Simon Glassa626dff2015-03-25 12:21:54 -0600348 const struct driver *drv;
Simon Glass2543b162020-12-22 19:30:29 -0700349 void *ptr;
Simon Glassad337e62020-12-19 10:40:08 -0700350 int size;
Simon Glass44c47892020-04-05 15:38:19 -0600351
Simon Glassdd6ab882014-02-26 15:59:18 -0700352 drv = dev->driver;
353 assert(drv);
354
Bin Meng82cdd782015-08-24 01:14:02 -0700355 /* Allocate private data if requested and not reentered */
Simon Glass2543b162020-12-22 19:30:29 -0700356 if (drv->priv_auto && !dev_get_priv(dev)) {
357 ptr = alloc_priv(drv->priv_auto, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700358 if (!ptr)
359 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700360 dev_set_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700361 }
Simon Glassad337e62020-12-19 10:40:08 -0700362
Bin Meng82cdd782015-08-24 01:14:02 -0700363 /* Allocate private data if requested and not reentered */
Simon Glass8a2b47f2020-12-03 16:55:17 -0700364 size = dev->uclass->uc_drv->per_device_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700365 if (size && !dev_get_uclass_priv(dev)) {
366 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700367 if (!ptr)
368 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700369 dev_set_uclass_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700370 }
371
Simon Glassa8d8f162019-12-29 21:19:18 -0700372 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700373 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700374 size = dev->parent->driver->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700375 if (!size)
Simon Glass8a2b47f2020-12-03 16:55:17 -0700376 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700377 if (size && !dev_get_parent_priv(dev)) {
378 ptr = alloc_priv(size, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700379 if (!ptr)
380 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700381 dev_set_parent_priv(dev, ptr);
Simon Glass60d971b2014-07-23 06:55:20 -0600382 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700383 }
384
Simon Glassad337e62020-12-19 10:40:08 -0700385 return 0;
386}
387
388int device_of_to_plat(struct udevice *dev)
389{
390 const struct driver *drv;
391 int ret;
392
393 if (!dev)
394 return -EINVAL;
395
Simon Glass6211d762020-12-19 10:40:10 -0700396 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
Simon Glassad337e62020-12-19 10:40:08 -0700397 return 0;
398
Simon Glass47fd4a22021-03-15 17:25:15 +1300399 /*
400 * This is not needed if binding is disabled, since data is allocated
401 * at build time.
402 */
403 if (!CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND)) {
404 /* Ensure all parents have ofdata */
405 if (dev->parent) {
406 ret = device_of_to_plat(dev->parent);
407 if (ret)
408 goto fail;
409
410 /*
411 * The device might have already been probed during
412 * the call to device_probe() on its parent device
413 * (e.g. PCI bridge devices). Test the flags again
414 * so that we don't mess up the device.
415 */
416 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
417 return 0;
418 }
419
420 ret = device_alloc_priv(dev);
Simon Glassad337e62020-12-19 10:40:08 -0700421 if (ret)
422 goto fail;
Simon Glassad337e62020-12-19 10:40:08 -0700423 }
Simon Glassad337e62020-12-19 10:40:08 -0700424 drv = dev->driver;
425 assert(drv);
426
Simon Glassaad29ae2020-12-03 16:55:21 -0700427 if (drv->of_to_plat &&
Simon Glass07c17772020-12-19 10:40:12 -0700428 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
Simon Glassaad29ae2020-12-03 16:55:21 -0700429 ret = drv->of_to_plat(dev);
Simon Glassa8d8f162019-12-29 21:19:18 -0700430 if (ret)
431 goto fail;
432 }
Simon Glass60d971b2014-07-23 06:55:20 -0600433
Simon Glass6211d762020-12-19 10:40:10 -0700434 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
Simon Glassd3c9d302019-12-29 21:19:21 -0700435
Simon Glass4a263852019-12-29 21:19:20 -0700436 return 0;
437fail:
438 device_free(dev);
439
440 return ret;
441}
442
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100443/**
444 * device_get_dma_constraints() - Populate device's DMA constraints
445 *
446 * Gets a device's DMA constraints from firmware. This information is later
447 * used by drivers to translate physcal addresses to the device's bus address
448 * space. For now only device-tree is supported.
449 *
450 * @dev: Pointer to target device
451 * Return: 0 if OK or if no DMA constraints were found, error otherwise
452 */
453static int device_get_dma_constraints(struct udevice *dev)
454{
455 struct udevice *parent = dev->parent;
456 phys_addr_t cpu = 0;
457 dma_addr_t bus = 0;
458 u64 size = 0;
459 int ret;
460
461 if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
462 return 0;
463
464 /*
465 * We start parsing for dma-ranges from the device's bus node. This is
466 * specially important on nested buses.
467 */
468 ret = dev_get_dma_range(parent, &cpu, &bus, &size);
469 /* Don't return an error if no 'dma-ranges' were found */
470 if (ret && ret != -ENOENT) {
471 dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret);
472 return ret;
473 }
474
475 dev_set_dma_offset(dev, cpu - bus);
476
477 return 0;
478}
479
Simon Glass4a263852019-12-29 21:19:20 -0700480int device_probe(struct udevice *dev)
481{
482 const struct driver *drv;
483 int ret;
Simon Glass4a263852019-12-29 21:19:20 -0700484
485 if (!dev)
486 return -EINVAL;
487
Simon Glass6211d762020-12-19 10:40:10 -0700488 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass4a263852019-12-29 21:19:20 -0700489 return 0;
490
Simon Glass3ee337a2022-03-04 08:43:03 -0700491 ret = device_notify(dev, EVT_DM_PRE_PROBE);
492 if (ret)
493 return ret;
494
Simon Glass4a263852019-12-29 21:19:20 -0700495 drv = dev->driver;
496 assert(drv);
497
Simon Glassaad29ae2020-12-03 16:55:21 -0700498 ret = device_of_to_plat(dev);
Simon Glass4a263852019-12-29 21:19:20 -0700499 if (ret)
500 goto fail;
501
Simon Glassa8d8f162019-12-29 21:19:18 -0700502 /* Ensure all parents are probed */
503 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700504 ret = device_probe(dev->parent);
505 if (ret)
506 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700507
508 /*
509 * The device might have already been probed during
510 * the call to device_probe() on its parent device
511 * (e.g. PCI bridge devices). Test the flags again
512 * so that we don't mess up the device.
513 */
Simon Glass6211d762020-12-19 10:40:10 -0700514 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Bin Meng82cdd782015-08-24 01:14:02 -0700515 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700516 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600517
Simon Glass6211d762020-12-19 10:40:10 -0700518 dev_or_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600519
Michal Simekc5190ed2022-01-07 16:38:09 +0100520 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
521 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
522 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
523 ret = dev_power_domain_on(dev);
524 if (ret)
525 goto fail;
526 }
527
Simon Glassbb53bda2015-09-12 08:45:19 -0600528 /*
529 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700530 * continue regardless of the result of pinctrl. Don't process pinctrl
531 * settings for pinctrl devices since the device may not yet be
532 * probed.
Simon Glass303c3c22021-01-21 13:57:13 -0700533 *
534 * This call can produce some non-intuitive results. For example, on an
535 * x86 device where dev is the main PCI bus, the pinctrl device may be
536 * child or grandchild of that bus, meaning that the child will be
537 * probed here. If the child happens to be the P2SB and the pinctrl
538 * device is a child of that, then both the pinctrl and P2SB will be
539 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
540 * is set just above. However, the PCI bus' probe() method and
541 * associated uclass methods have not yet been called.
Simon Glassbb53bda2015-09-12 08:45:19 -0600542 */
Patrick Delaunay99f0cd22021-11-19 10:02:27 +0100543 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL) {
544 ret = pinctrl_select_state(dev, "default");
545 if (ret && ret != -ENOSYS)
546 log_debug("Device '%s' failed to configure default pinctrl: %d (%s)\n",
547 dev->name, ret, errno_str(ret));
548 }
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900549
Mark Kettenise1311b62021-10-23 16:58:01 +0200550 if (CONFIG_IS_ENABLED(IOMMU) && dev->parent &&
551 (device_get_uclass_id(dev) != UCLASS_IOMMU)) {
552 ret = dev_iommu_enable(dev);
553 if (ret)
554 goto fail;
555 }
556
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100557 ret = device_get_dma_constraints(dev);
558 if (ret)
559 goto fail;
560
Simon Glass9c1f3822015-03-05 12:25:22 -0700561 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700562 if (ret)
563 goto fail;
564
Simon Glassd45560d2014-07-23 06:55:21 -0600565 if (dev->parent && dev->parent->driver->child_pre_probe) {
566 ret = dev->parent->driver->child_pre_probe(dev);
567 if (ret)
568 goto fail;
569 }
570
Bin Meng513cf402019-07-05 09:23:16 -0700571 /* Only handle devices that have a valid ofnode */
Simon Glassf1d50f72020-12-19 10:40:13 -0700572 if (dev_has_ofnode(dev)) {
Bin Meng513cf402019-07-05 09:23:16 -0700573 /*
574 * Process 'assigned-{clocks/clock-parents/clock-rates}'
575 * properties
576 */
Sean Anderson08d531c2021-06-11 00:16:07 -0400577 ret = clk_set_defaults(dev, CLK_DEFAULTS_PRE);
Bin Meng513cf402019-07-05 09:23:16 -0700578 if (ret)
579 goto fail;
580 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100581
Simon Glassdd6ab882014-02-26 15:59:18 -0700582 if (drv->probe) {
583 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700584 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700585 goto fail;
586 }
587
Simon Glassdd6ab882014-02-26 15:59:18 -0700588 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600589 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700590 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700591
Patrick Delaunay99f0cd22021-11-19 10:02:27 +0100592 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL) {
593 ret = pinctrl_select_state(dev, "default");
594 if (ret && ret != -ENOSYS)
595 log_debug("Device '%s' failed to configure default pinctrl: %d (%s)\n",
596 dev->name, ret, errno_str(ret));
597 }
Peng Fan7cf58dd2016-03-12 13:17:38 +0800598
Simon Glass3ee337a2022-03-04 08:43:03 -0700599 ret = device_notify(dev, EVT_DM_POST_PROBE);
600 if (ret)
Simon Glass375067f2023-08-24 13:55:35 -0600601 goto fail_event;
Simon Glass3ee337a2022-03-04 08:43:03 -0700602
Simon Glassdd6ab882014-02-26 15:59:18 -0700603 return 0;
Simon Glass375067f2023-08-24 13:55:35 -0600604fail_event:
Simon Glassdd6ab882014-02-26 15:59:18 -0700605fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100606 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700607 dm_warn("%s: Device '%s' failed to remove on error path\n",
608 __func__, dev->name);
609 }
610fail:
Simon Glass6211d762020-12-19 10:40:10 -0700611 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600612
Simon Glassdd6ab882014-02-26 15:59:18 -0700613 device_free(dev);
614
615 return ret;
616}
617
Simon Glassfa20e932020-12-03 16:55:20 -0700618void *dev_get_plat(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700619{
620 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700621 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700622 return NULL;
623 }
624
Simon Glass781aa3e2021-03-15 17:25:40 +1300625 return dm_priv_to_rw(dev->plat_);
Simon Glassdd6ab882014-02-26 15:59:18 -0700626}
627
Simon Glass71fa5b42020-12-03 16:55:18 -0700628void *dev_get_parent_plat(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700629{
630 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600631 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700632 return NULL;
633 }
634
Simon Glass781aa3e2021-03-15 17:25:40 +1300635 return dm_priv_to_rw(dev->parent_plat_);
Simon Glass11b61732015-01-25 08:27:01 -0700636}
637
Simon Glass71fa5b42020-12-03 16:55:18 -0700638void *dev_get_uclass_plat(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200639{
640 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600641 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200642 return NULL;
643 }
644
Simon Glass781aa3e2021-03-15 17:25:40 +1300645 return dm_priv_to_rw(dev->uclass_plat_);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200646}
647
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600648void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700649{
650 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700651 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700652 return NULL;
653 }
654
Simon Glass781aa3e2021-03-15 17:25:40 +1300655 return dm_priv_to_rw(dev->priv_);
Simon Glassdd6ab882014-02-26 15:59:18 -0700656}
Simon Glass48d4e292014-07-23 06:55:19 -0600657
Simon Glass7c1974b2023-01-15 14:15:42 -0700658/* notrace is needed as this is called by timer_get_rate() */
659notrace void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700660{
661 if (!dev) {
662 dm_warn("%s: null device\n", __func__);
663 return NULL;
664 }
665
Simon Glass781aa3e2021-03-15 17:25:40 +1300666 return dm_priv_to_rw(dev->uclass_priv_);
Simon Glassde0977b2015-03-05 12:25:20 -0700667}
668
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600669void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600670{
671 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700672 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600673 return NULL;
674 }
675
Simon Glass781aa3e2021-03-15 17:25:40 +1300676 return dm_priv_to_rw(dev->parent_priv_);
Simon Glass60d971b2014-07-23 06:55:20 -0600677}
678
Simon Glassd1f12cf2022-05-08 04:39:24 -0600679void *dev_get_attach_ptr(const struct udevice *dev, enum dm_tag_t tag)
680{
681 switch (tag) {
682 case DM_TAG_PLAT:
683 return dev_get_plat(dev);
684 case DM_TAG_PARENT_PLAT:
685 return dev_get_parent_plat(dev);
686 case DM_TAG_UC_PLAT:
687 return dev_get_uclass_plat(dev);
688 case DM_TAG_PRIV:
689 return dev_get_priv(dev);
690 case DM_TAG_PARENT_PRIV:
691 return dev_get_parent_priv(dev);
692 case DM_TAG_UC_PRIV:
693 return dev_get_uclass_priv(dev);
694 default:
695 return NULL;
696 }
697}
698
699int dev_get_attach_size(const struct udevice *dev, enum dm_tag_t tag)
700{
701 const struct udevice *parent = dev_get_parent(dev);
702 const struct uclass *uc = dev->uclass;
703 const struct uclass_driver *uc_drv = uc->uc_drv;
704 const struct driver *parent_drv = NULL;
705 int size = 0;
706
707 if (parent)
708 parent_drv = parent->driver;
709
710 switch (tag) {
711 case DM_TAG_PLAT:
712 size = dev->driver->plat_auto;
713 break;
714 case DM_TAG_PARENT_PLAT:
715 if (parent) {
716 size = parent_drv->per_child_plat_auto;
717 if (!size)
718 size = parent->uclass->uc_drv->per_child_plat_auto;
719 }
720 break;
721 case DM_TAG_UC_PLAT:
722 size = uc_drv->per_device_plat_auto;
723 break;
724 case DM_TAG_PRIV:
725 size = dev->driver->priv_auto;
726 break;
727 case DM_TAG_PARENT_PRIV:
728 if (parent) {
729 size = parent_drv->per_child_auto;
730 if (!size)
731 size = parent->uclass->uc_drv->per_child_auto;
732 }
733 break;
734 case DM_TAG_UC_PRIV:
735 size = uc_drv->per_device_auto;
736 break;
737 default:
738 break;
739 }
740
741 return size;
742}
743
Simon Glass48d4e292014-07-23 06:55:19 -0600744static int device_get_device_tail(struct udevice *dev, int ret,
745 struct udevice **devp)
746{
747 if (ret)
748 return ret;
749
750 ret = device_probe(dev);
751 if (ret)
752 return ret;
753
754 *devp = dev;
755
756 return 0;
757}
758
Simon Glass3580f6d2021-08-07 07:24:03 -0600759#if CONFIG_IS_ENABLED(OF_REAL)
Mario Six3bedfbe2018-06-26 08:46:50 +0200760/**
761 * device_find_by_ofnode() - Return device associated with given ofnode
762 *
763 * The returned device is *not* activated.
764 *
765 * @node: The ofnode for which a associated device should be looked up
766 * @devp: Pointer to structure to hold the found device
767 * Return: 0 if OK, -ve on error
768 */
769static int device_find_by_ofnode(ofnode node, struct udevice **devp)
770{
771 struct uclass *uc;
772 struct udevice *dev;
773 int ret;
774
Simon Glass784cd9e2020-12-19 10:40:17 -0700775 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +0200776 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
777 &dev);
778 if (!ret || dev) {
779 *devp = dev;
780 return 0;
781 }
782 }
783
784 return -ENODEV;
785}
Marek Vasut7f18c342019-08-31 18:03:28 +0200786#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200787
Simon Glass2a580882020-01-27 08:49:36 -0700788int device_get_child(const struct udevice *parent, int index,
789 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600790{
791 struct udevice *dev;
792
Sean Andersonf69a3302022-03-30 12:26:40 -0400793 device_foreach_child(dev, parent) {
Simon Glass48d4e292014-07-23 06:55:19 -0600794 if (!index--)
795 return device_get_device_tail(dev, 0, devp);
796 }
797
798 return -ENODEV;
799}
800
Simon Glass2a580882020-01-27 08:49:36 -0700801int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530802{
803 struct udevice *dev;
804 int count = 0;
805
Sean Andersonf69a3302022-03-30 12:26:40 -0400806 device_foreach_child(dev, parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530807 count++;
808
809 return count;
810}
811
Simon Glass51608c92021-12-16 20:59:32 -0700812int device_get_decendent_count(const struct udevice *parent)
813{
814 const struct udevice *dev;
815 int count = 1;
816
Sean Andersonf69a3302022-03-30 12:26:40 -0400817 device_foreach_child(dev, parent)
Simon Glass51608c92021-12-16 20:59:32 -0700818 count += device_get_decendent_count(dev);
819
820 return count;
821}
822
Simon Glass07e13382020-12-16 21:20:29 -0700823int device_find_child_by_seq(const struct udevice *parent, int seq,
824 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600825{
826 struct udevice *dev;
827
828 *devp = NULL;
Simon Glass48d4e292014-07-23 06:55:19 -0600829
Sean Andersonf69a3302022-03-30 12:26:40 -0400830 device_foreach_child(dev, parent) {
Simon Glass5e349922020-12-19 10:40:09 -0700831 if (dev->seq_ == seq) {
Simon Glass48d4e292014-07-23 06:55:19 -0600832 *devp = dev;
833 return 0;
834 }
835 }
836
837 return -ENODEV;
838}
839
Simon Glass2a580882020-01-27 08:49:36 -0700840int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600841 struct udevice **devp)
842{
843 struct udevice *dev;
844 int ret;
845
846 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700847 ret = device_find_child_by_seq(parent, seq, &dev);
848
Simon Glass48d4e292014-07-23 06:55:19 -0600849 return device_get_device_tail(dev, ret, devp);
850}
851
Simon Glass2a580882020-01-27 08:49:36 -0700852int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600853 struct udevice **devp)
854{
855 struct udevice *dev;
856
857 *devp = NULL;
858
Sean Andersonf69a3302022-03-30 12:26:40 -0400859 device_foreach_child(dev, parent) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700860 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600861 *devp = dev;
862 return 0;
863 }
864 }
865
866 return -ENODEV;
867}
868
Simon Glass2a580882020-01-27 08:49:36 -0700869int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600870 struct udevice **devp)
871{
872 struct udevice *dev;
873 int ret;
874
875 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600876 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600877 return device_get_device_tail(dev, ret, devp);
878}
Simon Glass44da7352014-10-13 23:41:49 -0600879
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200880static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
881 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600882{
883 struct udevice *dev, *found;
884
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200885 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600886 return parent;
887
Sean Andersonf69a3302022-03-30 12:26:40 -0400888 device_foreach_child(dev, parent) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200889 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600890 if (found)
891 return found;
892 }
893
894 return NULL;
895}
896
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200897int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
898{
899 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
900
901 return *devp ? 0 : -ENOENT;
902}
903
904int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600905{
906 struct udevice *dev;
907
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200908 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600909 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
910}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300911
912#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass0000e0d2021-03-15 17:25:28 +1300913int device_get_by_ofplat_idx(uint idx, struct udevice **devp)
Simon Glass5792f4b2020-10-03 11:31:40 -0600914{
Simon Glass5792f4b2020-10-03 11:31:40 -0600915 struct udevice *dev;
916
Simon Glass0000e0d2021-03-15 17:25:28 +1300917 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
918 struct udevice *base = ll_entry_start(struct udevice, udevice);
919
920 dev = base + idx;
921 } else {
922 struct driver_rt *drt = gd_dm_driver_rt() + idx;
923
924 dev = drt->dev;
925 }
Simon Glass5792f4b2020-10-03 11:31:40 -0600926 *devp = NULL;
927
928 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
929}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300930#endif
Simon Glassae2efac2015-06-23 15:38:37 -0600931
Simon Glass2a580882020-01-27 08:49:36 -0700932int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600933{
934 if (list_empty(&parent->child_head)) {
935 *devp = NULL;
936 } else {
937 *devp = list_first_entry(&parent->child_head, struct udevice,
938 sibling_node);
939 }
940
941 return 0;
942}
943
944int device_find_next_child(struct udevice **devp)
945{
946 struct udevice *dev = *devp;
947 struct udevice *parent = dev->parent;
948
949 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
950 *devp = NULL;
951 } else {
952 *devp = list_entry(dev->sibling_node.next, struct udevice,
953 sibling_node);
954 }
955
956 return 0;
957}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700958
Simon Glass2a580882020-01-27 08:49:36 -0700959int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600960 enum uclass_id uclass_id,
961 struct udevice **devp)
962{
963 struct udevice *dev;
964
965 *devp = NULL;
Sean Andersonf69a3302022-03-30 12:26:40 -0400966 device_foreach_child(dev, parent) {
Simon Glassb775e872018-10-01 12:22:07 -0600967 if (!device_active(dev) &&
968 device_get_uclass_id(dev) == uclass_id) {
969 *devp = dev;
970 return 0;
971 }
972 }
973
974 return -ENODEV;
975}
976
Simon Glass2a580882020-01-27 08:49:36 -0700977int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700978 enum uclass_id uclass_id,
979 struct udevice **devp)
980{
981 struct udevice *dev;
982
983 *devp = NULL;
Sean Andersonf69a3302022-03-30 12:26:40 -0400984 device_foreach_child(dev, parent) {
Simon Glassa11341a2018-11-18 08:14:31 -0700985 if (device_get_uclass_id(dev) == uclass_id) {
986 *devp = dev;
987 return 0;
988 }
989 }
990
991 return -ENODEV;
992}
993
Simon Glass4b23e452021-10-23 17:26:08 -0600994int device_find_child_by_namelen(const struct udevice *parent, const char *name,
995 int len, struct udevice **devp)
Simon Glassa11341a2018-11-18 08:14:31 -0700996{
997 struct udevice *dev;
998
999 *devp = NULL;
1000
Sean Andersonf69a3302022-03-30 12:26:40 -04001001 device_foreach_child(dev, parent) {
Simon Glass4b23e452021-10-23 17:26:08 -06001002 if (!strncmp(dev->name, name, len) &&
1003 strlen(dev->name) == len) {
Simon Glassa11341a2018-11-18 08:14:31 -07001004 *devp = dev;
1005 return 0;
1006 }
1007 }
1008
1009 return -ENODEV;
1010}
1011
Simon Glass4b23e452021-10-23 17:26:08 -06001012int device_find_child_by_name(const struct udevice *parent, const char *name,
1013 struct udevice **devp)
1014{
1015 return device_find_child_by_namelen(parent, name, strlen(name), devp);
1016}
1017
Simon Glass76db9b72020-01-27 08:49:48 -07001018int device_first_child_err(struct udevice *parent, struct udevice **devp)
1019{
1020 struct udevice *dev;
1021
1022 device_find_first_child(parent, &dev);
1023 if (!dev)
1024 return -ENODEV;
1025
1026 return device_get_device_tail(dev, 0, devp);
1027}
1028
1029int device_next_child_err(struct udevice **devp)
1030{
1031 struct udevice *dev = *devp;
1032
1033 device_find_next_child(&dev);
1034 if (!dev)
1035 return -ENODEV;
1036
1037 return device_get_device_tail(dev, 0, devp);
1038}
1039
Simon Glass000676b2020-01-27 08:49:47 -07001040int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
1041{
1042 struct udevice *dev;
1043 int ret;
1044
1045 device_find_first_child(parent, &dev);
1046 if (!dev)
1047 return -ENODEV;
1048
Simon Glassaad29ae2020-12-03 16:55:21 -07001049 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -07001050 if (ret)
1051 return ret;
1052
1053 *devp = dev;
1054
1055 return 0;
1056}
1057
1058int device_next_child_ofdata_err(struct udevice **devp)
1059{
1060 struct udevice *dev = *devp;
1061 int ret;
1062
1063 device_find_next_child(&dev);
1064 if (!dev)
1065 return -ENODEV;
1066
Simon Glassaad29ae2020-12-03 16:55:21 -07001067 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -07001068 if (ret)
1069 return ret;
1070
1071 *devp = dev;
1072
1073 return 0;
1074}
1075
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001076struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -07001077{
1078 return child->parent;
1079}
1080
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001081ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -07001082{
Simon Glass46227bd2015-03-25 12:21:55 -06001083 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -07001084}
Simon Glass98fd5d12015-01-25 08:27:04 -07001085
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001086const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +02001087{
1088 if (!dev || !dev->driver->ops)
1089 return NULL;
1090
1091 return dev->driver->ops;
1092}
1093
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001094enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -07001095{
1096 return dev->uclass->uc_drv->id;
1097}
Peng Fan99b72352015-02-10 14:46:32 +08001098
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001099const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +02001100{
1101 if (!dev)
1102 return NULL;
1103
1104 return dev->uclass->uc_drv->name;
1105}
1106
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001107bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001108{
1109 return !list_empty(&dev->child_head);
1110}
1111
Simon Glass2a580882020-01-27 08:49:36 -07001112bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001113{
1114 struct udevice *child;
1115
1116 for (device_find_first_child(dev, &child);
1117 child;
1118 device_find_next_child(&child)) {
1119 if (device_active(child))
1120 return true;
1121 }
1122
1123 return false;
1124}
1125
Simon Glass2a580882020-01-27 08:49:36 -07001126bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001127{
1128 struct udevice *parent = dev->parent;
1129
1130 if (!parent)
1131 return false;
1132 return list_is_last(&dev->sibling_node, &parent->child_head);
1133}
Simon Glasse3b23e22015-07-30 13:40:39 -06001134
Simon Glass7760ba22016-05-01 13:52:23 -06001135void device_set_name_alloced(struct udevice *dev)
1136{
Simon Glass6211d762020-12-19 10:40:10 -07001137 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
Simon Glass7760ba22016-05-01 13:52:23 -06001138}
1139
Simon Glasse3b23e22015-07-30 13:40:39 -06001140int device_set_name(struct udevice *dev, const char *name)
1141{
1142 name = strdup(name);
1143 if (!name)
1144 return -ENOMEM;
1145 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -06001146 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -06001147
1148 return 0;
1149}
Mugunthan V N4666bd92016-04-28 15:36:02 +05301150
Simon Glassd4f0c942020-12-22 19:30:27 -07001151void dev_set_priv(struct udevice *dev, void *priv)
1152{
Simon Glass541f62c2020-12-22 19:30:30 -07001153 dev->priv_ = priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001154}
1155
1156void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
1157{
Simon Glass541f62c2020-12-22 19:30:30 -07001158 dev->parent_priv_ = parent_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001159}
1160
1161void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
1162{
Simon Glass541f62c2020-12-22 19:30:30 -07001163 dev->uclass_priv_ = uclass_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001164}
1165
1166void dev_set_plat(struct udevice *dev, void *plat)
1167{
Simon Glass541f62c2020-12-22 19:30:30 -07001168 dev->plat_ = plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001169}
1170
1171void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1172{
Simon Glass541f62c2020-12-22 19:30:30 -07001173 dev->parent_plat_ = parent_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001174}
1175
1176void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1177{
Simon Glass541f62c2020-12-22 19:30:30 -07001178 dev->uclass_plat_ = uclass_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001179}
1180
Simon Glass3580f6d2021-08-07 07:24:03 -06001181#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass2a580882020-01-27 08:49:36 -07001182bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +05301183{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001184 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +05301185}
1186
1187bool of_machine_is_compatible(const char *compat)
1188{
Patrick Delaunayc5574ea2022-05-17 14:37:05 +02001189 return ofnode_device_is_compatible(ofnode_root(), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +05301190}
Mario Six3bedfbe2018-06-26 08:46:50 +02001191
1192int dev_disable_by_path(const char *path)
1193{
1194 struct uclass *uc;
1195 ofnode node = ofnode_path(path);
1196 struct udevice *dev;
1197 int ret = 1;
1198
1199 if (!of_live_active())
1200 return -ENOSYS;
1201
Simon Glass784cd9e2020-12-19 10:40:17 -07001202 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +02001203 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1204 if (!ret)
1205 break;
1206 }
1207
1208 if (ret)
1209 return ret;
1210
1211 ret = device_remove(dev, DM_REMOVE_NORMAL);
1212 if (ret)
1213 return ret;
1214
1215 ret = device_unbind(dev);
1216 if (ret)
1217 return ret;
1218
1219 return ofnode_set_enabled(node, false);
1220}
1221
1222int dev_enable_by_path(const char *path)
1223{
1224 ofnode node = ofnode_path(path);
1225 ofnode pnode = ofnode_get_parent(node);
1226 struct udevice *parent;
1227 int ret = 1;
1228
1229 if (!of_live_active())
1230 return -ENOSYS;
1231
1232 ret = device_find_by_ofnode(pnode, &parent);
1233 if (ret)
1234 return ret;
1235
1236 ret = ofnode_set_enabled(node, true);
1237 if (ret)
1238 return ret;
1239
Patrice Chotardcc523cf2021-09-10 16:16:20 +02001240 return lists_bind_fdt(parent, node, NULL, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001241}
Marek Vasut7f18c342019-08-31 18:03:28 +02001242#endif
Simon Glassb5660c42021-03-15 17:25:37 +13001243
1244#if CONFIG_IS_ENABLED(OF_PLATDATA_RT)
1245static struct udevice_rt *dev_get_rt(const struct udevice *dev)
1246{
1247 struct udevice *base = ll_entry_start(struct udevice, udevice);
Simon Glass308d91b2022-03-27 14:26:20 -06001248 uint each_size = dm_udevice_size();
1249 int idx = ((void *)dev - (void *)base) / each_size;
Simon Glassb5660c42021-03-15 17:25:37 +13001250
1251 struct udevice_rt *urt = gd_dm_udevice_rt() + idx;
1252
1253 return urt;
1254}
1255
1256u32 dev_get_flags(const struct udevice *dev)
1257{
1258 const struct udevice_rt *urt = dev_get_rt(dev);
1259
1260 return urt->flags_;
1261}
1262
1263void dev_or_flags(const struct udevice *dev, u32 or)
1264{
1265 struct udevice_rt *urt = dev_get_rt(dev);
1266
1267 urt->flags_ |= or;
1268}
1269
1270void dev_bic_flags(const struct udevice *dev, u32 bic)
1271{
1272 struct udevice_rt *urt = dev_get_rt(dev);
1273
1274 urt->flags_ &= ~bic;
1275}
1276#endif /* OF_PLATDATA_RT */