]> git.sur5r.net Git - u-boot/blobdiff - drivers/block/blk-uclass.c
imx: add macro to detect whether USB PHY is active
[u-boot] / drivers / block / blk-uclass.c
index f67f9b9d5524cc7c01d5063ff06cb472a75868c0..537cf5f0bbcb0d6122c247dcf09c326ee701add9 100644 (file)
@@ -22,11 +22,12 @@ static const char *if_typename_str[IF_TYPE_COUNT] = {
        [IF_TYPE_SATA]          = "sata",
        [IF_TYPE_HOST]          = "host",
        [IF_TYPE_SYSTEMACE]     = "ace",
+       [IF_TYPE_NVME]          = "nvme",
 };
 
 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
-       [IF_TYPE_IDE]           = UCLASS_INVALID,
-       [IF_TYPE_SCSI]          = UCLASS_INVALID,
+       [IF_TYPE_IDE]           = UCLASS_IDE,
+       [IF_TYPE_SCSI]          = UCLASS_SCSI,
        [IF_TYPE_ATAPI]         = UCLASS_INVALID,
        [IF_TYPE_USB]           = UCLASS_MASS_STORAGE,
        [IF_TYPE_DOC]           = UCLASS_INVALID,
@@ -34,6 +35,7 @@ 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_NVME]          = UCLASS_NVME,
        [IF_TYPE_SYSTEMACE]     = UCLASS_INVALID,
 };
 
@@ -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;
@@ -156,6 +163,8 @@ static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
                                if (ret)
                                        return ret;
 
+                               *descp = desc;
+                               return 0;
                        } else if (desc->devnum > devnum) {
                                found_more = true;
                        }
@@ -165,6 +174,18 @@ static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
        return found_more ? -ENOENT : -ENODEV;
 }
 
+int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
+{
+       struct udevice *dev;
+       int ret;
+
+       ret = blk_get_device(if_type, devnum, &dev);
+       if (ret)
+               return ret;
+
+       return blk_select_hwpart(dev, hwpart);
+}
+
 int blk_list_part(enum if_type if_type)
 {
        struct blk_desc *desc;
@@ -273,9 +294,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;
 }
 
@@ -291,6 +309,23 @@ ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
        return blk_dwrite(desc, start, blkcnt, buffer);
 }
 
+int blk_select_hwpart(struct udevice *dev, int hwpart)
+{
+       const struct blk_ops *ops = blk_get_ops(dev);
+
+       if (!ops)
+               return -ENOSYS;
+       if (!ops->select_hwpart)
+               return 0;
+
+       return ops->select_hwpart(dev, hwpart);
+}
+
+int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
+{
+       return blk_select_hwpart(desc->bdev, hwpart);
+}
+
 int blk_first_device(int if_type, struct udevice **devp)
 {
        struct blk_desc *desc;
@@ -332,7 +367,7 @@ int blk_next_device(struct udevice **devp)
        } while (1);
 }
 
-int blk_get_device(int if_type, int devnum, struct udevice **devp)
+int blk_find_device(int if_type, int devnum, struct udevice **devp)
 {
        struct uclass *uc;
        struct udevice *dev;
@@ -348,13 +383,24 @@ int blk_get_device(int if_type, int devnum, struct udevice **devp)
                      if_type, devnum, dev->name, desc->if_type, desc->devnum);
                if (desc->if_type == if_type && desc->devnum == devnum) {
                        *devp = dev;
-                       return device_probe(dev);
+                       return 0;
                }
        }
 
        return -ENODEV;
 }
 
+int blk_get_device(int if_type, int devnum, struct udevice **devp)
+{
+       int ret;
+
+       ret = blk_find_device(if_type, devnum, devp);
+       if (ret)
+               return ret;
+
+       return device_probe(*devp);
+}
+
 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
                        lbaint_t blkcnt, void *buffer)
 {
@@ -411,6 +457,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;
@@ -431,30 +503,68 @@ int blk_find_max_devnum(enum if_type if_type)
        return max_devnum;
 }
 
+static int blk_next_free_devnum(enum if_type if_type)
+{
+       int ret;
+
+       ret = blk_find_max_devnum(if_type);
+       if (ret == -ENODEV)
+               return 0;
+       if (ret < 0)
+               return ret;
+
+       return ret + 1;
+}
+
+static int blk_claim_devnum(enum if_type if_type, int devnum)
+{
+       struct udevice *dev;
+       struct uclass *uc;
+       int ret;
+
+       ret = uclass_get(UCLASS_BLK, &uc);
+       if (ret)
+               return ret;
+       uclass_foreach_dev(dev, uc) {
+               struct blk_desc *desc = dev_get_uclass_platdata(dev);
+
+               if (desc->if_type == if_type && desc->devnum == devnum) {
+                       int next = blk_next_free_devnum(if_type);
+
+                       if (next < 0)
+                               return next;
+                       desc->devnum = next;
+                       return 0;
+               }
+       }
+
+       return -ENOENT;
+}
+
 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;
        int ret;
 
        if (devnum == -1) {
-               ret = blk_find_max_devnum(if_type);
-               if (ret == -ENODEV)
-                       devnum = 0;
-               else if (ret < 0)
+               devnum = blk_next_free_devnum(if_type);
+       } else {
+               ret = blk_claim_devnum(if_type, devnum);
+               if (ret < 0 && ret != -ENOENT)
                        return ret;
-               else
-                       devnum = ret + 1;
        }
+       if (devnum < 0)
+               return devnum;
        ret = device_bind_driver(parent, drv_name, name, &dev);
        if (ret)
                return ret;
        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;
@@ -465,17 +575,25 @@ 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;
 
        snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
        str = strdup(dev_name);
        if (!str)
                return -ENOMEM;
 
-       return blk_create_device(parent, drv_name, str, if_type, devnum,
-                                blksz, size, devp);
+       ret = blk_create_device(parent, drv_name, str, if_type, devnum,
+                               blksz, lba, devp);
+       if (ret) {
+               free(str);
+               return ret;
+       }
+       device_set_name_alloced(*devp);
+
+       return 0;
 }
 
 int blk_unbind_all(int if_type)
@@ -491,7 +609,7 @@ int blk_unbind_all(int if_type)
                struct blk_desc *desc = dev_get_uclass_platdata(dev);
 
                if (desc->if_type == if_type) {
-                       ret = device_remove(dev);
+                       ret = device_remove(dev, DM_REMOVE_NORMAL);
                        if (ret)
                                return ret;
                        ret = device_unbind(dev);