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