]> Git Repo - J-u-boot.git/blame - drivers/core/device.c
common: Drop asm/global_data.h from common header
[J-u-boot.git] / drivers / core / device.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
6494d708
SG
2/*
3 * Device manager
4 *
5 * Copyright (c) 2013 Google, Inc
6 *
7 * (C) Copyright 2012
8 * Pavel Herrmann <[email protected]>
6494d708
SG
9 */
10
11#include <common.h>
1eb69ae4 12#include <cpu_func.h>
f7ae49fc 13#include <log.h>
401d1c4f 14#include <asm/global_data.h>
7c616862 15#include <asm/io.h>
f4fcba5c 16#include <clk.h>
5a66a8ff 17#include <fdtdec.h>
ef5cd330 18#include <fdt_support.h>
6494d708 19#include <malloc.h>
90526e9f 20#include <asm/cache.h>
6494d708
SG
21#include <dm/device.h>
22#include <dm/device-internal.h>
23#include <dm/lists.h>
29d11b88 24#include <dm/of_access.h>
d90a5a30 25#include <dm/pinctrl.h>
6494d708 26#include <dm/platdata.h>
396e343b 27#include <dm/read.h>
6494d708
SG
28#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>
3ad30778 33#include <power-domain.h>
6494d708 34
5a66a8ff
SG
35DECLARE_GLOBAL_DATA_PTR;
36
daac3bfe 37static int device_bind_common(struct udevice *parent, const struct driver *drv,
caa4daa2 38 const char *name, void *plat,
7a61b0b5 39 ulong driver_data, ofnode node,
4f50086a 40 uint of_plat_size, struct udevice **devp)
6494d708 41{
54c5d08a 42 struct udevice *dev;
6494d708 43 struct uclass *uc;
5eaed880 44 int size, ret = 0;
cd53e5bf 45 bool auto_seq = true;
89ba6d55 46 void *ptr;
6494d708 47
e6cabe4a
MY
48 if (devp)
49 *devp = NULL;
6494d708
SG
50 if (!name)
51 return -EINVAL;
52
53 ret = uclass_get(drv->id, &uc);
3346c876
SG
54 if (ret) {
55 debug("Missing uclass for driver %s\n", drv->name);
6494d708 56 return ret;
3346c876 57 }
6494d708 58
54c5d08a 59 dev = calloc(1, sizeof(struct udevice));
6494d708
SG
60 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);
e2282d70 66#ifdef CONFIG_DEVRES
608f26c5 67 INIT_LIST_HEAD(&dev->devres_head);
e2282d70 68#endif
89ba6d55 69 dev_set_plat(dev, plat);
daac3bfe 70 dev->driver_data = driver_data;
6494d708 71 dev->name = name;
f10643cf 72 dev_set_ofnode(dev, node);
6494d708
SG
73 dev->parent = parent;
74 dev->driver = drv;
75 dev->uclass = uc;
5a66a8ff 76
2462139f 77 dev->seq_ = -1;
3542ff29
JJH
78 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
79 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
36fa61dc 80 /*
770eb30e
SR
81 * Some devices, such as a SPI bus, I2C bus and serial ports
82 * are numbered using aliases.
770eb30e 83 */
6d65ac31
SG
84 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
85 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
cd53e5bf 86 if (uc->uc_drv->name && ofnode_valid(node)) {
2462139f 87 if (!dev_read_alias_seq(dev, &dev->seq_))
7f20d1d2 88 auto_seq = false;
cd53e5bf 89 }
9cc36a2b 90 }
5a66a8ff 91 }
15a1196b 92 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
2462139f 93 dev->seq_ = uclass_find_next_free_seq(uc);
36fa61dc 94
caa4daa2
SG
95 if (drv->plat_auto) {
96 bool alloc = !plat;
9fa28190
SG
97
98 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
4f50086a 99 if (of_plat_size) {
73466df3 100 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
4f50086a 101 if (of_plat_size < drv->plat_auto)
9fa28190
SG
102 alloc = true;
103 }
104 }
105 if (alloc) {
73466df3 106 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
89ba6d55
SG
107 ptr = calloc(1, drv->plat_auto);
108 if (!ptr) {
9fa28190
SG
109 ret = -ENOMEM;
110 goto fail_alloc1;
111 }
89ba6d55
SG
112 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
113 memcpy(ptr, plat, of_plat_size);
114 dev_set_plat(dev, ptr);
f8a85449
SG
115 }
116 }
cdc133bd 117
caa4daa2 118 size = uc->uc_drv->per_device_plat_auto;
5eaed880 119 if (size) {
73466df3 120 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
89ba6d55
SG
121 ptr = calloc(1, size);
122 if (!ptr) {
5eaed880
PM
123 ret = -ENOMEM;
124 goto fail_alloc2;
125 }
89ba6d55 126 dev_set_uclass_plat(dev, ptr);
5eaed880
PM
127 }
128
129 if (parent) {
caa4daa2 130 size = parent->driver->per_child_plat_auto;
ba8da9dc 131 if (!size) {
caa4daa2 132 size = parent->uclass->uc_drv->per_child_plat_auto;
ba8da9dc 133 }
cdc133bd 134 if (size) {
73466df3 135 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
89ba6d55
SG
136 ptr = calloc(1, size);
137 if (!ptr) {
cdc133bd 138 ret = -ENOMEM;
5eaed880 139 goto fail_alloc3;
cdc133bd 140 }
89ba6d55 141 dev_set_parent_plat(dev, ptr);
cdc133bd 142 }
f93a07dd 143 /* put dev into parent's successor list */
6494d708 144 list_add_tail(&dev->sibling_node, &parent->child_head);
f93a07dd 145 }
6494d708
SG
146
147 ret = uclass_bind_device(dev);
148 if (ret)
72ebfe86 149 goto fail_uclass_bind;
6494d708
SG
150
151 /* if we fail to bind we remove device from successors and free it */
152 if (drv->bind) {
153 ret = drv->bind(dev);
72ebfe86 154 if (ret)
6494d708 155 goto fail_bind;
6494d708 156 }
0118ce79
SG
157 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 }
20af3c0a
SG
162 if (uc->uc_drv->post_bind) {
163 ret = uc->uc_drv->post_bind(dev);
164 if (ret)
165 goto fail_uclass_post_bind;
166 }
0118ce79 167
6494d708 168 if (parent)
ceb91909 169 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
e6cabe4a
MY
170 if (devp)
171 *devp = dev;
6494d708 172
73466df3 173 dev_or_flags(dev, DM_FLAG_BOUND);
aed1a4dd 174
6494d708
SG
175 return 0;
176
20af3c0a
SG
177fail_uclass_post_bind:
178 /* There is no child unbind() method, so no clean-up required */
0118ce79 179fail_child_post_bind:
0a5804b5 180 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
5a87c417
SG
181 if (drv->unbind && drv->unbind(dev)) {
182 dm_warn("unbind() method failed on dev '%s' on error path\n",
183 dev->name);
184 }
0118ce79
SG
185 }
186
6494d708 187fail_bind:
0a5804b5 188 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
5a87c417
SG
189 if (uclass_unbind_device(dev)) {
190 dm_warn("Failed to unbind dev '%s' on error path\n",
191 dev->name);
192 }
72ebfe86
SG
193 }
194fail_uclass_bind:
0a5804b5 195 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
5a87c417 196 list_del(&dev->sibling_node);
73466df3 197 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
89ba6d55
SG
198 free(dev_get_parent_plat(dev));
199 dev_set_parent_plat(dev, NULL);
5a87c417 200 }
cdc133bd 201 }
5eaed880 202fail_alloc3:
73466df3 203 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
89ba6d55
SG
204 free(dev_get_uclass_plat(dev));
205 dev_set_uclass_plat(dev, NULL);
5eaed880 206 }
cdc133bd 207fail_alloc2:
73466df3 208 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
89ba6d55
SG
209 free(dev_get_plat(dev));
210 dev_set_plat(dev, NULL);
f8a85449
SG
211 }
212fail_alloc1:
608f26c5
MY
213 devres_release_all(dev);
214
6494d708 215 free(dev);
72ebfe86 216
6494d708
SG
217 return ret;
218}
219
daac3bfe
SW
220int device_bind_with_driver_data(struct udevice *parent,
221 const struct driver *drv, const char *name,
396e343b 222 ulong driver_data, ofnode node,
daac3bfe
SW
223 struct udevice **devp)
224{
396e343b
SG
225 return device_bind_common(parent, drv, name, NULL, driver_data, node,
226 0, devp);
daac3bfe
SW
227}
228
734206dd 229int device_bind(struct udevice *parent, const struct driver *drv,
caa4daa2 230 const char *name, void *plat, ofnode node,
734206dd 231 struct udevice **devp)
d677b00c 232{
caa4daa2 233 return device_bind_common(parent, drv, name, plat, 0, node, 0,
d677b00c
SG
234 devp);
235}
236
00606d7e 237int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
a294ead8 238 const struct driver_info *info, struct udevice **devp)
6494d708
SG
239{
240 struct driver *drv;
4f50086a 241 uint plat_size = 0;
fed0f891 242 int ret;
6494d708
SG
243
244 drv = lists_driver_lookup_name(info->name);
245 if (!drv)
246 return -ENOENT;
00606d7e
SG
247 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
248 return -EPERM;
6494d708 249
9fa28190 250#if CONFIG_IS_ENABLED(OF_PLATDATA)
4f50086a 251 plat_size = info->plat_size;
9fa28190 252#endif
caa4daa2 253 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
4f50086a 254 ofnode_null(), plat_size, devp);
fed0f891
WL
255 if (ret)
256 return ret;
fed0f891
WL
257
258 return ret;
6494d708
SG
259}
260
cfecbaf4
CB
261int 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
2c03c463
SG
283static void *alloc_priv(int size, uint flags)
284{
285 void *priv;
286
287 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
5924da1d 288 size = ROUND(size, ARCH_DMA_MINALIGN);
2c03c463 289 priv = memalign(ARCH_DMA_MINALIGN, size);
5a8a8045 290 if (priv) {
2c03c463 291 memset(priv, '\0', size);
5a8a8045
SG
292
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([email protected]): Drop this microblaze
314 * exception.
315 */
316#ifndef CONFIG_MICROBLAZE
317 flush_dcache_range((ulong)priv, (ulong)priv + size);
318#endif
319 }
2c03c463
SG
320 } else {
321 priv = calloc(1, size);
322 }
323
324 return priv;
325}
326
82021e31
SG
327/**
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)
6494d708 334{
3479253d 335 const struct driver *drv;
89ba6d55 336 void *ptr;
82021e31 337 int size;
b0dcc871 338
6494d708
SG
339 drv = dev->driver;
340 assert(drv);
341
cdeb2ba9 342 /* Allocate private data if requested and not reentered */
89ba6d55
SG
343 if (drv->priv_auto && !dev_get_priv(dev)) {
344 ptr = alloc_priv(drv->priv_auto, drv->flags);
82021e31
SG
345 if (!ptr)
346 return -ENOMEM;
89ba6d55 347 dev_set_priv(dev, ptr);
6494d708 348 }
82021e31 349
cdeb2ba9 350 /* Allocate private data if requested and not reentered */
41575d8e 351 size = dev->uclass->uc_drv->per_device_auto;
89ba6d55
SG
352 if (size && !dev_get_uclass_priv(dev)) {
353 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
82021e31
SG
354 if (!ptr)
355 return -ENOMEM;
89ba6d55 356 dev_set_uclass_priv(dev, ptr);
6494d708
SG
357 }
358
82de42fa 359 /* Allocate parent data for this child */
6494d708 360 if (dev->parent) {
41575d8e 361 size = dev->parent->driver->per_child_auto;
89ba6d55 362 if (!size)
41575d8e 363 size = dev->parent->uclass->uc_drv->per_child_auto;
89ba6d55
SG
364 if (size && !dev_get_parent_priv(dev)) {
365 ptr = alloc_priv(size, drv->flags);
82021e31
SG
366 if (!ptr)
367 return -ENOMEM;
89ba6d55 368 dev_set_parent_priv(dev, ptr);
e59f458d 369 }
82de42fa
SG
370 }
371
82021e31
SG
372 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
73466df3 383 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
82021e31
SG
384 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 */
73466df3 398 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
82021e31
SG
399 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
d1998a9f 409 if (drv->of_to_plat &&
c23405f8 410 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
d1998a9f 411 ret = drv->of_to_plat(dev);
82de42fa
SG
412 if (ret)
413 goto fail;
414 }
e59f458d 415
73466df3 416 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
153851dd 417
bcd90cb6
SG
418 return 0;
419fail:
420 device_free(dev);
421
422 return ret;
423}
424
425int device_probe(struct udevice *dev)
426{
427 const struct driver *drv;
428 int ret;
bcd90cb6
SG
429
430 if (!dev)
431 return -EINVAL;
432
73466df3 433 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
bcd90cb6
SG
434 return 0;
435
436 drv = dev->driver;
437 assert(drv);
438
d1998a9f 439 ret = device_of_to_plat(dev);
bcd90cb6
SG
440 if (ret)
441 goto fail;
442
82de42fa
SG
443 /* Ensure all parents are probed */
444 if (dev->parent) {
6494d708
SG
445 ret = device_probe(dev->parent);
446 if (ret)
447 goto fail;
cdeb2ba9
BM
448
449 /*
450 * The device might have already been probed during
451 * the call to device_probe() on its parent device
452 * (e.g. PCI bridge devices). Test the flags again
453 * so that we don't mess up the device.
454 */
73466df3 455 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
cdeb2ba9 456 return 0;
6494d708
SG
457 }
458
73466df3 459 dev_or_flags(dev, DM_FLAG_ACTIVATED);
206d4d2b 460
84d26e29
SG
461 /*
462 * Process pinctrl for everything except the root device, and
0379597e
SG
463 * continue regardless of the result of pinctrl. Don't process pinctrl
464 * settings for pinctrl devices since the device may not yet be
465 * probed.
017d4218
SG
466 *
467 * This call can produce some non-intuitive results. For example, on an
468 * x86 device where dev is the main PCI bus, the pinctrl device may be
469 * child or grandchild of that bus, meaning that the child will be
470 * probed here. If the child happens to be the P2SB and the pinctrl
471 * device is a child of that, then both the pinctrl and P2SB will be
472 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
473 * is set just above. However, the PCI bus' probe() method and
474 * associated uclass methods have not yet been called.
84d26e29 475 */
0379597e 476 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
84d26e29 477 pinctrl_select_state(dev, "default");
d90a5a30 478
44e02e39 479 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
af94ad41
LV
480 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
481 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
f0cc4eae
PF
482 ret = dev_power_domain_on(dev);
483 if (ret)
484 goto fail;
3ad30778
PF
485 }
486
02c07b37 487 ret = uclass_pre_probe_device(dev);
83c7e434
SG
488 if (ret)
489 goto fail;
490
a327dee0
SG
491 if (dev->parent && dev->parent->driver->child_pre_probe) {
492 ret = dev->parent->driver->child_pre_probe(dev);
493 if (ret)
494 goto fail;
495 }
496
a1f99e46 497 /* Only handle devices that have a valid ofnode */
7d14ee44 498 if (dev_has_ofnode(dev)) {
a1f99e46
BM
499 /*
500 * Process 'assigned-{clocks/clock-parents/clock-rates}'
501 * properties
502 */
fd1ba296 503 ret = clk_set_defaults(dev, 0);
a1f99e46
BM
504 if (ret)
505 goto fail;
506 }
f4fcba5c 507
6494d708
SG
508 if (drv->probe) {
509 ret = drv->probe(dev);
a41e6daf 510 if (ret)
6494d708
SG
511 goto fail;
512 }
513
6494d708 514 ret = uclass_post_probe_device(dev);
206d4d2b 515 if (ret)
6494d708 516 goto fail_uclass;
6494d708 517
c3ab9853
PF
518 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
519 pinctrl_select_state(dev, "default");
520
6494d708
SG
521 return 0;
522fail_uclass:
706865af 523 if (device_remove(dev, DM_REMOVE_NORMAL)) {
6494d708
SG
524 dm_warn("%s: Device '%s' failed to remove on error path\n",
525 __func__, dev->name);
526 }
527fail:
73466df3 528 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
206d4d2b 529
6494d708
SG
530 device_free(dev);
531
532 return ret;
533}
534
c69cda25 535void *dev_get_plat(const struct udevice *dev)
6494d708
SG
536{
537 if (!dev) {
964d153c 538 dm_warn("%s: null device\n", __func__);
6494d708
SG
539 return NULL;
540 }
541
fb8c9fb3 542 return dev->plat_;
6494d708
SG
543}
544
caa4daa2 545void *dev_get_parent_plat(const struct udevice *dev)
cdc133bd
SG
546{
547 if (!dev) {
36d7cc17 548 dm_warn("%s: null device\n", __func__);
cdc133bd
SG
549 return NULL;
550 }
551
fb8c9fb3 552 return dev->parent_plat_;
cdc133bd
SG
553}
554
caa4daa2 555void *dev_get_uclass_plat(const struct udevice *dev)
5eaed880
PM
556{
557 if (!dev) {
36d7cc17 558 dm_warn("%s: null device\n", __func__);
5eaed880
PM
559 return NULL;
560 }
561
fb8c9fb3 562 return dev->uclass_plat_;
5eaed880
PM
563}
564
9f15cc14 565void *dev_get_priv(const struct udevice *dev)
6494d708
SG
566{
567 if (!dev) {
964d153c 568 dm_warn("%s: null device\n", __func__);
6494d708
SG
569 return NULL;
570 }
571
fb8c9fb3 572 return dev->priv_;
6494d708 573}
997c87bb 574
9f15cc14 575void *dev_get_uclass_priv(const struct udevice *dev)
e564f054
SG
576{
577 if (!dev) {
578 dm_warn("%s: null device\n", __func__);
579 return NULL;
580 }
581
fb8c9fb3 582 return dev->uclass_priv_;
e564f054
SG
583}
584
9f15cc14 585void *dev_get_parent_priv(const struct udevice *dev)
e59f458d
SG
586{
587 if (!dev) {
964d153c 588 dm_warn("%s: null device\n", __func__);
e59f458d
SG
589 return NULL;
590 }
591
fb8c9fb3 592 return dev->parent_priv_;
e59f458d
SG
593}
594
997c87bb
SG
595static int device_get_device_tail(struct udevice *dev, int ret,
596 struct udevice **devp)
597{
598 if (ret)
599 return ret;
600
601 ret = device_probe(dev);
602 if (ret)
603 return ret;
604
605 *devp = dev;
606
607 return 0;
608}
609
d7677bfc 610#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
e4c98a59
MS
611/**
612 * device_find_by_ofnode() - Return device associated with given ofnode
613 *
614 * The returned device is *not* activated.
615 *
616 * @node: The ofnode for which a associated device should be looked up
617 * @devp: Pointer to structure to hold the found device
618 * Return: 0 if OK, -ve on error
619 */
620static int device_find_by_ofnode(ofnode node, struct udevice **devp)
621{
622 struct uclass *uc;
623 struct udevice *dev;
624 int ret;
625
8a715530 626 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
e4c98a59
MS
627 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
628 &dev);
629 if (!ret || dev) {
630 *devp = dev;
631 return 0;
632 }
633 }
634
635 return -ENODEV;
636}
d7677bfc 637#endif
e4c98a59 638
fc347fbd
SG
639int device_get_child(const struct udevice *parent, int index,
640 struct udevice **devp)
997c87bb
SG
641{
642 struct udevice *dev;
643
644 list_for_each_entry(dev, &parent->child_head, sibling_node) {
645 if (!index--)
646 return device_get_device_tail(dev, 0, devp);
647 }
648
649 return -ENODEV;
650}
651
fc347fbd 652int device_get_child_count(const struct udevice *parent)
240b9320
LV
653{
654 struct udevice *dev;
655 int count = 0;
656
657 list_for_each_entry(dev, &parent->child_head, sibling_node)
658 count++;
659
660 return count;
661}
662
99175919
SG
663int device_find_child_by_seq(const struct udevice *parent, int seq,
664 struct udevice **devp)
997c87bb
SG
665{
666 struct udevice *dev;
667
668 *devp = NULL;
997c87bb
SG
669
670 list_for_each_entry(dev, &parent->child_head, sibling_node) {
2462139f 671 if (dev->seq_ == seq) {
997c87bb
SG
672 *devp = dev;
673 return 0;
674 }
675 }
676
677 return -ENODEV;
678}
679
fc347fbd 680int device_get_child_by_seq(const struct udevice *parent, int seq,
997c87bb
SG
681 struct udevice **devp)
682{
683 struct udevice *dev;
684 int ret;
685
686 *devp = NULL;
99175919
SG
687 ret = device_find_child_by_seq(parent, seq, &dev);
688
997c87bb
SG
689 return device_get_device_tail(dev, ret, devp);
690}
691
fc347fbd 692int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
997c87bb
SG
693 struct udevice **devp)
694{
695 struct udevice *dev;
696
697 *devp = NULL;
698
699 list_for_each_entry(dev, &parent->child_head, sibling_node) {
e160f7d4 700 if (dev_of_offset(dev) == of_offset) {
997c87bb
SG
701 *devp = dev;
702 return 0;
703 }
704 }
705
706 return -ENODEV;
707}
708
fc347fbd 709int device_get_child_by_of_offset(const struct udevice *parent, int node,
997c87bb
SG
710 struct udevice **devp)
711{
712 struct udevice *dev;
713 int ret;
714
715 *devp = NULL;
132f9bfc 716 ret = device_find_child_by_of_offset(parent, node, &dev);
997c87bb
SG
717 return device_get_device_tail(dev, ret, devp);
718}
a8981d4f 719
7ec9181d
JJH
720static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
721 ofnode ofnode)
2693047a
SG
722{
723 struct udevice *dev, *found;
724
7ec9181d 725 if (ofnode_equal(dev_ofnode(parent), ofnode))
2693047a
SG
726 return parent;
727
728 list_for_each_entry(dev, &parent->child_head, sibling_node) {
7ec9181d 729 found = _device_find_global_by_ofnode(dev, ofnode);
2693047a
SG
730 if (found)
731 return found;
732 }
733
734 return NULL;
735}
736
7ec9181d
JJH
737int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
738{
739 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
740
741 return *devp ? 0 : -ENOENT;
742}
743
744int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
2693047a
SG
745{
746 struct udevice *dev;
747
7ec9181d 748 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
2693047a
SG
749 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
750}
751
fed0f891
WL
752#if CONFIG_IS_ENABLED(OF_PLATDATA)
753int device_get_by_driver_info(const struct driver_info *info,
754 struct udevice **devp)
755{
a294ead8
SG
756 struct driver_info *info_base =
757 ll_entry_start(struct driver_info, driver_info);
758 int idx = info - info_base;
759 struct driver_rt *drt = gd_dm_driver_rt() + idx;
fed0f891
WL
760 struct udevice *dev;
761
a294ead8 762 dev = drt->dev;
36af37b9 763 *devp = NULL;
fed0f891
WL
764
765 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
766}
8a38abfc
SG
767
768int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
769{
770 struct driver_rt *drt = gd_dm_driver_rt() + idx;
771 struct udevice *dev;
772
773 dev = drt->dev;
774 *devp = NULL;
775
776 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
777}
fed0f891
WL
778#endif
779
fc347fbd 780int device_find_first_child(const struct udevice *parent, struct udevice **devp)
a8981d4f
SG
781{
782 if (list_empty(&parent->child_head)) {
783 *devp = NULL;
784 } else {
785 *devp = list_first_entry(&parent->child_head, struct udevice,
786 sibling_node);
787 }
788
789 return 0;
790}
791
792int device_find_next_child(struct udevice **devp)
793{
794 struct udevice *dev = *devp;
795 struct udevice *parent = dev->parent;
796
797 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
798 *devp = NULL;
799 } else {
800 *devp = list_entry(dev->sibling_node.next, struct udevice,
801 sibling_node);
802 }
803
804 return 0;
805}
2ef249b4 806
fc347fbd 807int device_find_first_inactive_child(const struct udevice *parent,
cdb6aa0a
SG
808 enum uclass_id uclass_id,
809 struct udevice **devp)
810{
811 struct udevice *dev;
812
813 *devp = NULL;
814 list_for_each_entry(dev, &parent->child_head, sibling_node) {
815 if (!device_active(dev) &&
816 device_get_uclass_id(dev) == uclass_id) {
817 *devp = dev;
818 return 0;
819 }
820 }
821
822 return -ENODEV;
823}
824
fc347fbd 825int device_find_first_child_by_uclass(const struct udevice *parent,
3abe1115
SG
826 enum uclass_id uclass_id,
827 struct udevice **devp)
828{
829 struct udevice *dev;
830
831 *devp = NULL;
832 list_for_each_entry(dev, &parent->child_head, sibling_node) {
833 if (device_get_uclass_id(dev) == uclass_id) {
834 *devp = dev;
835 return 0;
836 }
837 }
838
839 return -ENODEV;
840}
841
fc347fbd 842int device_find_child_by_name(const struct udevice *parent, const char *name,
3abe1115
SG
843 struct udevice **devp)
844{
845 struct udevice *dev;
846
847 *devp = NULL;
848
849 list_for_each_entry(dev, &parent->child_head, sibling_node) {
850 if (!strcmp(dev->name, name)) {
851 *devp = dev;
852 return 0;
853 }
854 }
855
856 return -ENODEV;
857}
858
903e83ee
SG
859int device_first_child_err(struct udevice *parent, struct udevice **devp)
860{
861 struct udevice *dev;
862
863 device_find_first_child(parent, &dev);
864 if (!dev)
865 return -ENODEV;
866
867 return device_get_device_tail(dev, 0, devp);
868}
869
870int device_next_child_err(struct udevice **devp)
871{
872 struct udevice *dev = *devp;
873
874 device_find_next_child(&dev);
875 if (!dev)
876 return -ENODEV;
877
878 return device_get_device_tail(dev, 0, devp);
879}
880
f262d4ca
SG
881int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
882{
883 struct udevice *dev;
884 int ret;
885
886 device_find_first_child(parent, &dev);
887 if (!dev)
888 return -ENODEV;
889
d1998a9f 890 ret = device_of_to_plat(dev);
f262d4ca
SG
891 if (ret)
892 return ret;
893
894 *devp = dev;
895
896 return 0;
897}
898
899int device_next_child_ofdata_err(struct udevice **devp)
900{
901 struct udevice *dev = *devp;
902 int ret;
903
904 device_find_next_child(&dev);
905 if (!dev)
906 return -ENODEV;
907
d1998a9f 908 ret = device_of_to_plat(dev);
f262d4ca
SG
909 if (ret)
910 return ret;
911
912 *devp = dev;
913
914 return 0;
915}
916
9f15cc14 917struct udevice *dev_get_parent(const struct udevice *child)
479728cb
SG
918{
919 return child->parent;
920}
921
9f15cc14 922ulong dev_get_driver_data(const struct udevice *dev)
2ef249b4 923{
39de8433 924 return dev->driver_data;
2ef249b4 925}
b3670531 926
9f15cc14 927const void *dev_get_driver_ops(const struct udevice *dev)
cc73d37b
PM
928{
929 if (!dev || !dev->driver->ops)
930 return NULL;
931
932 return dev->driver->ops;
933}
934
9f15cc14 935enum uclass_id device_get_uclass_id(const struct udevice *dev)
b3670531
SG
936{
937 return dev->uclass->uc_drv->id;
938}
c9cac3f8 939
9f15cc14 940const char *dev_get_uclass_name(const struct udevice *dev)
f9c370dc
PM
941{
942 if (!dev)
943 return NULL;
944
945 return dev->uclass->uc_drv->name;
946}
947
9f15cc14 948bool device_has_children(const struct udevice *dev)
c5785673
SG
949{
950 return !list_empty(&dev->child_head);
951}
952
fc347fbd 953bool device_has_active_children(const struct udevice *dev)
c5785673
SG
954{
955 struct udevice *child;
956
957 for (device_find_first_child(dev, &child);
958 child;
959 device_find_next_child(&child)) {
960 if (device_active(child))
961 return true;
962 }
963
964 return false;
965}
966
fc347fbd 967bool device_is_last_sibling(const struct udevice *dev)
c5785673
SG
968{
969 struct udevice *parent = dev->parent;
970
971 if (!parent)
972 return false;
973 return list_is_last(&dev->sibling_node, &parent->child_head);
974}
f5c67ea0 975
a2040fac
SG
976void device_set_name_alloced(struct udevice *dev)
977{
73466df3 978 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
a2040fac
SG
979}
980
f5c67ea0
SG
981int device_set_name(struct udevice *dev, const char *name)
982{
983 name = strdup(name);
984 if (!name)
985 return -ENOMEM;
986 dev->name = name;
a2040fac 987 device_set_name_alloced(dev);
f5c67ea0
SG
988
989 return 0;
990}
73443b9e 991
12559f5b
SG
992void dev_set_priv(struct udevice *dev, void *priv)
993{
fb8c9fb3 994 dev->priv_ = priv;
12559f5b
SG
995}
996
997void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
998{
fb8c9fb3 999 dev->parent_priv_ = parent_priv;
12559f5b
SG
1000}
1001
1002void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
1003{
fb8c9fb3 1004 dev->uclass_priv_ = uclass_priv;
12559f5b
SG
1005}
1006
1007void dev_set_plat(struct udevice *dev, void *plat)
1008{
fb8c9fb3 1009 dev->plat_ = plat;
12559f5b
SG
1010}
1011
1012void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1013{
fb8c9fb3 1014 dev->parent_plat_ = parent_plat;
12559f5b
SG
1015}
1016
1017void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1018{
fb8c9fb3 1019 dev->uclass_plat_ = uclass_plat;
12559f5b
SG
1020}
1021
d7677bfc 1022#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
fc347fbd 1023bool device_is_compatible(const struct udevice *dev, const char *compat)
73443b9e 1024{
5ccc2c21 1025 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
73443b9e
M
1026}
1027
1028bool of_machine_is_compatible(const char *compat)
1029{
1030 const void *fdt = gd->fdt_blob;
1031
1032 return !fdt_node_check_compatible(fdt, 0, compat);
1033}
e4c98a59
MS
1034
1035int dev_disable_by_path(const char *path)
1036{
1037 struct uclass *uc;
1038 ofnode node = ofnode_path(path);
1039 struct udevice *dev;
1040 int ret = 1;
1041
1042 if (!of_live_active())
1043 return -ENOSYS;
1044
8a715530 1045 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
e4c98a59
MS
1046 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1047 if (!ret)
1048 break;
1049 }
1050
1051 if (ret)
1052 return ret;
1053
1054 ret = device_remove(dev, DM_REMOVE_NORMAL);
1055 if (ret)
1056 return ret;
1057
1058 ret = device_unbind(dev);
1059 if (ret)
1060 return ret;
1061
1062 return ofnode_set_enabled(node, false);
1063}
1064
1065int dev_enable_by_path(const char *path)
1066{
1067 ofnode node = ofnode_path(path);
1068 ofnode pnode = ofnode_get_parent(node);
1069 struct udevice *parent;
1070 int ret = 1;
1071
1072 if (!of_live_active())
1073 return -ENOSYS;
1074
1075 ret = device_find_by_ofnode(pnode, &parent);
1076 if (ret)
1077 return ret;
1078
1079 ret = ofnode_set_enabled(node, true);
1080 if (ret)
1081 return ret;
1082
8d773c4a 1083 return lists_bind_fdt(parent, node, NULL, false);
e4c98a59 1084}
d7677bfc 1085#endif
This page took 0.541526 seconds and 4 git commands to generate.