dm: core: Use dev_has_ofnode() instead of dev_of_valid()
[platform/kernel/u-boot.git] / drivers / core / device.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Device manager
4  *
5  * Copyright (c) 2013 Google, Inc
6  *
7  * (C) Copyright 2012
8  * Pavel Herrmann <morpheus.ibis@gmail.com>
9  */
10
11 #include <common.h>
12 #include <cpu_func.h>
13 #include <log.h>
14 #include <asm/io.h>
15 #include <clk.h>
16 #include <fdtdec.h>
17 #include <fdt_support.h>
18 #include <malloc.h>
19 #include <asm/cache.h>
20 #include <dm/device.h>
21 #include <dm/device-internal.h>
22 #include <dm/lists.h>
23 #include <dm/of_access.h>
24 #include <dm/pinctrl.h>
25 #include <dm/platdata.h>
26 #include <dm/read.h>
27 #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>
32 #include <power-domain.h>
33
34 DECLARE_GLOBAL_DATA_PTR;
35
36 static int device_bind_common(struct udevice *parent, const struct driver *drv,
37                               const char *name, void *plat,
38                               ulong driver_data, ofnode node,
39                               uint of_plat_size, struct udevice **devp)
40 {
41         struct udevice *dev;
42         struct uclass *uc;
43         int size, ret = 0;
44         bool auto_seq = true;
45         void *ptr;
46
47         if (devp)
48                 *devp = NULL;
49         if (!name)
50                 return -EINVAL;
51
52         ret = uclass_get(drv->id, &uc);
53         if (ret) {
54                 debug("Missing uclass for driver %s\n", drv->name);
55                 return ret;
56         }
57
58         dev = calloc(1, sizeof(struct udevice));
59         if (!dev)
60                 return -ENOMEM;
61
62         INIT_LIST_HEAD(&dev->sibling_node);
63         INIT_LIST_HEAD(&dev->child_head);
64         INIT_LIST_HEAD(&dev->uclass_node);
65 #ifdef CONFIG_DEVRES
66         INIT_LIST_HEAD(&dev->devres_head);
67 #endif
68         dev_set_plat(dev, plat);
69         dev->driver_data = driver_data;
70         dev->name = name;
71         dev->node = node;
72         dev->parent = parent;
73         dev->driver = drv;
74         dev->uclass = uc;
75
76         dev->seq_ = -1;
77         if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
78             (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
79                 /*
80                  * Some devices, such as a SPI bus, I2C bus and serial ports
81                  * are numbered using aliases.
82                  */
83                 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
84                     !CONFIG_IS_ENABLED(OF_PLATDATA)) {
85                         if (uc->uc_drv->name && ofnode_valid(node)) {
86                                 if (!dev_read_alias_seq(dev, &dev->seq_))
87                                         auto_seq = false;
88                         }
89                 }
90         }
91         if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
92                 dev->seq_ = uclass_find_next_free_seq(uc);
93
94         if (drv->plat_auto) {
95                 bool alloc = !plat;
96
97                 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
98                         if (of_plat_size) {
99                                 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
100                                 if (of_plat_size < drv->plat_auto)
101                                         alloc = true;
102                         }
103                 }
104                 if (alloc) {
105                         dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
106                         ptr = calloc(1, drv->plat_auto);
107                         if (!ptr) {
108                                 ret = -ENOMEM;
109                                 goto fail_alloc1;
110                         }
111                         if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
112                                 memcpy(ptr, plat, of_plat_size);
113                         dev_set_plat(dev, ptr);
114                 }
115         }
116
117         size = uc->uc_drv->per_device_plat_auto;
118         if (size) {
119                 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
120                 ptr = calloc(1, size);
121                 if (!ptr) {
122                         ret = -ENOMEM;
123                         goto fail_alloc2;
124                 }
125                 dev_set_uclass_plat(dev, ptr);
126         }
127
128         if (parent) {
129                 size = parent->driver->per_child_plat_auto;
130                 if (!size) {
131                         size = parent->uclass->uc_drv->per_child_plat_auto;
132                 }
133                 if (size) {
134                         dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
135                         ptr = calloc(1, size);
136                         if (!ptr) {
137                                 ret = -ENOMEM;
138                                 goto fail_alloc3;
139                         }
140                         dev_set_parent_plat(dev, ptr);
141                 }
142                 /* put dev into parent's successor list */
143                 list_add_tail(&dev->sibling_node, &parent->child_head);
144         }
145
146         ret = uclass_bind_device(dev);
147         if (ret)
148                 goto fail_uclass_bind;
149
150         /* if we fail to bind we remove device from successors and free it */
151         if (drv->bind) {
152                 ret = drv->bind(dev);
153                 if (ret)
154                         goto fail_bind;
155         }
156         if (parent && parent->driver->child_post_bind) {
157                 ret = parent->driver->child_post_bind(dev);
158                 if (ret)
159                         goto fail_child_post_bind;
160         }
161         if (uc->uc_drv->post_bind) {
162                 ret = uc->uc_drv->post_bind(dev);
163                 if (ret)
164                         goto fail_uclass_post_bind;
165         }
166
167         if (parent)
168                 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
169         if (devp)
170                 *devp = dev;
171
172         dev_or_flags(dev, DM_FLAG_BOUND);
173
174         return 0;
175
176 fail_uclass_post_bind:
177         /* There is no child unbind() method, so no clean-up required */
178 fail_child_post_bind:
179         if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
180                 if (drv->unbind && drv->unbind(dev)) {
181                         dm_warn("unbind() method failed on dev '%s' on error path\n",
182                                 dev->name);
183                 }
184         }
185
186 fail_bind:
187         if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
188                 if (uclass_unbind_device(dev)) {
189                         dm_warn("Failed to unbind dev '%s' on error path\n",
190                                 dev->name);
191                 }
192         }
193 fail_uclass_bind:
194         if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
195                 list_del(&dev->sibling_node);
196                 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
197                         free(dev_get_parent_plat(dev));
198                         dev_set_parent_plat(dev, NULL);
199                 }
200         }
201 fail_alloc3:
202         if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
203                 free(dev_get_uclass_plat(dev));
204                 dev_set_uclass_plat(dev, NULL);
205         }
206 fail_alloc2:
207         if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
208                 free(dev_get_plat(dev));
209                 dev_set_plat(dev, NULL);
210         }
211 fail_alloc1:
212         devres_release_all(dev);
213
214         free(dev);
215
216         return ret;
217 }
218
219 int device_bind_with_driver_data(struct udevice *parent,
220                                  const struct driver *drv, const char *name,
221                                  ulong driver_data, ofnode node,
222                                  struct udevice **devp)
223 {
224         return device_bind_common(parent, drv, name, NULL, driver_data, node,
225                                   0, devp);
226 }
227
228 int device_bind(struct udevice *parent, const struct driver *drv,
229                 const char *name, void *plat, ofnode node,
230                 struct udevice **devp)
231 {
232         return device_bind_common(parent, drv, name, plat, 0, node, 0,
233                                   devp);
234 }
235
236 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
237                         const struct driver_info *info, struct udevice **devp)
238 {
239         struct driver *drv;
240         uint plat_size = 0;
241         int ret;
242
243         drv = lists_driver_lookup_name(info->name);
244         if (!drv)
245                 return -ENOENT;
246         if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
247                 return -EPERM;
248
249 #if CONFIG_IS_ENABLED(OF_PLATDATA)
250         plat_size = info->plat_size;
251 #endif
252         ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
253                                  ofnode_null(), plat_size, devp);
254         if (ret)
255                 return ret;
256
257         return ret;
258 }
259
260 int device_reparent(struct udevice *dev, struct udevice *new_parent)
261 {
262         struct udevice *pos, *n;
263
264         assert(dev);
265         assert(new_parent);
266
267         list_for_each_entry_safe(pos, n, &dev->parent->child_head,
268                                  sibling_node) {
269                 if (pos->driver != dev->driver)
270                         continue;
271
272                 list_del(&dev->sibling_node);
273                 list_add_tail(&dev->sibling_node, &new_parent->child_head);
274                 dev->parent = new_parent;
275
276                 break;
277         }
278
279         return 0;
280 }
281
282 static void *alloc_priv(int size, uint flags)
283 {
284         void *priv;
285
286         if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
287                 size = ROUND(size, ARCH_DMA_MINALIGN);
288                 priv = memalign(ARCH_DMA_MINALIGN, size);
289                 if (priv) {
290                         memset(priv, '\0', size);
291
292                         /*
293                          * Ensure that the zero bytes are flushed to memory.
294                          * This prevents problems if the driver uses this as
295                          * both an input and an output buffer:
296                          *
297                          * 1. Zeroes written to buffer (here) and sit in the
298                          *      cache
299                          * 2. Driver issues a read command to DMA
300                          * 3. CPU runs out of cache space and evicts some cache
301                          *      data in the buffer, writing zeroes to RAM from
302                          *      the memset() above
303                          * 4. DMA completes
304                          * 5. Buffer now has some DMA data and some zeroes
305                          * 6. Data being read is now incorrect
306                          *
307                          * To prevent this, ensure that the cache is clean
308                          * within this range at the start. The driver can then
309                          * use normal flush-after-write, invalidate-before-read
310                          * procedures.
311                          *
312                          * TODO(sjg@chromium.org): Drop this microblaze
313                          * exception.
314                          */
315 #ifndef CONFIG_MICROBLAZE
316                         flush_dcache_range((ulong)priv, (ulong)priv + size);
317 #endif
318                 }
319         } else {
320                 priv = calloc(1, size);
321         }
322
323         return priv;
324 }
325
326 /**
327  * device_alloc_priv() - Allocate priv/plat data required by the device
328  *
329  * @dev: Device to process
330  * @return 0 if OK, -ENOMEM if out of memory
331  */
332 static int device_alloc_priv(struct udevice *dev)
333 {
334         const struct driver *drv;
335         void *ptr;
336         int size;
337
338         drv = dev->driver;
339         assert(drv);
340
341         /* Allocate private data if requested and not reentered */
342         if (drv->priv_auto && !dev_get_priv(dev)) {
343                 ptr = alloc_priv(drv->priv_auto, drv->flags);
344                 if (!ptr)
345                         return -ENOMEM;
346                 dev_set_priv(dev, ptr);
347         }
348
349         /* Allocate private data if requested and not reentered */
350         size = dev->uclass->uc_drv->per_device_auto;
351         if (size && !dev_get_uclass_priv(dev)) {
352                 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
353                 if (!ptr)
354                         return -ENOMEM;
355                 dev_set_uclass_priv(dev, ptr);
356         }
357
358         /* Allocate parent data for this child */
359         if (dev->parent) {
360                 size = dev->parent->driver->per_child_auto;
361                 if (!size)
362                         size = dev->parent->uclass->uc_drv->per_child_auto;
363                 if (size && !dev_get_parent_priv(dev)) {
364                         ptr = alloc_priv(size, drv->flags);
365                         if (!ptr)
366                                 return -ENOMEM;
367                         dev_set_parent_priv(dev, ptr);
368                 }
369         }
370
371         return 0;
372 }
373
374 int device_of_to_plat(struct udevice *dev)
375 {
376         const struct driver *drv;
377         int ret;
378
379         if (!dev)
380                 return -EINVAL;
381
382         if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
383                 return 0;
384
385         /* Ensure all parents have ofdata */
386         if (dev->parent) {
387                 ret = device_of_to_plat(dev->parent);
388                 if (ret)
389                         goto fail;
390
391                 /*
392                  * The device might have already been probed during
393                  * the call to device_probe() on its parent device
394                  * (e.g. PCI bridge devices). Test the flags again
395                  * so that we don't mess up the device.
396                  */
397                 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
398                         return 0;
399         }
400
401         ret = device_alloc_priv(dev);
402         if (ret)
403                 goto fail;
404
405         drv = dev->driver;
406         assert(drv);
407
408         if (drv->of_to_plat &&
409             (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
410                 ret = drv->of_to_plat(dev);
411                 if (ret)
412                         goto fail;
413         }
414
415         dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
416
417         return 0;
418 fail:
419         device_free(dev);
420
421         return ret;
422 }
423
424 int device_probe(struct udevice *dev)
425 {
426         const struct driver *drv;
427         int ret;
428
429         if (!dev)
430                 return -EINVAL;
431
432         if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
433                 return 0;
434
435         drv = dev->driver;
436         assert(drv);
437
438         ret = device_of_to_plat(dev);
439         if (ret)
440                 goto fail;
441
442         /* Ensure all parents are probed */
443         if (dev->parent) {
444                 ret = device_probe(dev->parent);
445                 if (ret)
446                         goto fail;
447
448                 /*
449                  * The device might have already been probed during
450                  * the call to device_probe() on its parent device
451                  * (e.g. PCI bridge devices). Test the flags again
452                  * so that we don't mess up the device.
453                  */
454                 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
455                         return 0;
456         }
457
458         dev_or_flags(dev, DM_FLAG_ACTIVATED);
459
460         /*
461          * Process pinctrl for everything except the root device, and
462          * continue regardless of the result of pinctrl. Don't process pinctrl
463          * settings for pinctrl devices since the device may not yet be
464          * probed.
465          */
466         if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
467                 pinctrl_select_state(dev, "default");
468
469         if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
470             (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
471             !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
472                 ret = dev_power_domain_on(dev);
473                 if (ret)
474                         goto fail;
475         }
476
477         ret = uclass_pre_probe_device(dev);
478         if (ret)
479                 goto fail;
480
481         if (dev->parent && dev->parent->driver->child_pre_probe) {
482                 ret = dev->parent->driver->child_pre_probe(dev);
483                 if (ret)
484                         goto fail;
485         }
486
487         /* Only handle devices that have a valid ofnode */
488         if (dev_has_ofnode(dev)) {
489                 /*
490                  * Process 'assigned-{clocks/clock-parents/clock-rates}'
491                  * properties
492                  */
493                 ret = clk_set_defaults(dev, 0);
494                 if (ret)
495                         goto fail;
496         }
497
498         if (drv->probe) {
499                 ret = drv->probe(dev);
500                 if (ret)
501                         goto fail;
502         }
503
504         ret = uclass_post_probe_device(dev);
505         if (ret)
506                 goto fail_uclass;
507
508         if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
509                 pinctrl_select_state(dev, "default");
510
511         return 0;
512 fail_uclass:
513         if (device_remove(dev, DM_REMOVE_NORMAL)) {
514                 dm_warn("%s: Device '%s' failed to remove on error path\n",
515                         __func__, dev->name);
516         }
517 fail:
518         dev_bic_flags(dev, DM_FLAG_ACTIVATED);
519
520         device_free(dev);
521
522         return ret;
523 }
524
525 void *dev_get_plat(const struct udevice *dev)
526 {
527         if (!dev) {
528                 dm_warn("%s: null device\n", __func__);
529                 return NULL;
530         }
531
532         return dev->plat_;
533 }
534
535 void *dev_get_parent_plat(const struct udevice *dev)
536 {
537         if (!dev) {
538                 dm_warn("%s: null device\n", __func__);
539                 return NULL;
540         }
541
542         return dev->parent_plat_;
543 }
544
545 void *dev_get_uclass_plat(const struct udevice *dev)
546 {
547         if (!dev) {
548                 dm_warn("%s: null device\n", __func__);
549                 return NULL;
550         }
551
552         return dev->uclass_plat_;
553 }
554
555 void *dev_get_priv(const struct udevice *dev)
556 {
557         if (!dev) {
558                 dm_warn("%s: null device\n", __func__);
559                 return NULL;
560         }
561
562         return dev->priv_;
563 }
564
565 void *dev_get_uclass_priv(const struct udevice *dev)
566 {
567         if (!dev) {
568                 dm_warn("%s: null device\n", __func__);
569                 return NULL;
570         }
571
572         return dev->uclass_priv_;
573 }
574
575 void *dev_get_parent_priv(const struct udevice *dev)
576 {
577         if (!dev) {
578                 dm_warn("%s: null device\n", __func__);
579                 return NULL;
580         }
581
582         return dev->parent_priv_;
583 }
584
585 static int device_get_device_tail(struct udevice *dev, int ret,
586                                   struct udevice **devp)
587 {
588         if (ret)
589                 return ret;
590
591         ret = device_probe(dev);
592         if (ret)
593                 return ret;
594
595         *devp = dev;
596
597         return 0;
598 }
599
600 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
601 /**
602  * device_find_by_ofnode() - Return device associated with given ofnode
603  *
604  * The returned device is *not* activated.
605  *
606  * @node: The ofnode for which a associated device should be looked up
607  * @devp: Pointer to structure to hold the found device
608  * Return: 0 if OK, -ve on error
609  */
610 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
611 {
612         struct uclass *uc;
613         struct udevice *dev;
614         int ret;
615
616         list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
617                 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
618                                                    &dev);
619                 if (!ret || dev) {
620                         *devp = dev;
621                         return 0;
622                 }
623         }
624
625         return -ENODEV;
626 }
627 #endif
628
629 int device_get_child(const struct udevice *parent, int index,
630                      struct udevice **devp)
631 {
632         struct udevice *dev;
633
634         list_for_each_entry(dev, &parent->child_head, sibling_node) {
635                 if (!index--)
636                         return device_get_device_tail(dev, 0, devp);
637         }
638
639         return -ENODEV;
640 }
641
642 int device_get_child_count(const struct udevice *parent)
643 {
644         struct udevice *dev;
645         int count = 0;
646
647         list_for_each_entry(dev, &parent->child_head, sibling_node)
648                 count++;
649
650         return count;
651 }
652
653 int device_find_child_by_seq(const struct udevice *parent, int seq,
654                              struct udevice **devp)
655 {
656         struct udevice *dev;
657
658         *devp = NULL;
659
660         list_for_each_entry(dev, &parent->child_head, sibling_node) {
661                 if (dev->seq_ == seq) {
662                         *devp = dev;
663                         return 0;
664                 }
665         }
666
667         return -ENODEV;
668 }
669
670 int device_get_child_by_seq(const struct udevice *parent, int seq,
671                             struct udevice **devp)
672 {
673         struct udevice *dev;
674         int ret;
675
676         *devp = NULL;
677         ret = device_find_child_by_seq(parent, seq, &dev);
678
679         return device_get_device_tail(dev, ret, devp);
680 }
681
682 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
683                                    struct udevice **devp)
684 {
685         struct udevice *dev;
686
687         *devp = NULL;
688
689         list_for_each_entry(dev, &parent->child_head, sibling_node) {
690                 if (dev_of_offset(dev) == of_offset) {
691                         *devp = dev;
692                         return 0;
693                 }
694         }
695
696         return -ENODEV;
697 }
698
699 int device_get_child_by_of_offset(const struct udevice *parent, int node,
700                                   struct udevice **devp)
701 {
702         struct udevice *dev;
703         int ret;
704
705         *devp = NULL;
706         ret = device_find_child_by_of_offset(parent, node, &dev);
707         return device_get_device_tail(dev, ret, devp);
708 }
709
710 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
711                                                      ofnode ofnode)
712 {
713         struct udevice *dev, *found;
714
715         if (ofnode_equal(dev_ofnode(parent), ofnode))
716                 return parent;
717
718         list_for_each_entry(dev, &parent->child_head, sibling_node) {
719                 found = _device_find_global_by_ofnode(dev, ofnode);
720                 if (found)
721                         return found;
722         }
723
724         return NULL;
725 }
726
727 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
728 {
729         *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
730
731         return *devp ? 0 : -ENOENT;
732 }
733
734 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
735 {
736         struct udevice *dev;
737
738         dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
739         return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
740 }
741
742 #if CONFIG_IS_ENABLED(OF_PLATDATA)
743 int device_get_by_driver_info(const struct driver_info *info,
744                               struct udevice **devp)
745 {
746         struct driver_info *info_base =
747                 ll_entry_start(struct driver_info, driver_info);
748         int idx = info - info_base;
749         struct driver_rt *drt = gd_dm_driver_rt() + idx;
750         struct udevice *dev;
751
752         dev = drt->dev;
753         *devp = NULL;
754
755         return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
756 }
757
758 int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
759 {
760         struct driver_rt *drt = gd_dm_driver_rt() + idx;
761         struct udevice *dev;
762
763         dev = drt->dev;
764         *devp = NULL;
765
766         return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
767 }
768 #endif
769
770 int device_find_first_child(const struct udevice *parent, struct udevice **devp)
771 {
772         if (list_empty(&parent->child_head)) {
773                 *devp = NULL;
774         } else {
775                 *devp = list_first_entry(&parent->child_head, struct udevice,
776                                          sibling_node);
777         }
778
779         return 0;
780 }
781
782 int device_find_next_child(struct udevice **devp)
783 {
784         struct udevice *dev = *devp;
785         struct udevice *parent = dev->parent;
786
787         if (list_is_last(&dev->sibling_node, &parent->child_head)) {
788                 *devp = NULL;
789         } else {
790                 *devp = list_entry(dev->sibling_node.next, struct udevice,
791                                    sibling_node);
792         }
793
794         return 0;
795 }
796
797 int device_find_first_inactive_child(const struct udevice *parent,
798                                      enum uclass_id uclass_id,
799                                      struct udevice **devp)
800 {
801         struct udevice *dev;
802
803         *devp = NULL;
804         list_for_each_entry(dev, &parent->child_head, sibling_node) {
805                 if (!device_active(dev) &&
806                     device_get_uclass_id(dev) == uclass_id) {
807                         *devp = dev;
808                         return 0;
809                 }
810         }
811
812         return -ENODEV;
813 }
814
815 int device_find_first_child_by_uclass(const struct udevice *parent,
816                                       enum uclass_id uclass_id,
817                                       struct udevice **devp)
818 {
819         struct udevice *dev;
820
821         *devp = NULL;
822         list_for_each_entry(dev, &parent->child_head, sibling_node) {
823                 if (device_get_uclass_id(dev) == uclass_id) {
824                         *devp = dev;
825                         return 0;
826                 }
827         }
828
829         return -ENODEV;
830 }
831
832 int device_find_child_by_name(const struct udevice *parent, const char *name,
833                               struct udevice **devp)
834 {
835         struct udevice *dev;
836
837         *devp = NULL;
838
839         list_for_each_entry(dev, &parent->child_head, sibling_node) {
840                 if (!strcmp(dev->name, name)) {
841                         *devp = dev;
842                         return 0;
843                 }
844         }
845
846         return -ENODEV;
847 }
848
849 int device_first_child_err(struct udevice *parent, struct udevice **devp)
850 {
851         struct udevice *dev;
852
853         device_find_first_child(parent, &dev);
854         if (!dev)
855                 return -ENODEV;
856
857         return device_get_device_tail(dev, 0, devp);
858 }
859
860 int device_next_child_err(struct udevice **devp)
861 {
862         struct udevice *dev = *devp;
863
864         device_find_next_child(&dev);
865         if (!dev)
866                 return -ENODEV;
867
868         return device_get_device_tail(dev, 0, devp);
869 }
870
871 int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
872 {
873         struct udevice *dev;
874         int ret;
875
876         device_find_first_child(parent, &dev);
877         if (!dev)
878                 return -ENODEV;
879
880         ret = device_of_to_plat(dev);
881         if (ret)
882                 return ret;
883
884         *devp = dev;
885
886         return 0;
887 }
888
889 int device_next_child_ofdata_err(struct udevice **devp)
890 {
891         struct udevice *dev = *devp;
892         int ret;
893
894         device_find_next_child(&dev);
895         if (!dev)
896                 return -ENODEV;
897
898         ret = device_of_to_plat(dev);
899         if (ret)
900                 return ret;
901
902         *devp = dev;
903
904         return 0;
905 }
906
907 struct udevice *dev_get_parent(const struct udevice *child)
908 {
909         return child->parent;
910 }
911
912 ulong dev_get_driver_data(const struct udevice *dev)
913 {
914         return dev->driver_data;
915 }
916
917 const void *dev_get_driver_ops(const struct udevice *dev)
918 {
919         if (!dev || !dev->driver->ops)
920                 return NULL;
921
922         return dev->driver->ops;
923 }
924
925 enum uclass_id device_get_uclass_id(const struct udevice *dev)
926 {
927         return dev->uclass->uc_drv->id;
928 }
929
930 const char *dev_get_uclass_name(const struct udevice *dev)
931 {
932         if (!dev)
933                 return NULL;
934
935         return dev->uclass->uc_drv->name;
936 }
937
938 bool device_has_children(const struct udevice *dev)
939 {
940         return !list_empty(&dev->child_head);
941 }
942
943 bool device_has_active_children(const struct udevice *dev)
944 {
945         struct udevice *child;
946
947         for (device_find_first_child(dev, &child);
948              child;
949              device_find_next_child(&child)) {
950                 if (device_active(child))
951                         return true;
952         }
953
954         return false;
955 }
956
957 bool device_is_last_sibling(const struct udevice *dev)
958 {
959         struct udevice *parent = dev->parent;
960
961         if (!parent)
962                 return false;
963         return list_is_last(&dev->sibling_node, &parent->child_head);
964 }
965
966 void device_set_name_alloced(struct udevice *dev)
967 {
968         dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
969 }
970
971 int device_set_name(struct udevice *dev, const char *name)
972 {
973         name = strdup(name);
974         if (!name)
975                 return -ENOMEM;
976         dev->name = name;
977         device_set_name_alloced(dev);
978
979         return 0;
980 }
981
982 void dev_set_priv(struct udevice *dev, void *priv)
983 {
984         dev->priv_ = priv;
985 }
986
987 void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
988 {
989         dev->parent_priv_ = parent_priv;
990 }
991
992 void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
993 {
994         dev->uclass_priv_ = uclass_priv;
995 }
996
997 void dev_set_plat(struct udevice *dev, void *plat)
998 {
999         dev->plat_ = plat;
1000 }
1001
1002 void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1003 {
1004         dev->parent_plat_ = parent_plat;
1005 }
1006
1007 void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1008 {
1009         dev->uclass_plat_ = uclass_plat;
1010 }
1011
1012 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1013 bool device_is_compatible(const struct udevice *dev, const char *compat)
1014 {
1015         return ofnode_device_is_compatible(dev_ofnode(dev), compat);
1016 }
1017
1018 bool of_machine_is_compatible(const char *compat)
1019 {
1020         const void *fdt = gd->fdt_blob;
1021
1022         return !fdt_node_check_compatible(fdt, 0, compat);
1023 }
1024
1025 int dev_disable_by_path(const char *path)
1026 {
1027         struct uclass *uc;
1028         ofnode node = ofnode_path(path);
1029         struct udevice *dev;
1030         int ret = 1;
1031
1032         if (!of_live_active())
1033                 return -ENOSYS;
1034
1035         list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
1036                 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1037                 if (!ret)
1038                         break;
1039         }
1040
1041         if (ret)
1042                 return ret;
1043
1044         ret = device_remove(dev, DM_REMOVE_NORMAL);
1045         if (ret)
1046                 return ret;
1047
1048         ret = device_unbind(dev);
1049         if (ret)
1050                 return ret;
1051
1052         return ofnode_set_enabled(node, false);
1053 }
1054
1055 int dev_enable_by_path(const char *path)
1056 {
1057         ofnode node = ofnode_path(path);
1058         ofnode pnode = ofnode_get_parent(node);
1059         struct udevice *parent;
1060         int ret = 1;
1061
1062         if (!of_live_active())
1063                 return -ENOSYS;
1064
1065         ret = device_find_by_ofnode(pnode, &parent);
1066         if (ret)
1067                 return ret;
1068
1069         ret = ofnode_set_enabled(node, true);
1070         if (ret)
1071                 return ret;
1072
1073         return lists_bind_fdt(parent, node, NULL, false);
1074 }
1075 #endif