1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Allwinner A1X SoCs timer handling.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2012 Maxime Ripard
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Maxime Ripard <maxime.ripard@free-electrons.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Based on code from
9*4882a593Smuzhiyun * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
10*4882a593Smuzhiyun * Benn Huang <benn@allwinnertech.com>
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * This file is licensed under the terms of the GNU General Public
13*4882a593Smuzhiyun * License version 2. This program is licensed "as is" without any
14*4882a593Smuzhiyun * warranty of any kind, whether express or implied.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <linux/clk.h>
18*4882a593Smuzhiyun #include <linux/clockchips.h>
19*4882a593Smuzhiyun #include <linux/interrupt.h>
20*4882a593Smuzhiyun #include <linux/irq.h>
21*4882a593Smuzhiyun #include <linux/irqreturn.h>
22*4882a593Smuzhiyun #include <linux/sched_clock.h>
23*4882a593Smuzhiyun #include <linux/of.h>
24*4882a593Smuzhiyun #include <linux/of_address.h>
25*4882a593Smuzhiyun #include <linux/of_irq.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include "timer-of.h"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define TIMER_IRQ_EN_REG 0x00
30*4882a593Smuzhiyun #define TIMER_IRQ_EN(val) BIT(val)
31*4882a593Smuzhiyun #define TIMER_IRQ_ST_REG 0x04
32*4882a593Smuzhiyun #define TIMER_CTL_REG(val) (0x10 * val + 0x10)
33*4882a593Smuzhiyun #define TIMER_CTL_ENABLE BIT(0)
34*4882a593Smuzhiyun #define TIMER_CTL_RELOAD BIT(1)
35*4882a593Smuzhiyun #define TIMER_CTL_CLK_SRC(val) (((val) & 0x3) << 2)
36*4882a593Smuzhiyun #define TIMER_CTL_CLK_SRC_OSC24M (1)
37*4882a593Smuzhiyun #define TIMER_CTL_CLK_PRES(val) (((val) & 0x7) << 4)
38*4882a593Smuzhiyun #define TIMER_CTL_ONESHOT BIT(7)
39*4882a593Smuzhiyun #define TIMER_INTVAL_REG(val) (0x10 * (val) + 0x14)
40*4882a593Smuzhiyun #define TIMER_CNTVAL_REG(val) (0x10 * (val) + 0x18)
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define TIMER_SYNC_TICKS 3
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun * When we disable a timer, we need to wait at least for 2 cycles of
46*4882a593Smuzhiyun * the timer source clock. We will use for that the clocksource timer
47*4882a593Smuzhiyun * that is already setup and runs at the same frequency than the other
48*4882a593Smuzhiyun * timers, and we never will be disabled.
49*4882a593Smuzhiyun */
sun4i_clkevt_sync(void __iomem * base)50*4882a593Smuzhiyun static void sun4i_clkevt_sync(void __iomem *base)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun u32 old = readl(base + TIMER_CNTVAL_REG(1));
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun while ((old - readl(base + TIMER_CNTVAL_REG(1))) < TIMER_SYNC_TICKS)
55*4882a593Smuzhiyun cpu_relax();
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
sun4i_clkevt_time_stop(void __iomem * base,u8 timer)58*4882a593Smuzhiyun static void sun4i_clkevt_time_stop(void __iomem *base, u8 timer)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun u32 val = readl(base + TIMER_CTL_REG(timer));
61*4882a593Smuzhiyun writel(val & ~TIMER_CTL_ENABLE, base + TIMER_CTL_REG(timer));
62*4882a593Smuzhiyun sun4i_clkevt_sync(base);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
sun4i_clkevt_time_setup(void __iomem * base,u8 timer,unsigned long delay)65*4882a593Smuzhiyun static void sun4i_clkevt_time_setup(void __iomem *base, u8 timer,
66*4882a593Smuzhiyun unsigned long delay)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun writel(delay, base + TIMER_INTVAL_REG(timer));
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun
sun4i_clkevt_time_start(void __iomem * base,u8 timer,bool periodic)71*4882a593Smuzhiyun static void sun4i_clkevt_time_start(void __iomem *base, u8 timer,
72*4882a593Smuzhiyun bool periodic)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun u32 val = readl(base + TIMER_CTL_REG(timer));
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun if (periodic)
77*4882a593Smuzhiyun val &= ~TIMER_CTL_ONESHOT;
78*4882a593Smuzhiyun else
79*4882a593Smuzhiyun val |= TIMER_CTL_ONESHOT;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun writel(val | TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
82*4882a593Smuzhiyun base + TIMER_CTL_REG(timer));
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
sun4i_clkevt_shutdown(struct clock_event_device * evt)85*4882a593Smuzhiyun static int sun4i_clkevt_shutdown(struct clock_event_device *evt)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun struct timer_of *to = to_timer_of(evt);
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun sun4i_clkevt_time_stop(timer_of_base(to), 0);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun return 0;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
sun4i_clkevt_set_oneshot(struct clock_event_device * evt)94*4882a593Smuzhiyun static int sun4i_clkevt_set_oneshot(struct clock_event_device *evt)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun struct timer_of *to = to_timer_of(evt);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun sun4i_clkevt_time_stop(timer_of_base(to), 0);
99*4882a593Smuzhiyun sun4i_clkevt_time_start(timer_of_base(to), 0, false);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun return 0;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
sun4i_clkevt_set_periodic(struct clock_event_device * evt)104*4882a593Smuzhiyun static int sun4i_clkevt_set_periodic(struct clock_event_device *evt)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun struct timer_of *to = to_timer_of(evt);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun sun4i_clkevt_time_stop(timer_of_base(to), 0);
109*4882a593Smuzhiyun sun4i_clkevt_time_setup(timer_of_base(to), 0, timer_of_period(to));
110*4882a593Smuzhiyun sun4i_clkevt_time_start(timer_of_base(to), 0, true);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
sun4i_clkevt_next_event(unsigned long evt,struct clock_event_device * clkevt)115*4882a593Smuzhiyun static int sun4i_clkevt_next_event(unsigned long evt,
116*4882a593Smuzhiyun struct clock_event_device *clkevt)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun struct timer_of *to = to_timer_of(clkevt);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun sun4i_clkevt_time_stop(timer_of_base(to), 0);
121*4882a593Smuzhiyun sun4i_clkevt_time_setup(timer_of_base(to), 0, evt - TIMER_SYNC_TICKS);
122*4882a593Smuzhiyun sun4i_clkevt_time_start(timer_of_base(to), 0, false);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun return 0;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
sun4i_timer_clear_interrupt(void __iomem * base)127*4882a593Smuzhiyun static void sun4i_timer_clear_interrupt(void __iomem *base)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun writel(TIMER_IRQ_EN(0), base + TIMER_IRQ_ST_REG);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
sun4i_timer_interrupt(int irq,void * dev_id)132*4882a593Smuzhiyun static irqreturn_t sun4i_timer_interrupt(int irq, void *dev_id)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun struct clock_event_device *evt = (struct clock_event_device *)dev_id;
135*4882a593Smuzhiyun struct timer_of *to = to_timer_of(evt);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun sun4i_timer_clear_interrupt(timer_of_base(to));
138*4882a593Smuzhiyun evt->event_handler(evt);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun return IRQ_HANDLED;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun static struct timer_of to = {
144*4882a593Smuzhiyun .flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE,
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun .clkevt = {
147*4882a593Smuzhiyun .name = "sun4i_tick",
148*4882a593Smuzhiyun .rating = 350,
149*4882a593Smuzhiyun .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
150*4882a593Smuzhiyun .set_state_shutdown = sun4i_clkevt_shutdown,
151*4882a593Smuzhiyun .set_state_periodic = sun4i_clkevt_set_periodic,
152*4882a593Smuzhiyun .set_state_oneshot = sun4i_clkevt_set_oneshot,
153*4882a593Smuzhiyun .tick_resume = sun4i_clkevt_shutdown,
154*4882a593Smuzhiyun .set_next_event = sun4i_clkevt_next_event,
155*4882a593Smuzhiyun .cpumask = cpu_possible_mask,
156*4882a593Smuzhiyun },
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun .of_irq = {
159*4882a593Smuzhiyun .handler = sun4i_timer_interrupt,
160*4882a593Smuzhiyun .flags = IRQF_TIMER | IRQF_IRQPOLL,
161*4882a593Smuzhiyun },
162*4882a593Smuzhiyun };
163*4882a593Smuzhiyun
sun4i_timer_sched_read(void)164*4882a593Smuzhiyun static u64 notrace sun4i_timer_sched_read(void)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun return ~readl(timer_of_base(&to) + TIMER_CNTVAL_REG(1));
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
sun4i_timer_init(struct device_node * node)169*4882a593Smuzhiyun static int __init sun4i_timer_init(struct device_node *node)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun int ret;
172*4882a593Smuzhiyun u32 val;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun ret = timer_of_init(node, &to);
175*4882a593Smuzhiyun if (ret)
176*4882a593Smuzhiyun return ret;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun writel(~0, timer_of_base(&to) + TIMER_INTVAL_REG(1));
179*4882a593Smuzhiyun writel(TIMER_CTL_ENABLE | TIMER_CTL_RELOAD |
180*4882a593Smuzhiyun TIMER_CTL_CLK_SRC(TIMER_CTL_CLK_SRC_OSC24M),
181*4882a593Smuzhiyun timer_of_base(&to) + TIMER_CTL_REG(1));
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * sched_clock_register does not have priorities, and on sun6i and
185*4882a593Smuzhiyun * later there is a better sched_clock registered by arm_arch_timer.c
186*4882a593Smuzhiyun */
187*4882a593Smuzhiyun if (of_machine_is_compatible("allwinner,sun4i-a10") ||
188*4882a593Smuzhiyun of_machine_is_compatible("allwinner,sun5i-a13") ||
189*4882a593Smuzhiyun of_machine_is_compatible("allwinner,sun5i-a10s") ||
190*4882a593Smuzhiyun of_machine_is_compatible("allwinner,suniv-f1c100s"))
191*4882a593Smuzhiyun sched_clock_register(sun4i_timer_sched_read, 32,
192*4882a593Smuzhiyun timer_of_rate(&to));
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun ret = clocksource_mmio_init(timer_of_base(&to) + TIMER_CNTVAL_REG(1),
195*4882a593Smuzhiyun node->name, timer_of_rate(&to), 350, 32,
196*4882a593Smuzhiyun clocksource_mmio_readl_down);
197*4882a593Smuzhiyun if (ret) {
198*4882a593Smuzhiyun pr_err("Failed to register clocksource\n");
199*4882a593Smuzhiyun return ret;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun writel(TIMER_CTL_CLK_SRC(TIMER_CTL_CLK_SRC_OSC24M),
203*4882a593Smuzhiyun timer_of_base(&to) + TIMER_CTL_REG(0));
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /* Make sure timer is stopped before playing with interrupts */
206*4882a593Smuzhiyun sun4i_clkevt_time_stop(timer_of_base(&to), 0);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /* clear timer0 interrupt */
209*4882a593Smuzhiyun sun4i_timer_clear_interrupt(timer_of_base(&to));
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
212*4882a593Smuzhiyun TIMER_SYNC_TICKS, 0xffffffff);
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun /* Enable timer0 interrupt */
215*4882a593Smuzhiyun val = readl(timer_of_base(&to) + TIMER_IRQ_EN_REG);
216*4882a593Smuzhiyun writel(val | TIMER_IRQ_EN(0), timer_of_base(&to) + TIMER_IRQ_EN_REG);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun return ret;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun TIMER_OF_DECLARE(sun4i, "allwinner,sun4i-a10-timer",
221*4882a593Smuzhiyun sun4i_timer_init);
222*4882a593Smuzhiyun TIMER_OF_DECLARE(sun8i_a23, "allwinner,sun8i-a23-timer",
223*4882a593Smuzhiyun sun4i_timer_init);
224*4882a593Smuzhiyun TIMER_OF_DECLARE(sun8i_v3s, "allwinner,sun8i-v3s-timer",
225*4882a593Smuzhiyun sun4i_timer_init);
226*4882a593Smuzhiyun TIMER_OF_DECLARE(suniv, "allwinner,suniv-f1c100s-timer",
227*4882a593Smuzhiyun sun4i_timer_init);
228