-
Notifications
You must be signed in to change notification settings - Fork 8.2k
drivers: flash: stm32g0: Implement APIs for option bytes and RDP #93159
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
Merged
cfriedt
merged 2 commits into
zephyrproject-rtos:main
from
martinjaeger:stm32g0x-flash-rdp
Oct 8, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ LOG_MODULE_REGISTER(LOG_DOMAIN); | |
| #include <zephyr/device.h> | ||
| #include <string.h> | ||
| #include <zephyr/drivers/flash.h> | ||
| #include <zephyr/sys/barrier.h> | ||
| #include <zephyr/init.h> | ||
| #include <soc.h> | ||
|
|
||
|
|
@@ -195,6 +196,64 @@ int flash_stm32_write_range(const struct device *dev, unsigned int offset, | |
| return rc; | ||
| } | ||
|
|
||
| int flash_stm32_option_bytes_write(const struct device *dev, uint32_t mask, | ||
| uint32_t value) | ||
| { | ||
| FLASH_TypeDef *regs = FLASH_STM32_REGS(dev); | ||
| int rc; | ||
|
|
||
| if (regs->CR & FLASH_CR_OPTLOCK) { | ||
| return -EIO; | ||
| } | ||
|
|
||
| if ((regs->OPTR & mask) == value) { | ||
| return 0; | ||
| } | ||
|
|
||
| rc = flash_stm32_wait_flash_idle(dev); | ||
| if (rc < 0) { | ||
| return rc; | ||
| } | ||
|
|
||
| regs->OPTR = (regs->OPTR & ~mask) | value; | ||
| regs->CR |= FLASH_CR_OPTSTRT; | ||
|
|
||
| /* Make sure previous write is completed. */ | ||
| barrier_dsync_fence_full(); | ||
|
|
||
| rc = flash_stm32_wait_flash_idle(dev); | ||
| if (rc < 0) { | ||
| return rc; | ||
| } | ||
|
|
||
| /* Force the option byte loading */ | ||
| regs->CR |= FLASH_CR_OBL_LAUNCH; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may need to be adjusted based on the outcome of #91945. |
||
|
|
||
| return flash_stm32_wait_flash_idle(dev); | ||
| } | ||
|
|
||
| uint32_t flash_stm32_option_bytes_read(const struct device *dev) | ||
| { | ||
| FLASH_TypeDef *regs = FLASH_STM32_REGS(dev); | ||
|
|
||
| return regs->OPTR; | ||
| } | ||
|
|
||
| #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION) | ||
| uint8_t flash_stm32_get_rdp_level(const struct device *dev) | ||
| { | ||
| FLASH_TypeDef *regs = FLASH_STM32_REGS(dev); | ||
|
|
||
| return (regs->OPTR & FLASH_OPTR_RDP_Msk) >> FLASH_OPTR_RDP_Pos; | ||
| } | ||
|
|
||
| void flash_stm32_set_rdp_level(const struct device *dev, uint8_t level) | ||
| { | ||
| flash_stm32_option_bytes_write(dev, FLASH_OPTR_RDP_Msk, | ||
| (uint32_t)level << FLASH_OPTR_RDP_Pos); | ||
| } | ||
| #endif /* CONFIG_FLASH_STM32_READOUT_PROTECTION */ | ||
|
|
||
| /* | ||
| * The address space is always continuous, even though a subset of G0 SoCs has | ||
| * two flash banks. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
if (regs->CR & FLASH_CR_OPTLOCK != 0)(despite other STM32 drivers don't do this).Non-blocking comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0Uif we wanted to be pedantic 😉 (And I'd add parentheses to ensure no ambiguity about order of evaluation)