drivers: bbram: stm32-bbram: fix write for devices with TAMP

On devices where the backup memory is part of the TAMP peripheral, the
Backup Domain Protection prevents write to the backup registers. This
fix disables the protection before writing registers and re-enables it
afterwards.

Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
This commit is contained in:
Aurelien Jarno 2024-06-06 20:29:34 +02:00 committed by Maureen Helm
parent e90c89d453
commit 326d6059c6

View File

@ -11,6 +11,7 @@
#include <zephyr/drivers/bbram.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/util.h>
#include <stm32_ll_pwr.h>
#include <stm32_ll_rtc.h>
LOG_MODULE_REGISTER(bbram, CONFIG_BBRAM_LOG_LEVEL);
@ -65,6 +66,10 @@ static int bbram_stm32_write(const struct device *dev, size_t offset, size_t siz
return -EFAULT;
}
#if defined(PWR_CR_DBP) || defined(PWR_CR1_DBP) || defined(PWR_DBPCR_DBP) || defined(PWR_DBPR_DBP)
LL_PWR_EnableBkUpAccess();
#endif /* PWR_CR_DBP || PWR_CR1_DBP || PWR_DBPCR_DBP || PWR_DBPR_DBP */
for (size_t written = 0; written < size; written += to_copy) {
reg = STM32_BKP_REG(STM32_BKP_REG_INDEX(offset + written));
begin = STM32_BKP_REG_BYTE_INDEX(offset + written);
@ -73,6 +78,10 @@ static int bbram_stm32_write(const struct device *dev, size_t offset, size_t siz
STM32_BKP_REG(STM32_BKP_REG_INDEX(offset + written)) = reg;
}
#if defined(PWR_CR_DBP) || defined(PWR_CR1_DBP) || defined(PWR_DBPCR_DBP) || defined(PWR_DBPR_DBP)
LL_PWR_DisableBkUpAccess();
#endif /* PWR_CR_DBP || PWR_CR1_DBP || PWR_DBPCR_DBP || PWR_DBPR_DBP */
return 0;
}