xref: /OK3568_Linux_fs/kernel/drivers/clocksource/timer-atmel-pit.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * at91sam926x_time.c - Periodic Interval Timer (PIT) for at91sam926x
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2005-2006 M. Amine SAYA, ATMEL Rousset, France
6*4882a593Smuzhiyun  * Revision	 2005 M. Nicolas Diremdjian, ATMEL Rousset, France
7*4882a593Smuzhiyun  * Converted to ClockSource/ClockEvents by David Brownell.
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #define pr_fmt(fmt)	"AT91: PIT: " fmt
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/clk.h>
13*4882a593Smuzhiyun #include <linux/clockchips.h>
14*4882a593Smuzhiyun #include <linux/interrupt.h>
15*4882a593Smuzhiyun #include <linux/irq.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/of.h>
18*4882a593Smuzhiyun #include <linux/of_address.h>
19*4882a593Smuzhiyun #include <linux/of_irq.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define AT91_PIT_MR		0x00			/* Mode Register */
23*4882a593Smuzhiyun #define AT91_PIT_PITIEN			BIT(25)			/* Timer Interrupt Enable */
24*4882a593Smuzhiyun #define AT91_PIT_PITEN			BIT(24)			/* Timer Enabled */
25*4882a593Smuzhiyun #define AT91_PIT_PIV			GENMASK(19, 0)		/* Periodic Interval Value */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define AT91_PIT_SR		0x04			/* Status Register */
28*4882a593Smuzhiyun #define AT91_PIT_PITS			BIT(0)			/* Timer Status */
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define AT91_PIT_PIVR		0x08			/* Periodic Interval Value Register */
31*4882a593Smuzhiyun #define AT91_PIT_PIIR		0x0c			/* Periodic Interval Image Register */
32*4882a593Smuzhiyun #define AT91_PIT_PICNT			GENMASK(31, 20)		/* Interval Counter */
33*4882a593Smuzhiyun #define AT91_PIT_CPIV			GENMASK(19, 0)		/* Inverval Value */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #define PIT_CPIV(x)	((x) & AT91_PIT_CPIV)
36*4882a593Smuzhiyun #define PIT_PICNT(x)	(((x) & AT91_PIT_PICNT) >> 20)
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun struct pit_data {
39*4882a593Smuzhiyun 	struct clock_event_device	clkevt;
40*4882a593Smuzhiyun 	struct clocksource		clksrc;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	void __iomem	*base;
43*4882a593Smuzhiyun 	u32		cycle;
44*4882a593Smuzhiyun 	u32		cnt;
45*4882a593Smuzhiyun 	unsigned int	irq;
46*4882a593Smuzhiyun 	struct clk	*mck;
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun 
clksrc_to_pit_data(struct clocksource * clksrc)49*4882a593Smuzhiyun static inline struct pit_data *clksrc_to_pit_data(struct clocksource *clksrc)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	return container_of(clksrc, struct pit_data, clksrc);
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
clkevt_to_pit_data(struct clock_event_device * clkevt)54*4882a593Smuzhiyun static inline struct pit_data *clkevt_to_pit_data(struct clock_event_device *clkevt)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	return container_of(clkevt, struct pit_data, clkevt);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
pit_read(void __iomem * base,unsigned int reg_offset)59*4882a593Smuzhiyun static inline unsigned int pit_read(void __iomem *base, unsigned int reg_offset)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	return readl_relaxed(base + reg_offset);
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
pit_write(void __iomem * base,unsigned int reg_offset,unsigned long value)64*4882a593Smuzhiyun static inline void pit_write(void __iomem *base, unsigned int reg_offset, unsigned long value)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	writel_relaxed(value, base + reg_offset);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun  * Clocksource:  just a monotonic counter of MCK/16 cycles.
71*4882a593Smuzhiyun  * We don't care whether or not PIT irqs are enabled.
72*4882a593Smuzhiyun  */
read_pit_clk(struct clocksource * cs)73*4882a593Smuzhiyun static u64 read_pit_clk(struct clocksource *cs)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	struct pit_data *data = clksrc_to_pit_data(cs);
76*4882a593Smuzhiyun 	unsigned long flags;
77*4882a593Smuzhiyun 	u32 elapsed;
78*4882a593Smuzhiyun 	u32 t;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	raw_local_irq_save(flags);
81*4882a593Smuzhiyun 	elapsed = data->cnt;
82*4882a593Smuzhiyun 	t = pit_read(data->base, AT91_PIT_PIIR);
83*4882a593Smuzhiyun 	raw_local_irq_restore(flags);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	elapsed += PIT_PICNT(t) * data->cycle;
86*4882a593Smuzhiyun 	elapsed += PIT_CPIV(t);
87*4882a593Smuzhiyun 	return elapsed;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
pit_clkevt_shutdown(struct clock_event_device * dev)90*4882a593Smuzhiyun static int pit_clkevt_shutdown(struct clock_event_device *dev)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	struct pit_data *data = clkevt_to_pit_data(dev);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	/* disable irq, leaving the clocksource active */
95*4882a593Smuzhiyun 	pit_write(data->base, AT91_PIT_MR, (data->cycle - 1) | AT91_PIT_PITEN);
96*4882a593Smuzhiyun 	return 0;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun  * Clockevent device:  interrupts every 1/HZ (== pit_cycles * MCK/16)
101*4882a593Smuzhiyun  */
pit_clkevt_set_periodic(struct clock_event_device * dev)102*4882a593Smuzhiyun static int pit_clkevt_set_periodic(struct clock_event_device *dev)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun 	struct pit_data *data = clkevt_to_pit_data(dev);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	/* update clocksource counter */
107*4882a593Smuzhiyun 	data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
108*4882a593Smuzhiyun 	pit_write(data->base, AT91_PIT_MR,
109*4882a593Smuzhiyun 		  (data->cycle - 1) | AT91_PIT_PITEN | AT91_PIT_PITIEN);
110*4882a593Smuzhiyun 	return 0;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
at91sam926x_pit_suspend(struct clock_event_device * cedev)113*4882a593Smuzhiyun static void at91sam926x_pit_suspend(struct clock_event_device *cedev)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	struct pit_data *data = clkevt_to_pit_data(cedev);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	/* Disable timer */
118*4882a593Smuzhiyun 	pit_write(data->base, AT91_PIT_MR, 0);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
at91sam926x_pit_reset(struct pit_data * data)121*4882a593Smuzhiyun static void at91sam926x_pit_reset(struct pit_data *data)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	/* Disable timer and irqs */
124*4882a593Smuzhiyun 	pit_write(data->base, AT91_PIT_MR, 0);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	/* Clear any pending interrupts, wait for PIT to stop counting */
127*4882a593Smuzhiyun 	while (PIT_CPIV(pit_read(data->base, AT91_PIT_PIVR)) != 0)
128*4882a593Smuzhiyun 		cpu_relax();
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	/* Start PIT but don't enable IRQ */
131*4882a593Smuzhiyun 	pit_write(data->base, AT91_PIT_MR,
132*4882a593Smuzhiyun 		  (data->cycle - 1) | AT91_PIT_PITEN);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
at91sam926x_pit_resume(struct clock_event_device * cedev)135*4882a593Smuzhiyun static void at91sam926x_pit_resume(struct clock_event_device *cedev)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	struct pit_data *data = clkevt_to_pit_data(cedev);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	at91sam926x_pit_reset(data);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun  * IRQ handler for the timer.
144*4882a593Smuzhiyun  */
at91sam926x_pit_interrupt(int irq,void * dev_id)145*4882a593Smuzhiyun static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	struct pit_data *data = dev_id;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* The PIT interrupt may be disabled, and is shared */
150*4882a593Smuzhiyun 	if (clockevent_state_periodic(&data->clkevt) &&
151*4882a593Smuzhiyun 	    (pit_read(data->base, AT91_PIT_SR) & AT91_PIT_PITS)) {
152*4882a593Smuzhiyun 		/* Get number of ticks performed before irq, and ack it */
153*4882a593Smuzhiyun 		data->cnt += data->cycle * PIT_PICNT(pit_read(data->base,
154*4882a593Smuzhiyun 							      AT91_PIT_PIVR));
155*4882a593Smuzhiyun 		data->clkevt.event_handler(&data->clkevt);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 		return IRQ_HANDLED;
158*4882a593Smuzhiyun 	}
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	return IRQ_NONE;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun  * Set up both clocksource and clockevent support.
165*4882a593Smuzhiyun  */
at91sam926x_pit_dt_init(struct device_node * node)166*4882a593Smuzhiyun static int __init at91sam926x_pit_dt_init(struct device_node *node)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	unsigned long   pit_rate;
169*4882a593Smuzhiyun 	unsigned        bits;
170*4882a593Smuzhiyun 	int             ret;
171*4882a593Smuzhiyun 	struct pit_data *data;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	data = kzalloc(sizeof(*data), GFP_KERNEL);
174*4882a593Smuzhiyun 	if (!data)
175*4882a593Smuzhiyun 		return -ENOMEM;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	data->base = of_iomap(node, 0);
178*4882a593Smuzhiyun 	if (!data->base) {
179*4882a593Smuzhiyun 		pr_err("Could not map PIT address\n");
180*4882a593Smuzhiyun 		ret = -ENXIO;
181*4882a593Smuzhiyun 		goto exit;
182*4882a593Smuzhiyun 	}
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	data->mck = of_clk_get(node, 0);
185*4882a593Smuzhiyun 	if (IS_ERR(data->mck)) {
186*4882a593Smuzhiyun 		pr_err("Unable to get mck clk\n");
187*4882a593Smuzhiyun 		ret = PTR_ERR(data->mck);
188*4882a593Smuzhiyun 		goto exit;
189*4882a593Smuzhiyun 	}
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	ret = clk_prepare_enable(data->mck);
192*4882a593Smuzhiyun 	if (ret) {
193*4882a593Smuzhiyun 		pr_err("Unable to enable mck\n");
194*4882a593Smuzhiyun 		goto exit;
195*4882a593Smuzhiyun 	}
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	/* Get the interrupts property */
198*4882a593Smuzhiyun 	data->irq = irq_of_parse_and_map(node, 0);
199*4882a593Smuzhiyun 	if (!data->irq) {
200*4882a593Smuzhiyun 		pr_err("Unable to get IRQ from DT\n");
201*4882a593Smuzhiyun 		ret = -EINVAL;
202*4882a593Smuzhiyun 		goto exit;
203*4882a593Smuzhiyun 	}
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/*
206*4882a593Smuzhiyun 	 * Use our actual MCK to figure out how many MCK/16 ticks per
207*4882a593Smuzhiyun 	 * 1/HZ period (instead of a compile-time constant LATCH).
208*4882a593Smuzhiyun 	 */
209*4882a593Smuzhiyun 	pit_rate = clk_get_rate(data->mck) / 16;
210*4882a593Smuzhiyun 	data->cycle = DIV_ROUND_CLOSEST(pit_rate, HZ);
211*4882a593Smuzhiyun 	WARN_ON(((data->cycle - 1) & ~AT91_PIT_PIV) != 0);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	/* Initialize and enable the timer */
214*4882a593Smuzhiyun 	at91sam926x_pit_reset(data);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/*
217*4882a593Smuzhiyun 	 * Register clocksource.  The high order bits of PIV are unused,
218*4882a593Smuzhiyun 	 * so this isn't a 32-bit counter unless we get clockevent irqs.
219*4882a593Smuzhiyun 	 */
220*4882a593Smuzhiyun 	bits = 12 /* PICNT */ + ilog2(data->cycle) /* PIV */;
221*4882a593Smuzhiyun 	data->clksrc.mask = CLOCKSOURCE_MASK(bits);
222*4882a593Smuzhiyun 	data->clksrc.name = "pit";
223*4882a593Smuzhiyun 	data->clksrc.rating = 175;
224*4882a593Smuzhiyun 	data->clksrc.read = read_pit_clk;
225*4882a593Smuzhiyun 	data->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	ret = clocksource_register_hz(&data->clksrc, pit_rate);
228*4882a593Smuzhiyun 	if (ret) {
229*4882a593Smuzhiyun 		pr_err("Failed to register clocksource\n");
230*4882a593Smuzhiyun 		goto exit;
231*4882a593Smuzhiyun 	}
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	/* Set up irq handler */
234*4882a593Smuzhiyun 	ret = request_irq(data->irq, at91sam926x_pit_interrupt,
235*4882a593Smuzhiyun 			  IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
236*4882a593Smuzhiyun 			  "at91_tick", data);
237*4882a593Smuzhiyun 	if (ret) {
238*4882a593Smuzhiyun 		pr_err("Unable to setup IRQ\n");
239*4882a593Smuzhiyun 		clocksource_unregister(&data->clksrc);
240*4882a593Smuzhiyun 		goto exit;
241*4882a593Smuzhiyun 	}
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	/* Set up and register clockevents */
244*4882a593Smuzhiyun 	data->clkevt.name = "pit";
245*4882a593Smuzhiyun 	data->clkevt.features = CLOCK_EVT_FEAT_PERIODIC;
246*4882a593Smuzhiyun 	data->clkevt.shift = 32;
247*4882a593Smuzhiyun 	data->clkevt.mult = div_sc(pit_rate, NSEC_PER_SEC, data->clkevt.shift);
248*4882a593Smuzhiyun 	data->clkevt.rating = 100;
249*4882a593Smuzhiyun 	data->clkevt.cpumask = cpumask_of(0);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	data->clkevt.set_state_shutdown = pit_clkevt_shutdown;
252*4882a593Smuzhiyun 	data->clkevt.set_state_periodic = pit_clkevt_set_periodic;
253*4882a593Smuzhiyun 	data->clkevt.resume = at91sam926x_pit_resume;
254*4882a593Smuzhiyun 	data->clkevt.suspend = at91sam926x_pit_suspend;
255*4882a593Smuzhiyun 	clockevents_register_device(&data->clkevt);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	return 0;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun exit:
260*4882a593Smuzhiyun 	kfree(data);
261*4882a593Smuzhiyun 	return ret;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun TIMER_OF_DECLARE(at91sam926x_pit, "atmel,at91sam9260-pit",
264*4882a593Smuzhiyun 		       at91sam926x_pit_dt_init);
265