blob: b856cec817028c378a8c96ea17af617c6f99a5f0 [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>
15#include <linux/clockchips.h>
16#include <linux/interrupt.h>
17#include <linux/irq.h>
18#include <asm/mach/time.h>
Nicolas Pitrefdd8b072009-04-22 20:08:17 +010019#include <mach/bridge-regs.h>
Stefan Agner8a3269f2009-05-12 10:30:41 -070020#include <mach/hardware.h>
21#include <linux/sched.h>
22#include <linux/cnt32_to_63.h>
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040023
24/*
25 * Number of timer ticks per jiffy.
26 */
27static u32 ticks_per_jiffy;
28
29
30/*
31 * Timer block registers.
32 */
33#define TIMER_CTRL (TIMER_VIRT_BASE + 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 (TIMER_VIRT_BASE + 0x0010)
39#define TIMER0_VAL (TIMER_VIRT_BASE + 0x0014)
40#define TIMER1_RELOAD (TIMER_VIRT_BASE + 0x0018)
41#define TIMER1_VAL (TIMER_VIRT_BASE + 0x001c)
42
43
44/*
Stefan Agner8a3269f2009-05-12 10:30:41 -070045 * Orion's sched_clock implementation. It has a resolution of
46 * at least 7.5ns (133MHz TCLK) and a maximum value of 834 days.
47 */
48#define TCLK2NS_SCALE_FACTOR 8
49
50static unsigned long tclk2ns_scale;
51
52static void __init set_tclk2ns_scale(unsigned long tclk)
53{
54 unsigned long long v = NSEC_PER_SEC;
55 v <<= TCLK2NS_SCALE_FACTOR;
56 v += tclk/2;
57 do_div(v, tclk);
58 /*
59 * We want an even value to automatically clear the top bit
60 * returned by cnt32_to_63() without an additional run time
61 * instruction. So if the LSB is 1 then round it up.
62 */
63 if (v & 1)
64 v++;
65 tclk2ns_scale = v;
66}
67
68unsigned long long sched_clock(void)
69{
70 unsigned long long v = cnt32_to_63(0xffffffff - readl(TIMER0_VAL));
71 return (v * tclk2ns_scale) >> TCLK2NS_SCALE_FACTOR;
72}
73
74/*
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040075 * Clocksource handling.
76 */
Magnus Damm8e196082009-04-21 12:24:00 -070077static cycle_t orion_clksrc_read(struct clocksource *cs)
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -040078{
79 return 0xffffffff - readl(TIMER0_VAL);
80}
81
82static struct clocksource orion_clksrc = {
83 .name = "orion_clocksource",
84 .shift = 20,
85 .rating = 300,
86 .read = orion_clksrc_read,
87 .mask = CLOCKSOURCE_MASK(32),
88 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
89};
90
91
92
93/*
94 * Clockevent handling.
95 */
96static int
97orion_clkevt_next_event(unsigned long delta, struct clock_event_device *dev)
98{
99 unsigned long flags;
100 u32 u;
101
102 if (delta == 0)
103 return -ETIME;
104
105 local_irq_save(flags);
106
107 /*
108 * Clear and enable clockevent timer interrupt.
109 */
Ke Wei12197152008-05-23 10:23:22 +0200110 writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400111
112 u = readl(BRIDGE_MASK);
113 u |= BRIDGE_INT_TIMER1;
114 writel(u, BRIDGE_MASK);
115
116 /*
117 * Setup new clockevent timer value.
118 */
119 writel(delta, TIMER1_VAL);
120
121 /*
122 * Enable the timer.
123 */
124 u = readl(TIMER_CTRL);
125 u = (u & ~TIMER1_RELOAD_EN) | TIMER1_EN;
126 writel(u, TIMER_CTRL);
127
128 local_irq_restore(flags);
129
130 return 0;
131}
132
133static void
134orion_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
135{
136 unsigned long flags;
137 u32 u;
138
139 local_irq_save(flags);
140 if (mode == CLOCK_EVT_MODE_PERIODIC) {
141 /*
142 * Setup timer to fire at 1/HZ intervals.
143 */
144 writel(ticks_per_jiffy - 1, TIMER1_RELOAD);
145 writel(ticks_per_jiffy - 1, TIMER1_VAL);
146
147 /*
148 * Enable timer interrupt.
149 */
150 u = readl(BRIDGE_MASK);
151 writel(u | BRIDGE_INT_TIMER1, BRIDGE_MASK);
152
153 /*
154 * Enable timer.
155 */
156 u = readl(TIMER_CTRL);
157 writel(u | TIMER1_EN | TIMER1_RELOAD_EN, TIMER_CTRL);
158 } else {
159 /*
160 * Disable timer.
161 */
162 u = readl(TIMER_CTRL);
163 writel(u & ~TIMER1_EN, TIMER_CTRL);
164
165 /*
166 * Disable timer interrupt.
167 */
168 u = readl(BRIDGE_MASK);
169 writel(u & ~BRIDGE_INT_TIMER1, BRIDGE_MASK);
170
171 /*
172 * ACK pending timer interrupt.
173 */
Ke Wei12197152008-05-23 10:23:22 +0200174 writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400175
176 }
177 local_irq_restore(flags);
178}
179
180static struct clock_event_device orion_clkevt = {
181 .name = "orion_tick",
182 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
183 .shift = 32,
184 .rating = 300,
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400185 .set_next_event = orion_clkevt_next_event,
186 .set_mode = orion_clkevt_mode,
187};
188
189static irqreturn_t orion_timer_interrupt(int irq, void *dev_id)
190{
191 /*
192 * ACK timer interrupt and call event handler.
193 */
Ke Wei12197152008-05-23 10:23:22 +0200194 writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400195 orion_clkevt.event_handler(&orion_clkevt);
196
197 return IRQ_HANDLED;
198}
199
200static struct irqaction orion_timer_irq = {
201 .name = "orion_tick",
202 .flags = IRQF_DISABLED | IRQF_TIMER,
203 .handler = orion_timer_interrupt
204};
205
206void __init orion_time_init(unsigned int irq, unsigned int tclk)
207{
208 u32 u;
209
210 ticks_per_jiffy = (tclk + HZ/2) / HZ;
211
Stefan Agner8a3269f2009-05-12 10:30:41 -0700212 /*
213 * Set scale for sched_clock
214 */
215 set_tclk2ns_scale(tclk);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400216
217 /*
218 * Setup free-running clocksource timer (interrupts
219 * disabled.)
220 */
221 writel(0xffffffff, TIMER0_VAL);
222 writel(0xffffffff, TIMER0_RELOAD);
223 u = readl(BRIDGE_MASK);
224 writel(u & ~BRIDGE_INT_TIMER0, BRIDGE_MASK);
225 u = readl(TIMER_CTRL);
226 writel(u | TIMER0_EN | TIMER0_RELOAD_EN, TIMER_CTRL);
227 orion_clksrc.mult = clocksource_hz2mult(tclk, orion_clksrc.shift);
228 clocksource_register(&orion_clksrc);
229
230
231 /*
232 * Setup clockevent timer (interrupt-driven.)
233 */
234 setup_irq(irq, &orion_timer_irq);
235 orion_clkevt.mult = div_sc(tclk, NSEC_PER_SEC, orion_clkevt.shift);
236 orion_clkevt.max_delta_ns = clockevent_delta2ns(0xfffffffe, &orion_clkevt);
237 orion_clkevt.min_delta_ns = clockevent_delta2ns(1, &orion_clkevt);
Rusty Russell320ab2b2008-12-13 21:20:26 +1030238 orion_clkevt.cpumask = cpumask_of(0);
Lennert Buytenhek2bac1de2008-03-27 14:51:40 -0400239 clockevents_register_device(&orion_clkevt);
240}