Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions drivers/flash/flash_stm32_ospi.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,32 @@ static int stm32_ospi_write_enable(struct flash_stm32_ospi_data *dev_data,
return stm32_ospi_wait_auto_polling(dev_data, &s_config, HAL_OSPI_TIMEOUT_DEFAULT_VALUE);
}

static int ospi_write_unprotect(const struct device *dev)
{
struct flash_stm32_ospi_data *dev_data = dev->data;
int ret = 0;

/* This is a SPI/STR command to issue to the external Flash device */
OSPI_RegularCmdTypeDef cmd_unprotect = ospi_prepare_cmd(OSPI_SPI_MODE, OSPI_STR_TRANSFER);

cmd_unprotect.Instruction = SPI_NOR_CMD_ULBPR;
cmd_unprotect.InstructionMode = HAL_OSPI_INSTRUCTION_1_LINE;
cmd_unprotect.AddressMode = HAL_OSPI_ADDRESS_NONE;
cmd_unprotect.DataMode = HAL_OSPI_DATA_NONE;

if (IS_ENABLED(DT_INST_PROP(0, requires_ulbpr))) {
ret = stm32_ospi_write_enable(dev_data, OSPI_SPI_MODE, OSPI_STR_TRANSFER);

if (ret != 0) {
return ret;
}

ret = ospi_send_cmd(dev, &cmd_unprotect);
}

return ret;
}

/* Write Flash configuration register 2 with new dummy cycles */
static int stm32_ospi_write_cfg2reg_dummy(OSPI_HandleTypeDef *hospi,
uint8_t nor_mode, uint8_t nor_rate)
Expand Down Expand Up @@ -2569,6 +2595,13 @@ static int flash_stm32_ospi_init(const struct device *dev)
}
#endif /* CONFIG_FLASH_PAGE_LAYOUT */

ret = ospi_write_unprotect(dev);
if (ret != 0) {
LOG_ERR("write unprotect failed: %d", ret);
return -ENODEV;
}
LOG_DBG("Write Un-protected");

#ifdef CONFIG_STM32_MEMMAP
/* Now configure the octo Flash in MemoryMapped (access by address) */
ret = stm32_ospi_set_memorymap(dev);
Expand Down
32 changes: 32 additions & 0 deletions drivers/flash/flash_stm32_xspi.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,31 @@ static int stm32_xspi_write_enable(const struct device *dev,
return stm32_xspi_wait_auto_polling(dev, &s_config, HAL_XSPI_TIMEOUT_DEFAULT_VALUE);
}

static int xspi_write_unprotect(const struct device *dev)
{
int ret = 0;

/* This is a SPI/STR command to issue to the external Flash device */
XSPI_RegularCmdTypeDef cmd_unprotect = xspi_prepare_cmd(XSPI_SPI_MODE, XSPI_STR_TRANSFER);

cmd_unprotect.Instruction = SPI_NOR_CMD_ULBPR;
cmd_unprotect.InstructionMode = HAL_XSPI_INSTRUCTION_1_LINE;
cmd_unprotect.AddressMode = HAL_XSPI_ADDRESS_NONE;
cmd_unprotect.DataMode = HAL_XSPI_DATA_NONE;

if (IS_ENABLED(DT_INST_PROP(0, requires_ulbpr))) {
ret = stm32_xspi_write_enable(dev, XSPI_SPI_MODE, XSPI_STR_TRANSFER);

if (ret != 0) {
return ret;
}

ret = xspi_send_cmd(dev, &cmd_unprotect);
}

return ret;
}

/* Write Flash configuration register 2 with new dummy cycles */
static int stm32_xspi_write_cfg2reg_dummy(const struct device *dev,
uint8_t nor_mode, uint8_t nor_rate)
Expand Down Expand Up @@ -2372,6 +2397,13 @@ static int flash_stm32_xspi_init(const struct device *dev)
}
#endif /* CONFIG_FLASH_PAGE_LAYOUT */

ret = xspi_write_unprotect(dev);
if (ret != 0) {
LOG_ERR("write unprotect failed: %d", ret);
return -ENODEV;
}
LOG_DBG("Write Un-protected");

#ifdef CONFIG_STM32_MEMMAP
ret = stm32_xspi_set_memorymap(dev);
if (ret != 0) {
Expand Down
9 changes: 9 additions & 0 deletions dts/bindings/flash_controller/st,stm32-ospi-nor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ properties:
* 2READ 1-2-2 (0xBB) -> 2READ 1-2-2 4B (0xBC)
* QREAD 1-1-4 (0x6B) -> QREAD 1-1-4 4B (0x6C)
* 4READ 1-4-4 (0xEB) -> 4READ 1-4-4 4B (0xEC)
requires-ulbpr:
type: boolean
description: |
Indicates the device requires the ULBPR (0x98) command.

Some flash chips such as the Microchip SST26VF series have a block
protection register that initializes to write-protected. Use this
property to indicate that the BPR must be unlocked before write
operations can proceed.