2018-11-25 02:40:57 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Foundries.io
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 11:11:04 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2018-11-25 02:40:57 -07:00
|
|
|
|
2022-05-06 11:11:04 +02:00
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/init.h>
|
2018-11-25 02:40:57 -07:00
|
|
|
#include <fsl_clock.h>
|
2022-05-06 11:11:04 +02:00
|
|
|
#include <zephyr/sys/util.h>
|
2018-11-25 02:41:38 -07:00
|
|
|
|
|
|
|
#if defined(CONFIG_MULTI_LEVEL_INTERRUPTS)
|
|
|
|
#include <errno.h>
|
2022-05-06 11:11:04 +02:00
|
|
|
#include <zephyr/irq_nextlevel.h>
|
2018-11-25 02:41:38 -07:00
|
|
|
#endif
|
|
|
|
|
2024-01-18 15:27:19 +01:00
|
|
|
#include <soc.h>
|
|
|
|
|
2018-11-25 02:41:38 -07:00
|
|
|
#define LOG_LEVEL CONFIG_SOC_LOG_LEVEL
|
2022-05-06 11:11:04 +02:00
|
|
|
#include <zephyr/logging/log.h>
|
2018-11-25 02:41:38 -07:00
|
|
|
LOG_MODULE_REGISTER(soc);
|
2018-11-25 02:40:57 -07:00
|
|
|
|
|
|
|
#define SCG_LPFLL_DISABLE 0U
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static const struct device *dev_intmux;
|
2018-11-25 02:41:38 -07:00
|
|
|
|
2018-11-25 02:40:57 -07:00
|
|
|
/*
|
|
|
|
* Run-mode configuration for the fast internal reference clock (FIRC).
|
|
|
|
*/
|
|
|
|
static const scg_firc_config_t rv32m1_firc_config = {
|
|
|
|
.enableMode = kSCG_FircEnable,
|
|
|
|
.div1 = kSCG_AsyncClkDivBy1,
|
|
|
|
.div2 = kSCG_AsyncClkDivBy1,
|
|
|
|
.div3 = kSCG_AsyncClkDivBy1,
|
|
|
|
.range = kSCG_FircRange48M,
|
|
|
|
.trimConfig = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIRC-based system clock configuration.
|
|
|
|
*/
|
|
|
|
static const scg_sys_clk_config_t rv32m1_sys_clk_config_firc = {
|
|
|
|
.divSlow = kSCG_SysClkDivBy2,
|
|
|
|
.divBus = kSCG_SysClkDivBy1,
|
|
|
|
.divExt = kSCG_SysClkDivBy1,
|
|
|
|
.divCore = kSCG_SysClkDivBy1,
|
|
|
|
.src = kSCG_SysClkSrcFirc,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LPFLL configuration.
|
|
|
|
*/
|
|
|
|
static const scg_lpfll_config_t rv32m1_lpfll_cfg = {
|
|
|
|
.enableMode = SCG_LPFLL_DISABLE,
|
|
|
|
.div1 = kSCG_AsyncClkDivBy1,
|
|
|
|
.div2 = kSCG_AsyncClkDisable,
|
|
|
|
.div3 = kSCG_AsyncClkDisable,
|
|
|
|
.range = kSCG_LpFllRange48M,
|
|
|
|
.trimConfig = NULL,
|
|
|
|
};
|
|
|
|
|
2019-02-28 11:34:16 -06:00
|
|
|
void sys_arch_reboot(int type)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(type);
|
|
|
|
|
|
|
|
EVENT_UNIT->SLPCTRL |= EVENT_SLPCTRL_SYSRSTREQST_MASK;
|
|
|
|
}
|
|
|
|
|
2019-11-07 12:43:29 -08:00
|
|
|
void arch_irq_enable(unsigned int irq)
|
2018-11-25 02:40:57 -07:00
|
|
|
{
|
2018-11-25 02:41:38 -07:00
|
|
|
if (IS_ENABLED(CONFIG_MULTI_LEVEL_INTERRUPTS)) {
|
|
|
|
unsigned int level = rv32m1_irq_level(irq);
|
|
|
|
|
2019-03-26 19:57:45 -06:00
|
|
|
if (level == 1U) {
|
2018-11-25 02:41:38 -07:00
|
|
|
EVENT_UNIT->INTPTEN |= BIT(rv32m1_level1_irq(irq));
|
|
|
|
/* Ensures write has finished: */
|
|
|
|
(void)(EVENT_UNIT->INTPTEN);
|
|
|
|
} else {
|
|
|
|
irq_enable_next_level(dev_intmux, irq);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EVENT_UNIT->INTPTEN |= BIT(rv32m1_level1_irq(irq));
|
|
|
|
(void)(EVENT_UNIT->INTPTEN);
|
|
|
|
}
|
2018-11-25 02:40:57 -07:00
|
|
|
}
|
|
|
|
|
2019-11-07 12:43:29 -08:00
|
|
|
void arch_irq_disable(unsigned int irq)
|
2018-11-25 02:40:57 -07:00
|
|
|
{
|
2018-11-25 02:41:38 -07:00
|
|
|
if (IS_ENABLED(CONFIG_MULTI_LEVEL_INTERRUPTS)) {
|
|
|
|
unsigned int level = rv32m1_irq_level(irq);
|
|
|
|
|
2019-03-26 19:57:45 -06:00
|
|
|
if (level == 1U) {
|
2018-11-25 02:41:38 -07:00
|
|
|
EVENT_UNIT->INTPTEN &= ~BIT(rv32m1_level1_irq(irq));
|
|
|
|
/* Ensures write has finished: */
|
|
|
|
(void)(EVENT_UNIT->INTPTEN);
|
|
|
|
} else {
|
|
|
|
irq_disable_next_level(dev_intmux, irq);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EVENT_UNIT->INTPTEN &= ~BIT(rv32m1_level1_irq(irq));
|
|
|
|
(void)(EVENT_UNIT->INTPTEN);
|
|
|
|
}
|
2018-11-25 02:40:57 -07:00
|
|
|
}
|
|
|
|
|
2019-11-07 12:43:29 -08:00
|
|
|
int arch_irq_is_enabled(unsigned int irq)
|
2018-11-25 02:40:57 -07:00
|
|
|
{
|
2018-11-25 02:41:38 -07:00
|
|
|
if (IS_ENABLED(CONFIG_MULTI_LEVEL_INTERRUPTS)) {
|
|
|
|
unsigned int level = rv32m1_irq_level(irq);
|
|
|
|
|
2019-03-26 19:57:45 -06:00
|
|
|
if (level == 1U) {
|
2018-11-25 02:41:38 -07:00
|
|
|
return (EVENT_UNIT->INTPTEN &
|
|
|
|
BIT(rv32m1_level1_irq(irq))) != 0;
|
|
|
|
} else {
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t channel, line, ier;
|
2018-11-25 02:41:38 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we break the abstraction and look
|
|
|
|
* directly at the INTMUX registers. We can't
|
|
|
|
* use the irq_nextlevel.h API, as that only
|
|
|
|
* tells us whether some IRQ at the next level
|
|
|
|
* is enabled or not.
|
|
|
|
*/
|
|
|
|
channel = rv32m1_intmux_channel(irq);
|
|
|
|
line = rv32m1_intmux_line(irq);
|
|
|
|
ier = INTMUX->CHANNEL[channel].CHn_IER_31_0 & BIT(line);
|
|
|
|
|
2019-03-26 19:57:45 -06:00
|
|
|
return ier != 0U;
|
2018-11-25 02:41:38 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return (EVENT_UNIT->INTPTEN & BIT(rv32m1_level1_irq(irq))) != 0;
|
|
|
|
}
|
2018-11-25 02:40:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SoC-level interrupt initialization. Clear any pending interrupts or
|
2018-11-25 02:41:38 -07:00
|
|
|
* events, and find the INTMUX device if necessary.
|
|
|
|
*
|
2019-03-08 14:19:05 -07:00
|
|
|
* This gets called as almost the first thing z_cstart() does, so it
|
2018-11-25 02:41:38 -07:00
|
|
|
* will happen before any calls to the _arch_irq_xxx() routines above.
|
2018-11-25 02:40:57 -07:00
|
|
|
*/
|
|
|
|
void soc_interrupt_init(void)
|
|
|
|
{
|
|
|
|
EVENT_UNIT->INTPTPENDCLEAR = 0xFFFFFFFF;
|
|
|
|
(void)(EVENT_UNIT->INTPTPENDCLEAR); /* Ensures write has finished. */
|
|
|
|
EVENT_UNIT->EVTPENDCLEAR = 0xFFFFFFFF;
|
|
|
|
(void)(EVENT_UNIT->EVTPENDCLEAR); /* Ensures write has finished. */
|
2018-11-25 02:41:38 -07:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_MULTI_LEVEL_INTERRUPTS)) {
|
2021-07-01 11:28:26 -05:00
|
|
|
dev_intmux = DEVICE_DT_GET(DT_INST(0, openisa_rv32m1_intmux));
|
2018-11-25 02:41:38 -07:00
|
|
|
}
|
2018-11-25 02:40:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Switch system clock configuration in run mode.
|
|
|
|
*
|
|
|
|
* Blocks until the updated configuration takes effect.
|
|
|
|
*
|
|
|
|
* @param cfg New system clock configuration
|
|
|
|
*/
|
|
|
|
static void rv32m1_switch_sys_clk(const scg_sys_clk_config_t *cfg)
|
|
|
|
{
|
|
|
|
scg_sys_clk_config_t cur_cfg;
|
|
|
|
|
|
|
|
CLOCK_SetRunModeSysClkConfig(cfg);
|
|
|
|
do {
|
|
|
|
CLOCK_GetCurSysClkConfig(&cur_cfg);
|
|
|
|
} while (cur_cfg.src != cfg->src);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initializes SIRC and switches system clock source to SIRC.
|
|
|
|
*/
|
|
|
|
static void rv32m1_switch_to_sirc(void)
|
|
|
|
{
|
|
|
|
const scg_sirc_config_t sirc_config = {
|
|
|
|
.enableMode = kSCG_SircEnable,
|
|
|
|
.div1 = kSCG_AsyncClkDisable,
|
|
|
|
.div2 = kSCG_AsyncClkDivBy2,
|
|
|
|
.range = kSCG_SircRangeHigh,
|
|
|
|
};
|
|
|
|
const scg_sys_clk_config_t sys_clk_config_sirc = {
|
|
|
|
.divSlow = kSCG_SysClkDivBy4,
|
|
|
|
.divCore = kSCG_SysClkDivBy1,
|
|
|
|
.src = kSCG_SysClkSrcSirc,
|
|
|
|
};
|
|
|
|
|
|
|
|
CLOCK_InitSirc(&sirc_config);
|
|
|
|
rv32m1_switch_sys_clk(&sys_clk_config_sirc);
|
|
|
|
}
|
|
|
|
|
2019-12-03 17:17:28 +01:00
|
|
|
/**
|
|
|
|
* @brief Setup peripheral clocks
|
|
|
|
*
|
|
|
|
* Setup the peripheral clock sources.
|
|
|
|
*/
|
|
|
|
static void rv32m1_setup_peripheral_clocks(void)
|
|
|
|
{
|
2020-05-11 11:56:08 -07:00
|
|
|
#if DT_NODE_HAS_STATUS(DT_NODELABEL(tpm0), okay)
|
2019-12-03 17:17:28 +01:00
|
|
|
CLOCK_SetIpSrc(kCLOCK_Tpm0, kCLOCK_IpSrcFircAsync);
|
|
|
|
#endif
|
2020-05-11 11:56:08 -07:00
|
|
|
#if DT_NODE_HAS_STATUS(DT_NODELABEL(tpm1), okay)
|
2019-12-03 17:17:28 +01:00
|
|
|
CLOCK_SetIpSrc(kCLOCK_Tpm1, kCLOCK_IpSrcFircAsync);
|
|
|
|
#endif
|
2020-05-11 11:56:08 -07:00
|
|
|
#if DT_NODE_HAS_STATUS(DT_NODELABEL(tpm2), okay)
|
2019-12-03 17:17:28 +01:00
|
|
|
CLOCK_SetIpSrc(kCLOCK_Tpm2, kCLOCK_IpSrcFircAsync);
|
|
|
|
#endif
|
2020-05-11 11:56:08 -07:00
|
|
|
#if DT_NODE_HAS_STATUS(DT_NODELABEL(tpm3), okay)
|
2019-12-03 17:17:28 +01:00
|
|
|
CLOCK_SetIpSrc(kCLOCK_Tpm3, kCLOCK_IpSrcFircAsync);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-25 02:40:57 -07:00
|
|
|
/**
|
|
|
|
* @brief Perform basic hardware initialization
|
|
|
|
*
|
|
|
|
* Initializes the base clocks and LPFLL using helpers provided by the HAL.
|
|
|
|
*
|
|
|
|
* @return 0
|
|
|
|
*/
|
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 soc_rv32m1_init(void)
|
2018-11-25 02:40:57 -07:00
|
|
|
{
|
|
|
|
unsigned int key;
|
|
|
|
|
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
|
|
|
|
/* Switch to SIRC so we can initialize the FIRC. */
|
|
|
|
rv32m1_switch_to_sirc();
|
|
|
|
|
|
|
|
/* Now that we're running off of SIRC, set up and switch to FIRC. */
|
|
|
|
CLOCK_InitFirc(&rv32m1_firc_config);
|
|
|
|
rv32m1_switch_sys_clk(&rv32m1_sys_clk_config_firc);
|
|
|
|
|
|
|
|
/* Initialize LPFLL */
|
|
|
|
CLOCK_InitLpFll(&rv32m1_lpfll_cfg);
|
|
|
|
|
2019-12-03 17:17:28 +01:00
|
|
|
/* Initialize peripheral clocks */
|
|
|
|
rv32m1_setup_peripheral_clocks();
|
|
|
|
|
2018-11-25 02:40:57 -07:00
|
|
|
irq_unlock(key);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYS_INIT(soc_rv32m1_init, PRE_KERNEL_1, 0);
|