Skip to content

Commit faf661e

Browse files
martinjaegercfriedt
authored andcommitted
drivers: flash: stm32g0: Implement option_bytes_write|read API
Implementation based on STM32G4 series. This is a preparation to enable reading and writing the RDP bits. Signed-off-by: Martin Jäger <[email protected]>
1 parent 50ac559 commit faf661e

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

drivers/flash/flash_stm32_ex_op.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ int flash_stm32_ex_op(const struct device *dev, uint16_t code,
331331
#if defined(CONFIG_FLASH_STM32_OPTION_BYTES) && ( \
332332
defined(CONFIG_DT_HAS_ST_STM32F4_FLASH_CONTROLLER_ENABLED) || \
333333
defined(CONFIG_DT_HAS_ST_STM32F7_FLASH_CONTROLLER_ENABLED) || \
334+
defined(CONFIG_DT_HAS_ST_STM32G0_FLASH_CONTROLLER_ENABLED) || \
334335
defined(CONFIG_DT_HAS_ST_STM32G4_FLASH_CONTROLLER_ENABLED) || \
335336
defined(CONFIG_DT_HAS_ST_STM32L4_FLASH_CONTROLLER_ENABLED))
336337
case FLASH_STM32_EX_OP_OPTB_READ:

drivers/flash/flash_stm32g0x.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ LOG_MODULE_REGISTER(LOG_DOMAIN);
1515
#include <zephyr/device.h>
1616
#include <string.h>
1717
#include <zephyr/drivers/flash.h>
18+
#include <zephyr/sys/barrier.h>
1819
#include <zephyr/init.h>
1920
#include <soc.h>
2021

@@ -195,6 +196,49 @@ int flash_stm32_write_range(const struct device *dev, unsigned int offset,
195196
return rc;
196197
}
197198

199+
int flash_stm32_option_bytes_write(const struct device *dev, uint32_t mask,
200+
uint32_t value)
201+
{
202+
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
203+
int rc;
204+
205+
if (regs->CR & FLASH_CR_OPTLOCK) {
206+
return -EIO;
207+
}
208+
209+
if ((regs->OPTR & mask) == value) {
210+
return 0;
211+
}
212+
213+
rc = flash_stm32_wait_flash_idle(dev);
214+
if (rc < 0) {
215+
return rc;
216+
}
217+
218+
regs->OPTR = (regs->OPTR & ~mask) | value;
219+
regs->CR |= FLASH_CR_OPTSTRT;
220+
221+
/* Make sure previous write is completed. */
222+
barrier_dsync_fence_full();
223+
224+
rc = flash_stm32_wait_flash_idle(dev);
225+
if (rc < 0) {
226+
return rc;
227+
}
228+
229+
/* Force the option byte loading */
230+
regs->CR |= FLASH_CR_OBL_LAUNCH;
231+
232+
return flash_stm32_wait_flash_idle(dev);
233+
}
234+
235+
uint32_t flash_stm32_option_bytes_read(const struct device *dev)
236+
{
237+
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
238+
239+
return regs->OPTR;
240+
}
241+
198242
/*
199243
* The address space is always continuous, even though a subset of G0 SoCs has
200244
* two flash banks.

0 commit comments

Comments
 (0)