Merge branch '2020-05-18-reduce-size-of-common.h'
[oweals/u-boot.git] / drivers / mtd / mtdcore.c
index fb6c779abbfe18b1c2f16375c1965bf228fb75ea..1a4dec34d93ebb85c5a169408d966b0943393488 100644 (file)
@@ -9,6 +9,8 @@
  */
 
 #ifndef __UBOOT__
+#include <log.h>
+#include <dm/devres.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/ptrace.h>
@@ -26,6 +28,8 @@
 #include <linux/gfp.h>
 #include <linux/slab.h>
 #else
+#include <linux/bitops.h>
+#include <linux/bug.h>
 #include <linux/err.h>
 #include <ubi_uboot.h>
 #endif
@@ -76,8 +80,6 @@ static struct class mtd_class = {
        .resume = mtd_cls_resume,
 };
 #else
-struct mtd_info *mtd_table[MAX_MTD_DEVICES];
-
 #define MAX_IDR_ID     64
 
 struct idr_layer {
@@ -87,14 +89,17 @@ struct idr_layer {
 
 struct idr {
        struct idr_layer id[MAX_IDR_ID];
+       bool updated;
 };
 
 #define DEFINE_IDR(name)       struct idr name;
 
 void idr_remove(struct idr *idp, int id)
 {
-       if (idp->id[id].used)
+       if (idp->id[id].used) {
                idp->id[id].used = 0;
+               idp->updated = true;
+       }
 
        return;
 }
@@ -134,6 +139,7 @@ int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
                if (idl->used == 0) {
                        idl->used = 1;
                        idl->ptr = ptr;
+                       idp->updated = true;
                        return i;
                }
                i++;
@@ -155,6 +161,16 @@ struct mtd_info *__mtd_next_device(int i)
 }
 EXPORT_SYMBOL_GPL(__mtd_next_device);
 
+bool mtd_dev_list_updated(void)
+{
+       if (mtd_idr.updated) {
+               mtd_idr.updated = false;
+               return true;
+       }
+
+       return false;
+}
+
 #ifndef __UBOOT__
 static LIST_HEAD(mtd_notifiers);
 
@@ -514,6 +530,13 @@ int del_mtd_device(struct mtd_info *mtd)
        struct mtd_notifier *not;
 #endif
 
+       ret = del_mtd_partitions(mtd);
+       if (ret) {
+               debug("Failed to delete MTD partitions attached to %s (err %d)\n",
+                     mtd->name, ret);
+               return ret;
+       }
+
        mutex_lock(&mtd_table_mutex);
 
        if (idr_find(&mtd_idr, mtd->index) != mtd) {
@@ -1030,13 +1053,13 @@ static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
                return -EINVAL;
 
        if (ops->ooblen) {
-               u64 maxooblen;
+               size_t maxooblen;
 
                if (ops->ooboffs >= mtd_oobavail(mtd, ops))
                        return -EINVAL;
 
-               maxooblen = ((mtd_div_by_ws(mtd->size, mtd) -
-                             mtd_div_by_ws(offs, mtd)) *
+               maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
+                                     mtd_div_by_ws(offs, mtd)) *
                             mtd_oobavail(mtd, ops)) - ops->ooboffs;
                if (ops->ooblen > maxooblen)
                        return -EINVAL;
@@ -1160,10 +1183,10 @@ int mtd_ooblayout_free(struct mtd_info *mtd, int section,
        if (!mtd || section < 0)
                return -EINVAL;
 
-       if (!mtd->ooblayout || !mtd->ooblayout->free)
+       if (!mtd->ooblayout || !mtd->ooblayout->rfree)
                return -ENOTSUPP;
 
-       return mtd->ooblayout->free(mtd, section, oobfree);
+       return mtd->ooblayout->rfree(mtd, section, oobfree);
 }
 EXPORT_SYMBOL_GPL(mtd_ooblayout_free);