UPSTREAM: mtd: sf: Make sure we don't register the same device twice

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: 9fe6d8716e ("mtd, spi: Add MTD layer driver")
Change-Id: Ia2c2778f275d0bf46371b5f914fcf1d1bbefdede
Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
Tested-by: Heiko Schocher <hs@denx.de>
Reviewed-by: Jagan Teki <jagan@openedev.com>
Signed-off-by: Jon Lin <jon.lin@rock-chips.com>
(cherry picked from commit 492151b2093ee4df173f7e10da4938ef7c2a2156)
This commit is contained in:
Boris Brezillon 2018-12-02 10:54:25 +01:00 committed by Kever Yang
parent 002d1762d9
commit c79c393fb9
1 changed files with 12 additions and 1 deletions

View File

@ -11,6 +11,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)
@ -74,6 +75,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());
@ -95,7 +102,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)