[U-Boot] [PATCH 1/3] env_sf: generalize call to spi_flash_free after accesses

Valentin Longchamp valentin.longchamp at keymile.com
Tue Sep 17 08:46:17 CEST 2013


Some board require spi_flash_free to be called after all the accesses,
in order, for instance, to restore the pin multiplexing configuration in
the case where the SPI pins are multiplexed.

This was done only in env_relocate_spec and not in saveenv. saveenv is
thus changed in order to have the same behavior as env_relocate_spec.

Since the static env_flash variable will be NULL after every function
call, it is thus removed and its functionality is replaced by a
systematic call to spi_flash_probe at the start of both
env_relocate_spec and saveenv.

Signed-off-by: Valentin Longchamp <valentin.longchamp at keymile.com>
---
 common/env_sf.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)

diff --git a/common/env_sf.c b/common/env_sf.c
index 9f806fb..dd0e675 100644
--- a/common/env_sf.c
+++ b/common/env_sf.c
@@ -41,8 +41,6 @@ DECLARE_GLOBAL_DATA_PTR;
 
 char *env_name_spec = "SPI Flash";
 
-static struct spi_flash *env_flash;
-
 #if defined(CONFIG_ENV_OFFSET_REDUND)
 int saveenv(void)
 {
@@ -50,16 +48,14 @@ int saveenv(void)
 	ssize_t	len;
 	char	*res, *saved_buffer = NULL, flag = OBSOLETE_FLAG;
 	u32	saved_size, saved_offset, sector = 1;
+	struct spi_flash *env_flash = NULL;
 	int	ret;
 
+	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) {
-			set_default_env("!spi_flash_probe() failed");
-			return 1;
-		}
+		set_default_env("!spi_flash_probe() failed");
+		return 1;
 	}
 
 	res = (char *)&env_new.data;
@@ -135,6 +131,8 @@ int saveenv(void)
 	if (saved_buffer)
 		free(saved_buffer);
 
+	spi_flash_free(env_flash);
+
 	return ret;
 }
 
@@ -145,6 +143,7 @@ void env_relocate_spec(void)
 	env_t *tmp_env1 = NULL;
 	env_t *tmp_env2 = NULL;
 	env_t *ep = NULL;
+	struct spi_flash *env_flash = NULL;
 
 	tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
 	tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
@@ -216,7 +215,6 @@ void env_relocate_spec(void)
 
 err_read:
 	spi_flash_free(env_flash);
-	env_flash = NULL;
 out:
 	free(tmp_env1);
 	free(tmp_env2);
@@ -229,15 +227,13 @@ int saveenv(void)
 	int	ret = 1;
 	env_t	env_new;
 	ssize_t	len;
+	struct  spi_flash *env_flash = NULL;
 
+	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) {
-			set_default_env("!spi_flash_probe() failed");
-			return 1;
-		}
+		set_default_env("!spi_flash_probe() failed");
+		return 1;
 	}
 
 	/* Is the sector larger than the env (i.e. embedded) */
@@ -294,12 +290,15 @@ int saveenv(void)
 	if (saved_buffer)
 		free(saved_buffer);
 
+	spi_flash_free(env_flash);
+
 	return ret;
 }
 
 void env_relocate_spec(void)
 {
 	char buf[CONFIG_ENV_SIZE];
+	struct  spi_flash *env_flash = NULL;
 	int ret;
 
 	env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
@@ -321,7 +320,6 @@ void env_relocate_spec(void)
 		gd->env_valid = 1;
 out:
 	spi_flash_free(env_flash);
-	env_flash = NULL;
 }
 #endif
 
-- 
1.8.0.1



More information about the U-Boot mailing list