133745c3cSGuo Ren // SPDX-License-Identifier: GPL-2.0
233745c3cSGuo Ren // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
333745c3cSGuo Ren
433745c3cSGuo Ren #include <linux/init.h>
533745c3cSGuo Ren #include <linux/interrupt.h>
633745c3cSGuo Ren #include <linux/sched_clock.h>
733745c3cSGuo Ren
833745c3cSGuo Ren #include "timer-of.h"
933745c3cSGuo Ren
1033745c3cSGuo Ren #define CLKSRC_OFFSET 0x40
1133745c3cSGuo Ren
1233745c3cSGuo Ren #define TIMER_STATUS 0x00
1333745c3cSGuo Ren #define TIMER_VALUE 0x04
1433745c3cSGuo Ren #define TIMER_CONTRL 0x10
1533745c3cSGuo Ren #define TIMER_CONFIG 0x20
1633745c3cSGuo Ren #define TIMER_DIV 0x24
1733745c3cSGuo Ren #define TIMER_INI 0x28
1833745c3cSGuo Ren
1933745c3cSGuo Ren #define GX6605S_STATUS_CLR BIT(0)
2033745c3cSGuo Ren #define GX6605S_CONTRL_RST BIT(0)
2133745c3cSGuo Ren #define GX6605S_CONTRL_START BIT(1)
2233745c3cSGuo Ren #define GX6605S_CONFIG_EN BIT(0)
2333745c3cSGuo Ren #define GX6605S_CONFIG_IRQ_EN BIT(1)
2433745c3cSGuo Ren
gx6605s_timer_interrupt(int irq,void * dev)2533745c3cSGuo Ren static irqreturn_t gx6605s_timer_interrupt(int irq, void *dev)
2633745c3cSGuo Ren {
2733745c3cSGuo Ren struct clock_event_device *ce = dev;
2833745c3cSGuo Ren void __iomem *base = timer_of_base(to_timer_of(ce));
2933745c3cSGuo Ren
3033745c3cSGuo Ren writel_relaxed(GX6605S_STATUS_CLR, base + TIMER_STATUS);
31*bc6717d5SGuo Ren writel_relaxed(0, base + TIMER_INI);
3233745c3cSGuo Ren
3333745c3cSGuo Ren ce->event_handler(ce);
3433745c3cSGuo Ren
3533745c3cSGuo Ren return IRQ_HANDLED;
3633745c3cSGuo Ren }
3733745c3cSGuo Ren
gx6605s_timer_set_oneshot(struct clock_event_device * ce)3833745c3cSGuo Ren static int gx6605s_timer_set_oneshot(struct clock_event_device *ce)
3933745c3cSGuo Ren {
4033745c3cSGuo Ren void __iomem *base = timer_of_base(to_timer_of(ce));
4133745c3cSGuo Ren
4233745c3cSGuo Ren /* reset and stop counter */
4333745c3cSGuo Ren writel_relaxed(GX6605S_CONTRL_RST, base + TIMER_CONTRL);
4433745c3cSGuo Ren
4533745c3cSGuo Ren /* enable with irq and start */
4633745c3cSGuo Ren writel_relaxed(GX6605S_CONFIG_EN | GX6605S_CONFIG_IRQ_EN,
4733745c3cSGuo Ren base + TIMER_CONFIG);
4833745c3cSGuo Ren
4933745c3cSGuo Ren return 0;
5033745c3cSGuo Ren }
5133745c3cSGuo Ren
gx6605s_timer_set_next_event(unsigned long delta,struct clock_event_device * ce)5233745c3cSGuo Ren static int gx6605s_timer_set_next_event(unsigned long delta,
5333745c3cSGuo Ren struct clock_event_device *ce)
5433745c3cSGuo Ren {
5533745c3cSGuo Ren void __iomem *base = timer_of_base(to_timer_of(ce));
5633745c3cSGuo Ren
5733745c3cSGuo Ren /* use reset to pause timer */
5833745c3cSGuo Ren writel_relaxed(GX6605S_CONTRL_RST, base + TIMER_CONTRL);
5933745c3cSGuo Ren
6033745c3cSGuo Ren /* config next timeout value */
6133745c3cSGuo Ren writel_relaxed(ULONG_MAX - delta, base + TIMER_INI);
6233745c3cSGuo Ren writel_relaxed(GX6605S_CONTRL_START, base + TIMER_CONTRL);
6333745c3cSGuo Ren
6433745c3cSGuo Ren return 0;
6533745c3cSGuo Ren }
6633745c3cSGuo Ren
gx6605s_timer_shutdown(struct clock_event_device * ce)6733745c3cSGuo Ren static int gx6605s_timer_shutdown(struct clock_event_device *ce)
6833745c3cSGuo Ren {
6933745c3cSGuo Ren void __iomem *base = timer_of_base(to_timer_of(ce));
7033745c3cSGuo Ren
7133745c3cSGuo Ren writel_relaxed(0, base + TIMER_CONTRL);
7233745c3cSGuo Ren writel_relaxed(0, base + TIMER_CONFIG);
7333745c3cSGuo Ren
7433745c3cSGuo Ren return 0;
7533745c3cSGuo Ren }
7633745c3cSGuo Ren
7733745c3cSGuo Ren static struct timer_of to = {
7833745c3cSGuo Ren .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
7933745c3cSGuo Ren .clkevt = {
8033745c3cSGuo Ren .rating = 300,
8133745c3cSGuo Ren .features = CLOCK_EVT_FEAT_DYNIRQ |
8233745c3cSGuo Ren CLOCK_EVT_FEAT_ONESHOT,
8333745c3cSGuo Ren .set_state_shutdown = gx6605s_timer_shutdown,
8433745c3cSGuo Ren .set_state_oneshot = gx6605s_timer_set_oneshot,
8533745c3cSGuo Ren .set_next_event = gx6605s_timer_set_next_event,
8633745c3cSGuo Ren .cpumask = cpu_possible_mask,
8733745c3cSGuo Ren },
8833745c3cSGuo Ren .of_irq = {
8933745c3cSGuo Ren .handler = gx6605s_timer_interrupt,
9033745c3cSGuo Ren .flags = IRQF_TIMER | IRQF_IRQPOLL,
9133745c3cSGuo Ren },
9233745c3cSGuo Ren };
9333745c3cSGuo Ren
gx6605s_sched_clock_read(void)9433745c3cSGuo Ren static u64 notrace gx6605s_sched_clock_read(void)
9533745c3cSGuo Ren {
9633745c3cSGuo Ren void __iomem *base;
9733745c3cSGuo Ren
9833745c3cSGuo Ren base = timer_of_base(&to) + CLKSRC_OFFSET;
9933745c3cSGuo Ren
10033745c3cSGuo Ren return (u64)readl_relaxed(base + TIMER_VALUE);
10133745c3cSGuo Ren }
10233745c3cSGuo Ren
gx6605s_clkevt_init(void __iomem * base)10333745c3cSGuo Ren static void gx6605s_clkevt_init(void __iomem *base)
10433745c3cSGuo Ren {
10533745c3cSGuo Ren writel_relaxed(0, base + TIMER_DIV);
10633745c3cSGuo Ren writel_relaxed(0, base + TIMER_CONFIG);
10733745c3cSGuo Ren
10833745c3cSGuo Ren clockevents_config_and_register(&to.clkevt, timer_of_rate(&to), 2,
10933745c3cSGuo Ren ULONG_MAX);
11033745c3cSGuo Ren }
11133745c3cSGuo Ren
gx6605s_clksrc_init(void __iomem * base)11233745c3cSGuo Ren static int gx6605s_clksrc_init(void __iomem *base)
11333745c3cSGuo Ren {
11433745c3cSGuo Ren writel_relaxed(0, base + TIMER_DIV);
11533745c3cSGuo Ren writel_relaxed(0, base + TIMER_INI);
11633745c3cSGuo Ren
11733745c3cSGuo Ren writel_relaxed(GX6605S_CONTRL_RST, base + TIMER_CONTRL);
11833745c3cSGuo Ren
11933745c3cSGuo Ren writel_relaxed(GX6605S_CONFIG_EN, base + TIMER_CONFIG);
12033745c3cSGuo Ren
12133745c3cSGuo Ren writel_relaxed(GX6605S_CONTRL_START, base + TIMER_CONTRL);
12233745c3cSGuo Ren
12333745c3cSGuo Ren sched_clock_register(gx6605s_sched_clock_read, 32, timer_of_rate(&to));
12433745c3cSGuo Ren
12533745c3cSGuo Ren return clocksource_mmio_init(base + TIMER_VALUE, "gx6605s",
12633745c3cSGuo Ren timer_of_rate(&to), 200, 32, clocksource_mmio_readl_up);
12733745c3cSGuo Ren }
12833745c3cSGuo Ren
gx6605s_timer_init(struct device_node * np)12933745c3cSGuo Ren static int __init gx6605s_timer_init(struct device_node *np)
13033745c3cSGuo Ren {
13133745c3cSGuo Ren int ret;
13233745c3cSGuo Ren
13333745c3cSGuo Ren /*
13433745c3cSGuo Ren * The timer driver is for nationalchip gx6605s SOC and there are two
13533745c3cSGuo Ren * same timer in gx6605s. We use one for clkevt and another for clksrc.
13633745c3cSGuo Ren *
13733745c3cSGuo Ren * The timer is mmio map to access, so we need give mmio address in dts.
13833745c3cSGuo Ren *
13933745c3cSGuo Ren * It provides a 32bit countup timer and interrupt will be caused by
14033745c3cSGuo Ren * count-overflow.
14133745c3cSGuo Ren * So we need set-next-event by ULONG_MAX - delta in TIMER_INI reg.
14233745c3cSGuo Ren *
14333745c3cSGuo Ren * The counter at 0x0 offset is clock event.
14433745c3cSGuo Ren * The counter at 0x40 offset is clock source.
14533745c3cSGuo Ren * They are the same in hardware, just different used by driver.
14633745c3cSGuo Ren */
14733745c3cSGuo Ren ret = timer_of_init(np, &to);
14833745c3cSGuo Ren if (ret)
14933745c3cSGuo Ren return ret;
15033745c3cSGuo Ren
15133745c3cSGuo Ren gx6605s_clkevt_init(timer_of_base(&to));
15233745c3cSGuo Ren
15333745c3cSGuo Ren return gx6605s_clksrc_init(timer_of_base(&to) + CLKSRC_OFFSET);
15433745c3cSGuo Ren }
15533745c3cSGuo Ren TIMER_OF_DECLARE(csky_gx6605s_timer, "csky,gx6605s-timer", gx6605s_timer_init);
156