dm: core: Add a new sequence number for devices
[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)
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         if (seq_or_req_seq == -1)
665                 return -ENODEV;
666
667         list_for_each_entry(dev, &parent->child_head, sibling_node) {
668                 if ((find_req_seq ? dev->req_seq : dev_seq(dev)) ==
669                                 seq_or_req_seq) {
670                         *devp = dev;
671                         return 0;
672                 }
673         }
674
675         return -ENODEV;
676 }
677
678 int device_get_child_by_seq(const struct udevice *parent, int seq,
679                             struct udevice **devp)
680 {
681         struct udevice *dev;
682         int ret;
683
684         *devp = NULL;
685         ret = device_find_child_by_seq(parent, seq, false, &dev);
686         if (ret == -ENODEV) {
687                 /*
688                  * We didn't find it in probed devices. See if there is one
689                  * that will request this seq if probed.
690                  */
691                 ret = device_find_child_by_seq(parent, seq, true, &dev);
692         }
693         return device_get_device_tail(dev, ret, devp);
694 }
695
696 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
697                                    struct udevice **devp)
698 {
699         struct udevice *dev;
700
701         *devp = NULL;
702
703         list_for_each_entry(dev, &parent->child_head, sibling_node) {
704                 if (dev_of_offset(dev) == of_offset) {
705                         *devp = dev;
706                         return 0;
707                 }
708         }
709
710         return -ENODEV;
711 }
712
713 int device_get_child_by_of_offset(const struct udevice *parent, int node,
714                                   struct udevice **devp)
715 {
716         struct udevice *dev;
717         int ret;
718
719         *devp = NULL;
720         ret = device_find_child_by_of_offset(parent, node, &dev);
721         return device_get_device_tail(dev, ret, devp);
722 }
723
724 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
725                                                      ofnode ofnode)
726 {
727         struct udevice *dev, *found;
728
729         if (ofnode_equal(dev_ofnode(parent), ofnode))
730                 return parent;
731
732         list_for_each_entry(dev, &parent->child_head, sibling_node) {
733                 found = _device_find_global_by_ofnode(dev, ofnode);
734                 if (found)
735                         return found;
736         }
737
738         return NULL;
739 }
740
741 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
742 {
743         *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
744
745         return *devp ? 0 : -ENOENT;
746 }
747
748 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
749 {
750         struct udevice *dev;
751
752         dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
753         return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
754 }
755
756 #if CONFIG_IS_ENABLED(OF_PLATDATA)
757 int device_get_by_driver_info(const struct driver_info *info,
758                               struct udevice **devp)
759 {
760         struct driver_info *info_base =
761                 ll_entry_start(struct driver_info, driver_info);
762         int idx = info - info_base;
763         struct driver_rt *drt = gd_dm_driver_rt() + idx;
764         struct udevice *dev;
765
766         dev = drt->dev;
767         *devp = NULL;
768
769         return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
770 }
771
772 int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
773 {
774         struct driver_rt *drt = gd_dm_driver_rt() + idx;
775         struct udevice *dev;
776
777         dev = drt->dev;
778         *devp = NULL;
779
780         return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
781 }
782 #endif
783
784 int device_find_first_child(const struct udevice *parent, struct udevice **devp)
785 {
786         if (list_empty(&parent->child_head)) {
787                 *devp = NULL;
788         } else {
789                 *devp = list_first_entry(&parent->child_head, struct udevice,
790                                          sibling_node);
791         }
792
793         return 0;
794 }
795
796 int device_find_next_child(struct udevice **devp)
797 {
798         struct udevice *dev = *devp;
799         struct udevice *parent = dev->parent;
800
801         if (list_is_last(&dev->sibling_node, &parent->child_head)) {
802                 *devp = NULL;
803         } else {
804                 *devp = list_entry(dev->sibling_node.next, struct udevice,
805                                    sibling_node);
806         }
807
808         return 0;
809 }
810
811 int device_find_first_inactive_child(const struct udevice *parent,
812                                      enum uclass_id uclass_id,
813                                      struct udevice **devp)
814 {
815         struct udevice *dev;
816
817         *devp = NULL;
818         list_for_each_entry(dev, &parent->child_head, sibling_node) {
819                 if (!device_active(dev) &&
820                     device_get_uclass_id(dev) == uclass_id) {
821                         *devp = dev;
822                         return 0;
823                 }
824         }
825
826         return -ENODEV;
827 }
828
829 int device_find_first_child_by_uclass(const struct udevice *parent,
830                                       enum uclass_id uclass_id,
831                                       struct udevice **devp)
832 {
833         struct udevice *dev;
834
835         *devp = NULL;
836         list_for_each_entry(dev, &parent->child_head, sibling_node) {
837                 if (device_get_uclass_id(dev) == uclass_id) {
838                         *devp = dev;
839                         return 0;
840                 }
841         }
842
843         return -ENODEV;
844 }
845
846 int device_find_child_by_name(const struct udevice *parent, const char *name,
847                               struct udevice **devp)
848 {
849         struct udevice *dev;
850
851         *devp = NULL;
852
853         list_for_each_entry(dev, &parent->child_head, sibling_node) {
854                 if (!strcmp(dev->name, name)) {
855                         *devp = dev;
856                         return 0;
857                 }
858         }
859
860         return -ENODEV;
861 }
862
863 int device_first_child_err(struct udevice *parent, struct udevice **devp)
864 {
865         struct udevice *dev;
866
867         device_find_first_child(parent, &dev);
868         if (!dev)
869                 return -ENODEV;
870
871         return device_get_device_tail(dev, 0, devp);
872 }
873
874 int device_next_child_err(struct udevice **devp)
875 {
876         struct udevice *dev = *devp;
877
878         device_find_next_child(&dev);
879         if (!dev)
880                 return -ENODEV;
881
882         return device_get_device_tail(dev, 0, devp);
883 }
884
885 int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
886 {
887         struct udevice *dev;
888         int ret;
889
890         device_find_first_child(parent, &dev);
891         if (!dev)
892                 return -ENODEV;
893
894         ret = device_of_to_plat(dev);
895         if (ret)
896                 return ret;
897
898         *devp = dev;
899
900         return 0;
901 }
902
903 int device_next_child_ofdata_err(struct udevice **devp)
904 {
905         struct udevice *dev = *devp;
906         int ret;
907
908         device_find_next_child(&dev);
909         if (!dev)
910                 return -ENODEV;
911
912         ret = device_of_to_plat(dev);
913         if (ret)
914                 return ret;
915
916         *devp = dev;
917
918         return 0;
919 }
920
921 struct udevice *dev_get_parent(const struct udevice *child)
922 {
923         return child->parent;
924 }
925
926 ulong dev_get_driver_data(const struct udevice *dev)
927 {
928         return dev->driver_data;
929 }
930
931 const void *dev_get_driver_ops(const struct udevice *dev)
932 {
933         if (!dev || !dev->driver->ops)
934                 return NULL;
935
936         return dev->driver->ops;
937 }
938
939 enum uclass_id device_get_uclass_id(const struct udevice *dev)
940 {
941         return dev->uclass->uc_drv->id;
942 }
943
944 const char *dev_get_uclass_name(const struct udevice *dev)
945 {
946         if (!dev)
947                 return NULL;
948
949         return dev->uclass->uc_drv->name;
950 }
951
952 bool device_has_children(const struct udevice *dev)
953 {
954         return !list_empty(&dev->child_head);
955 }
956
957 bool device_has_active_children(const struct udevice *dev)
958 {
959         struct udevice *child;
960
961         for (device_find_first_child(dev, &child);
962              child;
963              device_find_next_child(&child)) {
964                 if (device_active(child))
965                         return true;
966         }
967
968         return false;
969 }
970
971 bool device_is_last_sibling(const struct udevice *dev)
972 {
973         struct udevice *parent = dev->parent;
974
975         if (!parent)
976                 return false;
977         return list_is_last(&dev->sibling_node, &parent->child_head);
978 }
979
980 void device_set_name_alloced(struct udevice *dev)
981 {
982         dev->flags |= DM_FLAG_NAME_ALLOCED;
983 }
984
985 int device_set_name(struct udevice *dev, const char *name)
986 {
987         name = strdup(name);
988         if (!name)
989                 return -ENOMEM;
990         dev->name = name;
991         device_set_name_alloced(dev);
992
993         return 0;
994 }
995
996 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
997 bool device_is_compatible(const struct udevice *dev, const char *compat)
998 {
999         return ofnode_device_is_compatible(dev_ofnode(dev), compat);
1000 }
1001
1002 bool of_machine_is_compatible(const char *compat)
1003 {
1004         const void *fdt = gd->fdt_blob;
1005
1006         return !fdt_node_check_compatible(fdt, 0, compat);
1007 }
1008
1009 int dev_disable_by_path(const char *path)
1010 {
1011         struct uclass *uc;
1012         ofnode node = ofnode_path(path);
1013         struct udevice *dev;
1014         int ret = 1;
1015
1016         if (!of_live_active())
1017                 return -ENOSYS;
1018
1019         list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
1020                 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1021                 if (!ret)
1022                         break;
1023         }
1024
1025         if (ret)
1026                 return ret;
1027
1028         ret = device_remove(dev, DM_REMOVE_NORMAL);
1029         if (ret)
1030                 return ret;
1031
1032         ret = device_unbind(dev);
1033         if (ret)
1034                 return ret;
1035
1036         return ofnode_set_enabled(node, false);
1037 }
1038
1039 int dev_enable_by_path(const char *path)
1040 {
1041         ofnode node = ofnode_path(path);
1042         ofnode pnode = ofnode_get_parent(node);
1043         struct udevice *parent;
1044         int ret = 1;
1045
1046         if (!of_live_active())
1047                 return -ENOSYS;
1048
1049         ret = device_find_by_ofnode(pnode, &parent);
1050         if (ret)
1051                 return ret;
1052
1053         ret = ofnode_set_enabled(node, true);
1054         if (ret)
1055                 return ret;
1056
1057         return lists_bind_fdt(parent, node, NULL, false);
1058 }
1059 #endif