blob: b878a4d4b6931b09310be3474fee0a719c1af2bd [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>
Vignesh R0dff3702016-07-06 09:58:55 +053014#include <asm/io.h>
Philipp Tomsich9cf03b02018-01-08 13:59:18 +010015#include <clk.h>
Simon Glassdb6f0202014-07-23 06:55:12 -060016#include <fdtdec.h>
Stefan Roeseadc09052015-09-02 07:41:12 +020017#include <fdt_support.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070018#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070020#include <dm/device.h>
21#include <dm/device-internal.h>
22#include <dm/lists.h>
Mario Six1f1c1ec2018-01-15 11:07:20 +010023#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090024#include <dm/pinctrl.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070025#include <dm/platdata.h>
Simon Glass322b2902017-05-18 20:09:05 -060026#include <dm/read.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070027#include <dm/uclass.h>
28#include <dm/uclass-internal.h>
29#include <dm/util.h>
30#include <linux/err.h>
31#include <linux/list.h>
Peng Fan6fba32d2018-07-27 10:20:38 +080032#include <power-domain.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070033
Simon Glassdb6f0202014-07-23 06:55:12 -060034DECLARE_GLOBAL_DATA_PTR;
35
Stephen Warren8c93df12016-05-11 15:26:24 -060036static int device_bind_common(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -070037 const char *name, void *plat,
Simon Glassb0cf4b32017-05-17 17:18:11 -060038 ulong driver_data, ofnode node,
Simon Glass39edb952020-12-03 16:55:19 -070039 uint of_plat_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070040{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020041 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070042 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020043 int size, ret = 0;
Simon Glass7272e922020-12-16 21:20:09 -070044 bool auto_seq = true;
Simon Glassdd6ab882014-02-26 15:59:18 -070045
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090046 if (devp)
47 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070048 if (!name)
49 return -EINVAL;
50
51 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060052 if (ret) {
53 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070054 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060055 }
Simon Glassdd6ab882014-02-26 15:59:18 -070056
Heiko Schocherb74fcb42014-05-22 12:43:05 +020057 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070058 if (!dev)
59 return -ENOMEM;
60
61 INIT_LIST_HEAD(&dev->sibling_node);
62 INIT_LIST_HEAD(&dev->child_head);
63 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090064#ifdef CONFIG_DEVRES
Masahiro Yamada8b15b162015-07-25 21:52:35 +090065 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090066#endif
Simon Glass71fa5b42020-12-03 16:55:18 -070067 dev->plat = plat;
Stephen Warren8c93df12016-05-11 15:26:24 -060068 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070069 dev->name = name;
Simon Glassb0cf4b32017-05-17 17:18:11 -060070 dev->node = node;
Simon Glassdd6ab882014-02-26 15:59:18 -070071 dev->parent = parent;
72 dev->driver = drv;
73 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060074
Simon Glassdb6f0202014-07-23 06:55:12 -060075 dev->seq = -1;
Simon Glass0ccb0972015-01-25 08:27:05 -070076 dev->req_seq = -1;
Simon Glass7272e922020-12-16 21:20:09 -070077 dev->sqq = -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.
83 *
84 * This is just a 'requested' sequence, and will be
85 * resolved (and ->seq updated) when the device is probed.
86 */
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)) {
90 dev_read_alias_seq(dev, &dev->sqq);
Simon Glass322b2902017-05-18 20:09:05 -060091 dev_read_alias_seq(dev, &dev->req_seq);
Simon Glass7272e922020-12-16 21:20:09 -070092 auto_seq = false;
93 }
94 if (CONFIG_IS_ENABLED(OF_PRIOR_STAGE)) {
95 if (dev->req_seq == -1) {
96 auto_seq = true;
97 dev->req_seq =
98 uclass_find_next_free_req_seq(
99 uc);
100 }
101 }
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100102 } else {
Simon Glass1ae093f2020-12-16 21:20:08 -0700103 dev->req_seq = uclass_find_next_free_req_seq(uc);
Simon Glass0ccb0972015-01-25 08:27:05 -0700104 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600105 }
Simon Glass3a9d5f12020-12-16 21:20:17 -0700106 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
Simon Glass7272e922020-12-16 21:20:09 -0700107 dev->sqq = uclass_find_next_free_req_seq(uc);
Simon Glass1bd3deb2015-02-27 22:06:30 -0700108
Simon Glass71fa5b42020-12-03 16:55:18 -0700109 if (drv->plat_auto) {
110 bool alloc = !plat;
Simon Glassafbf9b82016-07-04 11:58:18 -0600111
112 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass39edb952020-12-03 16:55:19 -0700113 if (of_plat_size) {
Simon Glassafbf9b82016-07-04 11:58:18 -0600114 dev->flags |= DM_FLAG_OF_PLATDATA;
Simon Glass39edb952020-12-03 16:55:19 -0700115 if (of_plat_size < drv->plat_auto)
Simon Glassafbf9b82016-07-04 11:58:18 -0600116 alloc = true;
117 }
118 }
119 if (alloc) {
120 dev->flags |= DM_FLAG_ALLOC_PDATA;
Simon Glass71fa5b42020-12-03 16:55:18 -0700121 dev->plat = calloc(1, drv->plat_auto);
122 if (!dev->plat) {
Simon Glassafbf9b82016-07-04 11:58:18 -0600123 ret = -ENOMEM;
124 goto fail_alloc1;
125 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700126 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat) {
Simon Glass39edb952020-12-03 16:55:19 -0700127 memcpy(dev->plat, plat, of_plat_size);
Simon Glassafbf9b82016-07-04 11:58:18 -0600128 }
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) {
134 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
Simon Glass71fa5b42020-12-03 16:55:18 -0700135 dev->uclass_plat = calloc(1, size);
136 if (!dev->uclass_plat) {
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200137 ret = -ENOMEM;
138 goto fail_alloc2;
139 }
140 }
Simon Glass11b61732015-01-25 08:27:01 -0700141
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200142 if (parent) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700143 size = parent->driver->per_child_plat_auto;
Simon Glass57f95402015-01-25 08:27:02 -0700144 if (!size) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700145 size = parent->uclass->uc_drv->per_child_plat_auto;
Simon Glass57f95402015-01-25 08:27:02 -0700146 }
Simon Glass11b61732015-01-25 08:27:01 -0700147 if (size) {
148 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
Simon Glass71fa5b42020-12-03 16:55:18 -0700149 dev->parent_plat = calloc(1, size);
150 if (!dev->parent_plat) {
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 }
154 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100155 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700156 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100157 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700158
159 ret = uclass_bind_device(dev);
160 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700161 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700162
163 /* if we fail to bind we remove device from successors and free it */
164 if (drv->bind) {
165 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700166 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700167 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700168 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700169 if (parent && parent->driver->child_post_bind) {
170 ret = parent->driver->child_post_bind(dev);
171 if (ret)
172 goto fail_child_post_bind;
173 }
Simon Glass286863d2016-01-05 09:30:59 -0700174 if (uc->uc_drv->post_bind) {
175 ret = uc->uc_drv->post_bind(dev);
176 if (ret)
177 goto fail_uclass_post_bind;
178 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700179
Simon Glassdd6ab882014-02-26 15:59:18 -0700180 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900181 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900182 if (devp)
183 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700184
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900185 dev->flags |= DM_FLAG_BOUND;
186
Simon Glassdd6ab882014-02-26 15:59:18 -0700187 return 0;
188
Simon Glass286863d2016-01-05 09:30:59 -0700189fail_uclass_post_bind:
190 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700191fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900192 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700193 if (drv->unbind && drv->unbind(dev)) {
194 dm_warn("unbind() method failed on dev '%s' on error path\n",
195 dev->name);
196 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700197 }
198
Simon Glassdd6ab882014-02-26 15:59:18 -0700199fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900200 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700201 if (uclass_unbind_device(dev)) {
202 dm_warn("Failed to unbind dev '%s' on error path\n",
203 dev->name);
204 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700205 }
206fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900207 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700208 list_del(&dev->sibling_node);
209 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700210 free(dev->parent_plat);
211 dev->parent_plat = NULL;
Simon Glasse70a4f42015-02-27 22:06:33 -0700212 }
Simon Glass11b61732015-01-25 08:27:01 -0700213 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200214fail_alloc3:
215 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700216 free(dev->uclass_plat);
217 dev->uclass_plat = NULL;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200218 }
Simon Glass11b61732015-01-25 08:27:01 -0700219fail_alloc2:
Simon Glassba750492015-01-25 08:27:00 -0700220 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700221 free(dev->plat);
222 dev->plat = NULL;
Simon Glassba750492015-01-25 08:27:00 -0700223 }
224fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900225 devres_release_all(dev);
226
Simon Glassdd6ab882014-02-26 15:59:18 -0700227 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700228
Simon Glassdd6ab882014-02-26 15:59:18 -0700229 return ret;
230}
231
Stephen Warren8c93df12016-05-11 15:26:24 -0600232int device_bind_with_driver_data(struct udevice *parent,
233 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600234 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600235 struct udevice **devp)
236{
Simon Glass322b2902017-05-18 20:09:05 -0600237 return device_bind_common(parent, drv, name, NULL, driver_data, node,
238 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600239}
240
Simon Glass884870f2020-11-28 17:50:01 -0700241int device_bind(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -0700242 const char *name, void *plat, ofnode node,
Simon Glass884870f2020-11-28 17:50:01 -0700243 struct udevice **devp)
Simon Glass9eb151e2018-06-11 13:07:15 -0600244{
Simon Glass71fa5b42020-12-03 16:55:18 -0700245 return device_bind_common(parent, drv, name, plat, 0, node, 0,
Simon Glass9eb151e2018-06-11 13:07:15 -0600246 devp);
247}
248
Simon Glassfef72b72014-07-23 06:55:03 -0600249int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Simon Glasscfd6a002020-10-03 11:31:33 -0600250 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700251{
252 struct driver *drv;
Simon Glass39edb952020-12-03 16:55:19 -0700253 uint plat_size = 0;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300254 int ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700255
256 drv = lists_driver_lookup_name(info->name);
257 if (!drv)
258 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600259 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
260 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700261
Simon Glassafbf9b82016-07-04 11:58:18 -0600262#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass39edb952020-12-03 16:55:19 -0700263 plat_size = info->plat_size;
Simon Glassafbf9b82016-07-04 11:58:18 -0600264#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700265 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
Simon Glass39edb952020-12-03 16:55:19 -0700266 ofnode_null(), plat_size, devp);
Walter Lozanoffc41b02020-06-25 01:10:11 -0300267 if (ret)
268 return ret;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300269
270 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700271}
272
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300273int device_reparent(struct udevice *dev, struct udevice *new_parent)
274{
275 struct udevice *pos, *n;
276
277 assert(dev);
278 assert(new_parent);
279
280 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
281 sibling_node) {
282 if (pos->driver != dev->driver)
283 continue;
284
285 list_del(&dev->sibling_node);
286 list_add_tail(&dev->sibling_node, &new_parent->child_head);
287 dev->parent = new_parent;
288
289 break;
290 }
291
292 return 0;
293}
294
Simon Glass825d3f92015-03-25 12:21:53 -0600295static void *alloc_priv(int size, uint flags)
296{
297 void *priv;
298
299 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530300 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600301 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600302 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600303 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600304
305 /*
306 * Ensure that the zero bytes are flushed to memory.
307 * This prevents problems if the driver uses this as
308 * both an input and an output buffer:
309 *
310 * 1. Zeroes written to buffer (here) and sit in the
311 * cache
312 * 2. Driver issues a read command to DMA
313 * 3. CPU runs out of cache space and evicts some cache
314 * data in the buffer, writing zeroes to RAM from
315 * the memset() above
316 * 4. DMA completes
317 * 5. Buffer now has some DMA data and some zeroes
318 * 6. Data being read is now incorrect
319 *
320 * To prevent this, ensure that the cache is clean
321 * within this range at the start. The driver can then
322 * use normal flush-after-write, invalidate-before-read
323 * procedures.
324 *
325 * TODO(sjg@chromium.org): Drop this microblaze
326 * exception.
327 */
328#ifndef CONFIG_MICROBLAZE
329 flush_dcache_range((ulong)priv, (ulong)priv + size);
330#endif
331 }
Simon Glass825d3f92015-03-25 12:21:53 -0600332 } else {
333 priv = calloc(1, size);
334 }
335
336 return priv;
337}
338
Simon Glassaad29ae2020-12-03 16:55:21 -0700339int device_of_to_plat(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700340{
Simon Glassa626dff2015-03-25 12:21:54 -0600341 const struct driver *drv;
Simon Glassdd6ab882014-02-26 15:59:18 -0700342 int size = 0;
343 int ret;
344
345 if (!dev)
346 return -EINVAL;
347
Simon Glassd3c9d302019-12-29 21:19:21 -0700348 if (dev->flags & DM_FLAG_PLATDATA_VALID)
Simon Glassdd6ab882014-02-26 15:59:18 -0700349 return 0;
350
Simon Glass44c47892020-04-05 15:38:19 -0600351 /* Ensure all parents have ofdata */
352 if (dev->parent) {
Simon Glassaad29ae2020-12-03 16:55:21 -0700353 ret = device_of_to_plat(dev->parent);
Simon Glass44c47892020-04-05 15:38:19 -0600354 if (ret)
355 goto fail;
356
357 /*
358 * The device might have already been probed during
359 * the call to device_probe() on its parent device
360 * (e.g. PCI bridge devices). Test the flags again
361 * so that we don't mess up the device.
362 */
363 if (dev->flags & DM_FLAG_PLATDATA_VALID)
364 return 0;
365 }
366
Simon Glassdd6ab882014-02-26 15:59:18 -0700367 drv = dev->driver;
368 assert(drv);
369
Bin Meng82cdd782015-08-24 01:14:02 -0700370 /* Allocate private data if requested and not reentered */
Simon Glass8a2b47f2020-12-03 16:55:17 -0700371 if (drv->priv_auto && !dev->priv) {
372 dev->priv = alloc_priv(drv->priv_auto, drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700373 if (!dev->priv) {
374 ret = -ENOMEM;
375 goto fail;
376 }
377 }
Bin Meng82cdd782015-08-24 01:14:02 -0700378 /* Allocate private data if requested and not reentered */
Simon Glass8a2b47f2020-12-03 16:55:17 -0700379 size = dev->uclass->uc_drv->per_device_auto;
Bin Meng82cdd782015-08-24 01:14:02 -0700380 if (size && !dev->uclass_priv) {
Simon Glass7ebd13d2018-10-01 12:22:05 -0600381 dev->uclass_priv = alloc_priv(size,
382 dev->uclass->uc_drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700383 if (!dev->uclass_priv) {
384 ret = -ENOMEM;
385 goto fail;
386 }
387 }
388
Simon Glassa8d8f162019-12-29 21:19:18 -0700389 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700390 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700391 size = dev->parent->driver->per_child_auto;
Simon Glassc23b4282015-01-25 08:27:06 -0700392 if (!size) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700393 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glassc23b4282015-01-25 08:27:06 -0700394 }
Bin Meng82cdd782015-08-24 01:14:02 -0700395 if (size && !dev->parent_priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600396 dev->parent_priv = alloc_priv(size, drv->flags);
Simon Glass60d971b2014-07-23 06:55:20 -0600397 if (!dev->parent_priv) {
398 ret = -ENOMEM;
399 goto fail;
400 }
401 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700402 }
403
Simon Glassaad29ae2020-12-03 16:55:21 -0700404 if (drv->of_to_plat &&
Simon Glassa8d8f162019-12-29 21:19:18 -0700405 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
Simon Glassaad29ae2020-12-03 16:55:21 -0700406 ret = drv->of_to_plat(dev);
Simon Glassa8d8f162019-12-29 21:19:18 -0700407 if (ret)
408 goto fail;
409 }
Simon Glass60d971b2014-07-23 06:55:20 -0600410
Simon Glassd3c9d302019-12-29 21:19:21 -0700411 dev->flags |= DM_FLAG_PLATDATA_VALID;
412
Simon Glass4a263852019-12-29 21:19:20 -0700413 return 0;
414fail:
415 device_free(dev);
416
417 return ret;
418}
419
420int device_probe(struct udevice *dev)
421{
422 const struct driver *drv;
423 int ret;
Simon Glass4a263852019-12-29 21:19:20 -0700424
425 if (!dev)
426 return -EINVAL;
427
428 if (dev->flags & DM_FLAG_ACTIVATED)
429 return 0;
430
431 drv = dev->driver;
432 assert(drv);
433
Simon Glassaad29ae2020-12-03 16:55:21 -0700434 ret = device_of_to_plat(dev);
Simon Glass4a263852019-12-29 21:19:20 -0700435 if (ret)
436 goto fail;
437
Simon Glassa8d8f162019-12-29 21:19:18 -0700438 /* Ensure all parents are probed */
439 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700440 ret = device_probe(dev->parent);
441 if (ret)
442 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700443
444 /*
445 * The device might have already been probed during
446 * the call to device_probe() on its parent device
447 * (e.g. PCI bridge devices). Test the flags again
448 * so that we don't mess up the device.
449 */
450 if (dev->flags & DM_FLAG_ACTIVATED)
451 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700452 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600453
Simon Glass8b04e732015-03-25 12:21:56 -0600454 dev->flags |= DM_FLAG_ACTIVATED;
455
Simon Glassbb53bda2015-09-12 08:45:19 -0600456 /*
457 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700458 * continue regardless of the result of pinctrl. Don't process pinctrl
459 * settings for pinctrl devices since the device may not yet be
460 * probed.
Simon Glassbb53bda2015-09-12 08:45:19 -0600461 */
Simon Glass27ce9622016-01-21 19:43:25 -0700462 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600463 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900464
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200465 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlac10e52b2019-09-27 13:48:12 +0530466 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
467 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fand2201112019-09-17 09:29:22 +0000468 ret = dev_power_domain_on(dev);
469 if (ret)
470 goto fail;
Peng Fan6fba32d2018-07-27 10:20:38 +0800471 }
472
Simon Glass9c1f3822015-03-05 12:25:22 -0700473 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700474 if (ret)
475 goto fail;
476
Simon Glassd45560d2014-07-23 06:55:21 -0600477 if (dev->parent && dev->parent->driver->child_pre_probe) {
478 ret = dev->parent->driver->child_pre_probe(dev);
479 if (ret)
480 goto fail;
481 }
482
Bin Meng513cf402019-07-05 09:23:16 -0700483 /* Only handle devices that have a valid ofnode */
484 if (dev_of_valid(dev)) {
485 /*
486 * Process 'assigned-{clocks/clock-parents/clock-rates}'
487 * properties
488 */
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200489 ret = clk_set_defaults(dev, 0);
Bin Meng513cf402019-07-05 09:23:16 -0700490 if (ret)
491 goto fail;
492 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100493
Simon Glassdd6ab882014-02-26 15:59:18 -0700494 if (drv->probe) {
495 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700496 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700497 goto fail;
498 }
499
Simon Glassdd6ab882014-02-26 15:59:18 -0700500 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600501 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700502 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700503
Peng Fan7cf58dd2016-03-12 13:17:38 +0800504 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
505 pinctrl_select_state(dev, "default");
506
Simon Glassdd6ab882014-02-26 15:59:18 -0700507 return 0;
508fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100509 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700510 dm_warn("%s: Device '%s' failed to remove on error path\n",
511 __func__, dev->name);
512 }
513fail:
Simon Glass8b04e732015-03-25 12:21:56 -0600514 dev->flags &= ~DM_FLAG_ACTIVATED;
515
Simon Glassdb6f0202014-07-23 06:55:12 -0600516 dev->seq = -1;
Simon Glassdd6ab882014-02-26 15:59:18 -0700517 device_free(dev);
518
519 return ret;
520}
521
Simon Glassfa20e932020-12-03 16:55:20 -0700522void *dev_get_plat(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700523{
524 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700525 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700526 return NULL;
527 }
528
Simon Glass71fa5b42020-12-03 16:55:18 -0700529 return dev->plat;
Simon Glassdd6ab882014-02-26 15:59:18 -0700530}
531
Simon Glass71fa5b42020-12-03 16:55:18 -0700532void *dev_get_parent_plat(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700533{
534 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600535 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700536 return NULL;
537 }
538
Simon Glass71fa5b42020-12-03 16:55:18 -0700539 return dev->parent_plat;
Simon Glass11b61732015-01-25 08:27:01 -0700540}
541
Simon Glass71fa5b42020-12-03 16:55:18 -0700542void *dev_get_uclass_plat(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200543{
544 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600545 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200546 return NULL;
547 }
548
Simon Glass71fa5b42020-12-03 16:55:18 -0700549 return dev->uclass_plat;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200550}
551
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600552void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700553{
554 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700555 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700556 return NULL;
557 }
558
559 return dev->priv;
560}
Simon Glass48d4e292014-07-23 06:55:19 -0600561
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600562void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700563{
564 if (!dev) {
565 dm_warn("%s: null device\n", __func__);
566 return NULL;
567 }
568
569 return dev->uclass_priv;
570}
571
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600572void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600573{
574 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700575 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600576 return NULL;
577 }
578
579 return dev->parent_priv;
580}
581
Simon Glass48d4e292014-07-23 06:55:19 -0600582static int device_get_device_tail(struct udevice *dev, int ret,
583 struct udevice **devp)
584{
585 if (ret)
586 return ret;
587
588 ret = device_probe(dev);
589 if (ret)
590 return ret;
591
592 *devp = dev;
593
594 return 0;
595}
596
Marek Vasut7f18c342019-08-31 18:03:28 +0200597#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mario Six3bedfbe2018-06-26 08:46:50 +0200598/**
599 * device_find_by_ofnode() - Return device associated with given ofnode
600 *
601 * The returned device is *not* activated.
602 *
603 * @node: The ofnode for which a associated device should be looked up
604 * @devp: Pointer to structure to hold the found device
605 * Return: 0 if OK, -ve on error
606 */
607static int device_find_by_ofnode(ofnode node, struct udevice **devp)
608{
609 struct uclass *uc;
610 struct udevice *dev;
611 int ret;
612
613 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
614 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
615 &dev);
616 if (!ret || dev) {
617 *devp = dev;
618 return 0;
619 }
620 }
621
622 return -ENODEV;
623}
Marek Vasut7f18c342019-08-31 18:03:28 +0200624#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200625
Simon Glass2a580882020-01-27 08:49:36 -0700626int device_get_child(const struct udevice *parent, int index,
627 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600628{
629 struct udevice *dev;
630
631 list_for_each_entry(dev, &parent->child_head, sibling_node) {
632 if (!index--)
633 return device_get_device_tail(dev, 0, devp);
634 }
635
636 return -ENODEV;
637}
638
Simon Glass2a580882020-01-27 08:49:36 -0700639int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530640{
641 struct udevice *dev;
642 int count = 0;
643
644 list_for_each_entry(dev, &parent->child_head, sibling_node)
645 count++;
646
647 return count;
648}
649
Simon Glass07e13382020-12-16 21:20:29 -0700650int device_find_child_by_seq(const struct udevice *parent, int seq,
651 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600652{
653 struct udevice *dev;
654
655 *devp = NULL;
Simon Glass48d4e292014-07-23 06:55:19 -0600656
657 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glass07e13382020-12-16 21:20:29 -0700658 if (dev->sqq == seq) {
Simon Glass48d4e292014-07-23 06:55:19 -0600659 *devp = dev;
660 return 0;
661 }
662 }
663
664 return -ENODEV;
665}
666
Simon Glass2a580882020-01-27 08:49:36 -0700667int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600668 struct udevice **devp)
669{
670 struct udevice *dev;
671 int ret;
672
673 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700674 ret = device_find_child_by_seq(parent, seq, &dev);
675
Simon Glass48d4e292014-07-23 06:55:19 -0600676 return device_get_device_tail(dev, ret, devp);
677}
678
Simon Glass2a580882020-01-27 08:49:36 -0700679int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600680 struct udevice **devp)
681{
682 struct udevice *dev;
683
684 *devp = NULL;
685
686 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700687 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600688 *devp = dev;
689 return 0;
690 }
691 }
692
693 return -ENODEV;
694}
695
Simon Glass2a580882020-01-27 08:49:36 -0700696int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600697 struct udevice **devp)
698{
699 struct udevice *dev;
700 int ret;
701
702 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600703 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600704 return device_get_device_tail(dev, ret, devp);
705}
Simon Glass44da7352014-10-13 23:41:49 -0600706
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200707static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
708 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600709{
710 struct udevice *dev, *found;
711
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200712 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600713 return parent;
714
715 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200716 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600717 if (found)
718 return found;
719 }
720
721 return NULL;
722}
723
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200724int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
725{
726 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
727
728 return *devp ? 0 : -ENOENT;
729}
730
731int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600732{
733 struct udevice *dev;
734
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200735 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600736 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
737}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300738
739#if CONFIG_IS_ENABLED(OF_PLATDATA)
740int device_get_by_driver_info(const struct driver_info *info,
741 struct udevice **devp)
742{
Simon Glasscfd6a002020-10-03 11:31:33 -0600743 struct driver_info *info_base =
744 ll_entry_start(struct driver_info, driver_info);
745 int idx = info - info_base;
746 struct driver_rt *drt = gd_dm_driver_rt() + idx;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300747 struct udevice *dev;
748
Simon Glasscfd6a002020-10-03 11:31:33 -0600749 dev = drt->dev;
Simon Glassd825ea72020-10-03 11:31:31 -0600750 *devp = NULL;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300751
752 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
753}
Simon Glass5792f4b2020-10-03 11:31:40 -0600754
755int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
756{
757 struct driver_rt *drt = gd_dm_driver_rt() + idx;
758 struct udevice *dev;
759
760 dev = drt->dev;
761 *devp = NULL;
762
763 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
764}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300765#endif
Simon Glassae2efac2015-06-23 15:38:37 -0600766
Simon Glass2a580882020-01-27 08:49:36 -0700767int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600768{
769 if (list_empty(&parent->child_head)) {
770 *devp = NULL;
771 } else {
772 *devp = list_first_entry(&parent->child_head, struct udevice,
773 sibling_node);
774 }
775
776 return 0;
777}
778
779int device_find_next_child(struct udevice **devp)
780{
781 struct udevice *dev = *devp;
782 struct udevice *parent = dev->parent;
783
784 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
785 *devp = NULL;
786 } else {
787 *devp = list_entry(dev->sibling_node.next, struct udevice,
788 sibling_node);
789 }
790
791 return 0;
792}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700793
Simon Glass2a580882020-01-27 08:49:36 -0700794int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600795 enum uclass_id uclass_id,
796 struct udevice **devp)
797{
798 struct udevice *dev;
799
800 *devp = NULL;
801 list_for_each_entry(dev, &parent->child_head, sibling_node) {
802 if (!device_active(dev) &&
803 device_get_uclass_id(dev) == uclass_id) {
804 *devp = dev;
805 return 0;
806 }
807 }
808
809 return -ENODEV;
810}
811
Simon Glass2a580882020-01-27 08:49:36 -0700812int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700813 enum uclass_id uclass_id,
814 struct udevice **devp)
815{
816 struct udevice *dev;
817
818 *devp = NULL;
819 list_for_each_entry(dev, &parent->child_head, sibling_node) {
820 if (device_get_uclass_id(dev) == uclass_id) {
821 *devp = dev;
822 return 0;
823 }
824 }
825
826 return -ENODEV;
827}
828
Simon Glass2a580882020-01-27 08:49:36 -0700829int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glassa11341a2018-11-18 08:14:31 -0700830 struct udevice **devp)
831{
832 struct udevice *dev;
833
834 *devp = NULL;
835
836 list_for_each_entry(dev, &parent->child_head, sibling_node) {
837 if (!strcmp(dev->name, name)) {
838 *devp = dev;
839 return 0;
840 }
841 }
842
843 return -ENODEV;
844}
845
Simon Glass76db9b72020-01-27 08:49:48 -0700846int device_first_child_err(struct udevice *parent, struct udevice **devp)
847{
848 struct udevice *dev;
849
850 device_find_first_child(parent, &dev);
851 if (!dev)
852 return -ENODEV;
853
854 return device_get_device_tail(dev, 0, devp);
855}
856
857int device_next_child_err(struct udevice **devp)
858{
859 struct udevice *dev = *devp;
860
861 device_find_next_child(&dev);
862 if (!dev)
863 return -ENODEV;
864
865 return device_get_device_tail(dev, 0, devp);
866}
867
Simon Glass000676b2020-01-27 08:49:47 -0700868int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
869{
870 struct udevice *dev;
871 int ret;
872
873 device_find_first_child(parent, &dev);
874 if (!dev)
875 return -ENODEV;
876
Simon Glassaad29ae2020-12-03 16:55:21 -0700877 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700878 if (ret)
879 return ret;
880
881 *devp = dev;
882
883 return 0;
884}
885
886int device_next_child_ofdata_err(struct udevice **devp)
887{
888 struct udevice *dev = *devp;
889 int ret;
890
891 device_find_next_child(&dev);
892 if (!dev)
893 return -ENODEV;
894
Simon Glassaad29ae2020-12-03 16:55:21 -0700895 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700896 if (ret)
897 return ret;
898
899 *devp = dev;
900
901 return 0;
902}
903
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600904struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700905{
906 return child->parent;
907}
908
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600909ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700910{
Simon Glass46227bd2015-03-25 12:21:55 -0600911 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700912}
Simon Glass98fd5d12015-01-25 08:27:04 -0700913
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600914const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200915{
916 if (!dev || !dev->driver->ops)
917 return NULL;
918
919 return dev->driver->ops;
920}
921
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600922enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -0700923{
924 return dev->uclass->uc_drv->id;
925}
Peng Fan99b72352015-02-10 14:46:32 +0800926
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600927const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200928{
929 if (!dev)
930 return NULL;
931
932 return dev->uclass->uc_drv->name;
933}
934
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600935bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600936{
937 return !list_empty(&dev->child_head);
938}
939
Simon Glass2a580882020-01-27 08:49:36 -0700940bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600941{
942 struct udevice *child;
943
944 for (device_find_first_child(dev, &child);
945 child;
946 device_find_next_child(&child)) {
947 if (device_active(child))
948 return true;
949 }
950
951 return false;
952}
953
Simon Glass2a580882020-01-27 08:49:36 -0700954bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600955{
956 struct udevice *parent = dev->parent;
957
958 if (!parent)
959 return false;
960 return list_is_last(&dev->sibling_node, &parent->child_head);
961}
Simon Glasse3b23e22015-07-30 13:40:39 -0600962
Simon Glass7760ba22016-05-01 13:52:23 -0600963void device_set_name_alloced(struct udevice *dev)
964{
Simon Glass2d4c7dc2016-07-04 11:58:15 -0600965 dev->flags |= DM_FLAG_NAME_ALLOCED;
Simon Glass7760ba22016-05-01 13:52:23 -0600966}
967
Simon Glasse3b23e22015-07-30 13:40:39 -0600968int device_set_name(struct udevice *dev, const char *name)
969{
970 name = strdup(name);
971 if (!name)
972 return -ENOMEM;
973 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -0600974 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -0600975
976 return 0;
977}
Mugunthan V N4666bd92016-04-28 15:36:02 +0530978
Marek Vasut7f18c342019-08-31 18:03:28 +0200979#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2a580882020-01-27 08:49:36 -0700980bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +0530981{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900982 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +0530983}
984
985bool of_machine_is_compatible(const char *compat)
986{
987 const void *fdt = gd->fdt_blob;
988
989 return !fdt_node_check_compatible(fdt, 0, compat);
990}
Mario Six3bedfbe2018-06-26 08:46:50 +0200991
992int dev_disable_by_path(const char *path)
993{
994 struct uclass *uc;
995 ofnode node = ofnode_path(path);
996 struct udevice *dev;
997 int ret = 1;
998
999 if (!of_live_active())
1000 return -ENOSYS;
1001
1002 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
1003 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1004 if (!ret)
1005 break;
1006 }
1007
1008 if (ret)
1009 return ret;
1010
1011 ret = device_remove(dev, DM_REMOVE_NORMAL);
1012 if (ret)
1013 return ret;
1014
1015 ret = device_unbind(dev);
1016 if (ret)
1017 return ret;
1018
1019 return ofnode_set_enabled(node, false);
1020}
1021
1022int dev_enable_by_path(const char *path)
1023{
1024 ofnode node = ofnode_path(path);
1025 ofnode pnode = ofnode_get_parent(node);
1026 struct udevice *parent;
1027 int ret = 1;
1028
1029 if (!of_live_active())
1030 return -ENOSYS;
1031
1032 ret = device_find_by_ofnode(pnode, &parent);
1033 if (ret)
1034 return ret;
1035
1036 ret = ofnode_set_enabled(node, true);
1037 if (ret)
1038 return ret;
1039
Bin Meng9a9b0742018-10-10 22:06:58 -07001040 return lists_bind_fdt(parent, node, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001041}
Marek Vasut7f18c342019-08-31 18:03:28 +02001042#endif