|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Teslabs Engineering S.L. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT st_stm32_backup_sram |
| 8 | + |
| 9 | +#include <device.h> |
| 10 | +#include <drivers/clock_control/stm32_clock_control.h> |
| 11 | + |
| 12 | +#include <stm32_ll_pwr.h> |
| 13 | + |
| 14 | +#include <logging/log.h> |
| 15 | +LOG_MODULE_REGISTER(stm32_backup_sram, CONFIG_SOC_LOG_LEVEL); |
| 16 | + |
| 17 | +struct stm32_backup_sram_config { |
| 18 | + struct stm32_pclken pclken; |
| 19 | +}; |
| 20 | + |
| 21 | +static int stm32_backup_sram_init(const struct device *dev) |
| 22 | +{ |
| 23 | + const struct stm32_backup_sram_config *config = dev->config; |
| 24 | + |
| 25 | + int ret; |
| 26 | + const struct device *clk; |
| 27 | + |
| 28 | + /* enable backup SRAM clock */ |
| 29 | + clk = device_get_binding(STM32_CLOCK_CONTROL_NAME); |
| 30 | + __ASSERT_NO_MSG(clk); |
| 31 | + |
| 32 | + ret = clock_control_on(clk, (clock_control_subsys_t *)&config->pclken); |
| 33 | + if (ret < 0) { |
| 34 | + LOG_ERR("Could not initialize backup SRAM clock (%d)", ret); |
| 35 | + return ret; |
| 36 | + } |
| 37 | + |
| 38 | + /* enable write access to backup domain */ |
| 39 | + LL_PWR_EnableBkUpAccess(); |
| 40 | + while (!LL_PWR_IsEnabledBkUpAccess()) { |
| 41 | + } |
| 42 | + |
| 43 | + /* enable backup sram regulator (required to retain backup SRAM content |
| 44 | + * while in standby or VBAT modes). |
| 45 | + */ |
| 46 | + LL_PWR_EnableBkUpRegulator(); |
| 47 | + while (!LL_PWR_IsEnabledBkUpRegulator()) { |
| 48 | + } |
| 49 | + |
| 50 | + return 0; |
| 51 | +} |
| 52 | + |
| 53 | +static const struct stm32_backup_sram_config config = { |
| 54 | + .pclken = { .bus = DT_INST_CLOCKS_CELL(0, bus), |
| 55 | + .enr = DT_INST_CLOCKS_CELL(0, bits) }, |
| 56 | +}; |
| 57 | + |
| 58 | +DEVICE_DT_INST_DEFINE(0, stm32_backup_sram_init, device_pm_control_nop, |
| 59 | + NULL, &config, POST_KERNEL, |
| 60 | + CONFIG_APPLICATION_INIT_PRIORITY, NULL); |
0 commit comments