[PATCH 0/2] env_spi: support overriding spi dev from board code

This enables boards to choose where to/from the environment should be saved/loaded either QSPI or OSPI based on the bootmode.
Venkatesh Yadav Abbarapu (2): env_spi: support overriding spi dev from board code xilinx: versal-net: Handle spi seq number based on boot device
board/xilinx/versal-net/board.c | 45 +++++++++++++++++++++++++++++++++ drivers/mtd/spi/spi-nor-core.c | 9 +++++++ env/sf.c | 3 ++- include/spi.h | 2 ++ 4 files changed, 58 insertions(+), 1 deletion(-)

This enables boards to choose where to/from the environment should be saved/loaded. They can then for example support using the same device (dynamically) from which the bootloader was launched to load and save env data and do not have to define CONFIG_ENV_SPI_BUS statically.
In my use case, the environment needs to be on the same device I booted from. It can be the QSPI or OSPI device. I therefore would override spi_get_env_dev in the board code, read the bootmode registers to determine where we booted from and return the corresponding device index.
Signed-off-by: Venkatesh Yadav Abbarapu venkatesh.abbarapu@amd.com --- drivers/mtd/spi/spi-nor-core.c | 9 +++++++++ env/sf.c | 3 ++- include/spi.h | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/drivers/mtd/spi/spi-nor-core.c b/drivers/mtd/spi/spi-nor-core.c index f86003ca8c..2fb49a96f3 100644 --- a/drivers/mtd/spi/spi-nor-core.c +++ b/drivers/mtd/spi/spi-nor-core.c @@ -4206,3 +4206,12 @@ int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)
return (sr >> 2) & 7; } + +__weak int spi_get_env_dev(void) +{ +#ifdef CONFIG_ENV_SPI_BUS + return CONFIG_ENV_SPI_BUS; +#else + return 0; +#endif +} diff --git a/env/sf.c b/env/sf.c index 8f5c03b00d..4d40fd613e 100644 --- a/env/sf.c +++ b/env/sf.c @@ -44,9 +44,10 @@ static int setup_flash_device(struct spi_flash **env_flash) #if CONFIG_IS_ENABLED(DM_SPI_FLASH) struct udevice *new; int ret; + int dev = spi_get_env_dev();
/* speed and mode will be read from DT */ - ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, + ret = spi_flash_probe_bus_cs(dev, CONFIG_ENV_SPI_CS, &new); if (ret) { env_set_default("spi_flash_probe_bus_cs() failed", 0); diff --git a/include/spi.h b/include/spi.h index 7e38cc2a2a..9e9851284c 100644 --- a/include/spi.h +++ b/include/spi.h @@ -743,4 +743,6 @@ int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep, #define spi_get_ops(dev) ((struct dm_spi_ops *)(dev)->driver->ops) #define spi_emul_get_ops(dev) ((struct dm_spi_emul_ops *)(dev)->driver->ops)
+int spi_get_env_dev(void); + #endif /* _SPI_H_ */

Versal NET boards has QSPI and OSPI and default bus set to 0 is not working when system is booting out of OSPI which is controller 1, as fixed aliases are set for all the boards i.e., QSPI to 0 and OSPI to 1. Add controller autodetection via spi_get_env_dev().
Signed-off-by: Venkatesh Yadav Abbarapu venkatesh.abbarapu@amd.com --- board/xilinx/versal-net/board.c | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
diff --git a/board/xilinx/versal-net/board.c b/board/xilinx/versal-net/board.c index da03024e16..5a93cb29c0 100644 --- a/board/xilinx/versal-net/board.c +++ b/board/xilinx/versal-net/board.c @@ -194,6 +194,51 @@ static u8 versal_net_get_bootmode(void) return bootmode; }
+int spi_get_env_dev(void) +{ + struct udevice *dev; + const char *mode = NULL; + int bootseq = -1; + + switch (versal_net_get_bootmode()) { + case QSPI_MODE_24BIT: + puts("QSPI_MODE_24\n"); + if (uclass_get_device_by_name(UCLASS_SPI, + "spi@f1030000", &dev)) { + debug("QSPI driver for QSPI device is not present\n"); + break; + } + mode = "xspi"; + bootseq = dev_seq(dev); + break; + case QSPI_MODE_32BIT: + puts("QSPI_MODE_32\n"); + if (uclass_get_device_by_name(UCLASS_SPI, + "spi@f1030000", &dev)) { + debug("QSPI driver for QSPI device is not present\n"); + break; + } + mode = "xspi"; + bootseq = dev_seq(dev); + break; + case OSPI_MODE: + puts("OSPI_MODE\n"); + if (uclass_get_device_by_name(UCLASS_SPI, + "spi@f1010000", &dev)) { + debug("OSPI driver for OSPI device is not present\n"); + break; + } + mode = "xspi"; + bootseq = dev_seq(dev); + break; + default: + break; + } + + debug("bootseq %d\n", bootseq); + return bootseq; +} + static int boot_targets_setup(void) { u8 bootmode;

On 6/14/24 14:48, Venkatesh Yadav Abbarapu wrote:
This enables boards to choose where to/from the environment should be saved/loaded either QSPI or OSPI based on the bootmode.
Venkatesh Yadav Abbarapu (2): env_spi: support overriding spi dev from board code xilinx: versal-net: Handle spi seq number based on boot device
board/xilinx/versal-net/board.c | 45 +++++++++++++++++++++++++++++++++ drivers/mtd/spi/spi-nor-core.c | 9 +++++++ env/sf.c | 3 ++- include/spi.h | 2 ++ 4 files changed, 58 insertions(+), 1 deletion(-)
Applied. M
participants (2)
-
Michal Simek
-
Venkatesh Yadav Abbarapu