1*7f83a132SManivannan Sadhasivam // SPDX-License-Identifier: GPL-2.0+
2*7f83a132SManivannan Sadhasivam /*
3*7f83a132SManivannan Sadhasivam * RDA8810PL SoC timer driver
4*7f83a132SManivannan Sadhasivam *
5*7f83a132SManivannan Sadhasivam * Copyright RDA Microelectronics Company Limited
6*7f83a132SManivannan Sadhasivam * Copyright (c) 2017 Andreas Färber
7*7f83a132SManivannan Sadhasivam * Copyright (c) 2018 Manivannan Sadhasivam
8*7f83a132SManivannan Sadhasivam *
9*7f83a132SManivannan Sadhasivam * RDA8810PL has two independent timers: OSTIMER (56 bit) and HWTIMER (64 bit).
10*7f83a132SManivannan Sadhasivam * Each timer provides optional interrupt support. In this driver, OSTIMER is
11*7f83a132SManivannan Sadhasivam * used for clockevents and HWTIMER is used for clocksource.
12*7f83a132SManivannan Sadhasivam */
13*7f83a132SManivannan Sadhasivam
14*7f83a132SManivannan Sadhasivam #include <linux/init.h>
15*7f83a132SManivannan Sadhasivam #include <linux/interrupt.h>
16*7f83a132SManivannan Sadhasivam
17*7f83a132SManivannan Sadhasivam #include "timer-of.h"
18*7f83a132SManivannan Sadhasivam
19*7f83a132SManivannan Sadhasivam #define RDA_OSTIMER_LOADVAL_L 0x000
20*7f83a132SManivannan Sadhasivam #define RDA_OSTIMER_CTRL 0x004
21*7f83a132SManivannan Sadhasivam #define RDA_HWTIMER_LOCKVAL_L 0x024
22*7f83a132SManivannan Sadhasivam #define RDA_HWTIMER_LOCKVAL_H 0x028
23*7f83a132SManivannan Sadhasivam #define RDA_TIMER_IRQ_MASK_SET 0x02c
24*7f83a132SManivannan Sadhasivam #define RDA_TIMER_IRQ_MASK_CLR 0x030
25*7f83a132SManivannan Sadhasivam #define RDA_TIMER_IRQ_CLR 0x034
26*7f83a132SManivannan Sadhasivam
27*7f83a132SManivannan Sadhasivam #define RDA_OSTIMER_CTRL_ENABLE BIT(24)
28*7f83a132SManivannan Sadhasivam #define RDA_OSTIMER_CTRL_REPEAT BIT(28)
29*7f83a132SManivannan Sadhasivam #define RDA_OSTIMER_CTRL_LOAD BIT(30)
30*7f83a132SManivannan Sadhasivam
31*7f83a132SManivannan Sadhasivam #define RDA_TIMER_IRQ_MASK_OSTIMER BIT(0)
32*7f83a132SManivannan Sadhasivam
33*7f83a132SManivannan Sadhasivam #define RDA_TIMER_IRQ_CLR_OSTIMER BIT(0)
34*7f83a132SManivannan Sadhasivam
rda_ostimer_start(void __iomem * base,bool periodic,u64 cycles)35*7f83a132SManivannan Sadhasivam static int rda_ostimer_start(void __iomem *base, bool periodic, u64 cycles)
36*7f83a132SManivannan Sadhasivam {
37*7f83a132SManivannan Sadhasivam u32 ctrl, load_l;
38*7f83a132SManivannan Sadhasivam
39*7f83a132SManivannan Sadhasivam load_l = (u32)cycles;
40*7f83a132SManivannan Sadhasivam ctrl = ((cycles >> 32) & 0xffffff);
41*7f83a132SManivannan Sadhasivam ctrl |= RDA_OSTIMER_CTRL_LOAD | RDA_OSTIMER_CTRL_ENABLE;
42*7f83a132SManivannan Sadhasivam if (periodic)
43*7f83a132SManivannan Sadhasivam ctrl |= RDA_OSTIMER_CTRL_REPEAT;
44*7f83a132SManivannan Sadhasivam
45*7f83a132SManivannan Sadhasivam /* Enable ostimer interrupt first */
46*7f83a132SManivannan Sadhasivam writel_relaxed(RDA_TIMER_IRQ_MASK_OSTIMER,
47*7f83a132SManivannan Sadhasivam base + RDA_TIMER_IRQ_MASK_SET);
48*7f83a132SManivannan Sadhasivam
49*7f83a132SManivannan Sadhasivam /* Write low 32 bits first, high 24 bits are with ctrl */
50*7f83a132SManivannan Sadhasivam writel_relaxed(load_l, base + RDA_OSTIMER_LOADVAL_L);
51*7f83a132SManivannan Sadhasivam writel_relaxed(ctrl, base + RDA_OSTIMER_CTRL);
52*7f83a132SManivannan Sadhasivam
53*7f83a132SManivannan Sadhasivam return 0;
54*7f83a132SManivannan Sadhasivam }
55*7f83a132SManivannan Sadhasivam
rda_ostimer_stop(void __iomem * base)56*7f83a132SManivannan Sadhasivam static int rda_ostimer_stop(void __iomem *base)
57*7f83a132SManivannan Sadhasivam {
58*7f83a132SManivannan Sadhasivam /* Disable ostimer interrupt first */
59*7f83a132SManivannan Sadhasivam writel_relaxed(RDA_TIMER_IRQ_MASK_OSTIMER,
60*7f83a132SManivannan Sadhasivam base + RDA_TIMER_IRQ_MASK_CLR);
61*7f83a132SManivannan Sadhasivam
62*7f83a132SManivannan Sadhasivam writel_relaxed(0, base + RDA_OSTIMER_CTRL);
63*7f83a132SManivannan Sadhasivam
64*7f83a132SManivannan Sadhasivam return 0;
65*7f83a132SManivannan Sadhasivam }
66*7f83a132SManivannan Sadhasivam
rda_ostimer_set_state_shutdown(struct clock_event_device * evt)67*7f83a132SManivannan Sadhasivam static int rda_ostimer_set_state_shutdown(struct clock_event_device *evt)
68*7f83a132SManivannan Sadhasivam {
69*7f83a132SManivannan Sadhasivam struct timer_of *to = to_timer_of(evt);
70*7f83a132SManivannan Sadhasivam
71*7f83a132SManivannan Sadhasivam rda_ostimer_stop(timer_of_base(to));
72*7f83a132SManivannan Sadhasivam
73*7f83a132SManivannan Sadhasivam return 0;
74*7f83a132SManivannan Sadhasivam }
75*7f83a132SManivannan Sadhasivam
rda_ostimer_set_state_oneshot(struct clock_event_device * evt)76*7f83a132SManivannan Sadhasivam static int rda_ostimer_set_state_oneshot(struct clock_event_device *evt)
77*7f83a132SManivannan Sadhasivam {
78*7f83a132SManivannan Sadhasivam struct timer_of *to = to_timer_of(evt);
79*7f83a132SManivannan Sadhasivam
80*7f83a132SManivannan Sadhasivam rda_ostimer_stop(timer_of_base(to));
81*7f83a132SManivannan Sadhasivam
82*7f83a132SManivannan Sadhasivam return 0;
83*7f83a132SManivannan Sadhasivam }
84*7f83a132SManivannan Sadhasivam
rda_ostimer_set_state_periodic(struct clock_event_device * evt)85*7f83a132SManivannan Sadhasivam static int rda_ostimer_set_state_periodic(struct clock_event_device *evt)
86*7f83a132SManivannan Sadhasivam {
87*7f83a132SManivannan Sadhasivam struct timer_of *to = to_timer_of(evt);
88*7f83a132SManivannan Sadhasivam unsigned long cycles_per_jiffy;
89*7f83a132SManivannan Sadhasivam
90*7f83a132SManivannan Sadhasivam rda_ostimer_stop(timer_of_base(to));
91*7f83a132SManivannan Sadhasivam
92*7f83a132SManivannan Sadhasivam cycles_per_jiffy = ((unsigned long long)NSEC_PER_SEC / HZ *
93*7f83a132SManivannan Sadhasivam evt->mult) >> evt->shift;
94*7f83a132SManivannan Sadhasivam rda_ostimer_start(timer_of_base(to), true, cycles_per_jiffy);
95*7f83a132SManivannan Sadhasivam
96*7f83a132SManivannan Sadhasivam return 0;
97*7f83a132SManivannan Sadhasivam }
98*7f83a132SManivannan Sadhasivam
rda_ostimer_tick_resume(struct clock_event_device * evt)99*7f83a132SManivannan Sadhasivam static int rda_ostimer_tick_resume(struct clock_event_device *evt)
100*7f83a132SManivannan Sadhasivam {
101*7f83a132SManivannan Sadhasivam return 0;
102*7f83a132SManivannan Sadhasivam }
103*7f83a132SManivannan Sadhasivam
rda_ostimer_set_next_event(unsigned long evt,struct clock_event_device * ev)104*7f83a132SManivannan Sadhasivam static int rda_ostimer_set_next_event(unsigned long evt,
105*7f83a132SManivannan Sadhasivam struct clock_event_device *ev)
106*7f83a132SManivannan Sadhasivam {
107*7f83a132SManivannan Sadhasivam struct timer_of *to = to_timer_of(ev);
108*7f83a132SManivannan Sadhasivam
109*7f83a132SManivannan Sadhasivam rda_ostimer_start(timer_of_base(to), false, evt);
110*7f83a132SManivannan Sadhasivam
111*7f83a132SManivannan Sadhasivam return 0;
112*7f83a132SManivannan Sadhasivam }
113*7f83a132SManivannan Sadhasivam
rda_ostimer_interrupt(int irq,void * dev_id)114*7f83a132SManivannan Sadhasivam static irqreturn_t rda_ostimer_interrupt(int irq, void *dev_id)
115*7f83a132SManivannan Sadhasivam {
116*7f83a132SManivannan Sadhasivam struct clock_event_device *evt = dev_id;
117*7f83a132SManivannan Sadhasivam struct timer_of *to = to_timer_of(evt);
118*7f83a132SManivannan Sadhasivam
119*7f83a132SManivannan Sadhasivam /* clear timer int */
120*7f83a132SManivannan Sadhasivam writel_relaxed(RDA_TIMER_IRQ_CLR_OSTIMER,
121*7f83a132SManivannan Sadhasivam timer_of_base(to) + RDA_TIMER_IRQ_CLR);
122*7f83a132SManivannan Sadhasivam
123*7f83a132SManivannan Sadhasivam if (evt->event_handler)
124*7f83a132SManivannan Sadhasivam evt->event_handler(evt);
125*7f83a132SManivannan Sadhasivam
126*7f83a132SManivannan Sadhasivam return IRQ_HANDLED;
127*7f83a132SManivannan Sadhasivam }
128*7f83a132SManivannan Sadhasivam
129*7f83a132SManivannan Sadhasivam static struct timer_of rda_ostimer_of = {
130*7f83a132SManivannan Sadhasivam .flags = TIMER_OF_IRQ | TIMER_OF_BASE,
131*7f83a132SManivannan Sadhasivam
132*7f83a132SManivannan Sadhasivam .clkevt = {
133*7f83a132SManivannan Sadhasivam .name = "rda-ostimer",
134*7f83a132SManivannan Sadhasivam .rating = 250,
135*7f83a132SManivannan Sadhasivam .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
136*7f83a132SManivannan Sadhasivam CLOCK_EVT_FEAT_DYNIRQ,
137*7f83a132SManivannan Sadhasivam .set_state_shutdown = rda_ostimer_set_state_shutdown,
138*7f83a132SManivannan Sadhasivam .set_state_oneshot = rda_ostimer_set_state_oneshot,
139*7f83a132SManivannan Sadhasivam .set_state_periodic = rda_ostimer_set_state_periodic,
140*7f83a132SManivannan Sadhasivam .tick_resume = rda_ostimer_tick_resume,
141*7f83a132SManivannan Sadhasivam .set_next_event = rda_ostimer_set_next_event,
142*7f83a132SManivannan Sadhasivam },
143*7f83a132SManivannan Sadhasivam
144*7f83a132SManivannan Sadhasivam .of_base = {
145*7f83a132SManivannan Sadhasivam .name = "rda-timer",
146*7f83a132SManivannan Sadhasivam .index = 0,
147*7f83a132SManivannan Sadhasivam },
148*7f83a132SManivannan Sadhasivam
149*7f83a132SManivannan Sadhasivam .of_irq = {
150*7f83a132SManivannan Sadhasivam .name = "ostimer",
151*7f83a132SManivannan Sadhasivam .handler = rda_ostimer_interrupt,
152*7f83a132SManivannan Sadhasivam .flags = IRQF_TIMER,
153*7f83a132SManivannan Sadhasivam },
154*7f83a132SManivannan Sadhasivam };
155*7f83a132SManivannan Sadhasivam
rda_hwtimer_read(struct clocksource * cs)156*7f83a132SManivannan Sadhasivam static u64 rda_hwtimer_read(struct clocksource *cs)
157*7f83a132SManivannan Sadhasivam {
158*7f83a132SManivannan Sadhasivam void __iomem *base = timer_of_base(&rda_ostimer_of);
159*7f83a132SManivannan Sadhasivam u32 lo, hi;
160*7f83a132SManivannan Sadhasivam
161*7f83a132SManivannan Sadhasivam /* Always read low 32 bits first */
162*7f83a132SManivannan Sadhasivam do {
163*7f83a132SManivannan Sadhasivam lo = readl_relaxed(base + RDA_HWTIMER_LOCKVAL_L);
164*7f83a132SManivannan Sadhasivam hi = readl_relaxed(base + RDA_HWTIMER_LOCKVAL_H);
165*7f83a132SManivannan Sadhasivam } while (hi != readl_relaxed(base + RDA_HWTIMER_LOCKVAL_H));
166*7f83a132SManivannan Sadhasivam
167*7f83a132SManivannan Sadhasivam return ((u64)hi << 32) | lo;
168*7f83a132SManivannan Sadhasivam }
169*7f83a132SManivannan Sadhasivam
170*7f83a132SManivannan Sadhasivam static struct clocksource rda_hwtimer_clocksource = {
171*7f83a132SManivannan Sadhasivam .name = "rda-timer",
172*7f83a132SManivannan Sadhasivam .rating = 400,
173*7f83a132SManivannan Sadhasivam .read = rda_hwtimer_read,
174*7f83a132SManivannan Sadhasivam .mask = CLOCKSOURCE_MASK(64),
175*7f83a132SManivannan Sadhasivam .flags = CLOCK_SOURCE_IS_CONTINUOUS,
176*7f83a132SManivannan Sadhasivam };
177*7f83a132SManivannan Sadhasivam
rda_timer_init(struct device_node * np)178*7f83a132SManivannan Sadhasivam static int __init rda_timer_init(struct device_node *np)
179*7f83a132SManivannan Sadhasivam {
180*7f83a132SManivannan Sadhasivam unsigned long rate = 2000000;
181*7f83a132SManivannan Sadhasivam int ret;
182*7f83a132SManivannan Sadhasivam
183*7f83a132SManivannan Sadhasivam ret = timer_of_init(np, &rda_ostimer_of);
184*7f83a132SManivannan Sadhasivam if (ret)
185*7f83a132SManivannan Sadhasivam return ret;
186*7f83a132SManivannan Sadhasivam
187*7f83a132SManivannan Sadhasivam clocksource_register_hz(&rda_hwtimer_clocksource, rate);
188*7f83a132SManivannan Sadhasivam
189*7f83a132SManivannan Sadhasivam clockevents_config_and_register(&rda_ostimer_of.clkevt, rate,
190*7f83a132SManivannan Sadhasivam 0x2, UINT_MAX);
191*7f83a132SManivannan Sadhasivam
192*7f83a132SManivannan Sadhasivam return 0;
193*7f83a132SManivannan Sadhasivam }
194*7f83a132SManivannan Sadhasivam
195*7f83a132SManivannan Sadhasivam TIMER_OF_DECLARE(rda8810pl, "rda,8810pl-timer", rda_timer_init);
196