1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * This file contains driver for the Cadence Triple Timer Counter Rev 06
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2011-2013 Xilinx
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * based on arch/mips/kernel/time.c timer driver
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/clk.h>
11*4882a593Smuzhiyun #include <linux/interrupt.h>
12*4882a593Smuzhiyun #include <linux/clockchips.h>
13*4882a593Smuzhiyun #include <linux/clocksource.h>
14*4882a593Smuzhiyun #include <linux/of_address.h>
15*4882a593Smuzhiyun #include <linux/of_irq.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/sched_clock.h>
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/of_platform.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun * This driver configures the 2 16/32-bit count-up timers as follows:
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * T1: Timer 1, clocksource for generic timekeeping
25*4882a593Smuzhiyun * T2: Timer 2, clockevent source for hrtimers
26*4882a593Smuzhiyun * T3: Timer 3, <unused>
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * The input frequency to the timer module for emulation is 2.5MHz which is
29*4882a593Smuzhiyun * common to all the timer channels (T1, T2, and T3). With a pre-scaler of 32,
30*4882a593Smuzhiyun * the timers are clocked at 78.125KHz (12.8 us resolution).
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun * The input frequency to the timer module in silicon is configurable and
33*4882a593Smuzhiyun * obtained from device tree. The pre-scaler of 32 is used.
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun /*
37*4882a593Smuzhiyun * Timer Register Offset Definitions of Timer 1, Increment base address by 4
38*4882a593Smuzhiyun * and use same offsets for Timer 2
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun #define TTC_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
41*4882a593Smuzhiyun #define TTC_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
42*4882a593Smuzhiyun #define TTC_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
43*4882a593Smuzhiyun #define TTC_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
44*4882a593Smuzhiyun #define TTC_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
45*4882a593Smuzhiyun #define TTC_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #define TTC_CNT_CNTRL_DISABLE_MASK 0x1
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #define TTC_CLK_CNTRL_CSRC_MASK (1 << 5) /* clock source */
50*4882a593Smuzhiyun #define TTC_CLK_CNTRL_PSV_MASK 0x1e
51*4882a593Smuzhiyun #define TTC_CLK_CNTRL_PSV_SHIFT 1
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun * Setup the timers to use pre-scaling, using a fixed value for now that will
55*4882a593Smuzhiyun * work across most input frequency, but it may need to be more dynamic
56*4882a593Smuzhiyun */
57*4882a593Smuzhiyun #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
58*4882a593Smuzhiyun #define PRESCALE 2048 /* The exponent must match this */
59*4882a593Smuzhiyun #define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
60*4882a593Smuzhiyun #define CLK_CNTRL_PRESCALE_EN 1
61*4882a593Smuzhiyun #define CNT_CNTRL_RESET (1 << 4)
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun #define MAX_F_ERR 50
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /**
66*4882a593Smuzhiyun * struct ttc_timer - This definition defines local timer structure
67*4882a593Smuzhiyun *
68*4882a593Smuzhiyun * @base_addr: Base address of timer
69*4882a593Smuzhiyun * @freq: Timer input clock frequency
70*4882a593Smuzhiyun * @clk: Associated clock source
71*4882a593Smuzhiyun * @clk_rate_change_nb Notifier block for clock rate changes
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun struct ttc_timer {
74*4882a593Smuzhiyun void __iomem *base_addr;
75*4882a593Smuzhiyun unsigned long freq;
76*4882a593Smuzhiyun struct clk *clk;
77*4882a593Smuzhiyun struct notifier_block clk_rate_change_nb;
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun #define to_ttc_timer(x) \
81*4882a593Smuzhiyun container_of(x, struct ttc_timer, clk_rate_change_nb)
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun struct ttc_timer_clocksource {
84*4882a593Smuzhiyun u32 scale_clk_ctrl_reg_old;
85*4882a593Smuzhiyun u32 scale_clk_ctrl_reg_new;
86*4882a593Smuzhiyun struct ttc_timer ttc;
87*4882a593Smuzhiyun struct clocksource cs;
88*4882a593Smuzhiyun };
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun #define to_ttc_timer_clksrc(x) \
91*4882a593Smuzhiyun container_of(x, struct ttc_timer_clocksource, cs)
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun struct ttc_timer_clockevent {
94*4882a593Smuzhiyun struct ttc_timer ttc;
95*4882a593Smuzhiyun struct clock_event_device ce;
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun #define to_ttc_timer_clkevent(x) \
99*4882a593Smuzhiyun container_of(x, struct ttc_timer_clockevent, ce)
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun static void __iomem *ttc_sched_clock_val_reg;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /**
104*4882a593Smuzhiyun * ttc_set_interval - Set the timer interval value
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * @timer: Pointer to the timer instance
107*4882a593Smuzhiyun * @cycles: Timer interval ticks
108*4882a593Smuzhiyun **/
ttc_set_interval(struct ttc_timer * timer,unsigned long cycles)109*4882a593Smuzhiyun static void ttc_set_interval(struct ttc_timer *timer,
110*4882a593Smuzhiyun unsigned long cycles)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun u32 ctrl_reg;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* Disable the counter, set the counter value and re-enable counter */
115*4882a593Smuzhiyun ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
116*4882a593Smuzhiyun ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
117*4882a593Smuzhiyun writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun writel_relaxed(cycles, timer->base_addr + TTC_INTR_VAL_OFFSET);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /*
122*4882a593Smuzhiyun * Reset the counter (0x10) so that it starts from 0, one-shot
123*4882a593Smuzhiyun * mode makes this needed for timing to be right.
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun ctrl_reg |= CNT_CNTRL_RESET;
126*4882a593Smuzhiyun ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
127*4882a593Smuzhiyun writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /**
131*4882a593Smuzhiyun * ttc_clock_event_interrupt - Clock event timer interrupt handler
132*4882a593Smuzhiyun *
133*4882a593Smuzhiyun * @irq: IRQ number of the Timer
134*4882a593Smuzhiyun * @dev_id: void pointer to the ttc_timer instance
135*4882a593Smuzhiyun *
136*4882a593Smuzhiyun * returns: Always IRQ_HANDLED - success
137*4882a593Smuzhiyun **/
ttc_clock_event_interrupt(int irq,void * dev_id)138*4882a593Smuzhiyun static irqreturn_t ttc_clock_event_interrupt(int irq, void *dev_id)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun struct ttc_timer_clockevent *ttce = dev_id;
141*4882a593Smuzhiyun struct ttc_timer *timer = &ttce->ttc;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /* Acknowledge the interrupt and call event handler */
144*4882a593Smuzhiyun readl_relaxed(timer->base_addr + TTC_ISR_OFFSET);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun ttce->ce.event_handler(&ttce->ce);
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun return IRQ_HANDLED;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /**
152*4882a593Smuzhiyun * __ttc_clocksource_read - Reads the timer counter register
153*4882a593Smuzhiyun *
154*4882a593Smuzhiyun * returns: Current timer counter register value
155*4882a593Smuzhiyun **/
__ttc_clocksource_read(struct clocksource * cs)156*4882a593Smuzhiyun static u64 __ttc_clocksource_read(struct clocksource *cs)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun struct ttc_timer *timer = &to_ttc_timer_clksrc(cs)->ttc;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun return (u64)readl_relaxed(timer->base_addr +
161*4882a593Smuzhiyun TTC_COUNT_VAL_OFFSET);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
ttc_sched_clock_read(void)164*4882a593Smuzhiyun static u64 notrace ttc_sched_clock_read(void)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun return readl_relaxed(ttc_sched_clock_val_reg);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /**
170*4882a593Smuzhiyun * ttc_set_next_event - Sets the time interval for next event
171*4882a593Smuzhiyun *
172*4882a593Smuzhiyun * @cycles: Timer interval ticks
173*4882a593Smuzhiyun * @evt: Address of clock event instance
174*4882a593Smuzhiyun *
175*4882a593Smuzhiyun * returns: Always 0 - success
176*4882a593Smuzhiyun **/
ttc_set_next_event(unsigned long cycles,struct clock_event_device * evt)177*4882a593Smuzhiyun static int ttc_set_next_event(unsigned long cycles,
178*4882a593Smuzhiyun struct clock_event_device *evt)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
181*4882a593Smuzhiyun struct ttc_timer *timer = &ttce->ttc;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun ttc_set_interval(timer, cycles);
184*4882a593Smuzhiyun return 0;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /**
188*4882a593Smuzhiyun * ttc_set_{shutdown|oneshot|periodic} - Sets the state of timer
189*4882a593Smuzhiyun *
190*4882a593Smuzhiyun * @evt: Address of clock event instance
191*4882a593Smuzhiyun **/
ttc_shutdown(struct clock_event_device * evt)192*4882a593Smuzhiyun static int ttc_shutdown(struct clock_event_device *evt)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
195*4882a593Smuzhiyun struct ttc_timer *timer = &ttce->ttc;
196*4882a593Smuzhiyun u32 ctrl_reg;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
199*4882a593Smuzhiyun ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK;
200*4882a593Smuzhiyun writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
201*4882a593Smuzhiyun return 0;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
ttc_set_periodic(struct clock_event_device * evt)204*4882a593Smuzhiyun static int ttc_set_periodic(struct clock_event_device *evt)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
207*4882a593Smuzhiyun struct ttc_timer *timer = &ttce->ttc;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun ttc_set_interval(timer,
210*4882a593Smuzhiyun DIV_ROUND_CLOSEST(ttce->ttc.freq, PRESCALE * HZ));
211*4882a593Smuzhiyun return 0;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
ttc_resume(struct clock_event_device * evt)214*4882a593Smuzhiyun static int ttc_resume(struct clock_event_device *evt)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt);
217*4882a593Smuzhiyun struct ttc_timer *timer = &ttce->ttc;
218*4882a593Smuzhiyun u32 ctrl_reg;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET);
221*4882a593Smuzhiyun ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK;
222*4882a593Smuzhiyun writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET);
223*4882a593Smuzhiyun return 0;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
ttc_rate_change_clocksource_cb(struct notifier_block * nb,unsigned long event,void * data)226*4882a593Smuzhiyun static int ttc_rate_change_clocksource_cb(struct notifier_block *nb,
227*4882a593Smuzhiyun unsigned long event, void *data)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun struct clk_notifier_data *ndata = data;
230*4882a593Smuzhiyun struct ttc_timer *ttc = to_ttc_timer(nb);
231*4882a593Smuzhiyun struct ttc_timer_clocksource *ttccs = container_of(ttc,
232*4882a593Smuzhiyun struct ttc_timer_clocksource, ttc);
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun switch (event) {
235*4882a593Smuzhiyun case PRE_RATE_CHANGE:
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun u32 psv;
238*4882a593Smuzhiyun unsigned long factor, rate_low, rate_high;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun if (ndata->new_rate > ndata->old_rate) {
241*4882a593Smuzhiyun factor = DIV_ROUND_CLOSEST(ndata->new_rate,
242*4882a593Smuzhiyun ndata->old_rate);
243*4882a593Smuzhiyun rate_low = ndata->old_rate;
244*4882a593Smuzhiyun rate_high = ndata->new_rate;
245*4882a593Smuzhiyun } else {
246*4882a593Smuzhiyun factor = DIV_ROUND_CLOSEST(ndata->old_rate,
247*4882a593Smuzhiyun ndata->new_rate);
248*4882a593Smuzhiyun rate_low = ndata->new_rate;
249*4882a593Smuzhiyun rate_high = ndata->old_rate;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun if (!is_power_of_2(factor))
253*4882a593Smuzhiyun return NOTIFY_BAD;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun if (abs(rate_high - (factor * rate_low)) > MAX_F_ERR)
256*4882a593Smuzhiyun return NOTIFY_BAD;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun factor = __ilog2_u32(factor);
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /*
261*4882a593Smuzhiyun * store timer clock ctrl register so we can restore it in case
262*4882a593Smuzhiyun * of an abort.
263*4882a593Smuzhiyun */
264*4882a593Smuzhiyun ttccs->scale_clk_ctrl_reg_old =
265*4882a593Smuzhiyun readl_relaxed(ttccs->ttc.base_addr +
266*4882a593Smuzhiyun TTC_CLK_CNTRL_OFFSET);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun psv = (ttccs->scale_clk_ctrl_reg_old &
269*4882a593Smuzhiyun TTC_CLK_CNTRL_PSV_MASK) >>
270*4882a593Smuzhiyun TTC_CLK_CNTRL_PSV_SHIFT;
271*4882a593Smuzhiyun if (ndata->new_rate < ndata->old_rate)
272*4882a593Smuzhiyun psv -= factor;
273*4882a593Smuzhiyun else
274*4882a593Smuzhiyun psv += factor;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /* prescaler within legal range? */
277*4882a593Smuzhiyun if (psv & ~(TTC_CLK_CNTRL_PSV_MASK >> TTC_CLK_CNTRL_PSV_SHIFT))
278*4882a593Smuzhiyun return NOTIFY_BAD;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun ttccs->scale_clk_ctrl_reg_new = ttccs->scale_clk_ctrl_reg_old &
281*4882a593Smuzhiyun ~TTC_CLK_CNTRL_PSV_MASK;
282*4882a593Smuzhiyun ttccs->scale_clk_ctrl_reg_new |= psv << TTC_CLK_CNTRL_PSV_SHIFT;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun /* scale down: adjust divider in post-change notification */
286*4882a593Smuzhiyun if (ndata->new_rate < ndata->old_rate)
287*4882a593Smuzhiyun return NOTIFY_DONE;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /* scale up: adjust divider now - before frequency change */
290*4882a593Smuzhiyun writel_relaxed(ttccs->scale_clk_ctrl_reg_new,
291*4882a593Smuzhiyun ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
292*4882a593Smuzhiyun break;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun case POST_RATE_CHANGE:
295*4882a593Smuzhiyun /* scale up: pre-change notification did the adjustment */
296*4882a593Smuzhiyun if (ndata->new_rate > ndata->old_rate)
297*4882a593Smuzhiyun return NOTIFY_OK;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /* scale down: adjust divider now - after frequency change */
300*4882a593Smuzhiyun writel_relaxed(ttccs->scale_clk_ctrl_reg_new,
301*4882a593Smuzhiyun ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
302*4882a593Smuzhiyun break;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun case ABORT_RATE_CHANGE:
305*4882a593Smuzhiyun /* we have to undo the adjustment in case we scale up */
306*4882a593Smuzhiyun if (ndata->new_rate < ndata->old_rate)
307*4882a593Smuzhiyun return NOTIFY_OK;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun /* restore original register value */
310*4882a593Smuzhiyun writel_relaxed(ttccs->scale_clk_ctrl_reg_old,
311*4882a593Smuzhiyun ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
312*4882a593Smuzhiyun fallthrough;
313*4882a593Smuzhiyun default:
314*4882a593Smuzhiyun return NOTIFY_DONE;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun return NOTIFY_DONE;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
ttc_setup_clocksource(struct clk * clk,void __iomem * base,u32 timer_width)320*4882a593Smuzhiyun static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base,
321*4882a593Smuzhiyun u32 timer_width)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun struct ttc_timer_clocksource *ttccs;
324*4882a593Smuzhiyun int err;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL);
327*4882a593Smuzhiyun if (!ttccs)
328*4882a593Smuzhiyun return -ENOMEM;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun ttccs->ttc.clk = clk;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun err = clk_prepare_enable(ttccs->ttc.clk);
333*4882a593Smuzhiyun if (err) {
334*4882a593Smuzhiyun kfree(ttccs);
335*4882a593Smuzhiyun return err;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun ttccs->ttc.freq = clk_get_rate(ttccs->ttc.clk);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun ttccs->ttc.clk_rate_change_nb.notifier_call =
341*4882a593Smuzhiyun ttc_rate_change_clocksource_cb;
342*4882a593Smuzhiyun ttccs->ttc.clk_rate_change_nb.next = NULL;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun err = clk_notifier_register(ttccs->ttc.clk,
345*4882a593Smuzhiyun &ttccs->ttc.clk_rate_change_nb);
346*4882a593Smuzhiyun if (err)
347*4882a593Smuzhiyun pr_warn("Unable to register clock notifier.\n");
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun ttccs->ttc.base_addr = base;
350*4882a593Smuzhiyun ttccs->cs.name = "ttc_clocksource";
351*4882a593Smuzhiyun ttccs->cs.rating = 200;
352*4882a593Smuzhiyun ttccs->cs.read = __ttc_clocksource_read;
353*4882a593Smuzhiyun ttccs->cs.mask = CLOCKSOURCE_MASK(timer_width);
354*4882a593Smuzhiyun ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun /*
357*4882a593Smuzhiyun * Setup the clock source counter to be an incrementing counter
358*4882a593Smuzhiyun * with no interrupt and it rolls over at 0xFFFF. Pre-scale
359*4882a593Smuzhiyun * it by 32 also. Let it start running now.
360*4882a593Smuzhiyun */
361*4882a593Smuzhiyun writel_relaxed(0x0, ttccs->ttc.base_addr + TTC_IER_OFFSET);
362*4882a593Smuzhiyun writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
363*4882a593Smuzhiyun ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
364*4882a593Smuzhiyun writel_relaxed(CNT_CNTRL_RESET,
365*4882a593Smuzhiyun ttccs->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun err = clocksource_register_hz(&ttccs->cs, ttccs->ttc.freq / PRESCALE);
368*4882a593Smuzhiyun if (err) {
369*4882a593Smuzhiyun kfree(ttccs);
370*4882a593Smuzhiyun return err;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun ttc_sched_clock_val_reg = base + TTC_COUNT_VAL_OFFSET;
374*4882a593Smuzhiyun sched_clock_register(ttc_sched_clock_read, timer_width,
375*4882a593Smuzhiyun ttccs->ttc.freq / PRESCALE);
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun return 0;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
ttc_rate_change_clockevent_cb(struct notifier_block * nb,unsigned long event,void * data)380*4882a593Smuzhiyun static int ttc_rate_change_clockevent_cb(struct notifier_block *nb,
381*4882a593Smuzhiyun unsigned long event, void *data)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun struct clk_notifier_data *ndata = data;
384*4882a593Smuzhiyun struct ttc_timer *ttc = to_ttc_timer(nb);
385*4882a593Smuzhiyun struct ttc_timer_clockevent *ttcce = container_of(ttc,
386*4882a593Smuzhiyun struct ttc_timer_clockevent, ttc);
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun switch (event) {
389*4882a593Smuzhiyun case POST_RATE_CHANGE:
390*4882a593Smuzhiyun /* update cached frequency */
391*4882a593Smuzhiyun ttc->freq = ndata->new_rate;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun clockevents_update_freq(&ttcce->ce, ndata->new_rate / PRESCALE);
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun fallthrough;
396*4882a593Smuzhiyun case PRE_RATE_CHANGE:
397*4882a593Smuzhiyun case ABORT_RATE_CHANGE:
398*4882a593Smuzhiyun default:
399*4882a593Smuzhiyun return NOTIFY_DONE;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun
ttc_setup_clockevent(struct clk * clk,void __iomem * base,u32 irq)403*4882a593Smuzhiyun static int __init ttc_setup_clockevent(struct clk *clk,
404*4882a593Smuzhiyun void __iomem *base, u32 irq)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun struct ttc_timer_clockevent *ttcce;
407*4882a593Smuzhiyun int err;
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL);
410*4882a593Smuzhiyun if (!ttcce)
411*4882a593Smuzhiyun return -ENOMEM;
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun ttcce->ttc.clk = clk;
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun err = clk_prepare_enable(ttcce->ttc.clk);
416*4882a593Smuzhiyun if (err)
417*4882a593Smuzhiyun goto out_kfree;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun ttcce->ttc.clk_rate_change_nb.notifier_call =
420*4882a593Smuzhiyun ttc_rate_change_clockevent_cb;
421*4882a593Smuzhiyun ttcce->ttc.clk_rate_change_nb.next = NULL;
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun err = clk_notifier_register(ttcce->ttc.clk,
424*4882a593Smuzhiyun &ttcce->ttc.clk_rate_change_nb);
425*4882a593Smuzhiyun if (err) {
426*4882a593Smuzhiyun pr_warn("Unable to register clock notifier.\n");
427*4882a593Smuzhiyun goto out_kfree;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun ttcce->ttc.freq = clk_get_rate(ttcce->ttc.clk);
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun ttcce->ttc.base_addr = base;
433*4882a593Smuzhiyun ttcce->ce.name = "ttc_clockevent";
434*4882a593Smuzhiyun ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
435*4882a593Smuzhiyun ttcce->ce.set_next_event = ttc_set_next_event;
436*4882a593Smuzhiyun ttcce->ce.set_state_shutdown = ttc_shutdown;
437*4882a593Smuzhiyun ttcce->ce.set_state_periodic = ttc_set_periodic;
438*4882a593Smuzhiyun ttcce->ce.set_state_oneshot = ttc_shutdown;
439*4882a593Smuzhiyun ttcce->ce.tick_resume = ttc_resume;
440*4882a593Smuzhiyun ttcce->ce.rating = 200;
441*4882a593Smuzhiyun ttcce->ce.irq = irq;
442*4882a593Smuzhiyun ttcce->ce.cpumask = cpu_possible_mask;
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun /*
445*4882a593Smuzhiyun * Setup the clock event timer to be an interval timer which
446*4882a593Smuzhiyun * is prescaled by 32 using the interval interrupt. Leave it
447*4882a593Smuzhiyun * disabled for now.
448*4882a593Smuzhiyun */
449*4882a593Smuzhiyun writel_relaxed(0x23, ttcce->ttc.base_addr + TTC_CNT_CNTRL_OFFSET);
450*4882a593Smuzhiyun writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN,
451*4882a593Smuzhiyun ttcce->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
452*4882a593Smuzhiyun writel_relaxed(0x1, ttcce->ttc.base_addr + TTC_IER_OFFSET);
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun err = request_irq(irq, ttc_clock_event_interrupt,
455*4882a593Smuzhiyun IRQF_TIMER, ttcce->ce.name, ttcce);
456*4882a593Smuzhiyun if (err)
457*4882a593Smuzhiyun goto out_kfree;
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun clockevents_config_and_register(&ttcce->ce,
460*4882a593Smuzhiyun ttcce->ttc.freq / PRESCALE, 1, 0xfffe);
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun return 0;
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun out_kfree:
465*4882a593Smuzhiyun kfree(ttcce);
466*4882a593Smuzhiyun return err;
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
ttc_timer_probe(struct platform_device * pdev)469*4882a593Smuzhiyun static int __init ttc_timer_probe(struct platform_device *pdev)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun unsigned int irq;
472*4882a593Smuzhiyun void __iomem *timer_baseaddr;
473*4882a593Smuzhiyun struct clk *clk_cs, *clk_ce;
474*4882a593Smuzhiyun static int initialized;
475*4882a593Smuzhiyun int clksel, ret;
476*4882a593Smuzhiyun u32 timer_width = 16;
477*4882a593Smuzhiyun struct device_node *timer = pdev->dev.of_node;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun if (initialized)
480*4882a593Smuzhiyun return 0;
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun initialized = 1;
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun /*
485*4882a593Smuzhiyun * Get the 1st Triple Timer Counter (TTC) block from the device tree
486*4882a593Smuzhiyun * and use it. Note that the event timer uses the interrupt and it's the
487*4882a593Smuzhiyun * 2nd TTC hence the irq_of_parse_and_map(,1)
488*4882a593Smuzhiyun */
489*4882a593Smuzhiyun timer_baseaddr = of_iomap(timer, 0);
490*4882a593Smuzhiyun if (!timer_baseaddr) {
491*4882a593Smuzhiyun pr_err("ERROR: invalid timer base address\n");
492*4882a593Smuzhiyun return -ENXIO;
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun irq = irq_of_parse_and_map(timer, 1);
496*4882a593Smuzhiyun if (irq <= 0) {
497*4882a593Smuzhiyun pr_err("ERROR: invalid interrupt number\n");
498*4882a593Smuzhiyun return -EINVAL;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun of_property_read_u32(timer, "timer-width", &timer_width);
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun clksel = readl_relaxed(timer_baseaddr + TTC_CLK_CNTRL_OFFSET);
504*4882a593Smuzhiyun clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
505*4882a593Smuzhiyun clk_cs = of_clk_get(timer, clksel);
506*4882a593Smuzhiyun if (IS_ERR(clk_cs)) {
507*4882a593Smuzhiyun pr_err("ERROR: timer input clock not found\n");
508*4882a593Smuzhiyun return PTR_ERR(clk_cs);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun clksel = readl_relaxed(timer_baseaddr + 4 + TTC_CLK_CNTRL_OFFSET);
512*4882a593Smuzhiyun clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK);
513*4882a593Smuzhiyun clk_ce = of_clk_get(timer, clksel);
514*4882a593Smuzhiyun if (IS_ERR(clk_ce)) {
515*4882a593Smuzhiyun pr_err("ERROR: timer input clock not found\n");
516*4882a593Smuzhiyun return PTR_ERR(clk_ce);
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun ret = ttc_setup_clocksource(clk_cs, timer_baseaddr, timer_width);
520*4882a593Smuzhiyun if (ret)
521*4882a593Smuzhiyun return ret;
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun ret = ttc_setup_clockevent(clk_ce, timer_baseaddr + 4, irq);
524*4882a593Smuzhiyun if (ret)
525*4882a593Smuzhiyun return ret;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun pr_info("%pOFn #0 at %p, irq=%d\n", timer, timer_baseaddr, irq);
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun return 0;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun static const struct of_device_id ttc_timer_of_match[] = {
533*4882a593Smuzhiyun {.compatible = "cdns,ttc"},
534*4882a593Smuzhiyun {},
535*4882a593Smuzhiyun };
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ttc_timer_of_match);
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun static struct platform_driver ttc_timer_driver = {
540*4882a593Smuzhiyun .driver = {
541*4882a593Smuzhiyun .name = "cdns_ttc_timer",
542*4882a593Smuzhiyun .of_match_table = ttc_timer_of_match,
543*4882a593Smuzhiyun },
544*4882a593Smuzhiyun };
545*4882a593Smuzhiyun builtin_platform_driver_probe(ttc_timer_driver, ttc_timer_probe);
546