blob: 8d1287f9a196a9e1b0a3bedd166d39c426f9a164 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6494d702014-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 Glass6494d702014-02-26 15:59:18 -07009 */
10
11#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070012#include <cpu_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Vignesh R7c616862016-07-06 09:58:55 +053014#include <asm/io.h>
Philipp Tomsichf4fcba52018-01-08 13:59:18 +010015#include <clk.h>
Simon Glass5a66a8f2014-07-23 06:55:12 -060016#include <fdtdec.h>
Stefan Roeseef5cd332015-09-02 07:41:12 +020017#include <fdt_support.h>
Simon Glass6494d702014-02-26 15:59:18 -070018#include <malloc.h>
Simon Glass90526e92020-05-10 11:39:56 -060019#include <asm/cache.h>
Simon Glass6494d702014-02-26 15:59:18 -070020#include <dm/device.h>
21#include <dm/device-internal.h>
22#include <dm/lists.h>
Mario Six29d11b82018-01-15 11:07:20 +010023#include <dm/of_access.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090024#include <dm/pinctrl.h>
Simon Glass6494d702014-02-26 15:59:18 -070025#include <dm/platdata.h>
Simon Glass396e3432017-05-18 20:09:05 -060026#include <dm/read.h>
Simon Glass6494d702014-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 Fan3ad30772018-07-27 10:20:38 +080032#include <power-domain.h>
Simon Glass6494d702014-02-26 15:59:18 -070033
Simon Glass5a66a8f2014-07-23 06:55:12 -060034DECLARE_GLOBAL_DATA_PTR;
35
Stephen Warrendaac3bf2016-05-11 15:26:24 -060036static int device_bind_common(struct udevice *parent, const struct driver *drv,
Simon Glasscaa4daa2020-12-03 16:55:18 -070037 const char *name, void *plat,
Simon Glass7a61b0b2017-05-17 17:18:11 -060038 ulong driver_data, ofnode node,
Simon Glass4f500862020-12-03 16:55:19 -070039 uint of_plat_size, struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -070040{
Heiko Schocher54c5d082014-05-22 12:43:05 +020041 struct udevice *dev;
Simon Glass6494d702014-02-26 15:59:18 -070042 struct uclass *uc;
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +020043 int size, ret = 0;
Simon Glasscd53e5b2020-12-16 21:20:09 -070044 bool auto_seq = true;
Simon Glass6494d702014-02-26 15:59:18 -070045
Masahiro Yamadae6cabe42015-08-27 12:44:28 +090046 if (devp)
47 *devp = NULL;
Simon Glass6494d702014-02-26 15:59:18 -070048 if (!name)
49 return -EINVAL;
50
51 ret = uclass_get(drv->id, &uc);
Simon Glass3346c872015-08-30 16:55:16 -060052 if (ret) {
53 debug("Missing uclass for driver %s\n", drv->name);
Simon Glass6494d702014-02-26 15:59:18 -070054 return ret;
Simon Glass3346c872015-08-30 16:55:16 -060055 }
Simon Glass6494d702014-02-26 15:59:18 -070056
Heiko Schocher54c5d082014-05-22 12:43:05 +020057 dev = calloc(1, sizeof(struct udevice));
Simon Glass6494d702014-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 Yamadae2282d72015-07-25 21:52:37 +090064#ifdef CONFIG_DEVRES
Masahiro Yamada608f26c2015-07-25 21:52:35 +090065 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamadae2282d72015-07-25 21:52:37 +090066#endif
Simon Glasscaa4daa2020-12-03 16:55:18 -070067 dev->plat = plat;
Stephen Warrendaac3bf2016-05-11 15:26:24 -060068 dev->driver_data = driver_data;
Simon Glass6494d702014-02-26 15:59:18 -070069 dev->name = name;
Simon Glass7a61b0b2017-05-17 17:18:11 -060070 dev->node = node;
Simon Glass6494d702014-02-26 15:59:18 -070071 dev->parent = parent;
72 dev->driver = drv;
73 dev->uclass = uc;
Simon Glass5a66a8f2014-07-23 06:55:12 -060074
Simon Glass5a66a8f2014-07-23 06:55:12 -060075 dev->seq = -1;
Simon Glass91cbd792014-09-17 09:02:38 -060076 dev->req_seq = -1;
Simon Glasscd53e5b2020-12-16 21:20:09 -070077 dev->sqq = -1;
Jean-Jacques Hiblot3542ff22018-12-07 14:50:39 +010078 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
79 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass36fa61d2015-02-27 22:06:30 -070080 /*
Stefan Roese770eb302016-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 Glass6d65ac32020-07-09 18:39:24 -060087 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
88 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glasscd53e5b2020-12-16 21:20:09 -070089 if (uc->uc_drv->name && ofnode_valid(node)) {
90 dev_read_alias_seq(dev, &dev->sqq);
Simon Glass396e3432017-05-18 20:09:05 -060091 dev_read_alias_seq(dev, &dev->req_seq);
Simon Glasscd53e5b2020-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 Hiblot3542ff22018-12-07 14:50:39 +0100102 } else {
Simon Glassd03adb42020-12-16 21:20:08 -0700103 dev->req_seq = uclass_find_next_free_req_seq(uc);
Simon Glass9cc36a22015-01-25 08:27:05 -0700104 }
105 }
Simon Glasscd53e5b2020-12-16 21:20:09 -0700106 if (auto_seq)
107 dev->sqq = uclass_find_next_free_req_seq(uc);
Simon Glass36fa61d2015-02-27 22:06:30 -0700108
Simon Glasscaa4daa2020-12-03 16:55:18 -0700109 if (drv->plat_auto) {
110 bool alloc = !plat;
Simon Glass9fa28192016-07-04 11:58:18 -0600111
112 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass4f500862020-12-03 16:55:19 -0700113 if (of_plat_size) {
Simon Glass9fa28192016-07-04 11:58:18 -0600114 dev->flags |= DM_FLAG_OF_PLATDATA;
Simon Glass4f500862020-12-03 16:55:19 -0700115 if (of_plat_size < drv->plat_auto)
Simon Glass9fa28192016-07-04 11:58:18 -0600116 alloc = true;
117 }
118 }
119 if (alloc) {
120 dev->flags |= DM_FLAG_ALLOC_PDATA;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700121 dev->plat = calloc(1, drv->plat_auto);
122 if (!dev->plat) {
Simon Glass9fa28192016-07-04 11:58:18 -0600123 ret = -ENOMEM;
124 goto fail_alloc1;
125 }
Simon Glasscaa4daa2020-12-03 16:55:18 -0700126 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat) {
Simon Glass4f500862020-12-03 16:55:19 -0700127 memcpy(dev->plat, plat, of_plat_size);
Simon Glass9fa28192016-07-04 11:58:18 -0600128 }
Simon Glassf8a85442015-01-25 08:27:00 -0700129 }
130 }
Simon Glasscdc133b2015-01-25 08:27:01 -0700131
Simon Glasscaa4daa2020-12-03 16:55:18 -0700132 size = uc->uc_drv->per_device_plat_auto;
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200133 if (size) {
134 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700135 dev->uclass_plat = calloc(1, size);
136 if (!dev->uclass_plat) {
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200137 ret = -ENOMEM;
138 goto fail_alloc2;
139 }
140 }
141
142 if (parent) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700143 size = parent->driver->per_child_plat_auto;
Simon Glassba8da9d2015-01-25 08:27:02 -0700144 if (!size) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700145 size = parent->uclass->uc_drv->per_child_plat_auto;
Simon Glassba8da9d2015-01-25 08:27:02 -0700146 }
Simon Glasscdc133b2015-01-25 08:27:01 -0700147 if (size) {
148 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700149 dev->parent_plat = calloc(1, size);
150 if (!dev->parent_plat) {
Simon Glasscdc133b2015-01-25 08:27:01 -0700151 ret = -ENOMEM;
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200152 goto fail_alloc3;
Simon Glasscdc133b2015-01-25 08:27:01 -0700153 }
154 }
Heinrich Schuchardtf93a07d2020-02-15 21:38:48 +0100155 /* put dev into parent's successor list */
Simon Glass6494d702014-02-26 15:59:18 -0700156 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardtf93a07d2020-02-15 21:38:48 +0100157 }
Simon Glass6494d702014-02-26 15:59:18 -0700158
159 ret = uclass_bind_device(dev);
160 if (ret)
Simon Glass72ebfe82015-01-25 08:26:59 -0700161 goto fail_uclass_bind;
Simon Glass6494d702014-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 Glass72ebfe82015-01-25 08:26:59 -0700166 if (ret)
Simon Glass6494d702014-02-26 15:59:18 -0700167 goto fail_bind;
Simon Glass6494d702014-02-26 15:59:18 -0700168 }
Simon Glass0118ce72015-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 Glass20af3c02016-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 Glass0118ce72015-01-25 08:27:03 -0700179
Simon Glass6494d702014-02-26 15:59:18 -0700180 if (parent)
Masahiro Yamadaceb91902017-09-29 12:31:20 +0900181 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae6cabe42015-08-27 12:44:28 +0900182 if (devp)
183 *devp = dev;
Simon Glass6494d702014-02-26 15:59:18 -0700184
Masahiro Yamadaaed1a4d2015-07-25 21:52:34 +0900185 dev->flags |= DM_FLAG_BOUND;
186
Simon Glass6494d702014-02-26 15:59:18 -0700187 return 0;
188
Simon Glass20af3c02016-01-05 09:30:59 -0700189fail_uclass_post_bind:
190 /* There is no child unbind() method, so no clean-up required */
Simon Glass0118ce72015-01-25 08:27:03 -0700191fail_child_post_bind:
Masahiro Yamada0a5804b2015-08-12 07:31:52 +0900192 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glass5a87c412015-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 Glass0118ce72015-01-25 08:27:03 -0700197 }
198
Simon Glass6494d702014-02-26 15:59:18 -0700199fail_bind:
Masahiro Yamada0a5804b2015-08-12 07:31:52 +0900200 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glass5a87c412015-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 Glass72ebfe82015-01-25 08:26:59 -0700205 }
206fail_uclass_bind:
Masahiro Yamada0a5804b2015-08-12 07:31:52 +0900207 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glass5a87c412015-02-27 22:06:33 -0700208 list_del(&dev->sibling_node);
209 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700210 free(dev->parent_plat);
211 dev->parent_plat = NULL;
Simon Glass5a87c412015-02-27 22:06:33 -0700212 }
Simon Glasscdc133b2015-01-25 08:27:01 -0700213 }
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200214fail_alloc3:
215 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700216 free(dev->uclass_plat);
217 dev->uclass_plat = NULL;
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200218 }
Simon Glasscdc133b2015-01-25 08:27:01 -0700219fail_alloc2:
Simon Glassf8a85442015-01-25 08:27:00 -0700220 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700221 free(dev->plat);
222 dev->plat = NULL;
Simon Glassf8a85442015-01-25 08:27:00 -0700223 }
224fail_alloc1:
Masahiro Yamada608f26c2015-07-25 21:52:35 +0900225 devres_release_all(dev);
226
Simon Glass6494d702014-02-26 15:59:18 -0700227 free(dev);
Simon Glass72ebfe82015-01-25 08:26:59 -0700228
Simon Glass6494d702014-02-26 15:59:18 -0700229 return ret;
230}
231
Stephen Warrendaac3bf2016-05-11 15:26:24 -0600232int device_bind_with_driver_data(struct udevice *parent,
233 const struct driver *drv, const char *name,
Simon Glass396e3432017-05-18 20:09:05 -0600234 ulong driver_data, ofnode node,
Stephen Warrendaac3bf2016-05-11 15:26:24 -0600235 struct udevice **devp)
236{
Simon Glass396e3432017-05-18 20:09:05 -0600237 return device_bind_common(parent, drv, name, NULL, driver_data, node,
238 0, devp);
Stephen Warrendaac3bf2016-05-11 15:26:24 -0600239}
240
Simon Glass734206d2020-11-28 17:50:01 -0700241int device_bind(struct udevice *parent, const struct driver *drv,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700242 const char *name, void *plat, ofnode node,
Simon Glass734206d2020-11-28 17:50:01 -0700243 struct udevice **devp)
Simon Glassd677b002018-06-11 13:07:15 -0600244{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700245 return device_bind_common(parent, drv, name, plat, 0, node, 0,
Simon Glassd677b002018-06-11 13:07:15 -0600246 devp);
247}
248
Simon Glass00606d72014-07-23 06:55:03 -0600249int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Simon Glassa294ead2020-10-03 11:31:33 -0600250 const struct driver_info *info, struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -0700251{
252 struct driver *drv;
Simon Glass4f500862020-12-03 16:55:19 -0700253 uint plat_size = 0;
Walter Lozanofed0f892020-06-25 01:10:11 -0300254 int ret;
Simon Glass6494d702014-02-26 15:59:18 -0700255
256 drv = lists_driver_lookup_name(info->name);
257 if (!drv)
258 return -ENOENT;
Simon Glass00606d72014-07-23 06:55:03 -0600259 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
260 return -EPERM;
Simon Glass6494d702014-02-26 15:59:18 -0700261
Simon Glass9fa28192016-07-04 11:58:18 -0600262#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass4f500862020-12-03 16:55:19 -0700263 plat_size = info->plat_size;
Simon Glass9fa28192016-07-04 11:58:18 -0600264#endif
Simon Glasscaa4daa2020-12-03 16:55:18 -0700265 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
Simon Glass4f500862020-12-03 16:55:19 -0700266 ofnode_null(), plat_size, devp);
Walter Lozanofed0f892020-06-25 01:10:11 -0300267 if (ret)
268 return ret;
Walter Lozanofed0f892020-06-25 01:10:11 -0300269
270 return ret;
Simon Glass6494d702014-02-26 15:59:18 -0700271}
272
Claudiu Bezneacfecbaf2020-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 Glass2c03c462015-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 Abbas5924da12017-09-19 16:53:50 +0530300 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass2c03c462015-03-25 12:21:53 -0600301 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass5a8a8042017-04-04 13:00:19 -0600302 if (priv) {
Simon Glass2c03c462015-03-25 12:21:53 -0600303 memset(priv, '\0', size);
Simon Glass5a8a8042017-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 Glass2c03c462015-03-25 12:21:53 -0600332 } else {
333 priv = calloc(1, size);
334 }
335
336 return priv;
337}
338
Simon Glassd1998a92020-12-03 16:55:21 -0700339int device_of_to_plat(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700340{
Simon Glass34792532015-03-25 12:21:54 -0600341 const struct driver *drv;
Simon Glass6494d702014-02-26 15:59:18 -0700342 int size = 0;
343 int ret;
344
345 if (!dev)
346 return -EINVAL;
347
Simon Glass153851d2019-12-29 21:19:21 -0700348 if (dev->flags & DM_FLAG_PLATDATA_VALID)
Simon Glass6494d702014-02-26 15:59:18 -0700349 return 0;
350
Simon Glassb0dcc872020-04-05 15:38:19 -0600351 /* Ensure all parents have ofdata */
352 if (dev->parent) {
Simon Glassd1998a92020-12-03 16:55:21 -0700353 ret = device_of_to_plat(dev->parent);
Simon Glassb0dcc872020-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 Glass6494d702014-02-26 15:59:18 -0700367 drv = dev->driver;
368 assert(drv);
369
Bin Mengcdeb2ba2015-08-24 01:14:02 -0700370 /* Allocate private data if requested and not reentered */
Simon Glass41575d82020-12-03 16:55:17 -0700371 if (drv->priv_auto && !dev->priv) {
372 dev->priv = alloc_priv(drv->priv_auto, drv->flags);
Simon Glass6494d702014-02-26 15:59:18 -0700373 if (!dev->priv) {
374 ret = -ENOMEM;
375 goto fail;
376 }
377 }
Bin Mengcdeb2ba2015-08-24 01:14:02 -0700378 /* Allocate private data if requested and not reentered */
Simon Glass41575d82020-12-03 16:55:17 -0700379 size = dev->uclass->uc_drv->per_device_auto;
Bin Mengcdeb2ba2015-08-24 01:14:02 -0700380 if (size && !dev->uclass_priv) {
Simon Glassc7a3acc2018-10-01 12:22:05 -0600381 dev->uclass_priv = alloc_priv(size,
382 dev->uclass->uc_drv->flags);
Simon Glass6494d702014-02-26 15:59:18 -0700383 if (!dev->uclass_priv) {
384 ret = -ENOMEM;
385 goto fail;
386 }
387 }
388
Simon Glass82de42f2019-12-29 21:19:18 -0700389 /* Allocate parent data for this child */
Simon Glass6494d702014-02-26 15:59:18 -0700390 if (dev->parent) {
Simon Glass41575d82020-12-03 16:55:17 -0700391 size = dev->parent->driver->per_child_auto;
Simon Glassdac8db22015-01-25 08:27:06 -0700392 if (!size) {
Simon Glass41575d82020-12-03 16:55:17 -0700393 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glassdac8db22015-01-25 08:27:06 -0700394 }
Bin Mengcdeb2ba2015-08-24 01:14:02 -0700395 if (size && !dev->parent_priv) {
Simon Glass2c03c462015-03-25 12:21:53 -0600396 dev->parent_priv = alloc_priv(size, drv->flags);
Simon Glasse59f4582014-07-23 06:55:20 -0600397 if (!dev->parent_priv) {
398 ret = -ENOMEM;
399 goto fail;
400 }
401 }
Simon Glass82de42f2019-12-29 21:19:18 -0700402 }
Simon Glasse59f4582014-07-23 06:55:20 -0600403
Simon Glassd1998a92020-12-03 16:55:21 -0700404 if (drv->of_to_plat &&
Simon Glass82de42f2019-12-29 21:19:18 -0700405 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
Simon Glassd1998a92020-12-03 16:55:21 -0700406 ret = drv->of_to_plat(dev);
Simon Glass82de42f2019-12-29 21:19:18 -0700407 if (ret)
408 goto fail;
409 }
410
Simon Glass153851d2019-12-29 21:19:21 -0700411 dev->flags |= DM_FLAG_PLATDATA_VALID;
412
Simon Glassbcd90cb2019-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;
424 int seq;
425
426 if (!dev)
427 return -EINVAL;
428
429 if (dev->flags & DM_FLAG_ACTIVATED)
430 return 0;
431
432 drv = dev->driver;
433 assert(drv);
434
Simon Glassd1998a92020-12-03 16:55:21 -0700435 ret = device_of_to_plat(dev);
Simon Glassbcd90cb2019-12-29 21:19:20 -0700436 if (ret)
437 goto fail;
438
Simon Glass82de42f2019-12-29 21:19:18 -0700439 /* Ensure all parents are probed */
440 if (dev->parent) {
Simon Glass6494d702014-02-26 15:59:18 -0700441 ret = device_probe(dev->parent);
442 if (ret)
443 goto fail;
Bin Mengcdeb2ba2015-08-24 01:14:02 -0700444
445 /*
446 * The device might have already been probed during
447 * the call to device_probe() on its parent device
448 * (e.g. PCI bridge devices). Test the flags again
449 * so that we don't mess up the device.
450 */
451 if (dev->flags & DM_FLAG_ACTIVATED)
452 return 0;
Simon Glass6494d702014-02-26 15:59:18 -0700453 }
454
Simon Glass5a66a8f2014-07-23 06:55:12 -0600455 seq = uclass_resolve_seq(dev);
456 if (seq < 0) {
457 ret = seq;
458 goto fail;
459 }
460 dev->seq = seq;
461
Simon Glass206d4d22015-03-25 12:21:56 -0600462 dev->flags |= DM_FLAG_ACTIVATED;
463
Simon Glass84d26e22015-09-12 08:45:19 -0600464 /*
465 * Process pinctrl for everything except the root device, and
Simon Glass03795972016-01-21 19:43:25 -0700466 * continue regardless of the result of pinctrl. Don't process pinctrl
467 * settings for pinctrl devices since the device may not yet be
468 * probed.
Simon Glass84d26e22015-09-12 08:45:19 -0600469 */
Simon Glass03795972016-01-21 19:43:25 -0700470 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glass84d26e22015-09-12 08:45:19 -0600471 pinctrl_select_state(dev, "default");
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900472
Anatolij Gustschin44e02e32019-07-14 21:11:01 +0200473 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlaaf94ad42019-09-27 13:48:12 +0530474 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
475 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fanf0cc4ea2019-09-17 09:29:22 +0000476 ret = dev_power_domain_on(dev);
477 if (ret)
478 goto fail;
Peng Fan3ad30772018-07-27 10:20:38 +0800479 }
480
Simon Glass02c07b32015-03-05 12:25:22 -0700481 ret = uclass_pre_probe_device(dev);
Simon Glass83c7e432015-01-25 08:27:10 -0700482 if (ret)
483 goto fail;
484
Simon Glassa327dee2014-07-23 06:55:21 -0600485 if (dev->parent && dev->parent->driver->child_pre_probe) {
486 ret = dev->parent->driver->child_pre_probe(dev);
487 if (ret)
488 goto fail;
489 }
490
Bin Menga1f99e42019-07-05 09:23:16 -0700491 /* Only handle devices that have a valid ofnode */
492 if (dev_of_valid(dev)) {
493 /*
494 * Process 'assigned-{clocks/clock-parents/clock-rates}'
495 * properties
496 */
Jean-Jacques Hiblotfd1ba292019-10-22 14:00:06 +0200497 ret = clk_set_defaults(dev, 0);
Bin Menga1f99e42019-07-05 09:23:16 -0700498 if (ret)
499 goto fail;
500 }
Philipp Tomsichf4fcba52018-01-08 13:59:18 +0100501
Simon Glass6494d702014-02-26 15:59:18 -0700502 if (drv->probe) {
503 ret = drv->probe(dev);
Simon Glassa41e6da2019-12-29 21:19:16 -0700504 if (ret)
Simon Glass6494d702014-02-26 15:59:18 -0700505 goto fail;
506 }
507
Simon Glass6494d702014-02-26 15:59:18 -0700508 ret = uclass_post_probe_device(dev);
Simon Glass206d4d22015-03-25 12:21:56 -0600509 if (ret)
Simon Glass6494d702014-02-26 15:59:18 -0700510 goto fail_uclass;
Simon Glass6494d702014-02-26 15:59:18 -0700511
Peng Fanc3ab9852016-03-12 13:17:38 +0800512 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
513 pinctrl_select_state(dev, "default");
514
Simon Glass6494d702014-02-26 15:59:18 -0700515 return 0;
516fail_uclass:
Stefan Roese706865a2017-03-20 12:51:48 +0100517 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glass6494d702014-02-26 15:59:18 -0700518 dm_warn("%s: Device '%s' failed to remove on error path\n",
519 __func__, dev->name);
520 }
521fail:
Simon Glass206d4d22015-03-25 12:21:56 -0600522 dev->flags &= ~DM_FLAG_ACTIVATED;
523
Simon Glass5a66a8f2014-07-23 06:55:12 -0600524 dev->seq = -1;
Simon Glass6494d702014-02-26 15:59:18 -0700525 device_free(dev);
526
527 return ret;
528}
529
Simon Glassc69cda22020-12-03 16:55:20 -0700530void *dev_get_plat(const struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700531{
532 if (!dev) {
Simon Glass964d1532014-12-10 08:55:56 -0700533 dm_warn("%s: null device\n", __func__);
Simon Glass6494d702014-02-26 15:59:18 -0700534 return NULL;
535 }
536
Simon Glasscaa4daa2020-12-03 16:55:18 -0700537 return dev->plat;
Simon Glass6494d702014-02-26 15:59:18 -0700538}
539
Simon Glasscaa4daa2020-12-03 16:55:18 -0700540void *dev_get_parent_plat(const struct udevice *dev)
Simon Glasscdc133b2015-01-25 08:27:01 -0700541{
542 if (!dev) {
Simon Glass36d7cc12015-07-06 16:47:40 -0600543 dm_warn("%s: null device\n", __func__);
Simon Glasscdc133b2015-01-25 08:27:01 -0700544 return NULL;
545 }
546
Simon Glasscaa4daa2020-12-03 16:55:18 -0700547 return dev->parent_plat;
Simon Glasscdc133b2015-01-25 08:27:01 -0700548}
549
Simon Glasscaa4daa2020-12-03 16:55:18 -0700550void *dev_get_uclass_plat(const struct udevice *dev)
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200551{
552 if (!dev) {
Simon Glass36d7cc12015-07-06 16:47:40 -0600553 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200554 return NULL;
555 }
556
Simon Glasscaa4daa2020-12-03 16:55:18 -0700557 return dev->uclass_plat;
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200558}
559
Simon Glass9f15cc12018-10-01 12:22:06 -0600560void *dev_get_priv(const struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700561{
562 if (!dev) {
Simon Glass964d1532014-12-10 08:55:56 -0700563 dm_warn("%s: null device\n", __func__);
Simon Glass6494d702014-02-26 15:59:18 -0700564 return NULL;
565 }
566
567 return dev->priv;
568}
Simon Glass997c87b2014-07-23 06:55:19 -0600569
Simon Glass9f15cc12018-10-01 12:22:06 -0600570void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glasse564f052015-03-05 12:25:20 -0700571{
572 if (!dev) {
573 dm_warn("%s: null device\n", __func__);
574 return NULL;
575 }
576
577 return dev->uclass_priv;
578}
579
Simon Glass9f15cc12018-10-01 12:22:06 -0600580void *dev_get_parent_priv(const struct udevice *dev)
Simon Glasse59f4582014-07-23 06:55:20 -0600581{
582 if (!dev) {
Simon Glass964d1532014-12-10 08:55:56 -0700583 dm_warn("%s: null device\n", __func__);
Simon Glasse59f4582014-07-23 06:55:20 -0600584 return NULL;
585 }
586
587 return dev->parent_priv;
588}
589
Simon Glass997c87b2014-07-23 06:55:19 -0600590static int device_get_device_tail(struct udevice *dev, int ret,
591 struct udevice **devp)
592{
593 if (ret)
594 return ret;
595
596 ret = device_probe(dev);
597 if (ret)
598 return ret;
599
600 *devp = dev;
601
602 return 0;
603}
604
Marek Vasutd7677bf2019-08-31 18:03:28 +0200605#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mario Sixe4c98a52018-06-26 08:46:50 +0200606/**
607 * device_find_by_ofnode() - Return device associated with given ofnode
608 *
609 * The returned device is *not* activated.
610 *
611 * @node: The ofnode for which a associated device should be looked up
612 * @devp: Pointer to structure to hold the found device
613 * Return: 0 if OK, -ve on error
614 */
615static int device_find_by_ofnode(ofnode node, struct udevice **devp)
616{
617 struct uclass *uc;
618 struct udevice *dev;
619 int ret;
620
621 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
622 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
623 &dev);
624 if (!ret || dev) {
625 *devp = dev;
626 return 0;
627 }
628 }
629
630 return -ENODEV;
631}
Marek Vasutd7677bf2019-08-31 18:03:28 +0200632#endif
Mario Sixe4c98a52018-06-26 08:46:50 +0200633
Simon Glassfc347fb2020-01-27 08:49:36 -0700634int device_get_child(const struct udevice *parent, int index,
635 struct udevice **devp)
Simon Glass997c87b2014-07-23 06:55:19 -0600636{
637 struct udevice *dev;
638
639 list_for_each_entry(dev, &parent->child_head, sibling_node) {
640 if (!index--)
641 return device_get_device_tail(dev, 0, devp);
642 }
643
644 return -ENODEV;
645}
646
Simon Glassfc347fb2020-01-27 08:49:36 -0700647int device_get_child_count(const struct udevice *parent)
Lokesh Vutla240b9322019-09-04 16:01:26 +0530648{
649 struct udevice *dev;
650 int count = 0;
651
652 list_for_each_entry(dev, &parent->child_head, sibling_node)
653 count++;
654
655 return count;
656}
657
Simon Glassfc347fb2020-01-27 08:49:36 -0700658int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
Simon Glass997c87b2014-07-23 06:55:19 -0600659 bool find_req_seq, struct udevice **devp)
660{
661 struct udevice *dev;
662
663 *devp = NULL;
664 if (seq_or_req_seq == -1)
665 return -ENODEV;
666
667 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glass8b85dfc2020-12-16 21:20:07 -0700668 if ((find_req_seq ? dev->req_seq : dev_seq(dev)) ==
Simon Glass997c87b2014-07-23 06:55:19 -0600669 seq_or_req_seq) {
670 *devp = dev;
671 return 0;
672 }
673 }
674
675 return -ENODEV;
676}
677
Simon Glassfc347fb2020-01-27 08:49:36 -0700678int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass997c87b2014-07-23 06:55:19 -0600679 struct udevice **devp)
680{
681 struct udevice *dev;
682 int ret;
683
684 *devp = NULL;
685 ret = device_find_child_by_seq(parent, seq, false, &dev);
686 if (ret == -ENODEV) {
687 /*
688 * We didn't find it in probed devices. See if there is one
689 * that will request this seq if probed.
690 */
691 ret = device_find_child_by_seq(parent, seq, true, &dev);
692 }
693 return device_get_device_tail(dev, ret, devp);
694}
695
Simon Glassfc347fb2020-01-27 08:49:36 -0700696int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass997c87b2014-07-23 06:55:19 -0600697 struct udevice **devp)
698{
699 struct udevice *dev;
700
701 *devp = NULL;
702
703 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glasse160f7d2017-01-17 16:52:55 -0700704 if (dev_of_offset(dev) == of_offset) {
Simon Glass997c87b2014-07-23 06:55:19 -0600705 *devp = dev;
706 return 0;
707 }
708 }
709
710 return -ENODEV;
711}
712
Simon Glassfc347fb2020-01-27 08:49:36 -0700713int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass997c87b2014-07-23 06:55:19 -0600714 struct udevice **devp)
715{
716 struct udevice *dev;
717 int ret;
718
719 *devp = NULL;
Simon Glass132f9bf2015-06-23 15:38:38 -0600720 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass997c87b2014-07-23 06:55:19 -0600721 return device_get_device_tail(dev, ret, devp);
722}
Simon Glassa8981d42014-10-13 23:41:49 -0600723
Jean-Jacques Hiblot7ec91812018-08-09 16:17:44 +0200724static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
725 ofnode ofnode)
Simon Glass26930472015-06-23 15:38:37 -0600726{
727 struct udevice *dev, *found;
728
Jean-Jacques Hiblot7ec91812018-08-09 16:17:44 +0200729 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glass26930472015-06-23 15:38:37 -0600730 return parent;
731
732 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblot7ec91812018-08-09 16:17:44 +0200733 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glass26930472015-06-23 15:38:37 -0600734 if (found)
735 return found;
736 }
737
738 return NULL;
739}
740
Jean-Jacques Hiblot7ec91812018-08-09 16:17:44 +0200741int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
742{
743 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
744
745 return *devp ? 0 : -ENOENT;
746}
747
748int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glass26930472015-06-23 15:38:37 -0600749{
750 struct udevice *dev;
751
Jean-Jacques Hiblot7ec91812018-08-09 16:17:44 +0200752 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glass26930472015-06-23 15:38:37 -0600753 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
754}
755
Walter Lozanofed0f892020-06-25 01:10:11 -0300756#if CONFIG_IS_ENABLED(OF_PLATDATA)
757int device_get_by_driver_info(const struct driver_info *info,
758 struct udevice **devp)
759{
Simon Glassa294ead2020-10-03 11:31:33 -0600760 struct driver_info *info_base =
761 ll_entry_start(struct driver_info, driver_info);
762 int idx = info - info_base;
763 struct driver_rt *drt = gd_dm_driver_rt() + idx;
Walter Lozanofed0f892020-06-25 01:10:11 -0300764 struct udevice *dev;
765
Simon Glassa294ead2020-10-03 11:31:33 -0600766 dev = drt->dev;
Simon Glass36af37b2020-10-03 11:31:31 -0600767 *devp = NULL;
Walter Lozanofed0f892020-06-25 01:10:11 -0300768
769 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
770}
Simon Glass8a38abf2020-10-03 11:31:40 -0600771
772int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
773{
774 struct driver_rt *drt = gd_dm_driver_rt() + idx;
775 struct udevice *dev;
776
777 dev = drt->dev;
778 *devp = NULL;
779
780 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
781}
Walter Lozanofed0f892020-06-25 01:10:11 -0300782#endif
783
Simon Glassfc347fb2020-01-27 08:49:36 -0700784int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glassa8981d42014-10-13 23:41:49 -0600785{
786 if (list_empty(&parent->child_head)) {
787 *devp = NULL;
788 } else {
789 *devp = list_first_entry(&parent->child_head, struct udevice,
790 sibling_node);
791 }
792
793 return 0;
794}
795
796int device_find_next_child(struct udevice **devp)
797{
798 struct udevice *dev = *devp;
799 struct udevice *parent = dev->parent;
800
801 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
802 *devp = NULL;
803 } else {
804 *devp = list_entry(dev->sibling_node.next, struct udevice,
805 sibling_node);
806 }
807
808 return 0;
809}
Simon Glass2ef249b2014-11-11 10:46:18 -0700810
Simon Glassfc347fb2020-01-27 08:49:36 -0700811int device_find_first_inactive_child(const struct udevice *parent,
Simon Glasscdb6aa02018-10-01 12:22:07 -0600812 enum uclass_id uclass_id,
813 struct udevice **devp)
814{
815 struct udevice *dev;
816
817 *devp = NULL;
818 list_for_each_entry(dev, &parent->child_head, sibling_node) {
819 if (!device_active(dev) &&
820 device_get_uclass_id(dev) == uclass_id) {
821 *devp = dev;
822 return 0;
823 }
824 }
825
826 return -ENODEV;
827}
828
Simon Glassfc347fb2020-01-27 08:49:36 -0700829int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glass3abe1112018-11-18 08:14:31 -0700830 enum uclass_id uclass_id,
831 struct udevice **devp)
832{
833 struct udevice *dev;
834
835 *devp = NULL;
836 list_for_each_entry(dev, &parent->child_head, sibling_node) {
837 if (device_get_uclass_id(dev) == uclass_id) {
838 *devp = dev;
839 return 0;
840 }
841 }
842
843 return -ENODEV;
844}
845
Simon Glassfc347fb2020-01-27 08:49:36 -0700846int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glass3abe1112018-11-18 08:14:31 -0700847 struct udevice **devp)
848{
849 struct udevice *dev;
850
851 *devp = NULL;
852
853 list_for_each_entry(dev, &parent->child_head, sibling_node) {
854 if (!strcmp(dev->name, name)) {
855 *devp = dev;
856 return 0;
857 }
858 }
859
860 return -ENODEV;
861}
862
Simon Glass903e83e2020-01-27 08:49:48 -0700863int device_first_child_err(struct udevice *parent, struct udevice **devp)
864{
865 struct udevice *dev;
866
867 device_find_first_child(parent, &dev);
868 if (!dev)
869 return -ENODEV;
870
871 return device_get_device_tail(dev, 0, devp);
872}
873
874int device_next_child_err(struct udevice **devp)
875{
876 struct udevice *dev = *devp;
877
878 device_find_next_child(&dev);
879 if (!dev)
880 return -ENODEV;
881
882 return device_get_device_tail(dev, 0, devp);
883}
884
Simon Glassf262d4c2020-01-27 08:49:47 -0700885int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
886{
887 struct udevice *dev;
888 int ret;
889
890 device_find_first_child(parent, &dev);
891 if (!dev)
892 return -ENODEV;
893
Simon Glassd1998a92020-12-03 16:55:21 -0700894 ret = device_of_to_plat(dev);
Simon Glassf262d4c2020-01-27 08:49:47 -0700895 if (ret)
896 return ret;
897
898 *devp = dev;
899
900 return 0;
901}
902
903int device_next_child_ofdata_err(struct udevice **devp)
904{
905 struct udevice *dev = *devp;
906 int ret;
907
908 device_find_next_child(&dev);
909 if (!dev)
910 return -ENODEV;
911
Simon Glassd1998a92020-12-03 16:55:21 -0700912 ret = device_of_to_plat(dev);
Simon Glassf262d4c2020-01-27 08:49:47 -0700913 if (ret)
914 return ret;
915
916 *devp = dev;
917
918 return 0;
919}
920
Simon Glass9f15cc12018-10-01 12:22:06 -0600921struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass479728c2014-11-11 10:46:19 -0700922{
923 return child->parent;
924}
925
Simon Glass9f15cc12018-10-01 12:22:06 -0600926ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass2ef249b2014-11-11 10:46:18 -0700927{
Simon Glass39de8432015-03-25 12:21:55 -0600928 return dev->driver_data;
Simon Glass2ef249b2014-11-11 10:46:18 -0700929}
Simon Glassb3670532015-01-25 08:27:04 -0700930
Simon Glass9f15cc12018-10-01 12:22:06 -0600931const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakcc73d372015-04-15 13:07:24 +0200932{
933 if (!dev || !dev->driver->ops)
934 return NULL;
935
936 return dev->driver->ops;
937}
938
Simon Glass9f15cc12018-10-01 12:22:06 -0600939enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glassb3670532015-01-25 08:27:04 -0700940{
941 return dev->uclass->uc_drv->id;
942}
Peng Fanc9cac3f2015-02-10 14:46:32 +0800943
Simon Glass9f15cc12018-10-01 12:22:06 -0600944const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczakf9c370d2015-04-15 13:07:25 +0200945{
946 if (!dev)
947 return NULL;
948
949 return dev->uclass->uc_drv->name;
950}
951
Simon Glass9f15cc12018-10-01 12:22:06 -0600952bool device_has_children(const struct udevice *dev)
Simon Glassc5785672015-03-25 12:21:57 -0600953{
954 return !list_empty(&dev->child_head);
955}
956
Simon Glassfc347fb2020-01-27 08:49:36 -0700957bool device_has_active_children(const struct udevice *dev)
Simon Glassc5785672015-03-25 12:21:57 -0600958{
959 struct udevice *child;
960
961 for (device_find_first_child(dev, &child);
962 child;
963 device_find_next_child(&child)) {
964 if (device_active(child))
965 return true;
966 }
967
968 return false;
969}
970
Simon Glassfc347fb2020-01-27 08:49:36 -0700971bool device_is_last_sibling(const struct udevice *dev)
Simon Glassc5785672015-03-25 12:21:57 -0600972{
973 struct udevice *parent = dev->parent;
974
975 if (!parent)
976 return false;
977 return list_is_last(&dev->sibling_node, &parent->child_head);
978}
Simon Glassf5c67ea2015-07-30 13:40:39 -0600979
Simon Glassa2040fa2016-05-01 13:52:23 -0600980void device_set_name_alloced(struct udevice *dev)
981{
Simon Glassfd1c2d92016-07-04 11:58:15 -0600982 dev->flags |= DM_FLAG_NAME_ALLOCED;
Simon Glassa2040fa2016-05-01 13:52:23 -0600983}
984
Simon Glassf5c67ea2015-07-30 13:40:39 -0600985int device_set_name(struct udevice *dev, const char *name)
986{
987 name = strdup(name);
988 if (!name)
989 return -ENOMEM;
990 dev->name = name;
Simon Glassa2040fa2016-05-01 13:52:23 -0600991 device_set_name_alloced(dev);
Simon Glassf5c67ea2015-07-30 13:40:39 -0600992
993 return 0;
994}
Mugunthan V N73443b92016-04-28 15:36:02 +0530995
Marek Vasutd7677bf2019-08-31 18:03:28 +0200996#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassfc347fb2020-01-27 08:49:36 -0700997bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N73443b92016-04-28 15:36:02 +0530998{
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +0900999 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N73443b92016-04-28 15:36:02 +05301000}
1001
1002bool of_machine_is_compatible(const char *compat)
1003{
1004 const void *fdt = gd->fdt_blob;
1005
1006 return !fdt_node_check_compatible(fdt, 0, compat);
1007}
Mario Sixe4c98a52018-06-26 08:46:50 +02001008
1009int dev_disable_by_path(const char *path)
1010{
1011 struct uclass *uc;
1012 ofnode node = ofnode_path(path);
1013 struct udevice *dev;
1014 int ret = 1;
1015
1016 if (!of_live_active())
1017 return -ENOSYS;
1018
1019 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
1020 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1021 if (!ret)
1022 break;
1023 }
1024
1025 if (ret)
1026 return ret;
1027
1028 ret = device_remove(dev, DM_REMOVE_NORMAL);
1029 if (ret)
1030 return ret;
1031
1032 ret = device_unbind(dev);
1033 if (ret)
1034 return ret;
1035
1036 return ofnode_set_enabled(node, false);
1037}
1038
1039int dev_enable_by_path(const char *path)
1040{
1041 ofnode node = ofnode_path(path);
1042 ofnode pnode = ofnode_get_parent(node);
1043 struct udevice *parent;
1044 int ret = 1;
1045
1046 if (!of_live_active())
1047 return -ENOSYS;
1048
1049 ret = device_find_by_ofnode(pnode, &parent);
1050 if (ret)
1051 return ret;
1052
1053 ret = ofnode_set_enabled(node, true);
1054 if (ret)
1055 return ret;
1056
Bin Meng8d773c42018-10-10 22:06:58 -07001057 return lists_bind_fdt(parent, node, NULL, false);
Mario Sixe4c98a52018-06-26 08:46:50 +02001058}
Marek Vasutd7677bf2019-08-31 18:03:28 +02001059#endif