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