All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Delaunay <patrick.delaunay@foss.st.com>
To: u-boot@lists.denx.de
Subject: [RFC PATCH 2/2] env: sf: remove the static env_flash variable
Date: Wed, 24 Feb 2021 11:52:36 +0100	[thread overview]
Message-ID: <20210224115218.RFC.2.I5c06755fe3aea5afd6b9880630bfc7c8218eaa8b@changeid> (raw)
In-Reply-To: <20210224105236.25663-1-patrick.delaunay@foss.st.com>

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

  parent reply	other threads:[~2021-02-24 10:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-24 10:52 [RFC PATCH 0/2] env: sf: remove the static env_flash variable Patrick Delaunay
2021-02-24 10:52 ` [RFC PATCH 1/2] env: sf: add missing spi_flash_free Patrick Delaunay
2021-04-18 12:45   ` Tom Rini
2021-02-24 10:52 ` Patrick Delaunay [this message]
2021-04-18 12:46   ` [RFC PATCH 2/2] env: sf: remove the static env_flash variable Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210224115218.RFC.2.I5c06755fe3aea5afd6b9880630bfc7c8218eaa8b@changeid \
    --to=patrick.delaunay@foss.st.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.