Commit 10c35b8e authored by Vebjorn Myklebust's avatar Vebjorn Myklebust Committed by Benjamin Cabé
Browse files

drivers: timer: Add support for cc23x0 systim



Add support for systim to cc23x0 SoC.

Signed-off-by: default avatarLars Thalian Morstad <l-morstad@ti.com>
Signed-off-by: default avatarVebjorn Myklebust <v.myklebust@ti.com>
Signed-off-by: default avatarStoyan Bogdanov <sbogdanov@baylibre.com>
Signed-off-by: default avatarJulien Panis <jpanis@baylibre.com>
parent 496c815c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ zephyr_library_sources_ifdef(CONFIG_ARCV2_TIMER arcv2_timer0.c)
zephyr_library_sources_ifdef(CONFIG_ARM_ARCH_TIMER arm_arch_timer.c)
zephyr_library_sources_ifdef(CONFIG_INTEL_ADSP_TIMER intel_adsp_timer.c)
zephyr_library_sources_ifdef(CONFIG_CC13XX_CC26XX_RTC_TIMER cc13xx_cc26xx_rtc_timer.c)
zephyr_library_sources_ifdef(CONFIG_CC23X0_SYSTIM_TIMER cc23x0_systim_timer.c)
zephyr_library_sources_ifdef(CONFIG_CH32V00X_SYSTICK wch_systick_ch32v00x.c)
zephyr_library_sources_ifdef(CONFIG_CORTEX_M_SYSTICK cortex_m_systick.c)
zephyr_library_sources_ifdef(CONFIG_ESP32_SYS_TIMER esp32_sys_timer.c)
+1 −0
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ source "drivers/timer/Kconfig.arcv2"
source "drivers/timer/Kconfig.arm_arch"
source "drivers/timer/Kconfig.cavs"
source "drivers/timer/Kconfig.cc13xx_cc26xx_rtc"
source "drivers/timer/Kconfig.cc23x0_systim"
source "drivers/timer/Kconfig.wch_ch32v00x"
source "drivers/timer/Kconfig.cortex_m_systick"
source "drivers/timer/Kconfig.esp32"
+13 −0
Original line number Diff line number Diff line
# Copyright (c) 2024 Texas Instruments Incorporated
# Copyright (c) 2024 BayLibre, SAS
#
# SPDX-License-Identifier: Apache-2.0

config CC23X0_SYSTIM_TIMER
	bool "TI SimpleLink CC23X0 system clock timer"
	default y
	depends on HAS_CC23X0_SDK
	select TICKLESS_CAPABLE
	help
	  This module provides the "system clock driver" interfaces
	  for the TI Simplelink CC23X0 devices.
+154 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2024 Texas Instruments Incorporated
 * Copyright (c) 2024 BayLibre, SAS
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#define DT_DRV_COMPAT ti_cc23x0_systim_timer

/*
 * TI SimpleLink CC23X0 timer driver based on SYSTIM
 */

#include <soc.h>

#include <zephyr/device.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/drivers/timer/system_timer.h>
#include <zephyr/irq.h>
#include <zephyr/spinlock.h>
#include <zephyr/sys_clock.h>
#include <zephyr/sys/util.h>

#include <inc/hw_types.h>
#include <inc/hw_memmap.h>
#include <inc/hw_systim.h>
#include <inc/hw_evtsvt.h>

/* Kernel tick period in microseconds (same timebase as systim) */
#define TICK_PERIOD_MICRO_SEC (1000000 / CONFIG_SYS_CLOCK_TICKS_PER_SEC)

/*
 * Max number of systim ticks into the future
 *
 * Under the hood, the kernel timer uses the SysTimer whose events trigger
 * immediately if the compare value is less than 2^22 systimer ticks in the past
 * (4.194sec at 1us resolution). Therefore, the max number of SysTimer ticks you
 * can schedule into the future is 2^32 - 2^22 - 1 ticks (~= 4290 sec at 1us
 * resolution).
 */
#define SYSTIM_TIMEOUT_MAX 0xFFBFFFFFU

/* Set systim interrupt to lowest priority */
#define SYSTIM_ISR_PRIORITY 3U

