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