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