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