[RFC PATCH 2/2] env: sf: remove the static env_flash variable
Patrick Delaunay
patrick.delaunay at foss.st.com
Wed Feb 24 11:52:36 CET 2021
As the the SPI flash is probed and is released in each ENV sf function
the env_flash no more need to be static.
This patch move this device handle as local variable of each function and
simplify the associated code (env_flash is never == NULL when
setup_flash_device is called).
Signed-off-by: Patrick Delaunay <patrick.delaunay at foss.st.com>
---
env/sf.c | 41 ++++++++++++++++++-----------------------
1 file changed, 18 insertions(+), 23 deletions(-)
diff --git a/env/sf.c b/env/sf.c
index acbd712aef..b7cf9c64d4 100644
--- a/env/sf.c
+++ b/env/sf.c
@@ -43,9 +43,7 @@ static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
DECLARE_GLOBAL_DATA_PTR;
-static struct spi_flash *env_flash;
-
-static int setup_flash_device(void)
+static int setup_flash_device(struct spi_flash **env_flash)
{
#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
struct udevice *new;
@@ -60,14 +58,11 @@ static int setup_flash_device(void)
return ret;
}
- env_flash = dev_get_uclass_priv(new);
+ *env_flash = dev_get_uclass_priv(new);
#else
- if (env_flash)
- spi_flash_free(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_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;
}
@@ -82,8 +77,9 @@ static int env_sf_save(void)
char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
u32 saved_size, saved_offset, sector;
int ret;
+ struct spi_flash *env_flash;
- ret = setup_flash_device();
+ ret = setup_flash_device(&env_flash);
if (ret)
return ret;
@@ -150,7 +146,6 @@ static int env_sf_save(void)
done:
spi_flash_free(env_flash);
- env_flash = NULL;
if (saved_buffer)
free(saved_buffer);
@@ -163,6 +158,7 @@ static int env_sf_load(void)
int ret;
int read1_fail, read2_fail;
env_t *tmp_env1, *tmp_env2;
+ struct spi_flash *env_flash;
tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
CONFIG_ENV_SIZE);
@@ -174,7 +170,7 @@ static int env_sf_load(void)
goto out;
}
- ret = setup_flash_device();
+ ret = setup_flash_device(&env_flash);
if (ret)
goto out;
@@ -187,7 +183,6 @@ static int env_sf_load(void)
read2_fail, H_EXTERNAL);
spi_flash_free(env_flash);
- env_flash = NULL;
out:
free(tmp_env1);
free(tmp_env2);
@@ -201,8 +196,9 @@ static int env_sf_save(void)
char *saved_buffer = NULL;
int ret = 1;
env_t env_new;
+ struct spi_flash *env_flash;
- ret = setup_flash_device();
+ ret = setup_flash_device(&env_flash);
if (ret)
return ret;
@@ -250,7 +246,6 @@ static int env_sf_save(void)
done:
spi_flash_free(env_flash);
- env_flash = NULL;
if (saved_buffer)
free(saved_buffer);
@@ -262,6 +257,7 @@ static int env_sf_load(void)
{
int ret;
char *buf = NULL;
+ struct spi_flash *env_flash;
buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
if (!buf) {
@@ -269,7 +265,7 @@ static int env_sf_load(void)
return -EIO;
}
- ret = setup_flash_device();
+ ret = setup_flash_device(&env_flash);
if (ret)
goto out;
@@ -286,7 +282,6 @@ static int env_sf_load(void)
err_read:
spi_flash_free(env_flash);
- env_flash = NULL;
out:
free(buf);
@@ -298,8 +293,9 @@ static int env_sf_erase(void)
{
int ret;
env_t env;
+ struct spi_flash *env_flash;
- ret = setup_flash_device();
+ ret = setup_flash_device(&env_flash);
if (ret)
return ret;
@@ -313,7 +309,6 @@ static int env_sf_erase(void)
done:
spi_flash_free(env_flash);
- env_flash = NULL;
return ret;
}
@@ -358,6 +353,7 @@ static int env_sf_init_early(void)
int crc1_ok;
env_t *tmp_env2 = NULL;
env_t *tmp_env1;
+ struct spi_flash *env_flash;
/*
* if malloc is not ready yet, we cannot use
@@ -375,7 +371,7 @@ static int env_sf_init_early(void)
if (!tmp_env1 || !tmp_env2)
goto out;
- ret = setup_flash_device();
+ ret = setup_flash_device(&env_flash);
if (ret)
goto out;
@@ -411,12 +407,11 @@ static int env_sf_init_early(void)
}
spi_flash_free(env_flash);
- env_flash = NULL;
return 0;
err_read:
spi_flash_free(env_flash);
- env_flash = NULL;
+
free(tmp_env1);
if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
free(tmp_env2);
--
2.17.1
More information about the U-Boot
mailing list