Skip to content

Commit 0932595

Browse files
khoatranyjduynguyenxa
authored andcommitted
soc: renesas: ra: Add battery backup support for RA8 family
Add support for the battery backup (VBAT) functionality on Renesas RA8 family. This allows the RTC to retain timekeeping data when the main power supply is lost by switching to the VBAT domain automatically. This commit add support for these SoC series: ra8m1, ra8p1, ra8d1 Signed-off-by: Khoa Tran <[email protected]>
1 parent 470285a commit 0932595

File tree

13 files changed

+144
-2
lines changed

13 files changed

+144
-2
lines changed

soc/renesas/ra/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
zephyr_include_directories(common)
66
zephyr_include_directories_ifdef(CONFIG_HAS_RENESAS_RA_FSP common_fsp)
77

8+
add_subdirectory_ifdef(CONFIG_HAS_RENESAS_RA_FSP common_fsp)
89
add_subdirectory(${SOC_SERIES})

soc/renesas/ra/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ config CPU_HAS_RENESAS_RA_IDAU
2424

2525
rsource "*/Kconfig"
2626

27+
config RENESAS_RA_BATTERY_BACKUP_MANUAL_CONFIGURE
28+
bool "VBAT switching manual"
29+
default y
30+
depends on DT_HAS_RENESAS_RA_BATTERY_BACKUP_ENABLED && $(dt_compat_any_has_prop,$(DT_COMPAT_RENESAS_RA_BATTERY_BACKUP),manual-configure)
31+
help
32+
Enable if this SoC's battery backup domain allows switching to VBAT manually.
33+
Leave disabled if switching is automatic.
34+
2735
endif # SOC_FAMILY_RENESAS_RA
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2025 Renesas Electronics Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
zephyr_sources_ifdef(CONFIG_RENESAS_RA_BATTERY_BACKUP_MANUAL_CONFIGURE battery_backup.c)
5+
zephyr_sources(cold_start.c)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include "battery_backup.h"
7+
8+
#define VCC_DROP_DETECTION_STABILIZATION_WAIT_TIME_US 20
9+
#define VBTBPSR_VBPORF_IS_SET BIT(0)
10+
#define VBTBPCR2_VDETLVL_SETTING_NOT_USED 0x6
11+
12+
static uint8_t vbtbpsr_state_at_boot;
13+
14+
bool is_backup_domain_reset_happen(void)
15+
{
16+
return (vbtbpsr_state_at_boot & VBTBPSR_VBPORF_IS_SET);
17+
}
18+
19+
void battery_backup_init(void)
20+
{
21+
#if DT_NODE_HAS_PROP(DT_NODELABEL(battery_backup), switch_threshold)
22+
/* Check VBPORM bit. If VBPORM flag is 0, wait until it changes to 1 */
23+
while (R_SYSTEM->VBTBPSR_b.VBPORM == 0) {
24+
}
25+
vbtbpsr_state_at_boot = R_SYSTEM->VBTBPSR;
26+
if (R_SYSTEM->VBTBPSR_b.VBPORF == 1) {
27+
R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_OM_LPC_BATT);
28+
R_SYSTEM->VBTBPSR_b.VBPORF = 0;
29+
R_SYSTEM->VBTBPCR2_b.VDETLVL =
30+
DT_ENUM_IDX(DT_NODELABEL(battery_backup), switch_threshold);
31+
R_BSP_SoftwareDelay(VCC_DROP_DETECTION_STABILIZATION_WAIT_TIME_US,
32+
BSP_DELAY_UNITS_MICROSECONDS);
33+
R_SYSTEM->VBTBPCR2_b.VDETE = 1;
34+
R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_OM_LPC_BATT);
35+
}
36+
#else
37+
/* Set the BPWSWSTP bit to 1. The power supply switch is stopped */
38+
R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_OM_LPC_BATT);
39+
R_SYSTEM->VBTBPCR1_b.BPWSWSTP = 1;
40+
41+
/* Check VBPORM flag. If VBPORM flag is 0, wait until it changes to 1 */
42+
while (R_SYSTEM->VBTBPSR_b.VBPORM == 0) {
43+
}
44+
vbtbpsr_state_at_boot = R_SYSTEM->VBTBPSR;
45+
R_SYSTEM->VBTBPSR_b.VBPORF = 0;
46+
R_SYSTEM->VBTBPCR2_b.VDETE = 0;
47+
R_SYSTEM->VBTBPCR2_b.VDETLVL = DT_ENUM_IDX_OR(
48+
DT_NODELABEL(battery_backup), switch_threshold, VBTBPCR2_VDETLVL_SETTING_NOT_USED);
49+
R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_OM_LPC_BATT);
50+
51+
/* Set the SOSTP bit to 1 regardless of its value. Stop Sub-Clock Oscillator */
52+
R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_CGC);
53+
R_SYSTEM->SOSCCR_b.SOSTP = 1;
54+
R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_CGC);
55+
#endif /* BATTERY_BACKUP_CONFIGURATION_NOT_USED */
56+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_SOC_RENESAS_RA_BATTERY_BACKUP_H_
8+
#define ZEPHYR_SOC_RENESAS_RA_BATTERY_BACKUP_H_
9+
10+
#include <zephyr/device.h>
11+
#include <bsp_api.h>
12+
13+
bool is_backup_domain_reset_happen(void);
14+
void battery_backup_init(void);
15+
16+
#endif /* ZEPHYR_SOC_RENESAS_RA_BATTERY_BACKUP_H_ */
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <zephyr/device.h>
7+
#include <bsp_api.h>
8+
#include "cold_start.h"
9+
10+
#define RSTSR2_CWSF_BIT_MASK BIT(0)
11+
12+
static uint8_t rstsr2_state_at_boot;
13+
14+
bool is_power_on_reset_happen(void)
15+
{
16+
return ((rstsr2_state_at_boot & RSTSR2_CWSF_BIT_MASK) == 0);
17+
}
18+
19+
void cold_start_handler(void)
20+
{
21+
/* Detect power on reset */
22+
rstsr2_state_at_boot = R_SYSTEM->RSTSR2;
23+
if (R_SYSTEM->RSTSR2_b.CWSF == 0) {
24+
#if defined(CONFIG_RENESAS_RA_BATTERY_BACKUP_MANUAL_CONFIGURE)
25+
battery_backup_init();
26+
#endif /* CONFIG_RENESAS_RA_BATTERY_BACKUP_MANUAL_CONFIGURE */
27+
R_SYSTEM->RSTSR2_b.CWSF = 1;
28+
}
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_SOC_RENESAS_RA_COLD_START_H_
8+
#define ZEPHYR_SOC_RENESAS_RA_COLD_START_H_
9+
10+
#include <zephyr/device.h>
11+
#include <bsp_api.h>
12+
#include "battery_backup.h"
13+
14+
bool is_power_on_reset_happen(void);
15+
void cold_start_handler(void);
16+
17+
#endif /* ZEPHYR_SOC_RENESAS_RA_COLD_START_H_ */

soc/renesas/ra/ra8d1/soc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ void soc_early_init_hook(void)
7474

7575
z_arm_nmi_set_handler(NMI_Handler);
7676
#endif /* CONFIG_RUNTIME_NMI */
77+
78+
cold_start_handler();
7779
}

soc/renesas/ra/ra8d1/soc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Renesas Electronics Corporation
2+
* Copyright (c) 2024-2025 Renesas Electronics Corporation
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -12,5 +12,6 @@
1212
#define ZEPHYR_SOC_RENESAS_RA8D1_SOC_H_
1313

1414
#include <bsp_api.h>
15+
#include <cold_start.h>
1516

1617
#endif /* ZEPHYR_SOC_RENESAS_RA8D1_SOC_H_ */

soc/renesas/ra/ra8m1/soc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ void soc_early_init_hook(void)
4747

4848
z_arm_nmi_set_handler(NMI_Handler);
4949
#endif /* CONFIG_RUNTIME_NMI */
50+
51+
cold_start_handler();
5052
}

0 commit comments

Comments
 (0)