Skip to content

Commit aeed3d3

Browse files
committed
drivers: flash: stm32 ospi driver aborts memmap before erase/write
This change is aborting the memoryMapped mode of the octo-flash before erasing or writing the NOR. Operations are performed in command mode. Reading is always performed in MemoryMapped mode (memcopy) Signed-off-by: Francois Ramu <[email protected]>
1 parent 44a1934 commit aeed3d3

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

drivers/flash/flash_stm32_ospi.c

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ static int stm32_ospi_set_memorymap(const struct device *dev)
998998
HAL_OSPI_ADDRESS_24_BITS)
999999
? SPI_NOR_CMD_READ_FAST
10001000
: SPI_NOR_CMD_READ_FAST_4B)
1001-
: SPI_NOR_OCMD_RD)
1001+
: dev_data->read_opcode)
10021002
: SPI_NOR_OCMD_DTR_RD;
10031003
s_command.AddressMode = (dev_cfg->data_rate == OSPI_STR_TRANSFER)
10041004
? ((dev_cfg->data_mode == OSPI_SPI_MODE)
@@ -1063,11 +1063,11 @@ static int stm32_ospi_set_memorymap(const struct device *dev)
10631063

10641064
ret = HAL_OSPI_MemoryMapped(&dev_data->hospi, &s_MemMappedCfg);
10651065
if (ret != HAL_OK) {
1066-
LOG_ERR("%d: Failed to set memory mapped", ret);
1066+
LOG_ERR("%d: Failed to enable memory mapped", ret);
10671067
return -EIO;
10681068
}
10691069

1070-
LOG_INF("MemoryMap mode enabled");
1070+
LOG_DBG("MemoryMap mode enabled");
10711071
return 0;
10721072
}
10731073

@@ -1080,6 +1080,20 @@ static bool stm32_ospi_is_memorymap(const struct device *dev)
10801080
OCTOSPI_CR_FMODE) == OCTOSPI_CR_FMODE) ?
10811081
true : false);
10821082
}
1083+
1084+
/* Abort the Memorymapped Mode so that erasing is possible */
1085+
static int stm32_ospi_abort_memmap(const struct device *dev)
1086+
{
1087+
struct flash_stm32_ospi_data *dev_data = dev->data;
1088+
1089+
if (HAL_OSPI_Abort(&dev_data->hospi) != HAL_OK) {
1090+
LOG_ERR("MemoryMap abort failed");
1091+
return -EIO;
1092+
}
1093+
LOG_DBG("MemoryMap mode disabled");
1094+
1095+
return 0;
1096+
}
10831097
#endif /* CONFIG_STM32_MEMMAP */
10841098

10851099
/*
@@ -1119,8 +1133,12 @@ static int flash_stm32_ospi_erase(const struct device *dev, off_t addr,
11191133

11201134
#ifdef CONFIG_STM32_MEMMAP
11211135
if (stm32_ospi_is_memorymap(dev)) {
1122-
LOG_INF("MemoryMap : cannot erase");
1123-
return 0;
1136+
/* If the Flash is in MemoryMapped mode, abort it and continue */
1137+
LOG_INF("MemoryMap : disable before erase");
1138+
if (stm32_ospi_abort_memmap(dev) != 0) {
1139+
LOG_ERR("MemoryMap not aborted correctly");
1140+
return -EIO;
1141+
}
11241142
}
11251143
#endif /* CONFIG_STM32_MEMMAP */
11261144

@@ -1276,6 +1294,14 @@ static int flash_stm32_ospi_read(const struct device *dev, off_t addr,
12761294
}
12771295

12781296
#ifdef CONFIG_STM32_MEMMAP
1297+
/* If not MemMapped then configure it */
1298+
if (!stm32_ospi_is_memorymap(dev)) {
1299+
if (stm32_ospi_set_memorymap(dev) != 0) {
1300+
LOG_ERR("READ failed: cannot enable MemoryMap");
1301+
return -EIO;
1302+
}
1303+
}
1304+
LOG_INF("MemoryMap : enable before read");
12791305
if (stm32_ospi_is_memorymap(dev)) {
12801306
LOG_DBG("MemoryMapped Read offset: 0x%lx, len: %zu",
12811307
(long)(STM32_OSPI_BASE_ADDRESS + addr),
@@ -1379,6 +1405,18 @@ static int flash_stm32_ospi_write(const struct device *dev, off_t addr,
13791405

13801406
#ifdef CONFIG_STM32_MEMMAP
13811407
if (stm32_ospi_is_memorymap(dev)) {
1408+
/* If the Flash is in MemoryMapped mode, abort it and continue */
1409+
LOG_INF("MemoryMap : disable before write");
1410+
if (stm32_ospi_abort_memmap(dev) != 0) {
1411+
LOG_ERR("MemoryMap not aborted correctly");
1412+
return -EIO;
1413+
}
1414+
}
1415+
if (stm32_ospi_is_memorymap(dev)) {
1416+
/*
1417+
* If the Flash is in MemoryMapped mode, write by memcopy
1418+
* should not due to previous operation
1419+
*/
13821420
LOG_DBG("MemoryMapped Write offset: 0x%lx, len: %zu",
13831421
(long)(STM32_OSPI_BASE_ADDRESS + addr),
13841422
size);

0 commit comments

Comments
 (0)