[RFC PATCH 0/2] env: sf: remove the static env_flash variable

Proposal to cleanup the SPI device support in env/sf.c, after the question of mail [1].
Release the SPI flash after each ENV request, so U-Boot can't have conflict of other SPI user.
This serie can be applied on top on previous serie [2].
Drawback: possible performance issue as SPI device is probed/release for each ENV access when several ENV opts are called.
[1] Mail "Question about env_flash variable in env/sf.c" Dec 17, 2020; 6:33pm http://u-boot.10912.n7.nabble.com/Question-about-env-flash-variable-in-env-s...
[2] env: sf: add support of command env erase http://patchwork.ozlabs.org/project/uboot/list/?series=228696&state=*
Patrick
Patrick Delaunay (2): env: sf: add missing spi_flash_free env: sf: remove the static env_flash variable
env/sf.c | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-)

Free the SPI resources by calling spi_flash_free() in each env sf function to avoid issue for other SPI users.
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com ---
env/sf.c | 9 +++++++++ 1 file changed, 9 insertions(+)
diff --git a/env/sf.c b/env/sf.c index 6b61a4b8de..acbd712aef 100644 --- a/env/sf.c +++ b/env/sf.c @@ -149,6 +149,9 @@ static int env_sf_save(void) printf("Valid environment: %d\n", (int)gd->env_valid);
done: + spi_flash_free(env_flash); + env_flash = NULL; + if (saved_buffer) free(saved_buffer);
@@ -246,6 +249,9 @@ static int env_sf_save(void) puts("done\n");
done: + spi_flash_free(env_flash); + env_flash = NULL; + if (saved_buffer) free(saved_buffer);
@@ -404,6 +410,9 @@ static int env_sf_init_early(void) gd->env_addr = (unsigned long)&tmp_env1->data; }
+ spi_flash_free(env_flash); + env_flash = NULL; + return 0; err_read: spi_flash_free(env_flash);

On Wed, Feb 24, 2021 at 11:52:35AM +0100, Patrick Delaunay wrote:
Free the SPI resources by calling spi_flash_free() in each env sf function to avoid issue for other SPI users.
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com
Applied to u-boot/master, thanks!

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@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);

On Wed, Feb 24, 2021 at 11:52:36AM +0100, Patrick Delaunay wrote:
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@foss.st.com
Applied to u-boot/master, thanks!
participants (2)
-
Patrick Delaunay
-
Tom Rini