2019-10-27 17:12:58 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Carlo Caione <ccaione@baylibre.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <drivers/timer/arm_arch_timer.h>
|
|
|
|
#include <drivers/timer/system_timer.h>
|
|
|
|
#include <sys_clock.h>
|
|
|
|
#include <spinlock.h>
|
|
|
|
#include <arch/cpu.h>
|
|
|
|
|
2020-07-03 13:32:20 +02:00
|
|
|
#define CYC_PER_TICK ((uint64_t)sys_clock_hw_cycles_per_sec() \
|
|
|
|
/ (uint64_t)CONFIG_SYS_CLOCK_TICKS_PER_SEC)
|
|
|
|
#define MAX_TICKS INT32_MAX
|
2019-10-27 17:12:58 +01:00
|
|
|
#define MIN_DELAY (1000)
|
|
|
|
|
|
|
|
static struct k_spinlock lock;
|
2020-05-27 18:26:57 +02:00
|
|
|
static volatile uint64_t last_cycle;
|
2019-10-27 17:12:58 +01:00
|
|
|
|
|
|
|
static void arm_arch_timer_compare_isr(void *arg)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(arg);
|
|
|
|
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint64_t curr_cycle = arm_arch_timer_count();
|
|
|
|
uint32_t delta_ticks = (uint32_t)((curr_cycle - last_cycle) / CYC_PER_TICK);
|
2019-10-27 17:12:58 +01:00
|
|
|
|
|
|
|
last_cycle += delta_ticks * CYC_PER_TICK;
|
|
|
|
|
2020-05-12 04:32:40 +02:00
|
|
|
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
|
2020-05-27 18:26:57 +02:00
|
|
|
uint64_t next_cycle = last_cycle + CYC_PER_TICK;
|
2019-10-27 17:12:58 +01:00
|
|
|
|
2020-07-03 13:32:20 +02:00
|
|
|
if ((uint64_t)(next_cycle - curr_cycle) < MIN_DELAY) {
|
2019-10-27 17:12:58 +01:00
|
|
|
next_cycle += CYC_PER_TICK;
|
|
|
|
}
|
|
|
|
arm_arch_timer_set_compare(next_cycle);
|
|
|
|
}
|
|
|
|
|
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
|
|
|
|
z_clock_announce(IS_ENABLED(CONFIG_TICKLESS_KERNEL) ? delta_ticks : 1);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
int z_clock_driver_init(const struct device *device)
|
2019-10-27 17:12:58 +01:00
|
|
|
{
|
|
|
|
ARG_UNUSED(device);
|
|
|
|
|
2020-05-12 12:00:03 +02:00
|
|
|
IRQ_CONNECT(ARM_ARCH_TIMER_IRQ, ARM_ARCH_TIMER_PRIO,
|
|
|
|
arm_arch_timer_compare_isr, NULL, ARM_ARCH_TIMER_FLAGS);
|
2019-10-27 17:12:58 +01:00
|
|
|
arm_arch_timer_set_compare(arm_arch_timer_count() + CYC_PER_TICK);
|
|
|
|
arm_arch_timer_enable(true);
|
|
|
|
irq_enable(ARM_ARCH_TIMER_IRQ);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
void z_clock_set_timeout(int32_t ticks, bool idle)
|
2019-10-27 17:12:58 +01:00
|
|
|
{
|
|
|
|
ARG_UNUSED(idle);
|
|
|
|
|
2020-05-12 04:32:40 +02:00
|
|
|
#if defined(CONFIG_TICKLESS_KERNEL)
|
2019-10-27 17:12:58 +01:00
|
|
|
|
|
|
|
if (idle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-03 13:32:20 +02:00
|
|
|
ticks = (ticks == K_TICKS_FOREVER) ? MAX_TICKS : \
|
|
|
|
MIN(MAX_TICKS, MAX(ticks - 1, 0));
|
2019-10-27 17:12:58 +01:00
|
|
|
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
2020-05-27 18:26:57 +02:00
|
|
|
uint64_t curr_cycle = arm_arch_timer_count();
|
2020-07-03 13:32:20 +02:00
|
|
|
uint64_t req_cycle = ticks * CYC_PER_TICK;
|
2019-10-27 17:12:58 +01:00
|
|
|
|
|
|
|
/* Round up to next tick boundary */
|
2020-07-03 13:32:20 +02:00
|
|
|
req_cycle += (curr_cycle - last_cycle) + (CYC_PER_TICK - 1);
|
|
|
|
|
2019-10-27 17:12:58 +01:00
|
|
|
req_cycle = (req_cycle / CYC_PER_TICK) * CYC_PER_TICK;
|
|
|
|
|
2020-07-03 13:32:20 +02:00
|
|
|
if ((req_cycle + last_cycle - curr_cycle) < MIN_DELAY) {
|
2019-10-27 17:12:58 +01:00
|
|
|
req_cycle += CYC_PER_TICK;
|
|
|
|
}
|
|
|
|
|
|
|
|
arm_arch_timer_set_compare(req_cycle + last_cycle);
|
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t z_clock_elapsed(void)
|
2019-10-27 17:12:58 +01:00
|
|
|
{
|
|
|
|
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&lock);
|
2020-07-03 13:32:20 +02:00
|
|
|
uint32_t ret = (uint32_t)((arm_arch_timer_count() - last_cycle)
|
|
|
|
/ CYC_PER_TICK);
|
2019-10-27 17:12:58 +01:00
|
|
|
|
|
|
|
k_spin_unlock(&lock, key);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t z_timer_cycle_get_32(void)
|
2019-10-27 17:12:58 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
return (uint32_t)arm_arch_timer_count();
|
2019-10-27 17:12:58 +01:00
|
|
|
}
|