Skip to content

Commit f8399bd

Browse files
benediktibknashif
authored andcommitted
drivers: flash: implement RDP for STM32F7
Implement the readout protection for the STM32F7 series. Signed-off-by: Benedikt Schmidt <[email protected]>
1 parent db2261b commit f8399bd

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

drivers/flash/Kconfig.stm32

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ config FLASH_STM32_WRITE_PROTECT_DISABLE_PREVENTION
5050

5151
config FLASH_STM32_READOUT_PROTECTION
5252
bool "Extended operation for flash readout protection control"
53-
depends on SOC_SERIES_STM32F4X || SOC_SERIES_STM32L4X || SOC_SERIES_STM32G4X
53+
depends on SOC_SERIES_STM32F4X || SOC_SERIES_STM32L4X || \
54+
SOC_SERIES_STM32G4X || SOC_SERIES_STM32F7X
5455
select FLASH_HAS_EX_OP
5556
default n
5657
help

drivers/flash/flash_stm32f7x.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,48 @@ int flash_stm32_write_range(const struct device *dev, unsigned int offset,
159159
return rc;
160160
}
161161

162+
static __unused int write_optb(const struct device *dev, uint32_t mask,
163+
uint32_t value)
164+
{
165+
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
166+
int rc;
167+
168+
if (regs->OPTCR & FLASH_OPTCR_OPTLOCK) {
169+
return -EIO;
170+
}
171+
172+
if ((regs->OPTCR & mask) == value) {
173+
return 0;
174+
}
175+
176+
rc = flash_stm32_wait_flash_idle(dev);
177+
if (rc < 0) {
178+
return rc;
179+
}
180+
181+
regs->OPTCR = (regs->OPTCR & ~mask) | value;
182+
regs->OPTCR |= FLASH_OPTCR_OPTSTRT;
183+
184+
/* Make sure previous write is completed. */
185+
barrier_dsync_fence_full();
186+
187+
return flash_stm32_wait_flash_idle(dev);
188+
}
189+
190+
#if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION)
191+
uint8_t flash_stm32_get_rdp_level(const struct device *dev)
192+
{
193+
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
194+
195+
return (regs->OPTCR & FLASH_OPTCR_RDP_Msk) >> FLASH_OPTCR_RDP_Pos;
196+
}
197+
198+
void flash_stm32_set_rdp_level(const struct device *dev, uint8_t level)
199+
{
200+
write_optb(dev, FLASH_OPTCR_RDP_Msk,
201+
(uint32_t)level << FLASH_OPTCR_RDP_Pos);
202+
}
203+
#endif /* CONFIG_FLASH_STM32_READOUT_PROTECTION */
162204

163205
/* Some SoC can run in single or dual bank mode, others can't.
164206
* Different SoC flash layouts are specified in various reference

0 commit comments

Comments
 (0)