xref: /OK3568_Linux_fs/kernel/drivers/clocksource/timer-atlas7.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * System timer for CSR SiRFprimaII
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/interrupt.h>
10*4882a593Smuzhiyun #include <linux/clockchips.h>
11*4882a593Smuzhiyun #include <linux/clocksource.h>
12*4882a593Smuzhiyun #include <linux/cpu.h>
13*4882a593Smuzhiyun #include <linux/bitops.h>
14*4882a593Smuzhiyun #include <linux/irq.h>
15*4882a593Smuzhiyun #include <linux/clk.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/of.h>
18*4882a593Smuzhiyun #include <linux/of_irq.h>
19*4882a593Smuzhiyun #include <linux/of_address.h>
20*4882a593Smuzhiyun #include <linux/sched_clock.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define SIRFSOC_TIMER_32COUNTER_0_CTRL			0x0000
23*4882a593Smuzhiyun #define SIRFSOC_TIMER_32COUNTER_1_CTRL			0x0004
24*4882a593Smuzhiyun #define SIRFSOC_TIMER_MATCH_0				0x0018
25*4882a593Smuzhiyun #define SIRFSOC_TIMER_MATCH_1				0x001c
26*4882a593Smuzhiyun #define SIRFSOC_TIMER_COUNTER_0				0x0048
27*4882a593Smuzhiyun #define SIRFSOC_TIMER_COUNTER_1				0x004c
28*4882a593Smuzhiyun #define SIRFSOC_TIMER_INTR_STATUS			0x0060
29*4882a593Smuzhiyun #define SIRFSOC_TIMER_WATCHDOG_EN			0x0064
30*4882a593Smuzhiyun #define SIRFSOC_TIMER_64COUNTER_CTRL			0x0068
31*4882a593Smuzhiyun #define SIRFSOC_TIMER_64COUNTER_LO			0x006c
32*4882a593Smuzhiyun #define SIRFSOC_TIMER_64COUNTER_HI			0x0070
33*4882a593Smuzhiyun #define SIRFSOC_TIMER_64COUNTER_LOAD_LO			0x0074
34*4882a593Smuzhiyun #define SIRFSOC_TIMER_64COUNTER_LOAD_HI			0x0078
35*4882a593Smuzhiyun #define SIRFSOC_TIMER_64COUNTER_RLATCHED_LO		0x007c
36*4882a593Smuzhiyun #define SIRFSOC_TIMER_64COUNTER_RLATCHED_HI		0x0080
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define SIRFSOC_TIMER_REG_CNT 6
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun static unsigned long atlas7_timer_rate;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun static const u32 sirfsoc_timer_reg_list[SIRFSOC_TIMER_REG_CNT] = {
43*4882a593Smuzhiyun 	SIRFSOC_TIMER_WATCHDOG_EN,
44*4882a593Smuzhiyun 	SIRFSOC_TIMER_32COUNTER_0_CTRL,
45*4882a593Smuzhiyun 	SIRFSOC_TIMER_32COUNTER_1_CTRL,
46*4882a593Smuzhiyun 	SIRFSOC_TIMER_64COUNTER_CTRL,
47*4882a593Smuzhiyun 	SIRFSOC_TIMER_64COUNTER_RLATCHED_LO,
48*4882a593Smuzhiyun 	SIRFSOC_TIMER_64COUNTER_RLATCHED_HI,
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun static u32 sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT];
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static void __iomem *sirfsoc_timer_base;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /* disable count and interrupt */
sirfsoc_timer_count_disable(int idx)56*4882a593Smuzhiyun static inline void sirfsoc_timer_count_disable(int idx)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) & ~0x7,
59*4882a593Smuzhiyun 		sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /* enable count and interrupt */
sirfsoc_timer_count_enable(int idx)63*4882a593Smuzhiyun static inline void sirfsoc_timer_count_enable(int idx)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) | 0x3,
66*4882a593Smuzhiyun 		sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /* timer interrupt handler */
sirfsoc_timer_interrupt(int irq,void * dev_id)70*4882a593Smuzhiyun static irqreturn_t sirfsoc_timer_interrupt(int irq, void *dev_id)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	struct clock_event_device *ce = dev_id;
73*4882a593Smuzhiyun 	int cpu = smp_processor_id();
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	/* clear timer interrupt */
76*4882a593Smuzhiyun 	writel_relaxed(BIT(cpu), sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	if (clockevent_state_oneshot(ce))
79*4882a593Smuzhiyun 		sirfsoc_timer_count_disable(cpu);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	ce->event_handler(ce);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	return IRQ_HANDLED;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /* read 64-bit timer counter */
sirfsoc_timer_read(struct clocksource * cs)87*4882a593Smuzhiyun static u64 sirfsoc_timer_read(struct clocksource *cs)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	u64 cycles;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	writel_relaxed((readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
92*4882a593Smuzhiyun 			BIT(0)) & ~BIT(1), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_HI);
95*4882a593Smuzhiyun 	cycles = (cycles << 32) | readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_LO);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	return cycles;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
sirfsoc_timer_set_next_event(unsigned long delta,struct clock_event_device * ce)100*4882a593Smuzhiyun static int sirfsoc_timer_set_next_event(unsigned long delta,
101*4882a593Smuzhiyun 	struct clock_event_device *ce)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	int cpu = smp_processor_id();
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/* disable timer first, then modify the related registers */
106*4882a593Smuzhiyun 	sirfsoc_timer_count_disable(cpu);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0 +
109*4882a593Smuzhiyun 		4 * cpu);
110*4882a593Smuzhiyun 	writel_relaxed(delta, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0 +
111*4882a593Smuzhiyun 		4 * cpu);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* enable the tick */
114*4882a593Smuzhiyun 	sirfsoc_timer_count_enable(cpu);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	return 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /* Oneshot is enabled in set_next_event */
sirfsoc_timer_shutdown(struct clock_event_device * evt)120*4882a593Smuzhiyun static int sirfsoc_timer_shutdown(struct clock_event_device *evt)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	sirfsoc_timer_count_disable(smp_processor_id());
123*4882a593Smuzhiyun 	return 0;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
sirfsoc_clocksource_suspend(struct clocksource * cs)126*4882a593Smuzhiyun static void sirfsoc_clocksource_suspend(struct clocksource *cs)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	int i;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
131*4882a593Smuzhiyun 		sirfsoc_timer_reg_val[i] = readl_relaxed(sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
sirfsoc_clocksource_resume(struct clocksource * cs)134*4882a593Smuzhiyun static void sirfsoc_clocksource_resume(struct clocksource *cs)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	int i;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	for (i = 0; i < SIRFSOC_TIMER_REG_CNT - 2; i++)
139*4882a593Smuzhiyun 		writel_relaxed(sirfsoc_timer_reg_val[i], sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 2],
142*4882a593Smuzhiyun 		sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
143*4882a593Smuzhiyun 	writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 1],
144*4882a593Smuzhiyun 		sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
147*4882a593Smuzhiyun 		BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun static struct clock_event_device __percpu *sirfsoc_clockevent;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun static struct clocksource sirfsoc_clocksource = {
153*4882a593Smuzhiyun 	.name = "sirfsoc_clocksource",
154*4882a593Smuzhiyun 	.rating = 200,
155*4882a593Smuzhiyun 	.mask = CLOCKSOURCE_MASK(64),
156*4882a593Smuzhiyun 	.flags = CLOCK_SOURCE_IS_CONTINUOUS,
157*4882a593Smuzhiyun 	.read = sirfsoc_timer_read,
158*4882a593Smuzhiyun 	.suspend = sirfsoc_clocksource_suspend,
159*4882a593Smuzhiyun 	.resume = sirfsoc_clocksource_resume,
160*4882a593Smuzhiyun };
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun static unsigned int sirfsoc_timer_irq, sirfsoc_timer1_irq;
163*4882a593Smuzhiyun 
sirfsoc_local_timer_starting_cpu(unsigned int cpu)164*4882a593Smuzhiyun static int sirfsoc_local_timer_starting_cpu(unsigned int cpu)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun 	struct clock_event_device *ce = per_cpu_ptr(sirfsoc_clockevent, cpu);
167*4882a593Smuzhiyun 	unsigned int irq;
168*4882a593Smuzhiyun 	const char *name;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	if (cpu == 0) {
171*4882a593Smuzhiyun 		irq = sirfsoc_timer_irq;
172*4882a593Smuzhiyun 		name = "sirfsoc_timer0";
173*4882a593Smuzhiyun 	} else {
174*4882a593Smuzhiyun 		irq = sirfsoc_timer1_irq;
175*4882a593Smuzhiyun 		name = "sirfsoc_timer1";
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	ce->irq = irq;
179*4882a593Smuzhiyun 	ce->name = "local_timer";
180*4882a593Smuzhiyun 	ce->features = CLOCK_EVT_FEAT_ONESHOT;
181*4882a593Smuzhiyun 	ce->rating = 200;
182*4882a593Smuzhiyun 	ce->set_state_shutdown = sirfsoc_timer_shutdown;
183*4882a593Smuzhiyun 	ce->set_state_oneshot = sirfsoc_timer_shutdown;
184*4882a593Smuzhiyun 	ce->tick_resume = sirfsoc_timer_shutdown;
185*4882a593Smuzhiyun 	ce->set_next_event = sirfsoc_timer_set_next_event;
186*4882a593Smuzhiyun 	clockevents_calc_mult_shift(ce, atlas7_timer_rate, 60);
187*4882a593Smuzhiyun 	ce->max_delta_ns = clockevent_delta2ns(-2, ce);
188*4882a593Smuzhiyun 	ce->max_delta_ticks = (unsigned long)-2;
189*4882a593Smuzhiyun 	ce->min_delta_ns = clockevent_delta2ns(2, ce);
190*4882a593Smuzhiyun 	ce->min_delta_ticks = 2;
191*4882a593Smuzhiyun 	ce->cpumask = cpumask_of(cpu);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	BUG_ON(request_irq(ce->irq, sirfsoc_timer_interrupt,
194*4882a593Smuzhiyun 			   IRQF_TIMER | IRQF_NOBALANCING, name, ce));
195*4882a593Smuzhiyun 	irq_force_affinity(ce->irq, cpumask_of(cpu));
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	clockevents_register_device(ce);
198*4882a593Smuzhiyun 	return 0;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
sirfsoc_local_timer_dying_cpu(unsigned int cpu)201*4882a593Smuzhiyun static int sirfsoc_local_timer_dying_cpu(unsigned int cpu)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	struct clock_event_device *ce = per_cpu_ptr(sirfsoc_clockevent, cpu);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	sirfsoc_timer_count_disable(1);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if (cpu == 0)
208*4882a593Smuzhiyun 		free_irq(sirfsoc_timer_irq, ce);
209*4882a593Smuzhiyun 	else
210*4882a593Smuzhiyun 		free_irq(sirfsoc_timer1_irq, ce);
211*4882a593Smuzhiyun 	return 0;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun 
sirfsoc_clockevent_init(void)214*4882a593Smuzhiyun static int __init sirfsoc_clockevent_init(void)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun 	sirfsoc_clockevent = alloc_percpu(struct clock_event_device);
217*4882a593Smuzhiyun 	BUG_ON(!sirfsoc_clockevent);
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	/* Install and invoke hotplug callbacks */
220*4882a593Smuzhiyun 	return cpuhp_setup_state(CPUHP_AP_MARCO_TIMER_STARTING,
221*4882a593Smuzhiyun 				 "clockevents/marco:starting",
222*4882a593Smuzhiyun 				 sirfsoc_local_timer_starting_cpu,
223*4882a593Smuzhiyun 				 sirfsoc_local_timer_dying_cpu);
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /* initialize the kernel jiffy timer source */
sirfsoc_atlas7_timer_init(struct device_node * np)227*4882a593Smuzhiyun static int __init sirfsoc_atlas7_timer_init(struct device_node *np)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	struct clk *clk;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	clk = of_clk_get(np, 0);
232*4882a593Smuzhiyun 	BUG_ON(IS_ERR(clk));
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	BUG_ON(clk_prepare_enable(clk));
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	atlas7_timer_rate = clk_get_rate(clk);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	/* timer dividers: 0, not divided */
239*4882a593Smuzhiyun 	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
240*4882a593Smuzhiyun 	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL);
241*4882a593Smuzhiyun 	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_1_CTRL);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	/* Initialize timer counters to 0 */
244*4882a593Smuzhiyun 	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
245*4882a593Smuzhiyun 	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
246*4882a593Smuzhiyun 	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
247*4882a593Smuzhiyun 		BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
248*4882a593Smuzhiyun 	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0);
249*4882a593Smuzhiyun 	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_1);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	/* Clear all interrupts */
252*4882a593Smuzhiyun 	writel_relaxed(0xFFFF, sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	BUG_ON(clocksource_register_hz(&sirfsoc_clocksource, atlas7_timer_rate));
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	return sirfsoc_clockevent_init();
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
sirfsoc_of_timer_init(struct device_node * np)259*4882a593Smuzhiyun static int __init sirfsoc_of_timer_init(struct device_node *np)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun 	sirfsoc_timer_base = of_iomap(np, 0);
262*4882a593Smuzhiyun 	if (!sirfsoc_timer_base) {
263*4882a593Smuzhiyun 		pr_err("unable to map timer cpu registers\n");
264*4882a593Smuzhiyun 		return -ENXIO;
265*4882a593Smuzhiyun 	}
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	sirfsoc_timer_irq = irq_of_parse_and_map(np, 0);
268*4882a593Smuzhiyun 	if (!sirfsoc_timer_irq) {
269*4882a593Smuzhiyun 		pr_err("No irq passed for timer0 via DT\n");
270*4882a593Smuzhiyun 		return -EINVAL;
271*4882a593Smuzhiyun 	}
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	sirfsoc_timer1_irq = irq_of_parse_and_map(np, 1);
274*4882a593Smuzhiyun 	if (!sirfsoc_timer1_irq) {
275*4882a593Smuzhiyun 		pr_err("No irq passed for timer1 via DT\n");
276*4882a593Smuzhiyun 		return -EINVAL;
277*4882a593Smuzhiyun 	}
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	return sirfsoc_atlas7_timer_init(np);
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun TIMER_OF_DECLARE(sirfsoc_atlas7_timer, "sirf,atlas7-tick", sirfsoc_of_timer_init);
282