blob: 42ba2dce46584ac52bce79487ebc5ad33c616b56 [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 Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Vignesh R0dff3702016-07-06 09:58:55 +053015#include <asm/io.h>
Philipp Tomsich9cf03b02018-01-08 13:59:18 +010016#include <clk.h>
Simon Glassdb6f0202014-07-23 06:55:12 -060017#include <fdtdec.h>
Stefan Roeseadc09052015-09-02 07:41:12 +020018#include <fdt_support.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070019#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <asm/cache.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070021#include <dm/device.h>
22#include <dm/device-internal.h>
23#include <dm/lists.h>
Mario Six1f1c1ec2018-01-15 11:07:20 +010024#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090025#include <dm/pinctrl.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070026#include <dm/platdata.h>
Simon Glass322b2902017-05-18 20:09:05 -060027#include <dm/read.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070028#include <dm/uclass.h>
29#include <dm/uclass-internal.h>
30#include <dm/util.h>
31#include <linux/err.h>
32#include <linux/list.h>
Peng Fan6fba32d2018-07-27 10:20:38 +080033#include <power-domain.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070034
Simon Glassdb6f0202014-07-23 06:55:12 -060035DECLARE_GLOBAL_DATA_PTR;
36
Stephen Warren8c93df12016-05-11 15:26:24 -060037static int device_bind_common(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -070038 const char *name, void *plat,
Simon Glassb0cf4b32017-05-17 17:18:11 -060039 ulong driver_data, ofnode node,
Simon Glass39edb952020-12-03 16:55:19 -070040 uint of_plat_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070041{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020042 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070043 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020044 int size, ret = 0;
Simon Glass7272e922020-12-16 21:20:09 -070045 bool auto_seq = true;
Simon Glass2543b162020-12-22 19:30:29 -070046 void *ptr;
Simon Glassdd6ab882014-02-26 15:59:18 -070047
Simon Glass47fd4a22021-03-15 17:25:15 +130048 if (CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND))
49 return -ENOSYS;
50
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090051 if (devp)
52 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070053 if (!name)
54 return -EINVAL;
55
56 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060057 if (ret) {
58 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070059 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060060 }
Simon Glassdd6ab882014-02-26 15:59:18 -070061
Heiko Schocherb74fcb42014-05-22 12:43:05 +020062 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070063 if (!dev)
64 return -ENOMEM;
65
66 INIT_LIST_HEAD(&dev->sibling_node);
67 INIT_LIST_HEAD(&dev->child_head);
68 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090069#ifdef CONFIG_DEVRES
Masahiro Yamada8b15b162015-07-25 21:52:35 +090070 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090071#endif
Simon Glass2543b162020-12-22 19:30:29 -070072 dev_set_plat(dev, plat);
Stephen Warren8c93df12016-05-11 15:26:24 -060073 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070074 dev->name = name;
Simon Glassa7ece582020-12-19 10:40:14 -070075 dev_set_ofnode(dev, node);
Simon Glassdd6ab882014-02-26 15:59:18 -070076 dev->parent = parent;
77 dev->driver = drv;
78 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060079
Simon Glass5e349922020-12-19 10:40:09 -070080 dev->seq_ = -1;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010081 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
82 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070083 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010084 * Some devices, such as a SPI bus, I2C bus and serial ports
85 * are numbered using aliases.
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010086 */
Simon Glasse422f8c2020-07-09 18:39:24 -060087 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
88 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass7272e922020-12-16 21:20:09 -070089 if (uc->uc_drv->name && ofnode_valid(node)) {
Simon Glass28823e32021-07-05 16:32:39 -060090 if (!dev_read_alias_seq(dev, &dev->seq_)) {
Simon Glass38040a92020-12-16 21:20:32 -070091 auto_seq = false;
Simon Glass28823e32021-07-05 16:32:39 -060092 log_debug(" - seq=%d\n", dev->seq_);
93 }
Simon Glass7272e922020-12-16 21:20:09 -070094 }
Simon Glass0ccb0972015-01-25 08:27:05 -070095 }
Simon Glassdb6f0202014-07-23 06:55:12 -060096 }
Simon Glass3a9d5f12020-12-16 21:20:17 -070097 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
Simon Glass5e349922020-12-19 10:40:09 -070098 dev->seq_ = uclass_find_next_free_seq(uc);
Simon Glass1bd3deb2015-02-27 22:06:30 -070099
Simon Glasse07434b2021-02-03 21:29:43 -0700100 /* Check if we need to allocate plat */
Simon Glass71fa5b42020-12-03 16:55:18 -0700101 if (drv->plat_auto) {
102 bool alloc = !plat;
Simon Glassafbf9b82016-07-04 11:58:18 -0600103
Simon Glasse07434b2021-02-03 21:29:43 -0700104 /*
105 * For of-platdata, we try use the existing data, but if
106 * plat_auto is larger, we must allocate a new space
107 */
Simon Glassafbf9b82016-07-04 11:58:18 -0600108 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glasse07434b2021-02-03 21:29:43 -0700109 if (of_plat_size)
Simon Glass6211d762020-12-19 10:40:10 -0700110 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
Simon Glasse07434b2021-02-03 21:29:43 -0700111 if (of_plat_size < drv->plat_auto)
112 alloc = true;
Simon Glassafbf9b82016-07-04 11:58:18 -0600113 }
114 if (alloc) {
Simon Glass6211d762020-12-19 10:40:10 -0700115 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700116 ptr = calloc(1, drv->plat_auto);
117 if (!ptr) {
Simon Glassafbf9b82016-07-04 11:58:18 -0600118 ret = -ENOMEM;
119 goto fail_alloc1;
120 }
Simon Glasse07434b2021-02-03 21:29:43 -0700121
122 /*
123 * For of-platdata, copy the old plat into the new
124 * space
125 */
Simon Glass2543b162020-12-22 19:30:29 -0700126 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
127 memcpy(ptr, plat, of_plat_size);
128 dev_set_plat(dev, ptr);
Simon Glassba750492015-01-25 08:27:00 -0700129 }
130 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200131
Simon Glass71fa5b42020-12-03 16:55:18 -0700132 size = uc->uc_drv->per_device_plat_auto;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200133 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700134 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700135 ptr = calloc(1, size);
136 if (!ptr) {
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200137 ret = -ENOMEM;
138 goto fail_alloc2;
139 }
Simon Glass2543b162020-12-22 19:30:29 -0700140 dev_set_uclass_plat(dev, ptr);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200141 }
Simon Glass11b61732015-01-25 08:27:01 -0700142
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200143 if (parent) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700144 size = parent->driver->per_child_plat_auto;
Simon Glassd6206a52021-02-03 21:29:44 -0700145 if (!size)
Simon Glass71fa5b42020-12-03 16:55:18 -0700146 size = parent->uclass->uc_drv->per_child_plat_auto;
Simon Glass11b61732015-01-25 08:27:01 -0700147 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700148 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700149 ptr = calloc(1, size);
150 if (!ptr) {
Simon Glass11b61732015-01-25 08:27:01 -0700151 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200152 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700153 }
Simon Glass2543b162020-12-22 19:30:29 -0700154 dev_set_parent_plat(dev, ptr);
Simon Glass11b61732015-01-25 08:27:01 -0700155 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100156 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700157 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100158 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700159
160 ret = uclass_bind_device(dev);
161 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700162 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700163
164 /* if we fail to bind we remove device from successors and free it */
165 if (drv->bind) {
166 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700167 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700168 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700169 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700170 if (parent && parent->driver->child_post_bind) {
171 ret = parent->driver->child_post_bind(dev);
172 if (ret)
173 goto fail_child_post_bind;
174 }
Simon Glass286863d2016-01-05 09:30:59 -0700175 if (uc->uc_drv->post_bind) {
176 ret = uc->uc_drv->post_bind(dev);
177 if (ret)
178 goto fail_uclass_post_bind;
179 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700180
Simon Glassdd6ab882014-02-26 15:59:18 -0700181 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900182 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900183 if (devp)
184 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700185
Simon Glass6211d762020-12-19 10:40:10 -0700186 dev_or_flags(dev, DM_FLAG_BOUND);
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900187
Simon Glassdd6ab882014-02-26 15:59:18 -0700188 return 0;
189
Simon Glass286863d2016-01-05 09:30:59 -0700190fail_uclass_post_bind:
191 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700192fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900193 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700194 if (drv->unbind && drv->unbind(dev)) {
195 dm_warn("unbind() method failed on dev '%s' on error path\n",
196 dev->name);
197 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700198 }
199
Simon Glassdd6ab882014-02-26 15:59:18 -0700200fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900201 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700202 if (uclass_unbind_device(dev)) {
203 dm_warn("Failed to unbind dev '%s' on error path\n",
204 dev->name);
205 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700206 }
207fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900208 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700209 list_del(&dev->sibling_node);
Simon Glass6211d762020-12-19 10:40:10 -0700210 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass2543b162020-12-22 19:30:29 -0700211 free(dev_get_parent_plat(dev));
212 dev_set_parent_plat(dev, NULL);
Simon Glasse70a4f42015-02-27 22:06:33 -0700213 }
Simon Glass11b61732015-01-25 08:27:01 -0700214 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200215fail_alloc3:
Simon Glassd6206a52021-02-03 21:29:44 -0700216 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
217 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
218 free(dev_get_uclass_plat(dev));
219 dev_set_uclass_plat(dev, NULL);
220 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200221 }
Simon Glass11b61732015-01-25 08:27:01 -0700222fail_alloc2:
Simon Glassd6206a52021-02-03 21:29:44 -0700223 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
224 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
225 free(dev_get_plat(dev));
226 dev_set_plat(dev, NULL);
227 }
Simon Glassba750492015-01-25 08:27:00 -0700228 }
229fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900230 devres_release_all(dev);
231
Simon Glassdd6ab882014-02-26 15:59:18 -0700232 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700233
Simon Glassdd6ab882014-02-26 15:59:18 -0700234 return ret;
235}
236
Stephen Warren8c93df12016-05-11 15:26:24 -0600237int device_bind_with_driver_data(struct udevice *parent,
238 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600239 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600240 struct udevice **devp)
241{
Simon Glass322b2902017-05-18 20:09:05 -0600242 return device_bind_common(parent, drv, name, NULL, driver_data, node,
243 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600244}
245
Simon Glass884870f2020-11-28 17:50:01 -0700246int device_bind(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -0700247 const char *name, void *plat, ofnode node,
Simon Glass884870f2020-11-28 17:50:01 -0700248 struct udevice **devp)
Simon Glass9eb151e2018-06-11 13:07:15 -0600249{
Simon Glass71fa5b42020-12-03 16:55:18 -0700250 return device_bind_common(parent, drv, name, plat, 0, node, 0,
Simon Glass9eb151e2018-06-11 13:07:15 -0600251 devp);
252}
253
Simon Glassfef72b72014-07-23 06:55:03 -0600254int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Simon Glasscfd6a002020-10-03 11:31:33 -0600255 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700256{
257 struct driver *drv;
Simon Glass39edb952020-12-03 16:55:19 -0700258 uint plat_size = 0;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300259 int ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700260
261 drv = lists_driver_lookup_name(info->name);
262 if (!drv)
263 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600264 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
265 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700266
Simon Glassafbf9b82016-07-04 11:58:18 -0600267#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass39edb952020-12-03 16:55:19 -0700268 plat_size = info->plat_size;
Simon Glassafbf9b82016-07-04 11:58:18 -0600269#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700270 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
Simon Glass39edb952020-12-03 16:55:19 -0700271 ofnode_null(), plat_size, devp);
Walter Lozanoffc41b02020-06-25 01:10:11 -0300272 if (ret)
273 return ret;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300274
275 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700276}
277
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300278int device_reparent(struct udevice *dev, struct udevice *new_parent)
279{
280 struct udevice *pos, *n;
281
282 assert(dev);
283 assert(new_parent);
284
285 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
286 sibling_node) {
287 if (pos->driver != dev->driver)
288 continue;
289
290 list_del(&dev->sibling_node);
291 list_add_tail(&dev->sibling_node, &new_parent->child_head);
292 dev->parent = new_parent;
293
294 break;
295 }
296
297 return 0;
298}
299
Simon Glass825d3f92015-03-25 12:21:53 -0600300static void *alloc_priv(int size, uint flags)
301{
302 void *priv;
303
304 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530305 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600306 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600307 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600308 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600309
310 /*
311 * Ensure that the zero bytes are flushed to memory.
312 * This prevents problems if the driver uses this as
313 * both an input and an output buffer:
314 *
315 * 1. Zeroes written to buffer (here) and sit in the
316 * cache
317 * 2. Driver issues a read command to DMA
318 * 3. CPU runs out of cache space and evicts some cache
319 * data in the buffer, writing zeroes to RAM from
320 * the memset() above
321 * 4. DMA completes
322 * 5. Buffer now has some DMA data and some zeroes
323 * 6. Data being read is now incorrect
324 *
325 * To prevent this, ensure that the cache is clean
326 * within this range at the start. The driver can then
327 * use normal flush-after-write, invalidate-before-read
328 * procedures.
329 *
330 * TODO(sjg@chromium.org): Drop this microblaze
331 * exception.
332 */
333#ifndef CONFIG_MICROBLAZE
334 flush_dcache_range((ulong)priv, (ulong)priv + size);
335#endif
336 }
Simon Glass825d3f92015-03-25 12:21:53 -0600337 } else {
338 priv = calloc(1, size);
339 }
340
341 return priv;
342}
343
Simon Glassad337e62020-12-19 10:40:08 -0700344/**
345 * device_alloc_priv() - Allocate priv/plat data required by the device
346 *
347 * @dev: Device to process
348 * @return 0 if OK, -ENOMEM if out of memory
349 */
350static int device_alloc_priv(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700351{
Simon Glassa626dff2015-03-25 12:21:54 -0600352 const struct driver *drv;
Simon Glass2543b162020-12-22 19:30:29 -0700353 void *ptr;
Simon Glassad337e62020-12-19 10:40:08 -0700354 int size;
Simon Glass44c47892020-04-05 15:38:19 -0600355
Simon Glassdd6ab882014-02-26 15:59:18 -0700356 drv = dev->driver;
357 assert(drv);
358
Bin Meng82cdd782015-08-24 01:14:02 -0700359 /* Allocate private data if requested and not reentered */
Simon Glass2543b162020-12-22 19:30:29 -0700360 if (drv->priv_auto && !dev_get_priv(dev)) {
361 ptr = alloc_priv(drv->priv_auto, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700362 if (!ptr)
363 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700364 dev_set_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700365 }
Simon Glassad337e62020-12-19 10:40:08 -0700366
Bin Meng82cdd782015-08-24 01:14:02 -0700367 /* Allocate private data if requested and not reentered */
Simon Glass8a2b47f2020-12-03 16:55:17 -0700368 size = dev->uclass->uc_drv->per_device_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700369 if (size && !dev_get_uclass_priv(dev)) {
370 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700371 if (!ptr)
372 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700373 dev_set_uclass_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700374 }
375
Simon Glassa8d8f162019-12-29 21:19:18 -0700376 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700377 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700378 size = dev->parent->driver->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700379 if (!size)
Simon Glass8a2b47f2020-12-03 16:55:17 -0700380 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700381 if (size && !dev_get_parent_priv(dev)) {
382 ptr = alloc_priv(size, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700383 if (!ptr)
384 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700385 dev_set_parent_priv(dev, ptr);
Simon Glass60d971b2014-07-23 06:55:20 -0600386 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700387 }
388
Simon Glassad337e62020-12-19 10:40:08 -0700389 return 0;
390}
391
392int device_of_to_plat(struct udevice *dev)
393{
394 const struct driver *drv;
395 int ret;
396
397 if (!dev)
398 return -EINVAL;
399
Simon Glass6211d762020-12-19 10:40:10 -0700400 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
Simon Glassad337e62020-12-19 10:40:08 -0700401 return 0;
402
Simon Glass47fd4a22021-03-15 17:25:15 +1300403 /*
404 * This is not needed if binding is disabled, since data is allocated
405 * at build time.
406 */
407 if (!CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND)) {
408 /* Ensure all parents have ofdata */
409 if (dev->parent) {
410 ret = device_of_to_plat(dev->parent);
411 if (ret)
412 goto fail;
413
414 /*
415 * The device might have already been probed during
416 * the call to device_probe() on its parent device
417 * (e.g. PCI bridge devices). Test the flags again
418 * so that we don't mess up the device.
419 */
420 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
421 return 0;
422 }
423
424 ret = device_alloc_priv(dev);
Simon Glassad337e62020-12-19 10:40:08 -0700425 if (ret)
426 goto fail;
Simon Glassad337e62020-12-19 10:40:08 -0700427 }
Simon Glassad337e62020-12-19 10:40:08 -0700428 drv = dev->driver;
429 assert(drv);
430
Simon Glassaad29ae2020-12-03 16:55:21 -0700431 if (drv->of_to_plat &&
Simon Glass07c17772020-12-19 10:40:12 -0700432 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
Simon Glassaad29ae2020-12-03 16:55:21 -0700433 ret = drv->of_to_plat(dev);
Simon Glassa8d8f162019-12-29 21:19:18 -0700434 if (ret)
435 goto fail;
436 }
Simon Glass60d971b2014-07-23 06:55:20 -0600437
Simon Glass6211d762020-12-19 10:40:10 -0700438 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
Simon Glassd3c9d302019-12-29 21:19:21 -0700439
Simon Glass4a263852019-12-29 21:19:20 -0700440 return 0;
441fail:
442 device_free(dev);
443
444 return ret;
445}
446
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100447/**
448 * device_get_dma_constraints() - Populate device's DMA constraints
449 *
450 * Gets a device's DMA constraints from firmware. This information is later
451 * used by drivers to translate physcal addresses to the device's bus address
452 * space. For now only device-tree is supported.
453 *
454 * @dev: Pointer to target device
455 * Return: 0 if OK or if no DMA constraints were found, error otherwise
456 */
457static int device_get_dma_constraints(struct udevice *dev)
458{
459 struct udevice *parent = dev->parent;
460 phys_addr_t cpu = 0;
461 dma_addr_t bus = 0;
462 u64 size = 0;
463 int ret;
464
465 if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
466 return 0;
467
468 /*
469 * We start parsing for dma-ranges from the device's bus node. This is
470 * specially important on nested buses.
471 */
472 ret = dev_get_dma_range(parent, &cpu, &bus, &size);
473 /* Don't return an error if no 'dma-ranges' were found */
474 if (ret && ret != -ENOENT) {
475 dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret);
476 return ret;
477 }
478
479 dev_set_dma_offset(dev, cpu - bus);
480
481 return 0;
482}
483
Simon Glass4a263852019-12-29 21:19:20 -0700484int device_probe(struct udevice *dev)
485{
486 const struct driver *drv;
487 int ret;
Simon Glass4a263852019-12-29 21:19:20 -0700488
489 if (!dev)
490 return -EINVAL;
491
Simon Glass6211d762020-12-19 10:40:10 -0700492 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass4a263852019-12-29 21:19:20 -0700493 return 0;
494
495 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
Simon Glassbb53bda2015-09-12 08:45:19 -0600520 /*
521 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700522 * continue regardless of the result of pinctrl. Don't process pinctrl
523 * settings for pinctrl devices since the device may not yet be
524 * probed.
Simon Glass303c3c22021-01-21 13:57:13 -0700525 *
526 * This call can produce some non-intuitive results. For example, on an
527 * x86 device where dev is the main PCI bus, the pinctrl device may be
528 * child or grandchild of that bus, meaning that the child will be
529 * probed here. If the child happens to be the P2SB and the pinctrl
530 * device is a child of that, then both the pinctrl and P2SB will be
531 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
532 * is set just above. However, the PCI bus' probe() method and
533 * associated uclass methods have not yet been called.
Simon Glassbb53bda2015-09-12 08:45:19 -0600534 */
Simon Glass27ce9622016-01-21 19:43:25 -0700535 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600536 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900537
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200538 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlac10e52b2019-09-27 13:48:12 +0530539 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
540 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fand2201112019-09-17 09:29:22 +0000541 ret = dev_power_domain_on(dev);
542 if (ret)
543 goto fail;
Peng Fan6fba32d2018-07-27 10:20:38 +0800544 }
545
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100546 ret = device_get_dma_constraints(dev);
547 if (ret)
548 goto fail;
549
Simon Glass9c1f3822015-03-05 12:25:22 -0700550 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700551 if (ret)
552 goto fail;
553
Simon Glassd45560d2014-07-23 06:55:21 -0600554 if (dev->parent && dev->parent->driver->child_pre_probe) {
555 ret = dev->parent->driver->child_pre_probe(dev);
556 if (ret)
557 goto fail;
558 }
559
Bin Meng513cf402019-07-05 09:23:16 -0700560 /* Only handle devices that have a valid ofnode */
Simon Glassf1d50f72020-12-19 10:40:13 -0700561 if (dev_has_ofnode(dev)) {
Bin Meng513cf402019-07-05 09:23:16 -0700562 /*
563 * Process 'assigned-{clocks/clock-parents/clock-rates}'
564 * properties
565 */
Sean Anderson08d531c2021-06-11 00:16:07 -0400566 ret = clk_set_defaults(dev, CLK_DEFAULTS_PRE);
Bin Meng513cf402019-07-05 09:23:16 -0700567 if (ret)
568 goto fail;
569 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100570
Simon Glassdd6ab882014-02-26 15:59:18 -0700571 if (drv->probe) {
572 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700573 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700574 goto fail;
575 }
576
Simon Glassdd6ab882014-02-26 15:59:18 -0700577 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600578 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700579 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700580
Peng Fan7cf58dd2016-03-12 13:17:38 +0800581 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
582 pinctrl_select_state(dev, "default");
583
Simon Glassdd6ab882014-02-26 15:59:18 -0700584 return 0;
585fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100586 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700587 dm_warn("%s: Device '%s' failed to remove on error path\n",
588 __func__, dev->name);
589 }
590fail:
Simon Glass6211d762020-12-19 10:40:10 -0700591 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600592
Simon Glassdd6ab882014-02-26 15:59:18 -0700593 device_free(dev);
594
595 return ret;
596}
597
Simon Glassfa20e932020-12-03 16:55:20 -0700598void *dev_get_plat(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700599{
600 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700601 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700602 return NULL;
603 }
604
Simon Glass781aa3e2021-03-15 17:25:40 +1300605 return dm_priv_to_rw(dev->plat_);
Simon Glassdd6ab882014-02-26 15:59:18 -0700606}
607
Simon Glass71fa5b42020-12-03 16:55:18 -0700608void *dev_get_parent_plat(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700609{
610 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600611 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700612 return NULL;
613 }
614
Simon Glass781aa3e2021-03-15 17:25:40 +1300615 return dm_priv_to_rw(dev->parent_plat_);
Simon Glass11b61732015-01-25 08:27:01 -0700616}
617
Simon Glass71fa5b42020-12-03 16:55:18 -0700618void *dev_get_uclass_plat(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200619{
620 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600621 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200622 return NULL;
623 }
624
Simon Glass781aa3e2021-03-15 17:25:40 +1300625 return dm_priv_to_rw(dev->uclass_plat_);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200626}
627
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600628void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700629{
630 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700631 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700632 return NULL;
633 }
634
Simon Glass781aa3e2021-03-15 17:25:40 +1300635 return dm_priv_to_rw(dev->priv_);
Simon Glassdd6ab882014-02-26 15:59:18 -0700636}
Simon Glass48d4e292014-07-23 06:55:19 -0600637
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600638void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700639{
640 if (!dev) {
641 dm_warn("%s: null device\n", __func__);
642 return NULL;
643 }
644
Simon Glass781aa3e2021-03-15 17:25:40 +1300645 return dm_priv_to_rw(dev->uclass_priv_);
Simon Glassde0977b2015-03-05 12:25:20 -0700646}
647
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600648void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600649{
650 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700651 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600652 return NULL;
653 }
654
Simon Glass781aa3e2021-03-15 17:25:40 +1300655 return dm_priv_to_rw(dev->parent_priv_);
Simon Glass60d971b2014-07-23 06:55:20 -0600656}
657
Simon Glass48d4e292014-07-23 06:55:19 -0600658static int device_get_device_tail(struct udevice *dev, int ret,
659 struct udevice **devp)
660{
661 if (ret)
662 return ret;
663
664 ret = device_probe(dev);
665 if (ret)
666 return ret;
667
668 *devp = dev;
669
670 return 0;
671}
672
Simon Glass3580f6d2021-08-07 07:24:03 -0600673#if CONFIG_IS_ENABLED(OF_REAL)
Mario Six3bedfbe2018-06-26 08:46:50 +0200674/**
675 * device_find_by_ofnode() - Return device associated with given ofnode
676 *
677 * The returned device is *not* activated.
678 *
679 * @node: The ofnode for which a associated device should be looked up
680 * @devp: Pointer to structure to hold the found device
681 * Return: 0 if OK, -ve on error
682 */
683static int device_find_by_ofnode(ofnode node, struct udevice **devp)
684{
685 struct uclass *uc;
686 struct udevice *dev;
687 int ret;
688
Simon Glass784cd9e2020-12-19 10:40:17 -0700689 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +0200690 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
691 &dev);
692 if (!ret || dev) {
693 *devp = dev;
694 return 0;
695 }
696 }
697
698 return -ENODEV;
699}
Marek Vasut7f18c342019-08-31 18:03:28 +0200700#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200701
Simon Glass2a580882020-01-27 08:49:36 -0700702int device_get_child(const struct udevice *parent, int index,
703 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600704{
705 struct udevice *dev;
706
707 list_for_each_entry(dev, &parent->child_head, sibling_node) {
708 if (!index--)
709 return device_get_device_tail(dev, 0, devp);
710 }
711
712 return -ENODEV;
713}
714
Simon Glass2a580882020-01-27 08:49:36 -0700715int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530716{
717 struct udevice *dev;
718 int count = 0;
719
720 list_for_each_entry(dev, &parent->child_head, sibling_node)
721 count++;
722
723 return count;
724}
725
Simon Glass07e13382020-12-16 21:20:29 -0700726int device_find_child_by_seq(const struct udevice *parent, int seq,
727 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600728{
729 struct udevice *dev;
730
731 *devp = NULL;
Simon Glass48d4e292014-07-23 06:55:19 -0600732
733 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glass5e349922020-12-19 10:40:09 -0700734 if (dev->seq_ == seq) {
Simon Glass48d4e292014-07-23 06:55:19 -0600735 *devp = dev;
736 return 0;
737 }
738 }
739
740 return -ENODEV;
741}
742
Simon Glass2a580882020-01-27 08:49:36 -0700743int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600744 struct udevice **devp)
745{
746 struct udevice *dev;
747 int ret;
748
749 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700750 ret = device_find_child_by_seq(parent, seq, &dev);
751
Simon Glass48d4e292014-07-23 06:55:19 -0600752 return device_get_device_tail(dev, ret, devp);
753}
754
Simon Glass2a580882020-01-27 08:49:36 -0700755int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600756 struct udevice **devp)
757{
758 struct udevice *dev;
759
760 *devp = NULL;
761
762 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700763 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600764 *devp = dev;
765 return 0;
766 }
767 }
768
769 return -ENODEV;
770}
771
Simon Glass2a580882020-01-27 08:49:36 -0700772int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600773 struct udevice **devp)
774{
775 struct udevice *dev;
776 int ret;
777
778 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600779 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600780 return device_get_device_tail(dev, ret, devp);
781}
Simon Glass44da7352014-10-13 23:41:49 -0600782
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200783static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
784 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600785{
786 struct udevice *dev, *found;
787
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200788 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600789 return parent;
790
791 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200792 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600793 if (found)
794 return found;
795 }
796
797 return NULL;
798}
799
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200800int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
801{
802 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
803
804 return *devp ? 0 : -ENOENT;
805}
806
807int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600808{
809 struct udevice *dev;
810
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200811 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600812 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
813}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300814
815#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass0000e0d2021-03-15 17:25:28 +1300816int device_get_by_ofplat_idx(uint idx, struct udevice **devp)
Simon Glass5792f4b2020-10-03 11:31:40 -0600817{
Simon Glass5792f4b2020-10-03 11:31:40 -0600818 struct udevice *dev;
819
Simon Glass0000e0d2021-03-15 17:25:28 +1300820 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
821 struct udevice *base = ll_entry_start(struct udevice, udevice);
822
823 dev = base + idx;
824 } else {
825 struct driver_rt *drt = gd_dm_driver_rt() + idx;
826
827 dev = drt->dev;
828 }
Simon Glass5792f4b2020-10-03 11:31:40 -0600829 *devp = NULL;
830
831 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
832}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300833#endif
Simon Glassae2efac2015-06-23 15:38:37 -0600834
Simon Glass2a580882020-01-27 08:49:36 -0700835int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600836{
837 if (list_empty(&parent->child_head)) {
838 *devp = NULL;
839 } else {
840 *devp = list_first_entry(&parent->child_head, struct udevice,
841 sibling_node);
842 }
843
844 return 0;
845}
846
847int device_find_next_child(struct udevice **devp)
848{
849 struct udevice *dev = *devp;
850 struct udevice *parent = dev->parent;
851
852 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
853 *devp = NULL;
854 } else {
855 *devp = list_entry(dev->sibling_node.next, struct udevice,
856 sibling_node);
857 }
858
859 return 0;
860}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700861
Simon Glass2a580882020-01-27 08:49:36 -0700862int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600863 enum uclass_id uclass_id,
864 struct udevice **devp)
865{
866 struct udevice *dev;
867
868 *devp = NULL;
869 list_for_each_entry(dev, &parent->child_head, sibling_node) {
870 if (!device_active(dev) &&
871 device_get_uclass_id(dev) == uclass_id) {
872 *devp = dev;
873 return 0;
874 }
875 }
876
877 return -ENODEV;
878}
879
Simon Glass2a580882020-01-27 08:49:36 -0700880int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700881 enum uclass_id uclass_id,
882 struct udevice **devp)
883{
884 struct udevice *dev;
885
886 *devp = NULL;
887 list_for_each_entry(dev, &parent->child_head, sibling_node) {
888 if (device_get_uclass_id(dev) == uclass_id) {
889 *devp = dev;
890 return 0;
891 }
892 }
893
894 return -ENODEV;
895}
896
Simon Glass2a580882020-01-27 08:49:36 -0700897int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glassa11341a2018-11-18 08:14:31 -0700898 struct udevice **devp)
899{
900 struct udevice *dev;
901
902 *devp = NULL;
903
904 list_for_each_entry(dev, &parent->child_head, sibling_node) {
905 if (!strcmp(dev->name, name)) {
906 *devp = dev;
907 return 0;
908 }
909 }
910
911 return -ENODEV;
912}
913
Simon Glass76db9b72020-01-27 08:49:48 -0700914int device_first_child_err(struct udevice *parent, struct udevice **devp)
915{
916 struct udevice *dev;
917
918 device_find_first_child(parent, &dev);
919 if (!dev)
920 return -ENODEV;
921
922 return device_get_device_tail(dev, 0, devp);
923}
924
925int device_next_child_err(struct udevice **devp)
926{
927 struct udevice *dev = *devp;
928
929 device_find_next_child(&dev);
930 if (!dev)
931 return -ENODEV;
932
933 return device_get_device_tail(dev, 0, devp);
934}
935
Simon Glass000676b2020-01-27 08:49:47 -0700936int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
937{
938 struct udevice *dev;
939 int ret;
940
941 device_find_first_child(parent, &dev);
942 if (!dev)
943 return -ENODEV;
944
Simon Glassaad29ae2020-12-03 16:55:21 -0700945 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700946 if (ret)
947 return ret;
948
949 *devp = dev;
950
951 return 0;
952}
953
954int device_next_child_ofdata_err(struct udevice **devp)
955{
956 struct udevice *dev = *devp;
957 int ret;
958
959 device_find_next_child(&dev);
960 if (!dev)
961 return -ENODEV;
962
Simon Glassaad29ae2020-12-03 16:55:21 -0700963 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700964 if (ret)
965 return ret;
966
967 *devp = dev;
968
969 return 0;
970}
971
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600972struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700973{
974 return child->parent;
975}
976
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600977ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700978{
Simon Glass46227bd2015-03-25 12:21:55 -0600979 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700980}
Simon Glass98fd5d12015-01-25 08:27:04 -0700981
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600982const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200983{
984 if (!dev || !dev->driver->ops)
985 return NULL;
986
987 return dev->driver->ops;
988}
989
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600990enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -0700991{
992 return dev->uclass->uc_drv->id;
993}
Peng Fan99b72352015-02-10 14:46:32 +0800994
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600995const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200996{
997 if (!dev)
998 return NULL;
999
1000 return dev->uclass->uc_drv->name;
1001}
1002
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001003bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001004{
1005 return !list_empty(&dev->child_head);
1006}
1007
Simon Glass2a580882020-01-27 08:49:36 -07001008bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001009{
1010 struct udevice *child;
1011
1012 for (device_find_first_child(dev, &child);
1013 child;
1014 device_find_next_child(&child)) {
1015 if (device_active(child))
1016 return true;
1017 }
1018
1019 return false;
1020}
1021
Simon Glass2a580882020-01-27 08:49:36 -07001022bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001023{
1024 struct udevice *parent = dev->parent;
1025
1026 if (!parent)
1027 return false;
1028 return list_is_last(&dev->sibling_node, &parent->child_head);
1029}
Simon Glasse3b23e22015-07-30 13:40:39 -06001030
Simon Glass7760ba22016-05-01 13:52:23 -06001031void device_set_name_alloced(struct udevice *dev)
1032{
Simon Glass6211d762020-12-19 10:40:10 -07001033 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
Simon Glass7760ba22016-05-01 13:52:23 -06001034}
1035
Simon Glasse3b23e22015-07-30 13:40:39 -06001036int device_set_name(struct udevice *dev, const char *name)
1037{
1038 name = strdup(name);
1039 if (!name)
1040 return -ENOMEM;
1041 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -06001042 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -06001043
1044 return 0;
1045}
Mugunthan V N4666bd92016-04-28 15:36:02 +05301046
Simon Glassd4f0c942020-12-22 19:30:27 -07001047void dev_set_priv(struct udevice *dev, void *priv)
1048{
Simon Glass541f62c2020-12-22 19:30:30 -07001049 dev->priv_ = priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001050}
1051
1052void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
1053{
Simon Glass541f62c2020-12-22 19:30:30 -07001054 dev->parent_priv_ = parent_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001055}
1056
1057void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
1058{
Simon Glass541f62c2020-12-22 19:30:30 -07001059 dev->uclass_priv_ = uclass_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001060}
1061
1062void dev_set_plat(struct udevice *dev, void *plat)
1063{
Simon Glass541f62c2020-12-22 19:30:30 -07001064 dev->plat_ = plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001065}
1066
1067void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1068{
Simon Glass541f62c2020-12-22 19:30:30 -07001069 dev->parent_plat_ = parent_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001070}
1071
1072void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1073{
Simon Glass541f62c2020-12-22 19:30:30 -07001074 dev->uclass_plat_ = uclass_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001075}
1076
Simon Glass3580f6d2021-08-07 07:24:03 -06001077#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass2a580882020-01-27 08:49:36 -07001078bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +05301079{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001080 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +05301081}
1082
1083bool of_machine_is_compatible(const char *compat)
1084{
1085 const void *fdt = gd->fdt_blob;
1086
1087 return !fdt_node_check_compatible(fdt, 0, compat);
1088}
Mario Six3bedfbe2018-06-26 08:46:50 +02001089
1090int dev_disable_by_path(const char *path)
1091{
1092 struct uclass *uc;
1093 ofnode node = ofnode_path(path);
1094 struct udevice *dev;
1095 int ret = 1;
1096
1097 if (!of_live_active())
1098 return -ENOSYS;
1099
Simon Glass784cd9e2020-12-19 10:40:17 -07001100 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +02001101 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1102 if (!ret)
1103 break;
1104 }
1105
1106 if (ret)
1107 return ret;
1108
1109 ret = device_remove(dev, DM_REMOVE_NORMAL);
1110 if (ret)
1111 return ret;
1112
1113 ret = device_unbind(dev);
1114 if (ret)
1115 return ret;
1116
1117 return ofnode_set_enabled(node, false);
1118}
1119
1120int dev_enable_by_path(const char *path)
1121{
1122 ofnode node = ofnode_path(path);
1123 ofnode pnode = ofnode_get_parent(node);
1124 struct udevice *parent;
1125 int ret = 1;
1126
1127 if (!of_live_active())
1128 return -ENOSYS;
1129
1130 ret = device_find_by_ofnode(pnode, &parent);
1131 if (ret)
1132 return ret;
1133
1134 ret = ofnode_set_enabled(node, true);
1135 if (ret)
1136 return ret;
1137
Bin Meng9a9b0742018-10-10 22:06:58 -07001138 return lists_bind_fdt(parent, node, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001139}
Marek Vasut7f18c342019-08-31 18:03:28 +02001140#endif
Simon Glassb5660c42021-03-15 17:25:37 +13001141
1142#if CONFIG_IS_ENABLED(OF_PLATDATA_RT)
1143static struct udevice_rt *dev_get_rt(const struct udevice *dev)
1144{
1145 struct udevice *base = ll_entry_start(struct udevice, udevice);
1146 int idx = dev - base;
1147
1148 struct udevice_rt *urt = gd_dm_udevice_rt() + idx;
1149
1150 return urt;
1151}
1152
1153u32 dev_get_flags(const struct udevice *dev)
1154{
1155 const struct udevice_rt *urt = dev_get_rt(dev);
1156
1157 return urt->flags_;
1158}
1159
1160void dev_or_flags(const struct udevice *dev, u32 or)
1161{
1162 struct udevice_rt *urt = dev_get_rt(dev);
1163
1164 urt->flags_ |= or;
1165}
1166
1167void dev_bic_flags(const struct udevice *dev, u32 bic)
1168{
1169 struct udevice_rt *urt = dev_get_rt(dev);
1170
1171 urt->flags_ &= ~bic;
1172}
1173#endif /* OF_PLATDATA_RT */