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