blob: 625134921d64fb81ad27ec28a429871a4c291ac8 [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
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090048 if (devp)
49 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070050 if (!name)
51 return -EINVAL;
52
53 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060054 if (ret) {
55 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070056 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060057 }
Simon Glassdd6ab882014-02-26 15:59:18 -070058
Heiko Schocherb74fcb42014-05-22 12:43:05 +020059 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070060 if (!dev)
61 return -ENOMEM;
62
63 INIT_LIST_HEAD(&dev->sibling_node);
64 INIT_LIST_HEAD(&dev->child_head);
65 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090066#ifdef CONFIG_DEVRES
Masahiro Yamada8b15b162015-07-25 21:52:35 +090067 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090068#endif
Simon Glass2543b162020-12-22 19:30:29 -070069 dev_set_plat(dev, plat);
Stephen Warren8c93df12016-05-11 15:26:24 -060070 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070071 dev->name = name;
Simon Glassa7ece582020-12-19 10:40:14 -070072 dev_set_ofnode(dev, node);
Simon Glassdd6ab882014-02-26 15:59:18 -070073 dev->parent = parent;
74 dev->driver = drv;
75 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060076
Simon Glass5e349922020-12-19 10:40:09 -070077 dev->seq_ = -1;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010078 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
79 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070080 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010081 * Some devices, such as a SPI bus, I2C bus and serial ports
82 * are numbered using aliases.
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010083 */
Simon Glasse422f8c2020-07-09 18:39:24 -060084 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
85 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass7272e922020-12-16 21:20:09 -070086 if (uc->uc_drv->name && ofnode_valid(node)) {
Simon Glass5e349922020-12-19 10:40:09 -070087 if (!dev_read_alias_seq(dev, &dev->seq_))
Simon Glass38040a92020-12-16 21:20:32 -070088 auto_seq = false;
Simon Glass7272e922020-12-16 21:20:09 -070089 }
Simon Glass0ccb0972015-01-25 08:27:05 -070090 }
Simon Glassdb6f0202014-07-23 06:55:12 -060091 }
Simon Glass3a9d5f12020-12-16 21:20:17 -070092 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
Simon Glass5e349922020-12-19 10:40:09 -070093 dev->seq_ = uclass_find_next_free_seq(uc);
Simon Glass1bd3deb2015-02-27 22:06:30 -070094
Simon Glass71fa5b42020-12-03 16:55:18 -070095 if (drv->plat_auto) {
96 bool alloc = !plat;
Simon Glassafbf9b82016-07-04 11:58:18 -060097
98 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass39edb952020-12-03 16:55:19 -070099 if (of_plat_size) {
Simon Glass6211d762020-12-19 10:40:10 -0700100 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
Simon Glass39edb952020-12-03 16:55:19 -0700101 if (of_plat_size < drv->plat_auto)
Simon Glassafbf9b82016-07-04 11:58:18 -0600102 alloc = true;
103 }
104 }
105 if (alloc) {
Simon Glass6211d762020-12-19 10:40:10 -0700106 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700107 ptr = calloc(1, drv->plat_auto);
108 if (!ptr) {
Simon Glassafbf9b82016-07-04 11:58:18 -0600109 ret = -ENOMEM;
110 goto fail_alloc1;
111 }
Simon Glass2543b162020-12-22 19:30:29 -0700112 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
113 memcpy(ptr, plat, of_plat_size);
114 dev_set_plat(dev, ptr);
Simon Glassba750492015-01-25 08:27:00 -0700115 }
116 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200117
Simon Glass71fa5b42020-12-03 16:55:18 -0700118 size = uc->uc_drv->per_device_plat_auto;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200119 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700120 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700121 ptr = calloc(1, size);
122 if (!ptr) {
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200123 ret = -ENOMEM;
124 goto fail_alloc2;
125 }
Simon Glass2543b162020-12-22 19:30:29 -0700126 dev_set_uclass_plat(dev, ptr);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200127 }
Simon Glass11b61732015-01-25 08:27:01 -0700128
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200129 if (parent) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700130 size = parent->driver->per_child_plat_auto;
Simon Glass57f95402015-01-25 08:27:02 -0700131 if (!size) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700132 size = parent->uclass->uc_drv->per_child_plat_auto;
Simon Glass57f95402015-01-25 08:27:02 -0700133 }
Simon Glass11b61732015-01-25 08:27:01 -0700134 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700135 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700136 ptr = calloc(1, size);
137 if (!ptr) {
Simon Glass11b61732015-01-25 08:27:01 -0700138 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200139 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700140 }
Simon Glass2543b162020-12-22 19:30:29 -0700141 dev_set_parent_plat(dev, ptr);
Simon Glass11b61732015-01-25 08:27:01 -0700142 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100143 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700144 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100145 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700146
147 ret = uclass_bind_device(dev);
148 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700149 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700150
151 /* if we fail to bind we remove device from successors and free it */
152 if (drv->bind) {
153 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700154 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700155 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700156 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700157 if (parent && parent->driver->child_post_bind) {
158 ret = parent->driver->child_post_bind(dev);
159 if (ret)
160 goto fail_child_post_bind;
161 }
Simon Glass286863d2016-01-05 09:30:59 -0700162 if (uc->uc_drv->post_bind) {
163 ret = uc->uc_drv->post_bind(dev);
164 if (ret)
165 goto fail_uclass_post_bind;
166 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700167
Simon Glassdd6ab882014-02-26 15:59:18 -0700168 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900169 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900170 if (devp)
171 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700172
Simon Glass6211d762020-12-19 10:40:10 -0700173 dev_or_flags(dev, DM_FLAG_BOUND);
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900174
Simon Glassdd6ab882014-02-26 15:59:18 -0700175 return 0;
176
Simon Glass286863d2016-01-05 09:30:59 -0700177fail_uclass_post_bind:
178 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700179fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900180 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700181 if (drv->unbind && drv->unbind(dev)) {
182 dm_warn("unbind() method failed on dev '%s' on error path\n",
183 dev->name);
184 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700185 }
186
Simon Glassdd6ab882014-02-26 15:59:18 -0700187fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900188 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700189 if (uclass_unbind_device(dev)) {
190 dm_warn("Failed to unbind dev '%s' on error path\n",
191 dev->name);
192 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700193 }
194fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900195 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700196 list_del(&dev->sibling_node);
Simon Glass6211d762020-12-19 10:40:10 -0700197 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass2543b162020-12-22 19:30:29 -0700198 free(dev_get_parent_plat(dev));
199 dev_set_parent_plat(dev, NULL);
Simon Glasse70a4f42015-02-27 22:06:33 -0700200 }
Simon Glass11b61732015-01-25 08:27:01 -0700201 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200202fail_alloc3:
Simon Glass6211d762020-12-19 10:40:10 -0700203 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
Simon Glass2543b162020-12-22 19:30:29 -0700204 free(dev_get_uclass_plat(dev));
205 dev_set_uclass_plat(dev, NULL);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200206 }
Simon Glass11b61732015-01-25 08:27:01 -0700207fail_alloc2:
Simon Glass6211d762020-12-19 10:40:10 -0700208 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
Simon Glass2543b162020-12-22 19:30:29 -0700209 free(dev_get_plat(dev));
210 dev_set_plat(dev, NULL);
Simon Glassba750492015-01-25 08:27:00 -0700211 }
212fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900213 devres_release_all(dev);
214
Simon Glassdd6ab882014-02-26 15:59:18 -0700215 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700216
Simon Glassdd6ab882014-02-26 15:59:18 -0700217 return ret;
218}
219
Stephen Warren8c93df12016-05-11 15:26:24 -0600220int device_bind_with_driver_data(struct udevice *parent,
221 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600222 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600223 struct udevice **devp)
224{
Simon Glass322b2902017-05-18 20:09:05 -0600225 return device_bind_common(parent, drv, name, NULL, driver_data, node,
226 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600227}
228
Simon Glass884870f2020-11-28 17:50:01 -0700229int device_bind(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -0700230 const char *name, void *plat, ofnode node,
Simon Glass884870f2020-11-28 17:50:01 -0700231 struct udevice **devp)
Simon Glass9eb151e2018-06-11 13:07:15 -0600232{
Simon Glass71fa5b42020-12-03 16:55:18 -0700233 return device_bind_common(parent, drv, name, plat, 0, node, 0,
Simon Glass9eb151e2018-06-11 13:07:15 -0600234 devp);
235}
236
Simon Glassfef72b72014-07-23 06:55:03 -0600237int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Simon Glasscfd6a002020-10-03 11:31:33 -0600238 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700239{
240 struct driver *drv;
Simon Glass39edb952020-12-03 16:55:19 -0700241 uint plat_size = 0;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300242 int ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700243
244 drv = lists_driver_lookup_name(info->name);
245 if (!drv)
246 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600247 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
248 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700249
Simon Glassafbf9b82016-07-04 11:58:18 -0600250#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass39edb952020-12-03 16:55:19 -0700251 plat_size = info->plat_size;
Simon Glassafbf9b82016-07-04 11:58:18 -0600252#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700253 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
Simon Glass39edb952020-12-03 16:55:19 -0700254 ofnode_null(), plat_size, devp);
Walter Lozanoffc41b02020-06-25 01:10:11 -0300255 if (ret)
256 return ret;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300257
258 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700259}
260
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300261int device_reparent(struct udevice *dev, struct udevice *new_parent)
262{
263 struct udevice *pos, *n;
264
265 assert(dev);
266 assert(new_parent);
267
268 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
269 sibling_node) {
270 if (pos->driver != dev->driver)
271 continue;
272
273 list_del(&dev->sibling_node);
274 list_add_tail(&dev->sibling_node, &new_parent->child_head);
275 dev->parent = new_parent;
276
277 break;
278 }
279
280 return 0;
281}
282
Simon Glass825d3f92015-03-25 12:21:53 -0600283static void *alloc_priv(int size, uint flags)
284{
285 void *priv;
286
287 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530288 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600289 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600290 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600291 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600292
293 /*
294 * Ensure that the zero bytes are flushed to memory.
295 * This prevents problems if the driver uses this as
296 * both an input and an output buffer:
297 *
298 * 1. Zeroes written to buffer (here) and sit in the
299 * cache
300 * 2. Driver issues a read command to DMA
301 * 3. CPU runs out of cache space and evicts some cache
302 * data in the buffer, writing zeroes to RAM from
303 * the memset() above
304 * 4. DMA completes
305 * 5. Buffer now has some DMA data and some zeroes
306 * 6. Data being read is now incorrect
307 *
308 * To prevent this, ensure that the cache is clean
309 * within this range at the start. The driver can then
310 * use normal flush-after-write, invalidate-before-read
311 * procedures.
312 *
313 * TODO(sjg@chromium.org): Drop this microblaze
314 * exception.
315 */
316#ifndef CONFIG_MICROBLAZE
317 flush_dcache_range((ulong)priv, (ulong)priv + size);
318#endif
319 }
Simon Glass825d3f92015-03-25 12:21:53 -0600320 } else {
321 priv = calloc(1, size);
322 }
323
324 return priv;
325}
326
Simon Glassad337e62020-12-19 10:40:08 -0700327/**
328 * device_alloc_priv() - Allocate priv/plat data required by the device
329 *
330 * @dev: Device to process
331 * @return 0 if OK, -ENOMEM if out of memory
332 */
333static int device_alloc_priv(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700334{
Simon Glassa626dff2015-03-25 12:21:54 -0600335 const struct driver *drv;
Simon Glass2543b162020-12-22 19:30:29 -0700336 void *ptr;
Simon Glassad337e62020-12-19 10:40:08 -0700337 int size;
Simon Glass44c47892020-04-05 15:38:19 -0600338
Simon Glassdd6ab882014-02-26 15:59:18 -0700339 drv = dev->driver;
340 assert(drv);
341
Bin Meng82cdd782015-08-24 01:14:02 -0700342 /* Allocate private data if requested and not reentered */
Simon Glass2543b162020-12-22 19:30:29 -0700343 if (drv->priv_auto && !dev_get_priv(dev)) {
344 ptr = alloc_priv(drv->priv_auto, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700345 if (!ptr)
346 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700347 dev_set_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700348 }
Simon Glassad337e62020-12-19 10:40:08 -0700349
Bin Meng82cdd782015-08-24 01:14:02 -0700350 /* Allocate private data if requested and not reentered */
Simon Glass8a2b47f2020-12-03 16:55:17 -0700351 size = dev->uclass->uc_drv->per_device_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700352 if (size && !dev_get_uclass_priv(dev)) {
353 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700354 if (!ptr)
355 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700356 dev_set_uclass_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700357 }
358
Simon Glassa8d8f162019-12-29 21:19:18 -0700359 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700360 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700361 size = dev->parent->driver->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700362 if (!size)
Simon Glass8a2b47f2020-12-03 16:55:17 -0700363 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700364 if (size && !dev_get_parent_priv(dev)) {
365 ptr = alloc_priv(size, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700366 if (!ptr)
367 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700368 dev_set_parent_priv(dev, ptr);
Simon Glass60d971b2014-07-23 06:55:20 -0600369 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700370 }
371
Simon Glassad337e62020-12-19 10:40:08 -0700372 return 0;
373}
374
375int device_of_to_plat(struct udevice *dev)
376{
377 const struct driver *drv;
378 int ret;
379
380 if (!dev)
381 return -EINVAL;
382
Simon Glass6211d762020-12-19 10:40:10 -0700383 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
Simon Glassad337e62020-12-19 10:40:08 -0700384 return 0;
385
386 /* Ensure all parents have ofdata */
387 if (dev->parent) {
388 ret = device_of_to_plat(dev->parent);
389 if (ret)
390 goto fail;
391
392 /*
393 * The device might have already been probed during
394 * the call to device_probe() on its parent device
395 * (e.g. PCI bridge devices). Test the flags again
396 * so that we don't mess up the device.
397 */
Simon Glass6211d762020-12-19 10:40:10 -0700398 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
Simon Glassad337e62020-12-19 10:40:08 -0700399 return 0;
400 }
401
402 ret = device_alloc_priv(dev);
403 if (ret)
404 goto fail;
405
406 drv = dev->driver;
407 assert(drv);
408
Simon Glassaad29ae2020-12-03 16:55:21 -0700409 if (drv->of_to_plat &&
Simon Glass07c17772020-12-19 10:40:12 -0700410 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
Simon Glassaad29ae2020-12-03 16:55:21 -0700411 ret = drv->of_to_plat(dev);
Simon Glassa8d8f162019-12-29 21:19:18 -0700412 if (ret)
413 goto fail;
414 }
Simon Glass60d971b2014-07-23 06:55:20 -0600415
Simon Glass6211d762020-12-19 10:40:10 -0700416 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
Simon Glassd3c9d302019-12-29 21:19:21 -0700417
Simon Glass4a263852019-12-29 21:19:20 -0700418 return 0;
419fail:
420 device_free(dev);
421
422 return ret;
423}
424
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100425/**
426 * device_get_dma_constraints() - Populate device's DMA constraints
427 *
428 * Gets a device's DMA constraints from firmware. This information is later
429 * used by drivers to translate physcal addresses to the device's bus address
430 * space. For now only device-tree is supported.
431 *
432 * @dev: Pointer to target device
433 * Return: 0 if OK or if no DMA constraints were found, error otherwise
434 */
435static int device_get_dma_constraints(struct udevice *dev)
436{
437 struct udevice *parent = dev->parent;
438 phys_addr_t cpu = 0;
439 dma_addr_t bus = 0;
440 u64 size = 0;
441 int ret;
442
443 if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
444 return 0;
445
446 /*
447 * We start parsing for dma-ranges from the device's bus node. This is
448 * specially important on nested buses.
449 */
450 ret = dev_get_dma_range(parent, &cpu, &bus, &size);
451 /* Don't return an error if no 'dma-ranges' were found */
452 if (ret && ret != -ENOENT) {
453 dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret);
454 return ret;
455 }
456
457 dev_set_dma_offset(dev, cpu - bus);
458
459 return 0;
460}
461
Simon Glass4a263852019-12-29 21:19:20 -0700462int device_probe(struct udevice *dev)
463{
464 const struct driver *drv;
465 int ret;
Simon Glass4a263852019-12-29 21:19:20 -0700466
467 if (!dev)
468 return -EINVAL;
469
Simon Glass6211d762020-12-19 10:40:10 -0700470 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass4a263852019-12-29 21:19:20 -0700471 return 0;
472
473 drv = dev->driver;
474 assert(drv);
475
Simon Glassaad29ae2020-12-03 16:55:21 -0700476 ret = device_of_to_plat(dev);
Simon Glass4a263852019-12-29 21:19:20 -0700477 if (ret)
478 goto fail;
479
Simon Glassa8d8f162019-12-29 21:19:18 -0700480 /* Ensure all parents are probed */
481 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700482 ret = device_probe(dev->parent);
483 if (ret)
484 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700485
486 /*
487 * The device might have already been probed during
488 * the call to device_probe() on its parent device
489 * (e.g. PCI bridge devices). Test the flags again
490 * so that we don't mess up the device.
491 */
Simon Glass6211d762020-12-19 10:40:10 -0700492 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Bin Meng82cdd782015-08-24 01:14:02 -0700493 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700494 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600495
Simon Glass6211d762020-12-19 10:40:10 -0700496 dev_or_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600497
Simon Glassbb53bda2015-09-12 08:45:19 -0600498 /*
499 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700500 * continue regardless of the result of pinctrl. Don't process pinctrl
501 * settings for pinctrl devices since the device may not yet be
502 * probed.
Simon Glass303c3c22021-01-21 13:57:13 -0700503 *
504 * This call can produce some non-intuitive results. For example, on an
505 * x86 device where dev is the main PCI bus, the pinctrl device may be
506 * child or grandchild of that bus, meaning that the child will be
507 * probed here. If the child happens to be the P2SB and the pinctrl
508 * device is a child of that, then both the pinctrl and P2SB will be
509 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
510 * is set just above. However, the PCI bus' probe() method and
511 * associated uclass methods have not yet been called.
Simon Glassbb53bda2015-09-12 08:45:19 -0600512 */
Simon Glass27ce9622016-01-21 19:43:25 -0700513 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600514 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900515
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200516 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlac10e52b2019-09-27 13:48:12 +0530517 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
518 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fand2201112019-09-17 09:29:22 +0000519 ret = dev_power_domain_on(dev);
520 if (ret)
521 goto fail;
Peng Fan6fba32d2018-07-27 10:20:38 +0800522 }
523
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100524 ret = device_get_dma_constraints(dev);
525 if (ret)
526 goto fail;
527
Simon Glass9c1f3822015-03-05 12:25:22 -0700528 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700529 if (ret)
530 goto fail;
531
Simon Glassd45560d2014-07-23 06:55:21 -0600532 if (dev->parent && dev->parent->driver->child_pre_probe) {
533 ret = dev->parent->driver->child_pre_probe(dev);
534 if (ret)
535 goto fail;
536 }
537
Bin Meng513cf402019-07-05 09:23:16 -0700538 /* Only handle devices that have a valid ofnode */
Simon Glassf1d50f72020-12-19 10:40:13 -0700539 if (dev_has_ofnode(dev)) {
Bin Meng513cf402019-07-05 09:23:16 -0700540 /*
541 * Process 'assigned-{clocks/clock-parents/clock-rates}'
542 * properties
543 */
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200544 ret = clk_set_defaults(dev, 0);
Bin Meng513cf402019-07-05 09:23:16 -0700545 if (ret)
546 goto fail;
547 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100548
Simon Glassdd6ab882014-02-26 15:59:18 -0700549 if (drv->probe) {
550 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700551 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700552 goto fail;
553 }
554
Simon Glassdd6ab882014-02-26 15:59:18 -0700555 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600556 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700557 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700558
Peng Fan7cf58dd2016-03-12 13:17:38 +0800559 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
560 pinctrl_select_state(dev, "default");
561
Simon Glassdd6ab882014-02-26 15:59:18 -0700562 return 0;
563fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100564 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700565 dm_warn("%s: Device '%s' failed to remove on error path\n",
566 __func__, dev->name);
567 }
568fail:
Simon Glass6211d762020-12-19 10:40:10 -0700569 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600570
Simon Glassdd6ab882014-02-26 15:59:18 -0700571 device_free(dev);
572
573 return ret;
574}
575
Simon Glassfa20e932020-12-03 16:55:20 -0700576void *dev_get_plat(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700577{
578 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700579 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700580 return NULL;
581 }
582
Simon Glass541f62c2020-12-22 19:30:30 -0700583 return dev->plat_;
Simon Glassdd6ab882014-02-26 15:59:18 -0700584}
585
Simon Glass71fa5b42020-12-03 16:55:18 -0700586void *dev_get_parent_plat(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700587{
588 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600589 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700590 return NULL;
591 }
592
Simon Glass541f62c2020-12-22 19:30:30 -0700593 return dev->parent_plat_;
Simon Glass11b61732015-01-25 08:27:01 -0700594}
595
Simon Glass71fa5b42020-12-03 16:55:18 -0700596void *dev_get_uclass_plat(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200597{
598 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600599 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200600 return NULL;
601 }
602
Simon Glass541f62c2020-12-22 19:30:30 -0700603 return dev->uclass_plat_;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200604}
605
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600606void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700607{
608 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700609 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700610 return NULL;
611 }
612
Simon Glass541f62c2020-12-22 19:30:30 -0700613 return dev->priv_;
Simon Glassdd6ab882014-02-26 15:59:18 -0700614}
Simon Glass48d4e292014-07-23 06:55:19 -0600615
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600616void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700617{
618 if (!dev) {
619 dm_warn("%s: null device\n", __func__);
620 return NULL;
621 }
622
Simon Glass541f62c2020-12-22 19:30:30 -0700623 return dev->uclass_priv_;
Simon Glassde0977b2015-03-05 12:25:20 -0700624}
625
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600626void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600627{
628 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700629 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600630 return NULL;
631 }
632
Simon Glass541f62c2020-12-22 19:30:30 -0700633 return dev->parent_priv_;
Simon Glass60d971b2014-07-23 06:55:20 -0600634}
635
Simon Glass48d4e292014-07-23 06:55:19 -0600636static int device_get_device_tail(struct udevice *dev, int ret,
637 struct udevice **devp)
638{
639 if (ret)
640 return ret;
641
642 ret = device_probe(dev);
643 if (ret)
644 return ret;
645
646 *devp = dev;
647
648 return 0;
649}
650
Marek Vasut7f18c342019-08-31 18:03:28 +0200651#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mario Six3bedfbe2018-06-26 08:46:50 +0200652/**
653 * device_find_by_ofnode() - Return device associated with given ofnode
654 *
655 * The returned device is *not* activated.
656 *
657 * @node: The ofnode for which a associated device should be looked up
658 * @devp: Pointer to structure to hold the found device
659 * Return: 0 if OK, -ve on error
660 */
661static int device_find_by_ofnode(ofnode node, struct udevice **devp)
662{
663 struct uclass *uc;
664 struct udevice *dev;
665 int ret;
666
Simon Glass784cd9e2020-12-19 10:40:17 -0700667 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +0200668 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
669 &dev);
670 if (!ret || dev) {
671 *devp = dev;
672 return 0;
673 }
674 }
675
676 return -ENODEV;
677}
Marek Vasut7f18c342019-08-31 18:03:28 +0200678#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200679
Simon Glass2a580882020-01-27 08:49:36 -0700680int device_get_child(const struct udevice *parent, int index,
681 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600682{
683 struct udevice *dev;
684
685 list_for_each_entry(dev, &parent->child_head, sibling_node) {
686 if (!index--)
687 return device_get_device_tail(dev, 0, devp);
688 }
689
690 return -ENODEV;
691}
692
Simon Glass2a580882020-01-27 08:49:36 -0700693int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530694{
695 struct udevice *dev;
696 int count = 0;
697
698 list_for_each_entry(dev, &parent->child_head, sibling_node)
699 count++;
700
701 return count;
702}
703
Simon Glass07e13382020-12-16 21:20:29 -0700704int device_find_child_by_seq(const struct udevice *parent, int seq,
705 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600706{
707 struct udevice *dev;
708
709 *devp = NULL;
Simon Glass48d4e292014-07-23 06:55:19 -0600710
711 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glass5e349922020-12-19 10:40:09 -0700712 if (dev->seq_ == seq) {
Simon Glass48d4e292014-07-23 06:55:19 -0600713 *devp = dev;
714 return 0;
715 }
716 }
717
718 return -ENODEV;
719}
720
Simon Glass2a580882020-01-27 08:49:36 -0700721int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600722 struct udevice **devp)
723{
724 struct udevice *dev;
725 int ret;
726
727 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700728 ret = device_find_child_by_seq(parent, seq, &dev);
729
Simon Glass48d4e292014-07-23 06:55:19 -0600730 return device_get_device_tail(dev, ret, devp);
731}
732
Simon Glass2a580882020-01-27 08:49:36 -0700733int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600734 struct udevice **devp)
735{
736 struct udevice *dev;
737
738 *devp = NULL;
739
740 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700741 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600742 *devp = dev;
743 return 0;
744 }
745 }
746
747 return -ENODEV;
748}
749
Simon Glass2a580882020-01-27 08:49:36 -0700750int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600751 struct udevice **devp)
752{
753 struct udevice *dev;
754 int ret;
755
756 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600757 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600758 return device_get_device_tail(dev, ret, devp);
759}
Simon Glass44da7352014-10-13 23:41:49 -0600760
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200761static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
762 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600763{
764 struct udevice *dev, *found;
765
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200766 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600767 return parent;
768
769 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200770 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600771 if (found)
772 return found;
773 }
774
775 return NULL;
776}
777
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200778int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
779{
780 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
781
782 return *devp ? 0 : -ENOENT;
783}
784
785int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600786{
787 struct udevice *dev;
788
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200789 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600790 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
791}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300792
793#if CONFIG_IS_ENABLED(OF_PLATDATA)
794int device_get_by_driver_info(const struct driver_info *info,
795 struct udevice **devp)
796{
Simon Glasscfd6a002020-10-03 11:31:33 -0600797 struct driver_info *info_base =
798 ll_entry_start(struct driver_info, driver_info);
799 int idx = info - info_base;
800 struct driver_rt *drt = gd_dm_driver_rt() + idx;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300801 struct udevice *dev;
802
Simon Glasscfd6a002020-10-03 11:31:33 -0600803 dev = drt->dev;
Simon Glassd825ea72020-10-03 11:31:31 -0600804 *devp = NULL;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300805
806 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
807}
Simon Glass5792f4b2020-10-03 11:31:40 -0600808
809int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
810{
811 struct driver_rt *drt = gd_dm_driver_rt() + idx;
812 struct udevice *dev;
813
814 dev = drt->dev;
815 *devp = NULL;
816
817 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
818}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300819#endif
Simon Glassae2efac2015-06-23 15:38:37 -0600820
Simon Glass2a580882020-01-27 08:49:36 -0700821int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600822{
823 if (list_empty(&parent->child_head)) {
824 *devp = NULL;
825 } else {
826 *devp = list_first_entry(&parent->child_head, struct udevice,
827 sibling_node);
828 }
829
830 return 0;
831}
832
833int device_find_next_child(struct udevice **devp)
834{
835 struct udevice *dev = *devp;
836 struct udevice *parent = dev->parent;
837
838 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
839 *devp = NULL;
840 } else {
841 *devp = list_entry(dev->sibling_node.next, struct udevice,
842 sibling_node);
843 }
844
845 return 0;
846}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700847
Simon Glass2a580882020-01-27 08:49:36 -0700848int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600849 enum uclass_id uclass_id,
850 struct udevice **devp)
851{
852 struct udevice *dev;
853
854 *devp = NULL;
855 list_for_each_entry(dev, &parent->child_head, sibling_node) {
856 if (!device_active(dev) &&
857 device_get_uclass_id(dev) == uclass_id) {
858 *devp = dev;
859 return 0;
860 }
861 }
862
863 return -ENODEV;
864}
865
Simon Glass2a580882020-01-27 08:49:36 -0700866int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700867 enum uclass_id uclass_id,
868 struct udevice **devp)
869{
870 struct udevice *dev;
871
872 *devp = NULL;
873 list_for_each_entry(dev, &parent->child_head, sibling_node) {
874 if (device_get_uclass_id(dev) == uclass_id) {
875 *devp = dev;
876 return 0;
877 }
878 }
879
880 return -ENODEV;
881}
882
Simon Glass2a580882020-01-27 08:49:36 -0700883int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glassa11341a2018-11-18 08:14:31 -0700884 struct udevice **devp)
885{
886 struct udevice *dev;
887
888 *devp = NULL;
889
890 list_for_each_entry(dev, &parent->child_head, sibling_node) {
891 if (!strcmp(dev->name, name)) {
892 *devp = dev;
893 return 0;
894 }
895 }
896
897 return -ENODEV;
898}
899
Simon Glass76db9b72020-01-27 08:49:48 -0700900int device_first_child_err(struct udevice *parent, struct udevice **devp)
901{
902 struct udevice *dev;
903
904 device_find_first_child(parent, &dev);
905 if (!dev)
906 return -ENODEV;
907
908 return device_get_device_tail(dev, 0, devp);
909}
910
911int device_next_child_err(struct udevice **devp)
912{
913 struct udevice *dev = *devp;
914
915 device_find_next_child(&dev);
916 if (!dev)
917 return -ENODEV;
918
919 return device_get_device_tail(dev, 0, devp);
920}
921
Simon Glass000676b2020-01-27 08:49:47 -0700922int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
923{
924 struct udevice *dev;
925 int ret;
926
927 device_find_first_child(parent, &dev);
928 if (!dev)
929 return -ENODEV;
930
Simon Glassaad29ae2020-12-03 16:55:21 -0700931 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700932 if (ret)
933 return ret;
934
935 *devp = dev;
936
937 return 0;
938}
939
940int device_next_child_ofdata_err(struct udevice **devp)
941{
942 struct udevice *dev = *devp;
943 int ret;
944
945 device_find_next_child(&dev);
946 if (!dev)
947 return -ENODEV;
948
Simon Glassaad29ae2020-12-03 16:55:21 -0700949 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700950 if (ret)
951 return ret;
952
953 *devp = dev;
954
955 return 0;
956}
957
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600958struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700959{
960 return child->parent;
961}
962
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600963ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700964{
Simon Glass46227bd2015-03-25 12:21:55 -0600965 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700966}
Simon Glass98fd5d12015-01-25 08:27:04 -0700967
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600968const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200969{
970 if (!dev || !dev->driver->ops)
971 return NULL;
972
973 return dev->driver->ops;
974}
975
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600976enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -0700977{
978 return dev->uclass->uc_drv->id;
979}
Peng Fan99b72352015-02-10 14:46:32 +0800980
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600981const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200982{
983 if (!dev)
984 return NULL;
985
986 return dev->uclass->uc_drv->name;
987}
988
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600989bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600990{
991 return !list_empty(&dev->child_head);
992}
993
Simon Glass2a580882020-01-27 08:49:36 -0700994bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600995{
996 struct udevice *child;
997
998 for (device_find_first_child(dev, &child);
999 child;
1000 device_find_next_child(&child)) {
1001 if (device_active(child))
1002 return true;
1003 }
1004
1005 return false;
1006}
1007
Simon Glass2a580882020-01-27 08:49:36 -07001008bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001009{
1010 struct udevice *parent = dev->parent;
1011
1012 if (!parent)
1013 return false;
1014 return list_is_last(&dev->sibling_node, &parent->child_head);
1015}
Simon Glasse3b23e22015-07-30 13:40:39 -06001016
Simon Glass7760ba22016-05-01 13:52:23 -06001017void device_set_name_alloced(struct udevice *dev)
1018{
Simon Glass6211d762020-12-19 10:40:10 -07001019 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
Simon Glass7760ba22016-05-01 13:52:23 -06001020}
1021
Simon Glasse3b23e22015-07-30 13:40:39 -06001022int device_set_name(struct udevice *dev, const char *name)
1023{
1024 name = strdup(name);
1025 if (!name)
1026 return -ENOMEM;
1027 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -06001028 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -06001029
1030 return 0;
1031}
Mugunthan V N4666bd92016-04-28 15:36:02 +05301032
Simon Glassd4f0c942020-12-22 19:30:27 -07001033void dev_set_priv(struct udevice *dev, void *priv)
1034{
Simon Glass541f62c2020-12-22 19:30:30 -07001035 dev->priv_ = priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001036}
1037
1038void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
1039{
Simon Glass541f62c2020-12-22 19:30:30 -07001040 dev->parent_priv_ = parent_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001041}
1042
1043void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
1044{
Simon Glass541f62c2020-12-22 19:30:30 -07001045 dev->uclass_priv_ = uclass_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001046}
1047
1048void dev_set_plat(struct udevice *dev, void *plat)
1049{
Simon Glass541f62c2020-12-22 19:30:30 -07001050 dev->plat_ = plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001051}
1052
1053void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1054{
Simon Glass541f62c2020-12-22 19:30:30 -07001055 dev->parent_plat_ = parent_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001056}
1057
1058void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1059{
Simon Glass541f62c2020-12-22 19:30:30 -07001060 dev->uclass_plat_ = uclass_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001061}
1062
Marek Vasut7f18c342019-08-31 18:03:28 +02001063#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2a580882020-01-27 08:49:36 -07001064bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +05301065{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001066 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +05301067}
1068
1069bool of_machine_is_compatible(const char *compat)
1070{
1071 const void *fdt = gd->fdt_blob;
1072
1073 return !fdt_node_check_compatible(fdt, 0, compat);
1074}
Mario Six3bedfbe2018-06-26 08:46:50 +02001075
1076int dev_disable_by_path(const char *path)
1077{
1078 struct uclass *uc;
1079 ofnode node = ofnode_path(path);
1080 struct udevice *dev;
1081 int ret = 1;
1082
1083 if (!of_live_active())
1084 return -ENOSYS;
1085
Simon Glass784cd9e2020-12-19 10:40:17 -07001086 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +02001087 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1088 if (!ret)
1089 break;
1090 }
1091
1092 if (ret)
1093 return ret;
1094
1095 ret = device_remove(dev, DM_REMOVE_NORMAL);
1096 if (ret)
1097 return ret;
1098
1099 ret = device_unbind(dev);
1100 if (ret)
1101 return ret;
1102
1103 return ofnode_set_enabled(node, false);
1104}
1105
1106int dev_enable_by_path(const char *path)
1107{
1108 ofnode node = ofnode_path(path);
1109 ofnode pnode = ofnode_get_parent(node);
1110 struct udevice *parent;
1111 int ret = 1;
1112
1113 if (!of_live_active())
1114 return -ENOSYS;
1115
1116 ret = device_find_by_ofnode(pnode, &parent);
1117 if (ret)
1118 return ret;
1119
1120 ret = ofnode_set_enabled(node, true);
1121 if (ret)
1122 return ret;
1123
Bin Meng9a9b0742018-10-10 22:06:58 -07001124 return lists_bind_fdt(parent, node, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001125}
Marek Vasut7f18c342019-08-31 18:03:28 +02001126#endif