@@ -29,6 +29,17 @@ LOG_MODULE_REGISTER(flash_stm32, CONFIG_FLASH_LOG_LEVEL);
2929 */
3030#define STM32_FLASH_TIMEOUT (2 * DT_PROP(DT_INST(0, soc_nv_flash), max_erase_time))
3131
32+ #if defined(FLASH_NSSR_NSBSY ) /* For STM32L5x in non-secure mode */
33+ #define FLASH_SECURITY_NS
34+ #elif defined(FLASH_SECSR_SECBSY ) /* For STM32L5x in secured mode */
35+ #error Flash is not supported in secure mode
36+ #define FLASH_SECURITY_SEC
37+ #else
38+ #define FLASH_SECURITY_NA /* For series which does not have
39+ * secured or non-secured mode
40+ */
41+ #endif
42+
3243static const struct flash_parameters flash_stm32_parameters = {
3344 .write_block_size = FLASH_STM32_WRITE_BLOCK_SIZE ,
3445 /* Some SoCs (L0/L1) use an EEPROM under the hood. Distinguish
@@ -93,13 +104,21 @@ static int flash_stm32_check_status(const struct device *dev)
93104#if defined(FLASH_FLAG_OPERR )
94105 FLASH_FLAG_OPERR |
95106#endif
107+ #if defined(FLASH_FLAG_PROGERR )
108+ FLASH_FLAG_PROGERR |
109+ #endif
96110#if defined(FLASH_FLAG_PGERR )
97111 FLASH_FLAG_PGERR |
98112#endif
99113 FLASH_FLAG_WRPERR ;
100114
115+ #if defined(FLASH_SECURITY_NS )
116+ if (FLASH_STM32_REGS (dev )-> NSSR & error ) {
117+ LOG_DBG ("Status: 0x%08x" , FLASH_STM32_REGS (dev )-> NSSR & error );
118+ #else /* FLASH_SECURITY_SEC | FLASH_SECURITY_NA */
101119 if (FLASH_STM32_REGS (dev )-> SR & error ) {
102120 LOG_DBG ("Status: 0x%08x" , FLASH_STM32_REGS (dev )-> SR & error );
121+ #endif /* FLASH_SECURITY_NS */
103122 return - EIO ;
104123 }
105124
@@ -116,11 +135,16 @@ int flash_stm32_wait_flash_idle(const struct device *dev)
116135 if (rc < 0 ) {
117136 return - EIO ;
118137 }
119- #if defined(CONFIG_SOC_SERIES_STM32G0X )
138+ #if defined(FLASH_SECURITY_NS )
139+ while ((FLASH_STM32_REGS (dev )-> NSSR & FLASH_FLAG_BSY )) {
140+ #else /* FLASH_SECURITY_SEC | FLASH_SECURITY_NA */
141+ #if defined(FLASH_SR_BSY1 )
142+ /* Applicable for STM32G0 series */
120143 while ((FLASH_STM32_REGS (dev )-> SR & FLASH_SR_BSY1 )) {
121144#else
122145 while ((FLASH_STM32_REGS (dev )-> SR & FLASH_SR_BSY )) {
123146#endif
147+ #endif /* FLASH_SECURITY_NS */
124148 if (k_uptime_get () > timeout_time ) {
125149 LOG_ERR ("Timeout! val: %d" , STM32_FLASH_TIMEOUT );
126150 return - EIO ;
@@ -134,7 +158,7 @@ static void flash_stm32_flush_caches(const struct device *dev,
134158 off_t offset , size_t len )
135159{
136160#if defined(CONFIG_SOC_SERIES_STM32F0X ) || defined(CONFIG_SOC_SERIES_STM32F3X ) || \
137- defined(CONFIG_SOC_SERIES_STM32G0X )
161+ defined(CONFIG_SOC_SERIES_STM32G0X ) || defined( CONFIG_SOC_SERIES_STM32L5X )
138162 ARG_UNUSED (dev );
139163 ARG_UNUSED (offset );
140164 ARG_UNUSED (len );
@@ -266,6 +290,16 @@ static int flash_stm32_write_protection(const struct device *dev, bool enable)
266290 }
267291 }
268292
293+ #if defined(FLASH_SECURITY_NS )
294+ if (enable ) {
295+ regs -> NSCR |= FLASH_NSCR_NSLOCK ;
296+ } else {
297+ if (regs -> NSCR & FLASH_NSCR_NSLOCK ) {
298+ regs -> NSKEYR = FLASH_KEY1 ;
299+ regs -> NSKEYR = FLASH_KEY2 ;
300+ }
301+ }
302+ #else /* FLASH_SECURITY_SEC | FLASH_SECURITY_NA */
269303#if defined(FLASH_CR_LOCK )
270304 if (enable ) {
271305 regs -> CR |= FLASH_CR_LOCK ;
@@ -293,6 +327,7 @@ static int flash_stm32_write_protection(const struct device *dev, bool enable)
293327 }
294328 }
295329#endif
330+ #endif /* FLASH_SECURITY_NS */
296331
297332 if (enable ) {
298333 LOG_DBG ("Enable write protection" );
@@ -337,7 +372,7 @@ static const struct flash_driver_api flash_stm32_api = {
337372static int stm32_flash_init (const struct device * dev )
338373{
339374 int rc ;
340- /* Below is applicable to F0, F1, F3, G0, G4, L1, L4 & WB55 series.
375+ /* Below is applicable to F0, F1, F3, G0, G4, L1, L4, L5 & WB55 series.
341376 * For F2, F4, F7 & H7 series, this is not applicable.
342377 */
343378#if DT_INST_NODE_HAS_PROP (0 , clocks )
0 commit comments