2 * arch/arm/plat-orion/time.c
4 * Marvell Orion SoC timer handling.
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.
10 * Timer 0 is used as free-running clocksource, while timer 1 is
11 * used as clock_event_device.
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/cnt32_to_63.h>
17 #include <linux/timer.h>
18 #include <linux/clockchips.h>
19 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <asm/mach/time.h>
22 #include <mach/bridge-regs.h>
23 #include <mach/hardware.h>
26 * Number of timer ticks per jiffy.
28 static u32 ticks_per_jiffy;
32 * Timer block registers.
34 #define TIMER_CTRL (TIMER_VIRT_BASE + 0x0000)
35 #define TIMER0_EN 0x0001
36 #define TIMER0_RELOAD_EN 0x0002
37 #define TIMER1_EN 0x0004
38 #define TIMER1_RELOAD_EN 0x0008
39 #define TIMER0_RELOAD (TIMER_VIRT_BASE + 0x0010)
40 #define TIMER0_VAL (TIMER_VIRT_BASE + 0x0014)
41 #define TIMER1_RELOAD (TIMER_VIRT_BASE + 0x0018)
42 #define TIMER1_VAL (TIMER_VIRT_BASE + 0x001c)
46 * Orion's sched_clock implementation. It has a resolution of
47 * at least 7.5ns (133MHz TCLK) and a maximum value of 834 days.
49 * Because the hardware timer period is quite short (21 secs if
50 * 200MHz TCLK) and because cnt32_to_63() needs to be called at
51 * least once per half period to work properly, a kernel timer is
52 * set up to ensure this requirement is always met.
54 #define TCLK2NS_SCALE_FACTOR 8
56 static unsigned long tclk2ns_scale;
58 unsigned long long notrace sched_clock(void)
60 unsigned long long v = cnt32_to_63(0xffffffff - readl(TIMER0_VAL));
61 return (v * tclk2ns_scale) >> TCLK2NS_SCALE_FACTOR;
64 static struct timer_list cnt32_to_63_keepwarm_timer;
66 static void cnt32_to_63_keepwarm(unsigned long data)
68 mod_timer(&cnt32_to_63_keepwarm_timer, round_jiffies(jiffies + data));
72 static void __init setup_sched_clock(unsigned long tclk)
78 v <<= TCLK2NS_SCALE_FACTOR;
82 * We want an even value to automatically clear the top bit
83 * returned by cnt32_to_63() without an additional run time
84 * instruction. So if the LSB is 1 then round it up.
90 data = (0xffffffffUL / tclk / 2 - 2) * HZ;
91 setup_timer(&cnt32_to_63_keepwarm_timer, cnt32_to_63_keepwarm, data);
92 mod_timer(&cnt32_to_63_keepwarm_timer, round_jiffies(jiffies + data));
96 * Clocksource handling.
98 static cycle_t orion_clksrc_read(struct clocksource *cs)
100 return 0xffffffff - readl(TIMER0_VAL);
103 static struct clocksource orion_clksrc = {
104 .name = "orion_clocksource",
106 .read = orion_clksrc_read,
107 .mask = CLOCKSOURCE_MASK(32),
108 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
114 * Clockevent handling.
117 orion_clkevt_next_event(unsigned long delta, struct clock_event_device *dev)
125 local_irq_save(flags);
128 * Clear and enable clockevent timer interrupt.
130 writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
132 u = readl(BRIDGE_MASK);
133 u |= BRIDGE_INT_TIMER1;
134 writel(u, BRIDGE_MASK);
137 * Setup new clockevent timer value.
139 writel(delta, TIMER1_VAL);
144 u = readl(TIMER_CTRL);
145 u = (u & ~TIMER1_RELOAD_EN) | TIMER1_EN;
146 writel(u, TIMER_CTRL);
148 local_irq_restore(flags);
154 orion_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
159 local_irq_save(flags);
160 if (mode == CLOCK_EVT_MODE_PERIODIC) {
162 * Setup timer to fire at 1/HZ intervals.
164 writel(ticks_per_jiffy - 1, TIMER1_RELOAD);
165 writel(ticks_per_jiffy - 1, TIMER1_VAL);
168 * Enable timer interrupt.
170 u = readl(BRIDGE_MASK);
171 writel(u | BRIDGE_INT_TIMER1, BRIDGE_MASK);
176 u = readl(TIMER_CTRL);
177 writel(u | TIMER1_EN | TIMER1_RELOAD_EN, TIMER_CTRL);
182 u = readl(TIMER_CTRL);
183 writel(u & ~TIMER1_EN, TIMER_CTRL);
186 * Disable timer interrupt.
188 u = readl(BRIDGE_MASK);
189 writel(u & ~BRIDGE_INT_TIMER1, BRIDGE_MASK);
192 * ACK pending timer interrupt.
194 writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
197 local_irq_restore(flags);
200 static struct clock_event_device orion_clkevt = {
201 .name = "orion_tick",
202 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
205 .set_next_event = orion_clkevt_next_event,
206 .set_mode = orion_clkevt_mode,
209 static irqreturn_t orion_timer_interrupt(int irq, void *dev_id)
212 * ACK timer interrupt and call event handler.
214 writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
215 orion_clkevt.event_handler(&orion_clkevt);
220 static struct irqaction orion_timer_irq = {
221 .name = "orion_tick",
222 .flags = IRQF_DISABLED | IRQF_TIMER,
223 .handler = orion_timer_interrupt
226 void __init orion_time_init(unsigned int irq, unsigned int tclk)
230 ticks_per_jiffy = (tclk + HZ/2) / HZ;
233 * Set scale and timer for sched_clock
235 setup_sched_clock(tclk);
238 * Setup free-running clocksource timer (interrupts
241 writel(0xffffffff, TIMER0_VAL);
242 writel(0xffffffff, TIMER0_RELOAD);
243 u = readl(BRIDGE_MASK);
244 writel(u & ~BRIDGE_INT_TIMER0, BRIDGE_MASK);
245 u = readl(TIMER_CTRL);
246 writel(u | TIMER0_EN | TIMER0_RELOAD_EN, TIMER_CTRL);
247 clocksource_register_hz(&orion_clksrc, tclk);
250 * Setup clockevent timer (interrupt-driven.)
252 setup_irq(irq, &orion_timer_irq);
253 orion_clkevt.mult = div_sc(tclk, NSEC_PER_SEC, orion_clkevt.shift);
254 orion_clkevt.max_delta_ns = clockevent_delta2ns(0xfffffffe, &orion_clkevt);
255 orion_clkevt.min_delta_ns = clockevent_delta2ns(1, &orion_clkevt);
256 orion_clkevt.cpumask = cpumask_of(0);
257 clockevents_register_device(&orion_clkevt);