Merge tag 'efi-2020-07-rc3' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / drivers / mtd / cfi_mtd.c
index c7e357b7f2a23fbea9b9af3ddda69ae57e3bd504..a5bb0962e556937781013210da098aed43e19b8c 100644 (file)
@@ -1,40 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2008 Semihalf
  *
  * Written by: Piotr Ziecik <kosmo@semihalf.com>
- *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
- *
  */
 
 #include <common.h>
 #include <flash.h>
 #include <malloc.h>
 
-#include <asm/errno.h>
+#include <linux/errno.h>
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/concat.h>
+#include <mtd/cfi_flash.h>
 
-extern flash_info_t flash_info[];
-
-static struct mtd_info cfi_mtd_info[CONFIG_SYS_MAX_FLASH_BANKS];
-static char cfi_mtd_names[CONFIG_SYS_MAX_FLASH_BANKS][16];
+static struct mtd_info cfi_mtd_info[CFI_MAX_FLASH_BANKS];
+static char cfi_mtd_names[CFI_MAX_FLASH_BANKS][16];
 #ifdef CONFIG_MTD_CONCAT
 static char c_mtd_name[16];
 #endif
@@ -154,8 +135,8 @@ static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
        int sect;
        int regions = 0;
        int numblocks = 0;
-       ulong offset = 0;
-       ulong base_addr = fi->start[0];
+       ulong offset;
+       ulong base_addr;
 
        /*
         * First detect the number of eraseregions so that we can allocate
@@ -167,29 +148,35 @@ static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
                sect_size_old = flash_sector_size(fi, sect);
        }
 
+       switch (regions) {
+       case 0:
+               return 1;
+       case 1: /* flash has uniform erase size */
+               mtd->numeraseregions = 0;
+               mtd->erasesize = sect_size_old;
+               return 0;
+       }
+
+       mtd->numeraseregions = regions;
        mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info) * regions);
 
        /*
         * Now detect the largest sector and fill the eraseregions
         */
-       sect_size_old = 0;
        regions = 0;
+       base_addr = offset = fi->start[0];
+       sect_size_old = flash_sector_size(fi, 0);
        for (sect = 0; sect < fi->sector_count; sect++) {
-               if ((sect_size_old != flash_sector_size(fi, sect)) &&
-                   (sect_size_old != 0)) {
+               if (sect_size_old != flash_sector_size(fi, sect)) {
                        mtd->eraseregions[regions].offset = offset - base_addr;
                        mtd->eraseregions[regions].erasesize = sect_size_old;
                        mtd->eraseregions[regions].numblocks = numblocks;
-
                        /* Now start counting the next eraseregions */
                        numblocks = 0;
                        regions++;
-               } else {
-                       numblocks++;
-               }
-
-               if (sect_size_old != flash_sector_size(fi, sect))
                        offset = fi->start[sect];
+               }
+               numblocks++;
 
                /*
                 * Select the largest sector size as erasesize (e.g. for UBI)
@@ -205,12 +192,7 @@ static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
         */
        mtd->eraseregions[regions].offset = offset - base_addr;
        mtd->eraseregions[regions].erasesize = sect_size_old;
-       mtd->eraseregions[regions].numblocks = numblocks + 1;
-
-       if (regions)
-               mtd->numeraseregions = regions + 1;
-       else
-               mtd->numeraseregions = 0;
+       mtd->eraseregions[regions].numblocks = numblocks;
 
        mtd->erasesize = sect_size;
 
@@ -222,8 +204,10 @@ int cfi_mtd_init(void)
        struct mtd_info *mtd;
        flash_info_t *fi;
        int error, i;
+#ifdef CONFIG_MTD_CONCAT
        int devices_found = 0;
        struct mtd_info *mtd_list[CONFIG_SYS_MAX_FLASH_BANKS];
+#endif
 
        for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
                fi = &flash_info[i];
@@ -241,19 +225,22 @@ int cfi_mtd_init(void)
                mtd->flags              = MTD_CAP_NORFLASH;
                mtd->size               = fi->size;
                mtd->writesize          = 1;
-
-               mtd->erase              = cfi_mtd_erase;
-               mtd->read               = cfi_mtd_read;
-               mtd->write              = cfi_mtd_write;
-               mtd->sync               = cfi_mtd_sync;
-               mtd->lock               = cfi_mtd_lock;
-               mtd->unlock             = cfi_mtd_unlock;
+               mtd->writebufsize       = mtd->writesize;
+
+               mtd->_erase             = cfi_mtd_erase;
+               mtd->_read              = cfi_mtd_read;
+               mtd->_write             = cfi_mtd_write;
+               mtd->_sync              = cfi_mtd_sync;
+               mtd->_lock              = cfi_mtd_lock;
+               mtd->_unlock            = cfi_mtd_unlock;
                mtd->priv               = fi;
 
                if (add_mtd_device(mtd))
                        return -ENOMEM;
 
+#ifdef CONFIG_MTD_CONCAT
                mtd_list[devices_found++] = mtd;
+#endif
        }
 
 #ifdef CONFIG_MTD_CONCAT