mtd: sf: Make sure we don't register the same device twice
authorBoris Brezillon <boris.brezillon@bootlin.com>
Sun, 2 Dec 2018 09:54:25 +0000 (10:54 +0100)
committerJagan Teki <jagan@amarulasolutions.com>
Wed, 5 Dec 2018 19:15:36 +0000 (00:45 +0530)
spi_flash_mtd_register() can be called several times and each time it
will register the same mtd_info instance like if it was a new one.
The MTD ID allocation gets crazy when that happens, so let's track the
status of the sf_mtd_info object to avoid that.

Fixes: 9fe6d8716e09 ("mtd, spi: Add MTD layer driver")
Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
Tested-by: Heiko Schocher <hs@denx.de>
Reviewed-by: Jagan Teki <jagan@openedev.com>
drivers/mtd/spi/sf_mtd.c

index 58d7e44399037e3433b703c2dda6c7354f3c8cb4..aabbc3589435e83247e13c70d5d55a17a636d777 100644 (file)
@@ -10,6 +10,7 @@
 #include <spi_flash.h>
 
 static struct mtd_info sf_mtd_info;
+static bool sf_mtd_registered;
 static char sf_mtd_name[8];
 
 static int spi_flash_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
@@ -73,6 +74,12 @@ static int spi_flash_mtd_number(void)
 
 int spi_flash_mtd_register(struct spi_flash *flash)
 {
+       int ret;
+
+       if (sf_mtd_registered)
+               del_mtd_device(&sf_mtd_info);
+
+       sf_mtd_registered = false;
        memset(&sf_mtd_info, 0, sizeof(sf_mtd_info));
        sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number());
 
@@ -94,7 +101,11 @@ int spi_flash_mtd_register(struct spi_flash *flash)
        sf_mtd_info.numeraseregions = 0;
        sf_mtd_info.erasesize = flash->sector_size;
 
-       return add_mtd_device(&sf_mtd_info);
+       ret = add_mtd_device(&sf_mtd_info);
+       if (!ret)
+               sf_mtd_registered = true;
+
+       return ret;
 }
 
 void spi_flash_mtd_unregister(void)