Skip to content

Commit

Permalink
env: aspeed: Support single flash ABR scenario
Browse files Browse the repository at this point in the history
Add single flash ABR supported for u-boot env.

Signed-off-by: Chin-Ting Kuo <chin-ting_kuo@aspeedtech.com>
Change-Id: I13e7555444ccaa8f1350790f9a9f29844bc8d527
  • Loading branch information
chintingkuo committed Feb 3, 2024
1 parent d6ea292 commit 66112de
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions env/sf.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <search.h>
#include <errno.h>
#include <uuid.h>
#include <asm/arch-aspeed/spi.h>
#include <asm/cache.h>
#include <asm/global_data.h>
#include <dm/device-internal.h>
Expand Down Expand Up @@ -196,6 +197,7 @@ static int env_sf_save(void)
char *saved_buffer = NULL;
int ret = 1;
env_t env_new;
u32 env_offset = CONFIG_ENV_OFFSET;
struct spi_flash *env_flash;

ret = setup_flash_device(&env_flash);
Expand All @@ -205,10 +207,12 @@ static int env_sf_save(void)
if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
sect_size = env_flash->mtd.erasesize;

env_offset += aspeed_spi_abr_offset();

/* Is the sector larger than the env (i.e. embedded) */
if (sect_size > CONFIG_ENV_SIZE) {
saved_size = sect_size - CONFIG_ENV_SIZE;
saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
saved_offset = env_offset + CONFIG_ENV_SIZE;
saved_buffer = malloc(saved_size);
if (!saved_buffer)
goto done;
Expand All @@ -226,14 +230,14 @@ static int env_sf_save(void)
sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);

puts("Erasing SPI flash...");
ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
sector * sect_size);
ret = spi_flash_erase(env_flash, env_offset,
sector * sect_size);
if (ret)
goto done;

puts("Writing to SPI flash...");
ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
CONFIG_ENV_SIZE, &env_new);
ret = spi_flash_write(env_flash, env_offset,
CONFIG_ENV_SIZE, &env_new);
if (ret)
goto done;

Expand Down Expand Up @@ -261,6 +265,7 @@ static int env_sf_load(void)
int ret;
char *buf = NULL;
struct spi_flash *env_flash;
u32 env_offset = CONFIG_ENV_OFFSET;

buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
if (!buf) {
Expand All @@ -272,8 +277,10 @@ static int env_sf_load(void)
if (ret)
goto out;

env_offset += aspeed_spi_abr_offset();

ret = spi_flash_read(env_flash,
CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
env_offset, CONFIG_ENV_SIZE, buf);
if (ret) {
env_set_default("spi_flash_read() failed", 0);
goto err_read;
Expand All @@ -297,13 +304,16 @@ static int env_sf_erase(void)
int ret;
env_t env;
struct spi_flash *env_flash;
u32 env_offset = CONFIG_ENV_OFFSET;

ret = setup_flash_device(&env_flash);
if (ret)
return ret;

env_offset += aspeed_spi_abr_offset();

memset(&env, 0, sizeof(env_t));
ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
ret = spi_flash_write(env_flash, env_offset, CONFIG_ENV_SIZE, &env);
if (ret)
goto done;

Expand All @@ -319,7 +329,7 @@ static int env_sf_erase(void)
__weak void *env_sf_get_env_addr(void)
{
#ifndef CONFIG_SPL_BUILD
return (void *)CONFIG_ENV_ADDR;
return (void *)(CONFIG_ENV_ADDR + (size_t)aspeed_spi_abr_offset());
#else
return NULL;
#endif
Expand Down

0 comments on commit 66112de

Please sign in to comment.