2018-11-25 10:40:57 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Foundries.io Ltd
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-04-02 22:25:40 +02:00
|
|
|
#define DT_DRV_COMPAT openisa_rv32m1_lptmr
|
|
|
|
|
2023-08-28 13:15:43 +02:00
|
|
|
#include <zephyr/init.h>
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
#include <zephyr/drivers/timer/system_timer.h>
|
2018-11-25 10:40:57 +01:00
|
|
|
#include <soc.h>
|
2022-10-17 10:24:11 +02:00
|
|
|
#include <zephyr/irq.h>
|
2018-11-25 10:40:57 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is just a getting started point.
|
|
|
|
*
|
|
|
|
* Assumptions and limitations:
|
|
|
|
*
|
2018-11-25 10:41:38 +01:00
|
|
|
* - system clock based on an LPTMR instance, clocked by SIRC output
|
|
|
|
* SIRCDIV3, prescaler divide-by-1, SIRC at 8MHz
|
2018-11-25 10:40:57 +01:00
|
|
|
* - no tickless
|
|
|
|
*/
|
|
|
|
|
2019-04-23 15:08:00 +02:00
|
|
|
#define CYCLES_PER_SEC sys_clock_hw_cycles_per_sec()
|
2018-11-25 10:41:38 +01:00
|
|
|
#define CYCLES_PER_TICK (CYCLES_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
|
2022-06-28 23:58:40 +02:00
|
|
|
#if defined(CONFIG_TEST)
|
|
|
|
const int32_t z_sys_timer_irq_for_test = DT_IRQN(DT_ALIAS(system_lptmr));
|
|
|
|
#endif
|
2018-11-25 10:40:57 +01:00
|
|
|
|
2018-11-25 10:41:38 +01:00
|
|
|
/*
|
|
|
|
* As a simplifying assumption, we only support a clock ticking at the
|
|
|
|
* SIRC reset rate of 8MHz.
|
|
|
|
*/
|
2019-04-23 15:08:00 +02:00
|
|
|
#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
|
|
|
|
(MHZ(8) != CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC)
|
2018-11-25 10:41:38 +01:00
|
|
|
#error "system timer misconfiguration; unsupported clock rate"
|
2018-11-25 10:40:57 +01:00
|
|
|
#endif
|
|
|
|
|
2019-03-04 19:28:19 +01:00
|
|
|
#define SYSTEM_TIMER_INSTANCE \
|
2020-04-02 22:25:40 +02:00
|
|
|
((LPTMR_Type *)(DT_INST_REG_ADDR(0)))
|
2018-11-25 10:40:57 +01:00
|
|
|
|
|
|
|
#define SIRC_RANGE_8MHZ SCG_SIRCCFG_RANGE(1)
|
|
|
|
#define SIRCDIV3_DIVIDE_BY_1 1
|
|
|
|
#define PCS_SOURCE_SIRCDIV3 0
|
|
|
|
|
|
|
|
struct device; /* forward declaration; type is not used. */
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static volatile uint32_t cycle_count;
|
2018-11-25 10:40:57 +01:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void lptmr_irq_handler(const struct device *unused)
|
2018-11-25 10:40:57 +01:00
|
|
|
{
|
|
|
|
ARG_UNUSED(unused);
|
|
|
|
|
2018-11-25 10:41:38 +01:00
|
|
|
SYSTEM_TIMER_INSTANCE->CSR |= LPTMR_CSR_TCF(1); /* Rearm timer. */
|
2018-11-25 10:40:57 +01:00
|
|
|
cycle_count += CYCLES_PER_TICK; /* Track cycles. */
|
2021-02-25 21:33:15 +01:00
|
|
|
sys_clock_announce(1); /* Poke the scheduler. */
|
2018-11-25 10:40:57 +01:00
|
|
|
}
|
|
|
|
|
2021-11-04 12:51:39 +01:00
|
|
|
uint32_t sys_clock_cycle_get_32(void)
|
|
|
|
{
|
|
|
|
return cycle_count + SYSTEM_TIMER_INSTANCE->CNR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since we're not tickless, this is identically zero.
|
|
|
|
*/
|
|
|
|
uint32_t sys_clock_elapsed(void)
|
|
|
|
{
|
|
|
|
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 sys_clock_driver_init(void)
|
2018-11-25 10:40:57 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t csr, psr, sircdiv; /* LPTMR registers */
|
2018-11-25 10:40:57 +01:00
|
|
|
|
2020-04-02 22:25:40 +02:00
|
|
|
IRQ_CONNECT(DT_INST_IRQN(0),
|
2019-06-21 15:21:50 +02:00
|
|
|
0, lptmr_irq_handler, NULL, 0);
|
2018-11-25 10:40:57 +01:00
|
|
|
|
|
|
|
if ((SCG->SIRCCSR & SCG_SIRCCSR_SIRCEN_MASK) == SCG_SIRCCSR_SIRCEN(0)) {
|
|
|
|
/*
|
|
|
|
* SIRC is on by default, so something else turned it off.
|
|
|
|
*
|
|
|
|
* This is incompatible with this driver, which is SIRC-based.
|
|
|
|
*/
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable the timer and clear any pending IRQ. */
|
2018-11-25 10:41:38 +01:00
|
|
|
csr = SYSTEM_TIMER_INSTANCE->CSR;
|
2018-11-25 10:40:57 +01:00
|
|
|
csr &= ~LPTMR_CSR_TEN(0);
|
|
|
|
csr |= LPTMR_CSR_TFC(1);
|
2018-11-25 10:41:38 +01:00
|
|
|
SYSTEM_TIMER_INSTANCE->CSR = csr;
|
2018-11-25 10:40:57 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the timer clock source and configure the timer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SIRCDIV3 is the SIRC divider for LPTMR (SoC dependent).
|
|
|
|
* Pass it directly through without any divider.
|
|
|
|
*/
|
|
|
|
sircdiv = SCG->SIRCDIV;
|
|
|
|
sircdiv &= ~SCG_SIRCDIV_SIRCDIV3_MASK;
|
|
|
|
sircdiv |= SCG_SIRCDIV_SIRCDIV3(SIRCDIV3_DIVIDE_BY_1);
|
|
|
|
SCG->SIRCDIV = sircdiv;
|
|
|
|
/*
|
|
|
|
* TMS = 0: time counter mode, not pulse counter
|
|
|
|
* TCF = 0: reset counter register on reaching compare value
|
|
|
|
* TDRE = 0: disable DMA request
|
|
|
|
*/
|
|
|
|
csr &= ~(LPTMR_CSR_TMS(1) | LPTMR_CSR_TFC(1) | LPTMR_CSR_TDRE(1));
|
|
|
|
/*
|
|
|
|
* TIE = 1: enable interrupt
|
|
|
|
*/
|
|
|
|
csr |= LPTMR_CSR_TIE(1);
|
2018-11-25 10:41:38 +01:00
|
|
|
SYSTEM_TIMER_INSTANCE->CSR = csr;
|
2018-11-25 10:40:57 +01:00
|
|
|
/*
|
|
|
|
* PCS = 0: clock source is SIRCDIV3 (SoC dependent)
|
|
|
|
* PBYP = 1: bypass the prescaler
|
|
|
|
*/
|
2018-11-25 10:41:38 +01:00
|
|
|
psr = SYSTEM_TIMER_INSTANCE->PSR;
|
2018-11-25 10:40:57 +01:00
|
|
|
psr &= ~LPTMR_PSR_PCS_MASK;
|
|
|
|
psr |= (LPTMR_PSR_PBYP(1) | LPTMR_PSR_PCS(PCS_SOURCE_SIRCDIV3));
|
2018-11-25 10:41:38 +01:00
|
|
|
SYSTEM_TIMER_INSTANCE->PSR = psr;
|
2018-11-25 10:40:57 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set compare register to the proper tick count. The check
|
|
|
|
* here makes sure SIRC is left at its default reset value to
|
|
|
|
* make the defconfig setting work properly.
|
|
|
|
*
|
|
|
|
* TODO: be smarter to meet arbitrary Kconfig settings.
|
|
|
|
*/
|
|
|
|
if ((SCG->SIRCCFG & SCG_SIRCCFG_RANGE_MASK) != SIRC_RANGE_8MHZ) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-11-25 10:41:38 +01:00
|
|
|
SYSTEM_TIMER_INSTANCE->CMR = CYCLES_PER_TICK;
|
2018-11-25 10:40:57 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable interrupts and the timer. There's no need to clear the
|
|
|
|
* TFC bit in the csr variable, as it's already clear.
|
|
|
|
*/
|
2020-04-02 22:25:40 +02:00
|
|
|
irq_enable(DT_INST_IRQN(0));
|
2018-11-25 10:41:38 +01:00
|
|
|
csr = SYSTEM_TIMER_INSTANCE->CSR;
|
2018-11-25 10:40:57 +01:00
|
|
|
csr |= LPTMR_CSR_TEN(1);
|
2018-11-25 10:41:38 +01:00
|
|
|
SYSTEM_TIMER_INSTANCE->CSR = csr;
|
2018-11-25 10:40:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-04 12:51:39 +01:00
|
|
|
SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2,
|
|
|
|
CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
|