/* Keep track of systim counter at previous announcement to the kernel */
static uint32_t last_systim_count;

static void systim_isr(const void *arg);
static int sys_clock_driver_init(void);

/*
 * Set system clock timeout.
 */
void sys_clock_set_timeout(int32_t ticks, bool idle)
{
	ARG_UNUSED(idle);

	/* If timeout is necessary */
	if (ticks != K_TICKS_FOREVER) {
		/* Get current value as early as possible */
		uint32_t now_tick = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
		uint32_t timeout = ticks * TICK_PERIOD_MICRO_SEC;

		if (timeout > SYSTIM_TIMEOUT_MAX) {
			timeout = SYSTIM_TIMEOUT_MAX;
		}
		/* This should wrap around */
		HWREG(SYSTIM_BASE + SYSTIM_O_CH0CC) = now_tick + timeout;
	}
}

uint32_t sys_clock_elapsed(void)
{
	/* Get current value as early as possible */
	uint32_t current_systim_count = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
	uint32_t elapsed_systim;

	if (current_systim_count >= last_systim_count) {
		elapsed_systim = current_systim_count - last_systim_count;
	} else {
		elapsed_systim = (UINT32_MAX - last_systim_count) + current_systim_count;
	}

	int32_t elapsed_ticks = elapsed_systim / TICK_PERIOD_MICRO_SEC;

	return elapsed_ticks;
}

uint32_t sys_clock_cycle_get_32(void)
{
	return HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
}

void systim_isr(const void *arg)
{
	/* Get current value as early as possible */
	uint32_t current_systim_count = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
	uint32_t elapsed_systim;

	if (current_systim_count >= last_systim_count) {
		elapsed_systim = current_systim_count - last_systim_count;
	} else {
		elapsed_systim = (UINT32_MAX - last_systim_count) + current_systim_count;
	}

	int32_t elapsed_ticks = elapsed_systim / TICK_PERIOD_MICRO_SEC;

	sys_clock_announce(elapsed_ticks);

	last_systim_count = current_systim_count;

	/* Do not re-arm systim. Zephyr will do so through sys_clock_set_timeout */
}

static int sys_clock_driver_init(void)
{
	uint32_t now_tick;

	/* Get current value as early as possible */
	now_tick = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
	last_systim_count = now_tick;

	/* Clear any pending interrupts on SysTimer channel 0 */
	HWREG(SYSTIM_BASE + SYSTIM_O_ICLR) = SYSTIM_ICLR_EV0_CLR;

	/*
	 * Configure SysTimer channel 0 to compare mode with timer
	 * resolution of 1 us.
	 */
	HWREG(SYSTIM_BASE + SYSTIM_O_CH0CFG) = 0;

	/* Make SysTimer halt on CPU debug halt */
	HWREG(SYSTIM_BASE + SYSTIM_O_EMU) = SYSTIM_EMU_HALT_STOP;

	HWREG(EVTSVT_BASE + EVTSVT_O_CPUIRQ16SEL) = EVTSVT_CPUIRQ16SEL_PUBID_SYSTIM0;

	/*
	 * Set IMASK for channel 0. IMASK is used by the power driver to know
	 * which systimer channels are active.
	 */
	HWREG(SYSTIM_BASE + SYSTIM_O_IMSET) = SYSTIM_IMSET_EV0_SET;

	/* This should wrap around and set a maximum timeout */
	HWREG(SYSTIM_BASE + SYSTIM_O_CH0CC) = now_tick + SYSTIM_TIMEOUT_MAX;

	/* Take configurable interrupt IRQ16 for systimer */
	IRQ_CONNECT(CPUIRQ16_IRQn, SYSTIM_ISR_PRIORITY, systim_isr, 0, 0);
	irq_enable(CPUIRQ16_IRQn);

	return 0;
}

SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2, CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
+15 −0
Original line number Diff line number Diff line
# Copyright (c) 2024 Baylibre SAS
# SPDX-License-Identifier: Apache-2.0

description: TI SimpleLink CC23x0 Timer Node

compatible: "ti,cc23x0-systim-timer"

include: base.yaml

properties:
  reg:
    required: true

  interrupts:
    required: true