From patchwork Thu May 14 12:11:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagan Teki X-Patchwork-Id: 245802 List-Id: U-Boot discussion From: jagan at amarulasolutions.com (Jagan Teki) Date: Thu, 14 May 2020 17:41:43 +0530 Subject: [PATCH 3/5] env: sf: Preserve and free the previous flash In-Reply-To: <20200514121145.28737-1-jagan@amarulasolutions.com> References: <20200514121145.28737-1-jagan@amarulasolutions.com> Message-ID: <20200514121145.28737-4-jagan@amarulasolutions.com> env_flash is a global flash pointer, and the probe would happen only if env_flash is NULL, but there is no checking and free the pointer if is not NULL. So, this patch frees the env_flash if it's not NULL, and get the probed flash in new flash pointer and finally assign into env_flash. Note: Similar approach has been followed and tested in cmd/sf.c Cc: Simon Glass Cc: Vignesh R Signed-off-by: Jagan Teki --- env/sf.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/env/sf.c b/env/sf.c index 64c57f2cdf..af59c8375c 100644 --- a/env/sf.c +++ b/env/sf.c @@ -50,15 +50,17 @@ static int setup_flash_device(void) env_flash = dev_get_uclass_priv(new); #else + struct spi_flash *new; - if (!env_flash) { - env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, - CONFIG_ENV_SPI_CS, - CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); - if (!env_flash) { - env_set_default("spi_flash_probe() failed", 0); - return -EIO; - } + if (env_flash) + spi_flash_free(env_flash); + + new = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, + CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); + env_flash = new; + if (!new) { + env_set_default("spi_flash_probe() failed", 0); + return -EIO; } #endif return 0;