Skip to content

Commit eae2f22

Browse files
committed
drivers: flash: flash_stm32_xspi: Allow flash driver init in ram load
Current driver protections prevent to initialize driver when run as from an external application running from ext flash. Aim is to avoid performing full driver initialization of the NOR flash controller the application is read from. But this problem is actually only valid when application is running in XIP mode (read in memory mapped mode at run time). In ram load mode, since there is no direct activity from the application on the NOR device, nothing prevents the ext flash driver to be used fully. Hence, we should allow the controller initialization to happen, with some adjustments. Mostly, what we need is to: - skip the hal init - abort memory mapping afterwards to let the jedec reading happen. Remove conditions around `stm32_xspi_is_memorymap()` and `stm32_xspi_abort()` which can finally be useful in various cases. Signed-off-by: Erwan Gouriou <[email protected]>
1 parent 211681d commit eae2f22

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

drivers/flash/flash_stm32_xspi.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ static int stm32_xspi_set_memorymap(const struct device *dev)
997997
LOG_DBG("MemoryMap mode enabled");
998998
return 0;
999999
}
1000+
#endif /* CONFIG_STM32_MEMMAP */
10001001

10011002
static int stm32_xspi_abort(const struct device *dev)
10021003
{
@@ -1009,18 +1010,14 @@ static int stm32_xspi_abort(const struct device *dev)
10091010

10101011
return 0;
10111012
}
1012-
#endif /* CONFIG_STM32_MEMMAP */
10131013

1014-
1015-
#if defined(CONFIG_STM32_MEMMAP) || defined(CONFIG_STM32_APP_IN_EXT_FLASH)
10161014
/* Function to return true if the octoflash is in MemoryMapped else false */
10171015
static bool stm32_xspi_is_memorymap(const struct device *dev)
10181016
{
10191017
struct flash_stm32_xspi_data *dev_data = dev->data;
10201018

10211019
return stm32_reg_read_bits(&dev_data->hxspi.Instance->CR, XSPI_CR_FMODE) == XSPI_CR_FMODE;
10221020
}
1023-
#endif
10241021

10251022
/*
10261023
* Function to erase the flash : chip or sector with possible OCTO/SPI and STR/DTR
@@ -1218,7 +1215,7 @@ static int flash_stm32_xspi_read(const struct device *dev, off_t addr,
12181215
return 0;
12191216
}
12201217

1221-
#if defined(CONFIG_STM32_MEMMAP) || defined(CONFIG_STM32_APP_IN_EXT_FLASH)
1218+
#if defined(CONFIG_STM32_MEMMAP) || defined(CONFIG_STM32_APP_IN_EXT_FLASH) && defined(CONFIG_XIP)
12221219
ARG_UNUSED(dev_cfg);
12231220
ARG_UNUSED(dev_data);
12241221
/*
@@ -2093,7 +2090,7 @@ static int flash_stm32_xspi_init(const struct device *dev)
20932090
return -ENODEV;
20942091
}
20952092

2096-
#ifdef CONFIG_STM32_APP_IN_EXT_FLASH
2093+
#if defined(CONFIG_STM32_APP_IN_EXT_FLASH) && defined(CONFIG_XIP)
20972094
/* If MemoryMapped then configure skip init
20982095
* Check clock status first as reading CR register without bus clock doesn't work on N6
20992096
* If clock is off, then MemoryMapped is off too and we do init
@@ -2195,13 +2192,29 @@ static int flash_stm32_xspi_init(const struct device *dev)
21952192
dev_data->hxspi.Init.DelayHoldQuarterCycle = HAL_XSPI_DHQC_ENABLE;
21962193
}
21972194

2195+
if (stm32_xspi_is_memorymap(dev)) {
2196+
/* Memory map could have been set by previous application */
2197+
/* Force HAL instance in correct state */
2198+
dev_data->hxspi.State = HAL_XSPI_STATE_BUSY_MEM_MAPPED;
2199+
}
2200+
21982201
if (HAL_XSPI_Init(&dev_data->hxspi) != HAL_OK) {
21992202
LOG_ERR("XSPI Init failed");
22002203
return -EIO;
22012204
}
22022205

22032206
LOG_DBG("XSPI Init'd");
22042207

2208+
if (stm32_xspi_is_memorymap(dev)) {
2209+
/* Memory map could have been set by previous application */
2210+
/* Abort to allow following card oriented transactions */
2211+
/* If needed by the application, it will be re-enabled just after */
2212+
ret = stm32_xspi_abort(dev);
2213+
if (ret != 0) {
2214+
LOG_ERR("Failed to abort memory-mapped access before init");
2215+
}
2216+
}
2217+
22052218
#if defined(HAL_XSPIM_IOPORT_1) || defined(HAL_XSPIM_IOPORT_2) || \
22062219
defined(XSPIM) || defined(XSPIM1) || defined(XSPIM2)
22072220
/* XSPI I/O manager init Function */

0 commit comments

Comments
 (0)