From ff34d575bc154f79271ecfb79c0362ef9e9cd73f Mon Sep 17 00:00:00 2001 From: Francois Ramu Date: Thu, 27 Jun 2024 09:50:38 +0200 Subject: [PATCH] drivers: flash: stm32 qspi flash driver with unprotect command Add the write_unprotect command to the stm32 qspi flash driver to un protect flash before any write operation to the external quad-NOR Signed-off-by: Francois Ramu --- drivers/flash/flash_stm32_qspi.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/drivers/flash/flash_stm32_qspi.c b/drivers/flash/flash_stm32_qspi.c index bb2fe258685..3e63cb979c7 100644 --- a/drivers/flash/flash_stm32_qspi.c +++ b/drivers/flash/flash_stm32_qspi.c @@ -352,6 +352,27 @@ static int qspi_read_jedec_id(const struct device *dev, uint8_t *id) } #endif /* CONFIG_FLASH_JESD216_API */ +static int qspi_write_unprotect(const struct device *dev) +{ + int ret = 0; + QSPI_CommandTypeDef cmd_unprotect = { + .Instruction = SPI_NOR_CMD_ULBPR, + .InstructionMode = QSPI_INSTRUCTION_1_LINE, + }; + + if (IS_ENABLED(DT_INST_PROP(0, requires_ulbpr))) { + ret = qspi_send_cmd(dev, &cmd_write_en); + + if (ret != 0) { + return ret; + } + + ret = qspi_send_cmd(dev, &cmd_unprotect); + } + + return ret; +} + /* * Read Serial Flash Discovery Parameter */ @@ -1498,6 +1519,13 @@ static int flash_stm32_qspi_init(const struct device *dev) } #endif /* CONFIG_FLASH_PAGE_LAYOUT */ + ret = qspi_write_unprotect(dev); + if (ret != 0) { + LOG_ERR("write unprotect failed: %d", ret); + return -ENODEV; + } + LOG_DBG("Write Un-protected"); + #ifdef CONFIG_STM32_MEMMAP #if DT_PROP(DT_NODELABEL(quadspi), dual_flash) && defined(QUADSPI_CR_DFM) /*