Merge branch 'master' of git://git.denx.de/u-boot-net
[oweals/u-boot.git] / drivers / block / blk-uclass.c
index 614567527182956ba5ff9e260087dfcbc76d5679..facf52711ccfd735171ba7c0235aa275ad089af1 100644 (file)
@@ -1,8 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2016 Google, Inc
  * Written by Simon Glass <sjg@chromium.org>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
@@ -10,6 +9,7 @@
 #include <dm.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
+#include <dm/uclass-internal.h>
 
 static const char *if_typename_str[IF_TYPE_COUNT] = {
        [IF_TYPE_IDE]           = "ide",
@@ -21,11 +21,12 @@ static const char *if_typename_str[IF_TYPE_COUNT] = {
        [IF_TYPE_SD]            = "sd",
        [IF_TYPE_SATA]          = "sata",
        [IF_TYPE_HOST]          = "host",
-       [IF_TYPE_SYSTEMACE]     = "ace",
+       [IF_TYPE_NVME]          = "nvme",
+       [IF_TYPE_EFI]           = "efi",
 };
 
 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
-       [IF_TYPE_IDE]           = UCLASS_INVALID,
+       [IF_TYPE_IDE]           = UCLASS_IDE,
        [IF_TYPE_SCSI]          = UCLASS_SCSI,
        [IF_TYPE_ATAPI]         = UCLASS_INVALID,
        [IF_TYPE_USB]           = UCLASS_MASS_STORAGE,
@@ -34,7 +35,8 @@ static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
        [IF_TYPE_SD]            = UCLASS_INVALID,
        [IF_TYPE_SATA]          = UCLASS_AHCI,
        [IF_TYPE_HOST]          = UCLASS_ROOT,
-       [IF_TYPE_SYSTEMACE]     = UCLASS_INVALID,
+       [IF_TYPE_NVME]          = UCLASS_NVME,
+       [IF_TYPE_EFI]           = UCLASS_EFI,
 };
 
 static enum if_type if_typename_to_iftype(const char *if_typename)
@@ -55,6 +57,11 @@ static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
        return if_type_uclass_id[if_type];
 }
 
+const char *blk_get_if_type_name(enum if_type if_type)
+{
+       return if_typename_str[if_type];
+}
+
 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
 {
        struct blk_desc *desc;
@@ -124,6 +131,29 @@ struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
        return NULL;
 }
 
+/**
+ * blk_get_by_device() - Get the block device descriptor for the given device
+ * @dev:       Instance of a storage device
+ *
+ * Return: With block device descriptor on success , NULL if there is no such
+ *        block device.
+ */
+struct blk_desc *blk_get_by_device(struct udevice *dev)
+{
+       struct udevice *child_dev, *next;
+
+       device_foreach_child_safe(child_dev, next, dev) {
+               if (device_get_uclass_id(child_dev) != UCLASS_BLK)
+                       continue;
+
+               return dev_get_uclass_platdata(child_dev);
+       }
+
+       debug("%s: No block device found\n", __func__);
+
+       return NULL;
+}
+
 /**
  * get_desc() - Get the block device descriptor for the given device number
  *
@@ -287,9 +317,6 @@ ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
        if (IS_ERR_VALUE(n))
                return n;
 
-       /* flush cache after read */
-       flush_cache((ulong)buffer, blkcnt * desc->blksz);
-
        return n;
 }
 
@@ -327,7 +354,7 @@ int blk_first_device(int if_type, struct udevice **devp)
        struct blk_desc *desc;
        int ret;
 
-       ret = uclass_first_device(UCLASS_BLK, devp);
+       ret = uclass_find_first_device(UCLASS_BLK, devp);
        if (ret)
                return ret;
        if (!*devp)
@@ -336,7 +363,7 @@ int blk_first_device(int if_type, struct udevice **devp)
                desc = dev_get_uclass_platdata(*devp);
                if (desc->if_type == if_type)
                        return 0;
-               ret = uclass_next_device(devp);
+               ret = uclass_find_next_device(devp);
                if (ret)
                        return ret;
        } while (*devp);
@@ -352,7 +379,7 @@ int blk_next_device(struct udevice **devp)
        desc = dev_get_uclass_platdata(*devp);
        if_type = desc->if_type;
        do {
-               ret = uclass_next_device(devp);
+               ret = uclass_find_next_device(devp);
                if (ret)
                        return ret;
                if (!*devp)
@@ -453,6 +480,32 @@ int blk_prepare_device(struct udevice *dev)
        return 0;
 }
 
+int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
+{
+       struct udevice *dev;
+       enum uclass_id id;
+       int ret;
+
+       device_find_first_child(parent, &dev);
+       if (!dev) {
+               debug("%s: No block device found for parent '%s'\n", __func__,
+                     parent->name);
+               return -ENODEV;
+       }
+       id = device_get_uclass_id(dev);
+       if (id != UCLASS_BLK) {
+               debug("%s: Incorrect uclass %s for block device '%s'\n",
+                     __func__, uclass_get_name(id), dev->name);
+               return -ENOTBLK;
+       }
+       ret = device_probe(dev);
+       if (ret)
+               return ret;
+       *devp = dev;
+
+       return 0;
+}
+
 int blk_find_max_devnum(enum if_type if_type)
 {
        struct udevice *dev;
@@ -513,7 +566,7 @@ static int blk_claim_devnum(enum if_type if_type, int devnum)
 
 int blk_create_device(struct udevice *parent, const char *drv_name,
                      const char *name, int if_type, int devnum, int blksz,
-                     lbaint_t size, struct udevice **devp)
+                     lbaint_t lba, struct udevice **devp)
 {
        struct blk_desc *desc;
        struct udevice *dev;
@@ -534,7 +587,7 @@ int blk_create_device(struct udevice *parent, const char *drv_name,
        desc = dev_get_uclass_platdata(dev);
        desc->if_type = if_type;
        desc->blksz = blksz;
-       desc->lba = size / blksz;
+       desc->lba = lba;
        desc->part_type = PART_TYPE_UNKNOWN;
        desc->bdev = dev;
        desc->devnum = devnum;
@@ -545,7 +598,7 @@ int blk_create_device(struct udevice *parent, const char *drv_name,
 
 int blk_create_devicef(struct udevice *parent, const char *drv_name,
                       const char *name, int if_type, int devnum, int blksz,
-                      lbaint_t size, struct udevice **devp)
+                      lbaint_t lba, struct udevice **devp)
 {
        char dev_name[30], *str;
        int ret;
@@ -556,14 +609,14 @@ int blk_create_devicef(struct udevice *parent, const char *drv_name,
                return -ENOMEM;
 
        ret = blk_create_device(parent, drv_name, str, if_type, devnum,
-                               blksz, size, devp);
+                               blksz, lba, devp);
        if (ret) {
                free(str);
                return ret;
        }
        device_set_name_alloced(*devp);
 
-       return ret;
+       return 0;
 }
 
 int blk_unbind_all(int if_type)