1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2f5bf0ee4SLinus Walleij /*
3f5bf0ee4SLinus Walleij  * Faraday Technology FTTMR010 timer driver
4f5bf0ee4SLinus Walleij  * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
5f5bf0ee4SLinus Walleij  *
6f5bf0ee4SLinus Walleij  * Based on a rewrite of arch/arm/mach-gemini/timer.c:
7f5bf0ee4SLinus Walleij  * Copyright (C) 2001-2006 Storlink, Corp.
8f5bf0ee4SLinus Walleij  * Copyright (C) 2008-2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
9f5bf0ee4SLinus Walleij  */
10f5bf0ee4SLinus Walleij #include <linux/interrupt.h>
11f5bf0ee4SLinus Walleij #include <linux/io.h>
12f5bf0ee4SLinus Walleij #include <linux/of.h>
13f5bf0ee4SLinus Walleij #include <linux/of_address.h>
14f5bf0ee4SLinus Walleij #include <linux/of_irq.h>
15f5bf0ee4SLinus Walleij #include <linux/clockchips.h>
16f5bf0ee4SLinus Walleij #include <linux/clocksource.h>
17f5bf0ee4SLinus Walleij #include <linux/sched_clock.h>
1828e71e2fSLinus Walleij #include <linux/clk.h>
19e7bad212SLinus Walleij #include <linux/slab.h>
20d0d76d57SLinus Walleij #include <linux/bitops.h>
21385c98fcSLinus Walleij #include <linux/delay.h>
22f5bf0ee4SLinus Walleij 
23f5bf0ee4SLinus Walleij /*
2486fe57fcSTao Ren  * Register definitions common for all the timer variants.
25f5bf0ee4SLinus Walleij  */
26f5bf0ee4SLinus Walleij #define TIMER1_COUNT		(0x00)
27f5bf0ee4SLinus Walleij #define TIMER1_LOAD		(0x04)
28f5bf0ee4SLinus Walleij #define TIMER1_MATCH1		(0x08)
29f5bf0ee4SLinus Walleij #define TIMER1_MATCH2		(0x0c)
30f5bf0ee4SLinus Walleij #define TIMER2_COUNT		(0x10)
31f5bf0ee4SLinus Walleij #define TIMER2_LOAD		(0x14)
32f5bf0ee4SLinus Walleij #define TIMER2_MATCH1		(0x18)
33f5bf0ee4SLinus Walleij #define TIMER2_MATCH2		(0x1c)
34f5bf0ee4SLinus Walleij #define TIMER3_COUNT		(0x20)
35f5bf0ee4SLinus Walleij #define TIMER3_LOAD		(0x24)
36f5bf0ee4SLinus Walleij #define TIMER3_MATCH1		(0x28)
37f5bf0ee4SLinus Walleij #define TIMER3_MATCH2		(0x2c)
38f5bf0ee4SLinus Walleij #define TIMER_CR		(0x30)
39f5bf0ee4SLinus Walleij 
4086fe57fcSTao Ren /*
415422413cSJoel Stanley  * Control register set to clear for ast2600 only.
425422413cSJoel Stanley  */
435422413cSJoel Stanley #define AST2600_TIMER_CR_CLR	(0x3c)
445422413cSJoel Stanley 
455422413cSJoel Stanley /*
4686fe57fcSTao Ren  * Control register (TMC30) bit fields for fttmr010/gemini/moxart timers.
4786fe57fcSTao Ren  */
48d0d76d57SLinus Walleij #define TIMER_1_CR_ENABLE	BIT(0)
49d0d76d57SLinus Walleij #define TIMER_1_CR_CLOCK	BIT(1)
50d0d76d57SLinus Walleij #define TIMER_1_CR_INT		BIT(2)
51d0d76d57SLinus Walleij #define TIMER_2_CR_ENABLE	BIT(3)
52d0d76d57SLinus Walleij #define TIMER_2_CR_CLOCK	BIT(4)
53d0d76d57SLinus Walleij #define TIMER_2_CR_INT		BIT(5)
54d0d76d57SLinus Walleij #define TIMER_3_CR_ENABLE	BIT(6)
55d0d76d57SLinus Walleij #define TIMER_3_CR_CLOCK	BIT(7)
56d0d76d57SLinus Walleij #define TIMER_3_CR_INT		BIT(8)
57d0d76d57SLinus Walleij #define TIMER_1_CR_UPDOWN	BIT(9)
58d0d76d57SLinus Walleij #define TIMER_2_CR_UPDOWN	BIT(10)
59d0d76d57SLinus Walleij #define TIMER_3_CR_UPDOWN	BIT(11)
60f5bf0ee4SLinus Walleij 
61ec14ba1eSLinus Walleij /*
6286fe57fcSTao Ren  * Control register (TMC30) bit fields for aspeed ast2400/ast2500 timers.
6386fe57fcSTao Ren  * The aspeed timers move bits around in the control register and lacks
6486fe57fcSTao Ren  * bits for setting the timer to count upwards.
65ec14ba1eSLinus Walleij  */
66ec14ba1eSLinus Walleij #define TIMER_1_CR_ASPEED_ENABLE	BIT(0)
67ec14ba1eSLinus Walleij #define TIMER_1_CR_ASPEED_CLOCK		BIT(1)
68ec14ba1eSLinus Walleij #define TIMER_1_CR_ASPEED_INT		BIT(2)
69ec14ba1eSLinus Walleij #define TIMER_2_CR_ASPEED_ENABLE	BIT(4)
70ec14ba1eSLinus Walleij #define TIMER_2_CR_ASPEED_CLOCK		BIT(5)
71ec14ba1eSLinus Walleij #define TIMER_2_CR_ASPEED_INT		BIT(6)
72ec14ba1eSLinus Walleij #define TIMER_3_CR_ASPEED_ENABLE	BIT(8)
73ec14ba1eSLinus Walleij #define TIMER_3_CR_ASPEED_CLOCK		BIT(9)
74ec14ba1eSLinus Walleij #define TIMER_3_CR_ASPEED_INT		BIT(10)
75ec14ba1eSLinus Walleij 
7686fe57fcSTao Ren /*
7786fe57fcSTao Ren  * Interrupt status/mask register definitions for fttmr010/gemini/moxart
7886fe57fcSTao Ren  * timers.
7986fe57fcSTao Ren  * The registers don't exist and they are not needed on aspeed timers
8086fe57fcSTao Ren  * because:
8186fe57fcSTao Ren  *   - aspeed timer overflow interrupt is controlled by bits in Control
8286fe57fcSTao Ren  *     Register (TMC30).
8386fe57fcSTao Ren  *   - aspeed timers always generate interrupt when either one of the
8486fe57fcSTao Ren  *     Match registers equals to Status register.
8586fe57fcSTao Ren  */
8686fe57fcSTao Ren #define TIMER_INTR_STATE	(0x34)
8786fe57fcSTao Ren #define TIMER_INTR_MASK		(0x38)
88d0d76d57SLinus Walleij #define TIMER_1_INT_MATCH1	BIT(0)
89d0d76d57SLinus Walleij #define TIMER_1_INT_MATCH2	BIT(1)
90d0d76d57SLinus Walleij #define TIMER_1_INT_OVERFLOW	BIT(2)
91d0d76d57SLinus Walleij #define TIMER_2_INT_MATCH1	BIT(3)
92d0d76d57SLinus Walleij #define TIMER_2_INT_MATCH2	BIT(4)
93d0d76d57SLinus Walleij #define TIMER_2_INT_OVERFLOW	BIT(5)
94d0d76d57SLinus Walleij #define TIMER_3_INT_MATCH1	BIT(6)
95d0d76d57SLinus Walleij #define TIMER_3_INT_MATCH2	BIT(7)
96d0d76d57SLinus Walleij #define TIMER_3_INT_OVERFLOW	BIT(8)
97f5bf0ee4SLinus Walleij #define TIMER_INT_ALL_MASK	0x1ff
98f5bf0ee4SLinus Walleij 
99e7bad212SLinus Walleij struct fttmr010 {
100e7bad212SLinus Walleij 	void __iomem *base;
101e7bad212SLinus Walleij 	unsigned int tick_rate;
10286fe57fcSTao Ren 	bool is_aspeed;
103ec14ba1eSLinus Walleij 	u32 t1_enable_val;
104e7bad212SLinus Walleij 	struct clock_event_device clkevt;
10584fb64c2SJoel Stanley 	int (*timer_shutdown)(struct clock_event_device *evt);
106385c98fcSLinus Walleij #ifdef CONFIG_ARM
107385c98fcSLinus Walleij 	struct delay_timer delay_timer;
108385c98fcSLinus Walleij #endif
109e7bad212SLinus Walleij };
110e7bad212SLinus Walleij 
111385c98fcSLinus Walleij /*
112385c98fcSLinus Walleij  * A local singleton used by sched_clock and delay timer reads, which are
113385c98fcSLinus Walleij  * fast and stateless
114385c98fcSLinus Walleij  */
115e7bad212SLinus Walleij static struct fttmr010 *local_fttmr;
116e7bad212SLinus Walleij 
to_fttmr010(struct clock_event_device * evt)117e7bad212SLinus Walleij static inline struct fttmr010 *to_fttmr010(struct clock_event_device *evt)
118e7bad212SLinus Walleij {
119e7bad212SLinus Walleij 	return container_of(evt, struct fttmr010, clkevt);
120e7bad212SLinus Walleij }
121f5bf0ee4SLinus Walleij 
fttmr010_read_current_timer_up(void)122385c98fcSLinus Walleij static unsigned long fttmr010_read_current_timer_up(void)
123385c98fcSLinus Walleij {
124385c98fcSLinus Walleij 	return readl(local_fttmr->base + TIMER2_COUNT);
125385c98fcSLinus Walleij }
126385c98fcSLinus Walleij 
fttmr010_read_current_timer_down(void)127385c98fcSLinus Walleij static unsigned long fttmr010_read_current_timer_down(void)
128385c98fcSLinus Walleij {
129385c98fcSLinus Walleij 	return ~readl(local_fttmr->base + TIMER2_COUNT);
130385c98fcSLinus Walleij }
131385c98fcSLinus Walleij 
fttmr010_read_sched_clock_up(void)132c4779902SLinus Walleij static u64 notrace fttmr010_read_sched_clock_up(void)
133c4779902SLinus Walleij {
134c4779902SLinus Walleij 	return fttmr010_read_current_timer_up();
135c4779902SLinus Walleij }
136c4779902SLinus Walleij 
fttmr010_read_sched_clock_down(void)137c4779902SLinus Walleij static u64 notrace fttmr010_read_sched_clock_down(void)
138c4779902SLinus Walleij {
139c4779902SLinus Walleij 	return fttmr010_read_current_timer_down();
140c4779902SLinus Walleij }
141385c98fcSLinus Walleij 
fttmr010_timer_set_next_event(unsigned long cycles,struct clock_event_device * evt)142f5bf0ee4SLinus Walleij static int fttmr010_timer_set_next_event(unsigned long cycles,
143f5bf0ee4SLinus Walleij 				       struct clock_event_device *evt)
144f5bf0ee4SLinus Walleij {
145e7bad212SLinus Walleij 	struct fttmr010 *fttmr010 = to_fttmr010(evt);
146f5bf0ee4SLinus Walleij 	u32 cr;
147f5bf0ee4SLinus Walleij 
148ec14ba1eSLinus Walleij 	/* Stop */
14984fb64c2SJoel Stanley 	fttmr010->timer_shutdown(evt);
150ec14ba1eSLinus Walleij 
15186fe57fcSTao Ren 	if (fttmr010->is_aspeed) {
1524451d3f5STao Ren 		/*
1534451d3f5STao Ren 		 * ASPEED Timer Controller will load TIMER1_LOAD register
1544451d3f5STao Ren 		 * into TIMER1_COUNT register when the timer is re-enabled.
1554451d3f5STao Ren 		 */
1564451d3f5STao Ren 		writel(cycles, fttmr010->base + TIMER1_LOAD);
1574451d3f5STao Ren 	} else {
1584451d3f5STao Ren 		/* Setup the match register forward in time */
159e7bad212SLinus Walleij 		cr = readl(fttmr010->base + TIMER1_COUNT);
1604451d3f5STao Ren 		writel(cr + cycles, fttmr010->base + TIMER1_MATCH1);
1614451d3f5STao Ren 	}
162ec14ba1eSLinus Walleij 
163ec14ba1eSLinus Walleij 	/* Start */
164ec14ba1eSLinus Walleij 	cr = readl(fttmr010->base + TIMER_CR);
165ec14ba1eSLinus Walleij 	cr |= fttmr010->t1_enable_val;
166ec14ba1eSLinus Walleij 	writel(cr, fttmr010->base + TIMER_CR);
167f5bf0ee4SLinus Walleij 
168f5bf0ee4SLinus Walleij 	return 0;
169f5bf0ee4SLinus Walleij }
170f5bf0ee4SLinus Walleij 
ast2600_timer_shutdown(struct clock_event_device * evt)1715422413cSJoel Stanley static int ast2600_timer_shutdown(struct clock_event_device *evt)
1725422413cSJoel Stanley {
1735422413cSJoel Stanley 	struct fttmr010 *fttmr010 = to_fttmr010(evt);
1745422413cSJoel Stanley 
1755422413cSJoel Stanley 	/* Stop */
1765422413cSJoel Stanley 	writel(fttmr010->t1_enable_val, fttmr010->base + AST2600_TIMER_CR_CLR);
1775422413cSJoel Stanley 
1785422413cSJoel Stanley 	return 0;
1795422413cSJoel Stanley }
1805422413cSJoel Stanley 
fttmr010_timer_shutdown(struct clock_event_device * evt)181f5bf0ee4SLinus Walleij static int fttmr010_timer_shutdown(struct clock_event_device *evt)
182f5bf0ee4SLinus Walleij {
183e7bad212SLinus Walleij 	struct fttmr010 *fttmr010 = to_fttmr010(evt);
184f5bf0ee4SLinus Walleij 	u32 cr;
185f5bf0ee4SLinus Walleij 
186ec14ba1eSLinus Walleij 	/* Stop */
187e7bad212SLinus Walleij 	cr = readl(fttmr010->base + TIMER_CR);
188ec14ba1eSLinus Walleij 	cr &= ~fttmr010->t1_enable_val;
189e7bad212SLinus Walleij 	writel(cr, fttmr010->base + TIMER_CR);
190e7bad212SLinus Walleij 
191e7bad212SLinus Walleij 	return 0;
192e7bad212SLinus Walleij }
193e7bad212SLinus Walleij 
fttmr010_timer_set_oneshot(struct clock_event_device * evt)194e7bad212SLinus Walleij static int fttmr010_timer_set_oneshot(struct clock_event_device *evt)
195e7bad212SLinus Walleij {
196e7bad212SLinus Walleij 	struct fttmr010 *fttmr010 = to_fttmr010(evt);
197e7bad212SLinus Walleij 	u32 cr;
198e7bad212SLinus Walleij 
199ec14ba1eSLinus Walleij 	/* Stop */
20084fb64c2SJoel Stanley 	fttmr010->timer_shutdown(evt);
201f5bf0ee4SLinus Walleij 
202ec14ba1eSLinus Walleij 	/* Setup counter start from 0 or ~0 */
203e7bad212SLinus Walleij 	writel(0, fttmr010->base + TIMER1_COUNT);
20486fe57fcSTao Ren 	if (fttmr010->is_aspeed) {
205ec14ba1eSLinus Walleij 		writel(~0, fttmr010->base + TIMER1_LOAD);
20686fe57fcSTao Ren 	} else {
207e7bad212SLinus Walleij 		writel(0, fttmr010->base + TIMER1_LOAD);
208f5bf0ee4SLinus Walleij 
209e7bad212SLinus Walleij 		/* Enable interrupt */
210e7bad212SLinus Walleij 		cr = readl(fttmr010->base + TIMER_INTR_MASK);
211f5bf0ee4SLinus Walleij 		cr &= ~(TIMER_1_INT_OVERFLOW | TIMER_1_INT_MATCH2);
212f5bf0ee4SLinus Walleij 		cr |= TIMER_1_INT_MATCH1;
213e7bad212SLinus Walleij 		writel(cr, fttmr010->base + TIMER_INTR_MASK);
21486fe57fcSTao Ren 	}
215f5bf0ee4SLinus Walleij 
216f5bf0ee4SLinus Walleij 	return 0;
217f5bf0ee4SLinus Walleij }
218f5bf0ee4SLinus Walleij 
fttmr010_timer_set_periodic(struct clock_event_device * evt)219f5bf0ee4SLinus Walleij static int fttmr010_timer_set_periodic(struct clock_event_device *evt)
220f5bf0ee4SLinus Walleij {
221e7bad212SLinus Walleij 	struct fttmr010 *fttmr010 = to_fttmr010(evt);
222e7bad212SLinus Walleij 	u32 period = DIV_ROUND_CLOSEST(fttmr010->tick_rate, HZ);
223f5bf0ee4SLinus Walleij 	u32 cr;
224f5bf0ee4SLinus Walleij 
225ec14ba1eSLinus Walleij 	/* Stop */
22684fb64c2SJoel Stanley 	fttmr010->timer_shutdown(evt);
227f5bf0ee4SLinus Walleij 
228ec14ba1eSLinus Walleij 	/* Setup timer to fire at 1/HZ intervals. */
22986fe57fcSTao Ren 	if (fttmr010->is_aspeed) {
230ec14ba1eSLinus Walleij 		writel(period, fttmr010->base + TIMER1_LOAD);
231ec14ba1eSLinus Walleij 	} else {
232f5bf0ee4SLinus Walleij 		cr = 0xffffffff - (period - 1);
233e7bad212SLinus Walleij 		writel(cr, fttmr010->base + TIMER1_COUNT);
234e7bad212SLinus Walleij 		writel(cr, fttmr010->base + TIMER1_LOAD);
235f5bf0ee4SLinus Walleij 
236ec14ba1eSLinus Walleij 		/* Enable interrupt on overflow */
237e7bad212SLinus Walleij 		cr = readl(fttmr010->base + TIMER_INTR_MASK);
238f5bf0ee4SLinus Walleij 		cr &= ~(TIMER_1_INT_MATCH1 | TIMER_1_INT_MATCH2);
239f5bf0ee4SLinus Walleij 		cr |= TIMER_1_INT_OVERFLOW;
240e7bad212SLinus Walleij 		writel(cr, fttmr010->base + TIMER_INTR_MASK);
241ec14ba1eSLinus Walleij 	}
242f5bf0ee4SLinus Walleij 
243f5bf0ee4SLinus Walleij 	/* Start the timer */
244e7bad212SLinus Walleij 	cr = readl(fttmr010->base + TIMER_CR);
245ec14ba1eSLinus Walleij 	cr |= fttmr010->t1_enable_val;
246e7bad212SLinus Walleij 	writel(cr, fttmr010->base + TIMER_CR);
247f5bf0ee4SLinus Walleij 
248f5bf0ee4SLinus Walleij 	return 0;
249f5bf0ee4SLinus Walleij }
250f5bf0ee4SLinus Walleij 
251f5bf0ee4SLinus Walleij /*
252f5bf0ee4SLinus Walleij  * IRQ handler for the timer
253f5bf0ee4SLinus Walleij  */
fttmr010_timer_interrupt(int irq,void * dev_id)254f5bf0ee4SLinus Walleij static irqreturn_t fttmr010_timer_interrupt(int irq, void *dev_id)
255f5bf0ee4SLinus Walleij {
256e7bad212SLinus Walleij 	struct clock_event_device *evt = dev_id;
257f5bf0ee4SLinus Walleij 
258f5bf0ee4SLinus Walleij 	evt->event_handler(evt);
259f5bf0ee4SLinus Walleij 	return IRQ_HANDLED;
260f5bf0ee4SLinus Walleij }
261f5bf0ee4SLinus Walleij 
ast2600_timer_interrupt(int irq,void * dev_id)2625422413cSJoel Stanley static irqreturn_t ast2600_timer_interrupt(int irq, void *dev_id)
2635422413cSJoel Stanley {
2645422413cSJoel Stanley 	struct clock_event_device *evt = dev_id;
2655422413cSJoel Stanley 	struct fttmr010 *fttmr010 = to_fttmr010(evt);
2665422413cSJoel Stanley 
2675422413cSJoel Stanley 	writel(0x1, fttmr010->base + TIMER_INTR_STATE);
2685422413cSJoel Stanley 
2695422413cSJoel Stanley 	evt->event_handler(evt);
2705422413cSJoel Stanley 	return IRQ_HANDLED;
2715422413cSJoel Stanley }
2725422413cSJoel Stanley 
fttmr010_common_init(struct device_node * np,bool is_aspeed,bool is_ast2600)2735422413cSJoel Stanley static int __init fttmr010_common_init(struct device_node *np,
274*3a95de59SLinus Walleij 				       bool is_aspeed, bool is_ast2600)
275f5bf0ee4SLinus Walleij {
276e7bad212SLinus Walleij 	struct fttmr010 *fttmr010;
277f5bf0ee4SLinus Walleij 	int irq;
278dd98442eSLinus Walleij 	struct clk *clk;
279dd98442eSLinus Walleij 	int ret;
280ec14ba1eSLinus Walleij 	u32 val;
281dd98442eSLinus Walleij 
282dd98442eSLinus Walleij 	/*
283dd98442eSLinus Walleij 	 * These implementations require a clock reference.
284dd98442eSLinus Walleij 	 * FIXME: we currently only support clocking using PCLK
285dd98442eSLinus Walleij 	 * and using EXTCLK is not supported in the driver.
286dd98442eSLinus Walleij 	 */
287dd98442eSLinus Walleij 	clk = of_clk_get_by_name(np, "PCLK");
288dd98442eSLinus Walleij 	if (IS_ERR(clk)) {
289dd98442eSLinus Walleij 		pr_err("could not get PCLK\n");
290dd98442eSLinus Walleij 		return PTR_ERR(clk);
291dd98442eSLinus Walleij 	}
292dd98442eSLinus Walleij 	ret = clk_prepare_enable(clk);
293dd98442eSLinus Walleij 	if (ret) {
294dd98442eSLinus Walleij 		pr_err("failed to enable PCLK\n");
295dd98442eSLinus Walleij 		return ret;
296dd98442eSLinus Walleij 	}
297f5bf0ee4SLinus Walleij 
298e7bad212SLinus Walleij 	fttmr010 = kzalloc(sizeof(*fttmr010), GFP_KERNEL);
299e7bad212SLinus Walleij 	if (!fttmr010) {
300e7bad212SLinus Walleij 		ret = -ENOMEM;
301e7bad212SLinus Walleij 		goto out_disable_clock;
302e7bad212SLinus Walleij 	}
303e7bad212SLinus Walleij 	fttmr010->tick_rate = clk_get_rate(clk);
304e7bad212SLinus Walleij 
305e7bad212SLinus Walleij 	fttmr010->base = of_iomap(np, 0);
306e7bad212SLinus Walleij 	if (!fttmr010->base) {
3071893428bSArvind Yadav 		pr_err("Can't remap registers\n");
308e7bad212SLinus Walleij 		ret = -ENXIO;
309e7bad212SLinus Walleij 		goto out_free;
310f5bf0ee4SLinus Walleij 	}
311f5bf0ee4SLinus Walleij 	/* IRQ for timer 1 */
312f5bf0ee4SLinus Walleij 	irq = irq_of_parse_and_map(np, 0);
313f5bf0ee4SLinus Walleij 	if (irq <= 0) {
3141893428bSArvind Yadav 		pr_err("Can't parse IRQ\n");
315e7bad212SLinus Walleij 		ret = -EINVAL;
316e7bad212SLinus Walleij 		goto out_unmap;
317f5bf0ee4SLinus Walleij 	}
318f5bf0ee4SLinus Walleij 
319f5bf0ee4SLinus Walleij 	/*
32086fe57fcSTao Ren 	 * The Aspeed timers move bits around in the control register.
321ec14ba1eSLinus Walleij 	 */
322ef89718aSDaniel Lezcano 	if (is_aspeed) {
323ec14ba1eSLinus Walleij 		fttmr010->t1_enable_val = TIMER_1_CR_ASPEED_ENABLE |
324ec14ba1eSLinus Walleij 			TIMER_1_CR_ASPEED_INT;
32586fe57fcSTao Ren 		fttmr010->is_aspeed = true;
326ec14ba1eSLinus Walleij 	} else {
327ec14ba1eSLinus Walleij 		fttmr010->t1_enable_val = TIMER_1_CR_ENABLE | TIMER_1_CR_INT;
328ec14ba1eSLinus Walleij 
329ec14ba1eSLinus Walleij 		/*
330f5bf0ee4SLinus Walleij 		 * Reset the interrupt mask and status
331f5bf0ee4SLinus Walleij 		 */
332e7bad212SLinus Walleij 		writel(TIMER_INT_ALL_MASK, fttmr010->base + TIMER_INTR_MASK);
333e7bad212SLinus Walleij 		writel(0, fttmr010->base + TIMER_INTR_STATE);
33486fe57fcSTao Ren 	}
335ec14ba1eSLinus Walleij 
336ec14ba1eSLinus Walleij 	/*
337ec14ba1eSLinus Walleij 	 * Enable timer 1 count up, timer 2 count up, except on Aspeed,
338ec14ba1eSLinus Walleij 	 * where everything just counts down.
339ec14ba1eSLinus Walleij 	 */
340ef89718aSDaniel Lezcano 	if (is_aspeed)
341ec14ba1eSLinus Walleij 		val = TIMER_2_CR_ASPEED_ENABLE;
342ec14ba1eSLinus Walleij 	else {
34386fe57fcSTao Ren 		val = TIMER_2_CR_ENABLE | TIMER_1_CR_UPDOWN |
34486fe57fcSTao Ren 			TIMER_2_CR_UPDOWN;
345ec14ba1eSLinus Walleij 	}
346ec14ba1eSLinus Walleij 	writel(val, fttmr010->base + TIMER_CR);
347f5bf0ee4SLinus Walleij 
348f5bf0ee4SLinus Walleij 	/*
349f5bf0ee4SLinus Walleij 	 * Setup free-running clocksource timer (interrupts
350f5bf0ee4SLinus Walleij 	 * disabled.)
351f5bf0ee4SLinus Walleij 	 */
352e7bad212SLinus Walleij 	local_fttmr = fttmr010;
353b589da8bSLinus Walleij 	writel(0, fttmr010->base + TIMER2_COUNT);
354b589da8bSLinus Walleij 	writel(0, fttmr010->base + TIMER2_MATCH1);
355b589da8bSLinus Walleij 	writel(0, fttmr010->base + TIMER2_MATCH2);
356ec14ba1eSLinus Walleij 
35786fe57fcSTao Ren 	if (fttmr010->is_aspeed) {
358ec14ba1eSLinus Walleij 		writel(~0, fttmr010->base + TIMER2_LOAD);
359ec14ba1eSLinus Walleij 		clocksource_mmio_init(fttmr010->base + TIMER2_COUNT,
360ec14ba1eSLinus Walleij 				      "FTTMR010-TIMER2",
361ec14ba1eSLinus Walleij 				      fttmr010->tick_rate,
362ec14ba1eSLinus Walleij 				      300, 32, clocksource_mmio_readl_down);
363740e237aSLinus Walleij 		sched_clock_register(fttmr010_read_sched_clock_down, 32,
364740e237aSLinus Walleij 				     fttmr010->tick_rate);
365ec14ba1eSLinus Walleij 	} else {
366ec14ba1eSLinus Walleij 		writel(0, fttmr010->base + TIMER2_LOAD);
367b589da8bSLinus Walleij 		clocksource_mmio_init(fttmr010->base + TIMER2_COUNT,
368b589da8bSLinus Walleij 				      "FTTMR010-TIMER2",
369e7bad212SLinus Walleij 				      fttmr010->tick_rate,
370f5bf0ee4SLinus Walleij 				      300, 32, clocksource_mmio_readl_up);
371740e237aSLinus Walleij 		sched_clock_register(fttmr010_read_sched_clock_up, 32,
372e7bad212SLinus Walleij 				     fttmr010->tick_rate);
373740e237aSLinus Walleij 	}
374f5bf0ee4SLinus Walleij 
375f5bf0ee4SLinus Walleij 	/*
376e7bad212SLinus Walleij 	 * Setup clockevent timer (interrupt-driven) on timer 1.
377f5bf0ee4SLinus Walleij 	 */
378e7bad212SLinus Walleij 	writel(0, fttmr010->base + TIMER1_COUNT);
379e7bad212SLinus Walleij 	writel(0, fttmr010->base + TIMER1_LOAD);
380e7bad212SLinus Walleij 	writel(0, fttmr010->base + TIMER1_MATCH1);
381e7bad212SLinus Walleij 	writel(0, fttmr010->base + TIMER1_MATCH2);
382*3a95de59SLinus Walleij 
383*3a95de59SLinus Walleij 	if (is_ast2600) {
384*3a95de59SLinus Walleij 		fttmr010->timer_shutdown = ast2600_timer_shutdown;
385*3a95de59SLinus Walleij 		ret = request_irq(irq, ast2600_timer_interrupt,
386*3a95de59SLinus Walleij 				  IRQF_TIMER, "FTTMR010-TIMER1",
387*3a95de59SLinus Walleij 				  &fttmr010->clkevt);
388*3a95de59SLinus Walleij 	} else {
389*3a95de59SLinus Walleij 		fttmr010->timer_shutdown = fttmr010_timer_shutdown;
390*3a95de59SLinus Walleij 		ret = request_irq(irq, fttmr010_timer_interrupt,
391*3a95de59SLinus Walleij 				  IRQF_TIMER, "FTTMR010-TIMER1",
392*3a95de59SLinus Walleij 				  &fttmr010->clkevt);
393*3a95de59SLinus Walleij 	}
394e7bad212SLinus Walleij 	if (ret) {
395e7bad212SLinus Walleij 		pr_err("FTTMR010-TIMER1 no IRQ\n");
396e7bad212SLinus Walleij 		goto out_unmap;
397e7bad212SLinus Walleij 	}
398e7bad212SLinus Walleij 
399e7bad212SLinus Walleij 	fttmr010->clkevt.name = "FTTMR010-TIMER1";
400e7bad212SLinus Walleij 	/* Reasonably fast and accurate clock event */
401e7bad212SLinus Walleij 	fttmr010->clkevt.rating = 300;
402e7bad212SLinus Walleij 	fttmr010->clkevt.features = CLOCK_EVT_FEAT_PERIODIC |
403e7bad212SLinus Walleij 		CLOCK_EVT_FEAT_ONESHOT;
404e7bad212SLinus Walleij 	fttmr010->clkevt.set_next_event = fttmr010_timer_set_next_event;
40584fb64c2SJoel Stanley 	fttmr010->clkevt.set_state_shutdown = fttmr010->timer_shutdown;
406e7bad212SLinus Walleij 	fttmr010->clkevt.set_state_periodic = fttmr010_timer_set_periodic;
407e7bad212SLinus Walleij 	fttmr010->clkevt.set_state_oneshot = fttmr010_timer_set_oneshot;
40884fb64c2SJoel Stanley 	fttmr010->clkevt.tick_resume = fttmr010->timer_shutdown;
409e7bad212SLinus Walleij 	fttmr010->clkevt.cpumask = cpumask_of(0);
410e7bad212SLinus Walleij 	fttmr010->clkevt.irq = irq;
411e7bad212SLinus Walleij 	clockevents_config_and_register(&fttmr010->clkevt,
412e7bad212SLinus Walleij 					fttmr010->tick_rate,
413f5bf0ee4SLinus Walleij 					1, 0xffffffff);
414f5bf0ee4SLinus Walleij 
415385c98fcSLinus Walleij #ifdef CONFIG_ARM
416385c98fcSLinus Walleij 	/* Also use this timer for delays */
41786fe57fcSTao Ren 	if (fttmr010->is_aspeed)
418385c98fcSLinus Walleij 		fttmr010->delay_timer.read_current_timer =
419385c98fcSLinus Walleij 			fttmr010_read_current_timer_down;
420385c98fcSLinus Walleij 	else
421385c98fcSLinus Walleij 		fttmr010->delay_timer.read_current_timer =
422385c98fcSLinus Walleij 			fttmr010_read_current_timer_up;
423385c98fcSLinus Walleij 	fttmr010->delay_timer.freq = fttmr010->tick_rate;
424385c98fcSLinus Walleij 	register_current_timer_delay(&fttmr010->delay_timer);
425385c98fcSLinus Walleij #endif
426385c98fcSLinus Walleij 
427f5bf0ee4SLinus Walleij 	return 0;
428e7bad212SLinus Walleij 
429e7bad212SLinus Walleij out_unmap:
430e7bad212SLinus Walleij 	iounmap(fttmr010->base);
431e7bad212SLinus Walleij out_free:
432e7bad212SLinus Walleij 	kfree(fttmr010);
433e7bad212SLinus Walleij out_disable_clock:
434e7bad212SLinus Walleij 	clk_disable_unprepare(clk);
435e7bad212SLinus Walleij 
436e7bad212SLinus Walleij 	return ret;
437f5bf0ee4SLinus Walleij }
438ef89718aSDaniel Lezcano 
ast2600_timer_init(struct device_node * np)4395422413cSJoel Stanley static __init int ast2600_timer_init(struct device_node *np)
4405422413cSJoel Stanley {
441*3a95de59SLinus Walleij 	return fttmr010_common_init(np, true, true);
4425422413cSJoel Stanley }
4435422413cSJoel Stanley 
aspeed_timer_init(struct device_node * np)444ef89718aSDaniel Lezcano static __init int aspeed_timer_init(struct device_node *np)
445ef89718aSDaniel Lezcano {
446*3a95de59SLinus Walleij 	return fttmr010_common_init(np, true, false);
447ef89718aSDaniel Lezcano }
448ef89718aSDaniel Lezcano 
fttmr010_timer_init(struct device_node * np)449ef89718aSDaniel Lezcano static __init int fttmr010_timer_init(struct device_node *np)
450ef89718aSDaniel Lezcano {
451*3a95de59SLinus Walleij 	return fttmr010_common_init(np, false, false);
452ef89718aSDaniel Lezcano }
453ef89718aSDaniel Lezcano 
45417273395SDaniel Lezcano TIMER_OF_DECLARE(fttmr010, "faraday,fttmr010", fttmr010_timer_init);
45517273395SDaniel Lezcano TIMER_OF_DECLARE(gemini, "cortina,gemini-timer", fttmr010_timer_init);
45617273395SDaniel Lezcano TIMER_OF_DECLARE(moxart, "moxa,moxart-timer", fttmr010_timer_init);
45717273395SDaniel Lezcano TIMER_OF_DECLARE(ast2400, "aspeed,ast2400-timer", aspeed_timer_init);
45817273395SDaniel Lezcano TIMER_OF_DECLARE(ast2500, "aspeed,ast2500-timer", aspeed_timer_init);
4595422413cSJoel Stanley TIMER_OF_DECLARE(ast2600, "aspeed,ast2600-timer", ast2600_timer_init);
460