2018-11-18 08:28:49 +01:00
|
|
|
/*
|
2020-03-18 05:47:19 +01:00
|
|
|
* Copyright (c) 2020 Stephanos Ioannidis <root@stephanos.io>
|
2018-11-18 08:28:49 +01:00
|
|
|
* Copyright (c) 2018 Xilinx, Inc.
|
2020-03-18 05:47:19 +01:00
|
|
|
*
|
2018-11-18 08:28:49 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-04-17 20:07:43 +02:00
|
|
|
#define DT_DRV_COMPAT xlnx_ttcps
|
|
|
|
|
2020-03-18 05:47:19 +01:00
|
|
|
#include <soc.h>
|
2018-11-18 08:28:49 +01:00
|
|
|
#include <drivers/timer/system_timer.h>
|
2020-03-18 05:47:19 +01:00
|
|
|
#include "xlnx_psttc_timer_priv.h"
|
2019-12-19 12:43:37 +01:00
|
|
|
|
2020-03-18 05:47:19 +01:00
|
|
|
#define TIMER_INDEX CONFIG_XLNX_PSTTC_TIMER_INDEX
|
2018-11-18 08:28:49 +01:00
|
|
|
|
2020-04-17 20:07:43 +02:00
|
|
|
#define TIMER_IRQ DT_INST_IRQN(0)
|
|
|
|
#define TIMER_BASE_ADDR DT_INST_REG_ADDR(0)
|
|
|
|
#define TIMER_CLOCK_FREQUECY DT_INST_PROP(0, clock_frequency)
|
2018-11-18 08:28:49 +01:00
|
|
|
|
2020-03-18 05:47:19 +01:00
|
|
|
#define TICKS_PER_SEC CONFIG_SYS_CLOCK_TICKS_PER_SEC
|
|
|
|
#define CYCLES_PER_SEC TIMER_CLOCK_FREQUECY
|
|
|
|
#define CYCLES_PER_TICK (CYCLES_PER_SEC / TICKS_PER_SEC)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CYCLES_NEXT_MIN must be large enough to ensure that the timer does not miss
|
|
|
|
* interrupts. This value was conservatively set using the trial and error
|
|
|
|
* method, and there is room for improvement.
|
|
|
|
*/
|
|
|
|
#define CYCLES_NEXT_MIN (10000)
|
|
|
|
#define CYCLES_NEXT_MAX (XTTC_MAX_INTERVAL_COUNT)
|
|
|
|
|
2020-04-17 20:07:43 +02:00
|
|
|
BUILD_ASSERT(TIMER_CLOCK_FREQUECY ==
|
2020-03-18 05:47:19 +01:00
|
|
|
CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC,
|
2020-03-12 16:16:00 +01:00
|
|
|
"Configured system timer frequency does not match the TTC "
|
|
|
|
"clock frequency in the device tree");
|
2020-03-18 05:47:19 +01:00
|
|
|
|
2020-03-12 16:16:00 +01:00
|
|
|
BUILD_ASSERT(CYCLES_PER_SEC >= TICKS_PER_SEC,
|
|
|
|
"Timer clock frequency must be greater than the system tick "
|
|
|
|
"frequency");
|
2020-03-18 05:47:19 +01:00
|
|
|
|
2020-03-12 16:16:00 +01:00
|
|
|
BUILD_ASSERT((CYCLES_PER_SEC % TICKS_PER_SEC) == 0,
|
|
|
|
"Timer clock frequency is not divisible by the system tick "
|
|
|
|
"frequency");
|
2020-03-18 05:47:19 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_TICKLESS_KERNEL
|
2020-05-27 18:26:57 +02:00
|
|
|
static uint32_t last_cycles;
|
2018-11-18 08:28:49 +01:00
|
|
|
#endif
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static uint32_t read_count(void)
|
2018-11-18 08:28:49 +01:00
|
|
|
{
|
2020-03-18 05:47:19 +01:00
|
|
|
/* Read current counter value */
|
|
|
|
return sys_read32(TIMER_BASE_ADDR + XTTCPS_COUNT_VALUE_OFFSET);
|
|
|
|
}
|
2018-11-18 08:28:49 +01:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static void update_match(uint32_t cycles, uint32_t match)
|
2020-03-18 05:47:19 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t delta = match - cycles;
|
2018-11-18 08:28:49 +01:00
|
|
|
|
2020-03-18 05:47:19 +01:00
|
|
|
/* Ensure that the match value meets the minimum timing requirements */
|
|
|
|
if (delta < CYCLES_NEXT_MIN) {
|
|
|
|
match += CYCLES_NEXT_MIN - delta;
|
2018-11-18 08:28:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 05:47:19 +01:00
|
|
|
/* Write counter match value for interrupt generation */
|
|
|
|
sys_write32(match, TIMER_BASE_ADDR + XTTCPS_MATCH_0_OFFSET);
|
2018-11-18 08:28:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 05:47:19 +01:00
|
|
|
static void ttc_isr(void *arg)
|
2018-11-18 08:28:49 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t cycles;
|
|
|
|
uint32_t ticks;
|
2020-03-18 05:47:19 +01:00
|
|
|
|
|
|
|
ARG_UNUSED(arg);
|
|
|
|
|
|
|
|
/* Acknowledge interrupt */
|
|
|
|
sys_read32(TIMER_BASE_ADDR + XTTCPS_ISR_OFFSET);
|
2018-11-18 08:28:49 +01:00
|
|
|
|
2020-03-18 05:47:19 +01:00
|
|
|
/* Read counter value */
|
|
|
|
cycles = read_count();
|
2018-11-18 08:28:49 +01:00
|
|
|
|
2020-03-18 05:47:19 +01:00
|
|
|
#ifdef CONFIG_TICKLESS_KERNEL
|
|
|
|
/* Calculate the number of ticks since last announcement */
|
|
|
|
ticks = (cycles - last_cycles) / CYCLES_PER_TICK;
|
|
|
|
|
|
|
|
/* Update last cycles count */
|
|
|
|
last_cycles = cycles;
|
|
|
|
#else
|
|
|
|
/* Update counter match value for the next interrupt */
|
|
|
|
update_match(cycles, cycles + CYCLES_PER_TICK);
|
|
|
|
|
|
|
|
/* Advance tick count by 1 */
|
|
|
|
ticks = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Announce to the kernel*/
|
|
|
|
z_clock_announce(ticks);
|
2018-11-18 08:28:49 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
int z_clock_driver_init(const struct device *device)
|
2018-11-18 08:28:49 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t reg_val;
|
2018-11-18 08:28:49 +01:00
|
|
|
|
|
|
|
/* Stop timer */
|
|
|
|
sys_write32(XTTCPS_CNT_CNTRL_DIS_MASK,
|
2020-03-18 05:47:19 +01:00
|
|
|
TIMER_BASE_ADDR + XTTCPS_CNT_CNTRL_OFFSET);
|
2018-11-18 08:28:49 +01:00
|
|
|
|
2020-03-18 05:47:19 +01:00
|
|
|
#ifdef CONFIG_TICKLESS_KERNEL
|
|
|
|
/* Initialise internal states */
|
|
|
|
last_cycles = 0;
|
|
|
|
#endif
|
2018-11-18 08:28:49 +01:00
|
|
|
|
2020-03-18 05:47:19 +01:00
|
|
|
/* Initialise timer registers */
|
2018-11-18 08:28:49 +01:00
|
|
|
sys_write32(XTTCPS_CNT_CNTRL_RESET_VALUE,
|
2020-03-18 05:47:19 +01:00
|
|
|
TIMER_BASE_ADDR + XTTCPS_CNT_CNTRL_OFFSET);
|
|
|
|
sys_write32(0, TIMER_BASE_ADDR + XTTCPS_CLK_CNTRL_OFFSET);
|
|
|
|
sys_write32(0, TIMER_BASE_ADDR + XTTCPS_INTERVAL_VAL_OFFSET);
|
|
|
|
sys_write32(0, TIMER_BASE_ADDR + XTTCPS_MATCH_0_OFFSET);
|
|
|
|
sys_write32(0, TIMER_BASE_ADDR + XTTCPS_MATCH_1_OFFSET);
|
|
|
|
sys_write32(0, TIMER_BASE_ADDR + XTTCPS_MATCH_2_OFFSET);
|
|
|
|
sys_write32(0, TIMER_BASE_ADDR + XTTCPS_IER_OFFSET);
|
|
|
|
sys_write32(XTTCPS_IXR_ALL_MASK, TIMER_BASE_ADDR + XTTCPS_ISR_OFFSET);
|
2019-12-19 12:43:37 +01:00
|
|
|
|
2018-11-18 08:28:49 +01:00
|
|
|
/* Reset counter value */
|
2020-03-18 05:47:19 +01:00
|
|
|
reg_val = sys_read32(TIMER_BASE_ADDR + XTTCPS_CNT_CNTRL_OFFSET);
|
|
|
|
reg_val |= XTTCPS_CNT_CNTRL_RST_MASK;
|
|
|
|
sys_write32(reg_val, TIMER_BASE_ADDR + XTTCPS_CNT_CNTRL_OFFSET);
|
|
|
|
|
|
|
|
/* Set match mode */
|
|
|
|
reg_val = sys_read32(TIMER_BASE_ADDR + XTTCPS_CNT_CNTRL_OFFSET);
|
|
|
|
reg_val |= XTTCPS_CNT_CNTRL_MATCH_MASK;
|
|
|
|
sys_write32(reg_val, TIMER_BASE_ADDR + XTTCPS_CNT_CNTRL_OFFSET);
|
|
|
|
|
|
|
|
/* Set initial timeout */
|
|
|
|
reg_val = IS_ENABLED(CONFIG_TICKLESS_KERNEL) ?
|
|
|
|
CYCLES_NEXT_MAX : CYCLES_PER_TICK;
|
|
|
|
sys_write32(reg_val, TIMER_BASE_ADDR + XTTCPS_MATCH_0_OFFSET);
|
|
|
|
|
|
|
|
/* Connect timer interrupt */
|
|
|
|
IRQ_CONNECT(TIMER_IRQ, 0, ttc_isr, 0, 0);
|
|
|
|
irq_enable(TIMER_IRQ);
|
2018-11-18 08:28:49 +01:00
|
|
|
|
|
|
|
/* Enable timer interrupt */
|
2020-03-18 05:47:19 +01:00
|
|
|
reg_val = sys_read32(TIMER_BASE_ADDR + XTTCPS_IER_OFFSET);
|
|
|
|
reg_val |= XTTCPS_IXR_MATCH_0_MASK;
|
|
|
|
sys_write32(reg_val, TIMER_BASE_ADDR + XTTCPS_IER_OFFSET);
|
2018-11-18 08:28:49 +01:00
|
|
|
|
|
|
|
/* Start timer */
|
2020-03-18 05:47:19 +01:00
|
|
|
reg_val = sys_read32(TIMER_BASE_ADDR + XTTCPS_CNT_CNTRL_OFFSET);
|
|
|
|
reg_val &= (~XTTCPS_CNT_CNTRL_DIS_MASK);
|
|
|
|
sys_write32(reg_val, TIMER_BASE_ADDR + XTTCPS_CNT_CNTRL_OFFSET);
|
2018-11-18 08:28:49 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
void z_clock_set_timeout(int32_t ticks, bool idle)
|
2020-03-18 05:47:19 +01:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_TICKLESS_KERNEL
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t cycles;
|
|
|
|
uint32_t next_cycles;
|
2020-03-18 05:47:19 +01:00
|
|
|
|
|
|
|
/* Read counter value */
|
|
|
|
cycles = read_count();
|
|
|
|
|
|
|
|
/* Calculate timeout counter value */
|
kernel/timeout: Make timeout arguments an opaque type
Add a k_timeout_t type, and use it everywhere that kernel API
functions were accepting a millisecond timeout argument. Instead of
forcing milliseconds everywhere (which are often not integrally
representable as system ticks), do the conversion to ticks at the
point where the timeout is created. This avoids an extra unit
conversion in some application code, and allows us to express the
timeout in units other than milliseconds to achieve greater precision.
The existing K_MSEC() et. al. macros now return initializers for a
k_timeout_t.
The K_NO_WAIT and K_FOREVER constants have now become k_timeout_t
values, which means they cannot be operated on as integers.
Applications which have their own APIs that need to inspect these
vs. user-provided timeouts can now use a K_TIMEOUT_EQ() predicate to
test for equality.
Timer drivers, which receive an integer tick count in ther
z_clock_set_timeout() functions, now use the integer-valued
K_TICKS_FOREVER constant instead of K_FOREVER.
For the initial release, to preserve source compatibility, a
CONFIG_LEGACY_TIMEOUT_API kconfig is provided. When true, the
k_timeout_t will remain a compatible 32 bit value that will work with
any legacy Zephyr application.
Some subsystems present timeout (or timeout-like) values to their own
users as APIs that would re-use the kernel's own constants and
conventions. These will require some minor design work to adapt to
the new scheme (in most cases just using k_timeout_t directly in their
own API), and they have not been changed in this patch, instead
selecting CONFIG_LEGACY_TIMEOUT_API via kconfig. These subsystems
include: CAN Bus, the Microbit display driver, I2S, LoRa modem
drivers, the UART Async API, Video hardware drivers, the console
subsystem, and the network buffer abstraction.
k_sleep() now takes a k_timeout_t argument, with a k_msleep() variant
provided that works identically to the original API.
Most of the changes here are just type/configuration management and
documentation, but there are logic changes in mempool, where a loop
that used a timeout numerically has been reworked using a new
z_timeout_end_calc() predicate. Also in queue.c, a (when POLL was
enabled) a similar loop was needlessly used to try to retry the
k_poll() call after a spurious failure. But k_poll() does not fail
spuriously, so the loop was removed.
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2020-03-06 00:18:14 +01:00
|
|
|
if (ticks == K_TICKS_FOREVER) {
|
2020-03-18 05:47:19 +01:00
|
|
|
next_cycles = cycles + CYCLES_NEXT_MAX;
|
|
|
|
} else {
|
2020-05-27 18:26:57 +02:00
|
|
|
next_cycles = cycles + ((uint32_t)ticks * CYCLES_PER_TICK);
|
2020-03-18 05:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set match value for the next interrupt */
|
|
|
|
update_match(cycles, next_cycles);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t z_clock_elapsed(void)
|
2019-12-19 12:43:37 +01:00
|
|
|
{
|
2020-03-18 05:47:19 +01:00
|
|
|
#ifdef CONFIG_TICKLESS_KERNEL
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t cycles;
|
2020-03-18 05:47:19 +01:00
|
|
|
|
|
|
|
/* Read counter value */
|
|
|
|
cycles = read_count();
|
|
|
|
|
|
|
|
/* Return the number of ticks since last announcement */
|
|
|
|
return (cycles - last_cycles) / CYCLES_PER_TICK;
|
|
|
|
#else
|
|
|
|
/* Always return 0 for tickful operation */
|
2019-12-19 12:43:37 +01:00
|
|
|
return 0;
|
2020-03-18 05:47:19 +01:00
|
|
|
#endif
|
2019-12-19 12:43:37 +01:00
|
|
|
}
|
2018-11-18 08:28:49 +01:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t z_timer_cycle_get_32(void)
|
2018-11-18 08:28:49 +01:00
|
|
|
{
|
2020-03-18 05:47:19 +01:00
|
|
|
/* Return the current counter value */
|
|
|
|
return read_count();
|
2018-11-18 08:28:49 +01:00
|
|
|
}
|