driver: bbram: add bbram driver for rts5912

Add bbram driver for Realtek rts5912.

Signed-off-by: Elmo Lan <elmo_lan@realtek.com>
This commit is contained in:
Elmo Lan 2025-02-18 21:54:57 +08:00 committed by Benjamin Cabé
commit 40c590fad7
6 changed files with 129 additions and 0 deletions

View file

@ -17,3 +17,4 @@ zephyr_library_sources_ifdef(CONFIG_BBRAM_MICROCHIP_MCP7940N bbram_microchip_mcp
zephyr_library_sources_ifdef(CONFIG_BBRAM_MICROCHIP_MCP7940N_EMUL bbram_microchip_mcp7940n_emul.c)
zephyr_library_sources_ifdef(CONFIG_BBRAM_XEC bbram_xec.c)
zephyr_library_sources_ifdef(CONFIG_BBRAM_STM32 bbram_stm32.c)
zephyr_library_sources_ifdef(CONFIG_BBRAM_RTS5912 bbram_rts5912.c)

View file

@ -42,4 +42,6 @@ source "drivers/bbram/Kconfig.xec"
source "drivers/bbram/Kconfig.stm32"
source "drivers/bbram/Kconfig.rts5912"
endif # BBRAM

View file

@ -0,0 +1,9 @@
# Copyright (c) 2025, Realtek, SIBG-SD7
# SPDX-License-Identifier: Apache-2.0
config BBRAM_RTS5912
bool "Realtek RTS5912 Battery-backed RAM drivers"
default y
depends on DT_HAS_REALTEK_RTS5912_BBRAM_ENABLED
help
This option enables the BBRAM driver for Realtek RTS5912 of processors.

View file

@ -0,0 +1,99 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright (c) 2025 Realtek Semiconductor Corporation, SIBG-SD7
* Author: Elmo Lan <elmo_lan@realtek.com>
*/
#define DT_DRV_COMPAT realtek_rts5912_bbram
#include <zephyr/drivers/bbram.h>
#include <errno.h>
#include <zephyr/sys/util.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(rts5912_bbram, CONFIG_BBRAM_LOG_LEVEL);
struct bbram_rts5912_config {
uintptr_t base;
int size;
};
static int bbram_rts5912_get_size(const struct device *dev, size_t *size)
{
const struct bbram_rts5912_config *config = dev->config;
*size = config->size;
LOG_INF("size: 0x%08x", *size);
return 0;
}
static int bbram_rts5912_read(const struct device *dev, size_t offset, size_t size, uint8_t *data)
{
const struct bbram_rts5912_config *config = dev->config;
uint32_t word;
size_t start_offset, bytes_to_read;
if (size < 1 || offset + size > config->size) {
LOG_ERR("Invalid params");
return -EFAULT;
}
/* The bbram in RTS5912 only accepts word-access.
* Read it as a word, then split it into bytes.
*/
for (int i = 0; i < size;) {
word = *(uint32_t *)(config->base + ROUND_DOWN((offset + i), 4));
start_offset = (offset + i) % 4;
bytes_to_read = MIN(4 - start_offset, size - i);
memcpy(&data[i], ((uint8_t *)&word) + start_offset, bytes_to_read);
i += bytes_to_read;
}
return 0;
}
static int bbram_rts5912_write(const struct device *dev, size_t offset, size_t size,
const uint8_t *data)
{
const struct bbram_rts5912_config *config = dev->config;
uint32_t word;
size_t start_offset, bytes_to_write;
if (size < 1 || offset + size > config->size) {
LOG_ERR("Invalid params");
return -EFAULT;
}
/* The BBRAM in RTS5912 only accepts word-access. Read it as a word,
* then modify the byte field, and finally write it back as a word
*/
for (int i = 0; i < size;) {
word = *(uint32_t *)(config->base + ROUND_DOWN((offset + i), 4));
start_offset = (offset + i) % 4;
bytes_to_write = MIN(4 - start_offset, size - i);
memcpy(((uint8_t *)&word) + start_offset, &data[i], bytes_to_write);
*(uint32_t *)(config->base + ROUND_DOWN((offset + i), 4)) = word;
i += bytes_to_write;
}
return 0;
}
static DEVICE_API(bbram, bbram_rts5912_driver_api) = {
.get_size = bbram_rts5912_get_size,
.read = bbram_rts5912_read,
.write = bbram_rts5912_write,
};
#define BBRAM_INIT(inst) \
static const struct bbram_rts5912_config bbram_cfg_##inst = { \
.base = DT_INST_REG_ADDR(inst), \
.size = DT_INST_REG_SIZE(inst), \
}; \
DEVICE_DT_INST_DEFINE(inst, NULL, NULL, NULL, &bbram_cfg_##inst, PRE_KERNEL_1, \
CONFIG_BBRAM_INIT_PRIORITY, &bbram_rts5912_driver_api);
DT_INST_FOREACH_STATUS_OKAY(BBRAM_INIT);

View file

@ -65,6 +65,12 @@
interrupt-parent = <&nvic>;
ranges;
bbram: bb-ram@40005000 {
compatible = "realtek,rts5912-bbram";
reg = <0x40005000 0x100>;
status = "okay";
};
sccon: clock-controller@40020000 {
compatible = "realtek,rts5912-sccon";
reg = <0x40020000 0xf0>;

View file

@ -0,0 +1,12 @@
# Copyright (c) 2025, Realtek, SIBG-SD7
# SPDX-License-Identifier: Apache-2.0
description: Realtek, RTS5912 Battery Backed RAM node
compatible: "realtek,rts5912-bbram"
include: base.yaml
properties:
reg:
required: true