2019-06-05 14:40:52 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
//
|
|
|
|
// Copyright 2017-2019 NXP
|
|
|
|
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/clockchips.h>
|
2024-02-05 11:17:58 +08:00
|
|
|
#include <linux/slab.h>
|
2019-06-05 14:40:52 +08:00
|
|
|
|
|
|
|
#include "timer-of.h"
|
|
|
|
|
|
|
|
#define CMP_OFFSET 0x10000
|
2024-02-05 11:17:59 +08:00
|
|
|
#define RD_OFFSET 0x20000
|
2019-06-05 14:40:52 +08:00
|
|
|
|
|
|
|
#define CNTCV_LO 0x8
|
|
|
|
#define CNTCV_HI 0xc
|
|
|
|
#define CMPCV_LO (CMP_OFFSET + 0x20)
|
|
|
|
#define CMPCV_HI (CMP_OFFSET + 0x24)
|
|
|
|
#define CMPCR (CMP_OFFSET + 0x2c)
|
2024-02-05 11:17:59 +08:00
|
|
|
#define CNTCV_LO_IMX95 (RD_OFFSET + 0x8)
|
|
|
|
#define CNTCV_HI_IMX95 (RD_OFFSET + 0xc)
|
2019-06-05 14:40:52 +08:00
|
|
|
|
|
|
|
#define SYS_CTR_EN 0x1
|
|
|
|
#define SYS_CTR_IRQ_MASK 0x2
|
|
|
|
|
2019-08-16 08:38:42 +08:00
|
|
|
#define SYS_CTR_CLK_DIV 0x3
|
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
struct sysctr_private {
|
|
|
|
u32 cmpcr;
|
2024-02-05 11:17:59 +08:00
|
|
|
u32 lo_off;
|
|
|
|
u32 hi_off;
|
2024-02-05 11:17:58 +08:00
|
|
|
};
|
2019-06-05 14:40:52 +08:00
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
static void sysctr_timer_enable(struct clock_event_device *evt, bool enable)
|
2019-06-05 14:40:52 +08:00
|
|
|
{
|
2024-02-05 11:17:58 +08:00
|
|
|
struct timer_of *to = to_timer_of(evt);
|
|
|
|
struct sysctr_private *priv = to->private_data;
|
|
|
|
void __iomem *base = timer_of_base(to);
|
|
|
|
|
|
|
|
writel(enable ? priv->cmpcr | SYS_CTR_EN : priv->cmpcr, base + CMPCR);
|
2019-06-05 14:40:52 +08:00
|
|
|
}
|
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
static void sysctr_irq_acknowledge(struct clock_event_device *evt)
|
2019-06-05 14:40:52 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* clear the enable bit(EN =0) will clear
|
|
|
|
* the status bit(ISTAT = 0), then the interrupt
|
|
|
|
* signal will be negated(acknowledged).
|
|
|
|
*/
|
2024-02-05 11:17:58 +08:00
|
|
|
sysctr_timer_enable(evt, false);
|
2019-06-05 14:40:52 +08:00
|
|
|
}
|
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
static inline u64 sysctr_read_counter(struct clock_event_device *evt)
|
2019-06-05 14:40:52 +08:00
|
|
|
{
|
2024-02-05 11:17:58 +08:00
|
|
|
struct timer_of *to = to_timer_of(evt);
|
2024-02-05 11:17:59 +08:00
|
|
|
struct sysctr_private *priv = to->private_data;
|
2024-02-05 11:17:58 +08:00
|
|
|
void __iomem *base = timer_of_base(to);
|
2019-06-05 14:40:52 +08:00
|
|
|
u32 cnt_hi, tmp_hi, cnt_lo;
|
|
|
|
|
|
|
|
do {
|
2024-02-05 11:17:59 +08:00
|
|
|
cnt_hi = readl_relaxed(base + priv->hi_off);
|
|
|
|
cnt_lo = readl_relaxed(base + priv->lo_off);
|
|
|
|
tmp_hi = readl_relaxed(base + priv->hi_off);
|
2019-06-05 14:40:52 +08:00
|
|
|
} while (tmp_hi != cnt_hi);
|
|
|
|
|
|
|
|
return ((u64) cnt_hi << 32) | cnt_lo;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sysctr_set_next_event(unsigned long delta,
|
|
|
|
struct clock_event_device *evt)
|
|
|
|
{
|
2024-02-05 11:17:58 +08:00
|
|
|
struct timer_of *to = to_timer_of(evt);
|
|
|
|
void __iomem *base = timer_of_base(to);
|
2019-06-05 14:40:52 +08:00
|
|
|
u32 cmp_hi, cmp_lo;
|
|
|
|
u64 next;
|
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
sysctr_timer_enable(evt, false);
|
2019-06-05 14:40:52 +08:00
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
next = sysctr_read_counter(evt);
|
2019-06-05 14:40:52 +08:00
|
|
|
|
|
|
|
next += delta;
|
|
|
|
|
|
|
|
cmp_hi = (next >> 32) & 0x00fffff;
|
|
|
|
cmp_lo = next & 0xffffffff;
|
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
writel_relaxed(cmp_hi, base + CMPCV_HI);
|
|
|
|
writel_relaxed(cmp_lo, base + CMPCV_LO);
|
2019-06-05 14:40:52 +08:00
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
sysctr_timer_enable(evt, true);
|
2019-06-05 14:40:52 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sysctr_set_state_oneshot(struct clock_event_device *evt)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sysctr_set_state_shutdown(struct clock_event_device *evt)
|
|
|
|
{
|
2024-02-05 11:17:58 +08:00
|
|
|
sysctr_timer_enable(evt, false);
|
2019-06-05 14:40:52 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t sysctr_timer_interrupt(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct clock_event_device *evt = dev_id;
|
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
sysctr_irq_acknowledge(evt);
|
2019-06-05 14:40:52 +08:00
|
|
|
|
|
|
|
evt->event_handler(evt);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct timer_of to_sysctr = {
|
|
|
|
.flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE,
|
|
|
|
.clkevt = {
|
|
|
|
.name = "i.MX system counter timer",
|
|
|
|
.features = CLOCK_EVT_FEAT_ONESHOT |
|
|
|
|
CLOCK_EVT_FEAT_DYNIRQ,
|
|
|
|
.set_state_oneshot = sysctr_set_state_oneshot,
|
|
|
|
.set_next_event = sysctr_set_next_event,
|
|
|
|
.set_state_shutdown = sysctr_set_state_shutdown,
|
|
|
|
.rating = 200,
|
|
|
|
},
|
|
|
|
.of_irq = {
|
|
|
|
.handler = sysctr_timer_interrupt,
|
2021-12-14 20:07:33 +08:00
|
|
|
.flags = IRQF_TIMER,
|
2019-06-05 14:40:52 +08:00
|
|
|
},
|
|
|
|
.of_clk = {
|
|
|
|
.name = "per",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-02-05 11:17:59 +08:00
|
|
|
static int __init __sysctr_timer_init(struct device_node *np)
|
2019-06-05 14:40:52 +08:00
|
|
|
{
|
2024-02-05 11:17:58 +08:00
|
|
|
struct sysctr_private *priv;
|
|
|
|
void __iomem *base;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv = kzalloc(sizeof(struct sysctr_private), GFP_KERNEL);
|
|
|
|
if (!priv)
|
|
|
|
return -ENOMEM;
|
2019-06-05 14:40:52 +08:00
|
|
|
|
|
|
|
ret = timer_of_init(np, &to_sysctr);
|
2024-02-05 11:17:58 +08:00
|
|
|
if (ret) {
|
|
|
|
kfree(priv);
|
2019-06-05 14:40:52 +08:00
|
|
|
return ret;
|
2024-02-05 11:17:58 +08:00
|
|
|
}
|
2019-06-05 14:40:52 +08:00
|
|
|
|
2022-09-02 19:12:07 +08:00
|
|
|
if (!of_property_read_bool(np, "nxp,no-divider")) {
|
|
|
|
/* system counter clock is divided by 3 internally */
|
|
|
|
to_sysctr.of_clk.rate /= SYS_CTR_CLK_DIV;
|
|
|
|
}
|
2019-08-16 08:38:42 +08:00
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
to_sysctr.clkevt.cpumask = cpu_possible_mask;
|
|
|
|
to_sysctr.private_data = priv;
|
2019-06-05 14:40:52 +08:00
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
base = timer_of_base(&to_sysctr);
|
|
|
|
priv->cmpcr = readl(base + CMPCR) & ~SYS_CTR_EN;
|
2019-06-05 14:40:52 +08:00
|
|
|
|
2024-02-05 11:17:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init sysctr_timer_init(struct device_node *np)
|
|
|
|
{
|
|
|
|
struct sysctr_private *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = __sysctr_timer_init(np);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
priv = to_sysctr.private_data;
|
|
|
|
priv->lo_off = CNTCV_LO;
|
|
|
|
priv->hi_off = CNTCV_HI;
|
|
|
|
|
2024-02-05 11:17:58 +08:00
|
|
|
clockevents_config_and_register(&to_sysctr.clkevt,
|
|
|
|
timer_of_rate(&to_sysctr),
|
|
|
|
0xff, 0x7fffffff);
|
2024-02-05 11:17:59 +08:00
|
|
|
|
2019-06-05 14:40:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2024-02-05 11:17:59 +08:00
|
|
|
|
|
|
|
static int __init sysctr_timer_imx95_init(struct device_node *np)
|
|
|
|
{
|
|
|
|
struct sysctr_private *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = __sysctr_timer_init(np);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
priv = to_sysctr.private_data;
|
|
|
|
priv->lo_off = CNTCV_LO_IMX95;
|
|
|
|
priv->hi_off = CNTCV_HI_IMX95;
|
|
|
|
|
|
|
|
clockevents_config_and_register(&to_sysctr.clkevt,
|
|
|
|
timer_of_rate(&to_sysctr),
|
|
|
|
0xff, 0x7fffffff);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-05 14:40:52 +08:00
|
|
|
TIMER_OF_DECLARE(sysctr_timer, "nxp,sysctr-timer", sysctr_timer_init);
|
2024-02-05 11:17:59 +08:00
|
|
|
TIMER_OF_DECLARE(sysctr_timer_imx95, "nxp,imx95-sysctr-timer", sysctr_timer_imx95_init);
|