zephyr/soc/riscv/riscv-privileged/andes_v5/l2_cache.c

118 lines
2.7 KiB
C
Raw Normal View History

/*
* Copyright (c) 2021 Andes Technology Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT andestech_l2c
/**
* @brief Andes V5 L2 Cache Controller driver
*/
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/arch/cpu.h>
#include <zephyr/drivers/syscon.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(andes_v5_l2_cache, CONFIG_SOC_LOG_LEVEL);
#if DT_NODE_EXISTS(DT_INST(0, andestech_l2c))
/* SMU Configuration Register offset */
#define SMU_SYSTEMCFG 0x08
/* Register bitmask */
#define SMU_SYSTEMCFG_L2C BIT(8)
/*
* L2C Register Base Address
*/
#define ANDES_V5_L2C_BASE DT_INST_REG_ADDR(0)
/*
* L2C Register Offset
*/
#define L2C_CONFIG 0x00
#define L2C_CTRL 0x08
/*
* L2C Helper Constant
*/
/* L2 cache version */
#define L2C_CONFIG_VERSION_SHIFT 24
/* enable L2C */
#define L2C_CTRL_CEN BIT(0)
/* instruction prefetch depth */
#define IPFDPT_FIELD(x) (x << 3)
#define L2C_CTRL_IPFDPT_0 IPFDPT_FIELD(0)
#define L2C_CTRL_IPFDPT_1 IPFDPT_FIELD(1)
#define L2C_CTRL_IPFDPT_2 IPFDPT_FIELD(2)
#define L2C_CTRL_IPFDPT_3 IPFDPT_FIELD(3)
/* data prefetch depth */
#define DPFDPT_FIELD(x) (x << 5)
#define L2C_CTRL_DPFDPT_0 DPFDPT_FIELD(0)
#define L2C_CTRL_DPFDPT_2 DPFDPT_FIELD(1)
#define L2C_CTRL_DPFDPT_4 DPFDPT_FIELD(2)
#define L2C_CTRL_DPFDPT_8 DPFDPT_FIELD(3)
static void andes_v5_l2c_enable(void)
{
unsigned long mcache_ctl;
volatile uint64_t *l2c_ctrl =
INT_TO_POINTER(ANDES_V5_L2C_BASE + L2C_CTRL);
__asm__ volatile ("csrr %0, %1"
: "=r" (mcache_ctl) : "i" (NDS_MCACHE_CTL));
/* Enable L2 cache if L1 I/D cache enabled */
if (mcache_ctl & (BIT(1) | BIT(0))) {
volatile uint64_t *l2c_config =
INT_TO_POINTER(ANDES_V5_L2C_BASE + L2C_CONFIG);
*l2c_ctrl |= (L2C_CTRL_IPFDPT_3 | L2C_CTRL_DPFDPT_8);
/* Enable L2 cache manually if device version less than 0xF0 */
if (!((*l2c_config >> L2C_CONFIG_VERSION_SHIFT) & 0xF0)) {
*l2c_ctrl |= L2C_CTRL_CEN;
}
} else {
/* Disable L2 cache */
*l2c_ctrl &= ~L2C_CTRL_CEN;
}
}
init: remove the need for a dummy device pointer in SYS_INIT functions The init infrastructure, found in `init.h`, is currently used by: - `SYS_INIT`: to call functions before `main` - `DEVICE_*`: to initialize devices They are all sorted according to an initialization level + a priority. `SYS_INIT` calls are really orthogonal to devices, however, the required function signature requires a `const struct device *dev` as a first argument. The only reason for that is because the same init machinery is used by devices, so we have something like: ```c struct init_entry { int (*init)(const struct device *dev); /* only set by DEVICE_*, otherwise NULL */ const struct device *dev; } ``` As a result, we end up with such weird/ugly pattern: ```c static int my_init(const struct device *dev) { /* always NULL! add ARG_UNUSED to avoid compiler warning */ ARG_UNUSED(dev); ... } ``` This is really a result of poor internals isolation. This patch proposes a to make init entries more flexible so that they can accept sytem initialization calls like this: ```c static int my_init(void) { ... } ``` This is achieved using a union: ```c union init_function { /* for SYS_INIT, used when init_entry.dev == NULL */ int (*sys)(void); /* for DEVICE*, used when init_entry.dev != NULL */ int (*dev)(const struct device *dev); }; struct init_entry { /* stores init function (either for SYS_INIT or DEVICE*) union init_function init_fn; /* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows * to know which union entry to call. */ const struct device *dev; } ``` This solution **does not increase ROM usage**, and allows to offer clean public APIs for both SYS_INIT and DEVICE*. Note that however, init machinery keeps a coupling with devices. **NOTE**: This is a breaking change! All `SYS_INIT` functions will need to be converted to the new signature. See the script offered in the following commit. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> init: convert SYS_INIT functions to the new signature Conversion scripted using scripts/utils/migrate_sys_init.py. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> manifest: update projects for SYS_INIT changes Update modules with updated SYS_INIT calls: - hal_ti - lvgl - sof - TraceRecorderSource Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> tests: devicetree: devices: adjust test Adjust test according to the recently introduced SYS_INIT infrastructure. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> tests: kernel: threads: adjust SYS_INIT call Adjust to the new signature: int (*init_fn)(void); Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-19 09:33:44 +02:00
static int andes_v5_l2c_init(void)
{
#if DT_NODE_HAS_STATUS(DT_INST(0, syscon), okay)
uint32_t system_cfg;
const struct device *const syscon_dev = DEVICE_DT_GET(DT_NODELABEL(syscon));
if (device_is_ready(syscon_dev)) {
syscon_read_reg(syscon_dev, SMU_SYSTEMCFG, &system_cfg);
if (!(system_cfg & SMU_SYSTEMCFG_L2C)) {
/* This SoC doesn't have L2 cache */
return -ENODEV;
}
} else {
LOG_DBG("Init might fail for some hardware combinations "
"if syscon driver isn't ready\n");
}
#endif
andes_v5_l2c_enable();
return 0;
}
SYS_INIT(andes_v5_l2c_init, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
#endif /* DT_NODE_EXISTS(DT_INST(0, andestech_l2c)) */