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