blob: 5d5ac0f05422a45f49c09fb4c3f35e01b947d859 [file] [log] [blame]
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -04001/*
2 * arch/arm/plat-orion/time.c
3 *
4 * Marvell Orion SoC timer handling.
5 *
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 *
10 * Timer 0 is used as free-running clocksource, while timer 1 is
11 * used as clock_event_device.
12 */
13
14#include <linux/kernel.h>
Nicolas Pitrea399e3f2009-05-15 00:42:36 -040015#include <linux/timer.h>
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040016#include <linux/clockchips.h>
17#include <linux/interrupt.h>
18#include <linux/irq.h>
Russell Kingf06a1622010-12-15 21:55:06 +000019#include <asm/sched_clock.h>
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040020
21/*
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020022 * MBus bridge block registers.
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040023 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020024#define BRIDGE_CAUSE_OFF 0x0110
25#define BRIDGE_MASK_OFF 0x0114
26#define BRIDGE_INT_TIMER0 0x0002
27#define BRIDGE_INT_TIMER1 0x0004
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040028
29
30/*
31 * Timer block registers.
32 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020033#define TIMER_CTRL_OFF 0x0000
34#define TIMER0_EN 0x0001
35#define TIMER0_RELOAD_EN 0x0002
36#define TIMER1_EN 0x0004
37#define TIMER1_RELOAD_EN 0x0008
38#define TIMER0_RELOAD_OFF 0x0010
39#define TIMER0_VAL_OFF 0x0014
40#define TIMER1_RELOAD_OFF 0x0018
41#define TIMER1_VAL_OFF 0x001c
42
43
44/*
45 * SoC-specific data.
46 */
47static void __iomem *bridge_base;
48static u32 bridge_timer1_clr_mask;
49static void __iomem *timer_base;
50
51
52/*
53 * Number of timer ticks per jiffy.
54 */
55static u32 ticks_per_jiffy;
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040056
57
58/*
Stefan Agner8a3269f2009-05-12 10:30:41 -070059 * Orion's sched_clock implementation. It has a resolution of
Russell Kingf06a1622010-12-15 21:55:06 +000060 * at least 7.5ns (133MHz TCLK).
Stefan Agner8a3269f2009-05-12 10:30:41 -070061 */
Stefan Agner8a3269f2009-05-12 10:30:41 -070062
Marc Zyngier2f0778af2011-12-15 12:19:23 +010063static u32 notrace orion_read_sched_clock(void)
Stefan Agner8a3269f2009-05-12 10:30:41 -070064{
Marc Zyngier2f0778af2011-12-15 12:19:23 +010065 return ~readl(timer_base + TIMER0_VAL_OFF);
Stefan Agner8a3269f2009-05-12 10:30:41 -070066}
67
68/*
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040069 * Clockevent handling.
70 */
71static int
72orion_clkevt_next_event(unsigned long delta, struct clock_event_device *dev)
73{
74 unsigned long flags;
75 u32 u;
76
77 if (delta == 0)
78 return -ETIME;
79
80 local_irq_save(flags);
81
82 /*
83 * Clear and enable clockevent timer interrupt.
84 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020085 writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040086
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020087 u = readl(bridge_base + BRIDGE_MASK_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040088 u |= BRIDGE_INT_TIMER1;
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020089 writel(u, bridge_base + BRIDGE_MASK_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040090
91 /*
92 * Setup new clockevent timer value.
93 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020094 writel(delta, timer_base + TIMER1_VAL_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040095
96 /*
97 * Enable the timer.
98 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +020099 u = readl(timer_base + TIMER_CTRL_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400100 u = (u & ~TIMER1_RELOAD_EN) | TIMER1_EN;
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200101 writel(u, timer_base + TIMER_CTRL_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400102
103 local_irq_restore(flags);
104
105 return 0;
106}
107
108static void
109orion_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
110{
111 unsigned long flags;
112 u32 u;
113
114 local_irq_save(flags);
115 if (mode == CLOCK_EVT_MODE_PERIODIC) {
116 /*
117 * Setup timer to fire at 1/HZ intervals.
118 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200119 writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD_OFF);
120 writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400121
122 /*
123 * Enable timer interrupt.
124 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200125 u = readl(bridge_base + BRIDGE_MASK_OFF);
126 writel(u | BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400127
128 /*
129 * Enable timer.
130 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200131 u = readl(timer_base + TIMER_CTRL_OFF);
132 writel(u | TIMER1_EN | TIMER1_RELOAD_EN,
133 timer_base + TIMER_CTRL_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400134 } else {
135 /*
136 * Disable timer.
137 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200138 u = readl(timer_base + TIMER_CTRL_OFF);
139 writel(u & ~TIMER1_EN, timer_base + TIMER_CTRL_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400140
141 /*
142 * Disable timer interrupt.
143 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200144 u = readl(bridge_base + BRIDGE_MASK_OFF);
145 writel(u & ~BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400146
147 /*
148 * ACK pending timer interrupt.
149 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200150 writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400151
152 }
153 local_irq_restore(flags);
154}
155
156static struct clock_event_device orion_clkevt = {
157 .name = "orion_tick",
158 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400159 .rating = 300,
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400160 .set_next_event = orion_clkevt_next_event,
161 .set_mode = orion_clkevt_mode,
162};
163
164static irqreturn_t orion_timer_interrupt(int irq, void *dev_id)
165{
166 /*
167 * ACK timer interrupt and call event handler.
168 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200169 writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400170 orion_clkevt.event_handler(&orion_clkevt);
171
172 return IRQ_HANDLED;
173}
174
175static struct irqaction orion_timer_irq = {
176 .name = "orion_tick",
177 .flags = IRQF_DISABLED | IRQF_TIMER,
178 .handler = orion_timer_interrupt
179};
180
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200181void __init
Thomas Petazzonie96a0302012-09-11 14:27:25 +0200182orion_time_set_base(void __iomem *_timer_base)
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200183{
Thomas Petazzonie96a0302012-09-11 14:27:25 +0200184 timer_base = _timer_base;
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200185}
186
187void __init
Thomas Petazzonie96a0302012-09-11 14:27:25 +0200188orion_time_init(void __iomem *_bridge_base, u32 _bridge_timer1_clr_mask,
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200189 unsigned int irq, unsigned int tclk)
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400190{
191 u32 u;
192
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200193 /*
194 * Set SoC-specific data.
195 */
Thomas Petazzonie96a0302012-09-11 14:27:25 +0200196 bridge_base = _bridge_base;
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200197 bridge_timer1_clr_mask = _bridge_timer1_clr_mask;
198
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400199 ticks_per_jiffy = (tclk + HZ/2) / HZ;
200
Stefan Agner8a3269f2009-05-12 10:30:41 -0700201 /*
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200202 * Set scale and timer for sched_clock.
Stefan Agner8a3269f2009-05-12 10:30:41 -0700203 */
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100204 setup_sched_clock(orion_read_sched_clock, 32, tclk);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400205
206 /*
207 * Setup free-running clocksource timer (interrupts
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200208 * disabled).
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400209 */
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200210 writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
211 writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
212 u = readl(bridge_base + BRIDGE_MASK_OFF);
213 writel(u & ~BRIDGE_INT_TIMER0, bridge_base + BRIDGE_MASK_OFF);
214 u = readl(timer_base + TIMER_CTRL_OFF);
215 writel(u | TIMER0_EN | TIMER0_RELOAD_EN, timer_base + TIMER_CTRL_OFF);
Russell Kingbfe45e02011-05-08 15:33:30 +0100216 clocksource_mmio_init(timer_base + TIMER0_VAL_OFF, "orion_clocksource",
217 tclk, 300, 32, clocksource_mmio_readl_down);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400218
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400219 /*
Lennert Buytenhek4ee1f6b2010-10-15 16:50:26 +0200220 * Setup clockevent timer (interrupt-driven).
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400221 */
222 setup_irq(irq, &orion_timer_irq);
Rusty Russell320ab2b2008-12-13 21:20:26 +1030223 orion_clkevt.cpumask = cpumask_of(0);
Shawn Guo838a2ae2013-01-12 11:50:05 +0000224 clockevents_config_and_register(&orion_clkevt, tclk, 1, 0xfffffffe);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400225}