Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: flash: qspi stm32 driver supporting requires_ulbpr #74534

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions drivers/flash/Kconfig.stm32
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ config FLASH_STM32_BLOCK_REGISTERS
registers improves system security, because flash content (or
protection settings) can't be changed even when exploit was found.

config USE_MICROCHIP_QSPI_FLASH_WITH_STM32
bool "Include patch for Microchip qspi flash when running with stm32"
depends on DT_HAS_ST_STM32_QSPI_NOR_ENABLED
help
Set to use Microchip qspi flash memories which supports
the Global Block Protection Unlock instruction (ULBPR - 98H),
and write with SPI_NOR_CMD_PP_1_1_4 on 4 lines

endif # SOC_FLASH_STM32
35 changes: 35 additions & 0 deletions drivers/flash/flash_stm32_qspi.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,16 @@
dev_data->qspi_write_cmd == SPI_NOR_CMD_PP_1_4_4);

cmd->Instruction = dev_data->qspi_write_cmd;
#if defined(CONFIG_USE_MICROCHIP_QSPI_FLASH_WITH_STM32)
/* Microchip qspi-NOR flash, does not follow the standard rules */
if (cmd->Instruction == SPI_NOR_CMD_PP_1_1_4) {
cmd->AddressMode = QSPI_ADDRESS_4_LINES;
}
#else
cmd->AddressMode = ((cmd->Instruction == SPI_NOR_CMD_PP_1_1_4)
? QSPI_ADDRESS_1_LINE
: QSPI_ADDRESS_4_LINES);
#endif /* CONFIG_USE_MICROCHIP_QSPI_FLASH_WITH_STM32 */
cmd->DataMode = QSPI_DATA_4_LINES;
cmd->DummyCycles = 0;

Expand Down Expand Up @@ -345,6 +352,27 @@
}
#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,
};

Check notice on line 361 in drivers/flash/flash_stm32_qspi.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/flash/flash_stm32_qspi.c:361 - .Instruction = SPI_NOR_CMD_ULBPR, - .InstructionMode = QSPI_INSTRUCTION_1_LINE, + .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
*/
Expand Down Expand Up @@ -1491,6 +1519,13 @@
}
#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");
FRASTM marked this conversation as resolved.
Show resolved Hide resolved

#ifdef CONFIG_STM32_MEMMAP
#if DT_PROP(DT_NODELABEL(quadspi), dual_flash) && defined(QUADSPI_CR_DFM)
/*
Expand Down
10 changes: 10 additions & 0 deletions dts/bindings/flash_controller/st,stm32-qspi-nor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,13 @@ properties:
supporting 1-4-4 mode also would support fast page programming.

If absent, then 1-4-4 program page is used in quad mode.

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.
Loading