RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / arm / mach-mmp / time.c
blob9a4a5b35aca24057a00818f52c442ea149b81d40
1 /*
2 * linux/arch/arm/mach-mmp/time.c
4 * Support for clocksource and clockevents
6 * Copyright (C) 2008 Marvell International Ltd.
7 * All rights reserved.
9 * 2008-04-11: Jason Chagas <Jason.chagas@marvell.com>
10 * 2008-10-08: Bin Yang <bin.yang@marvell.com>
12 * The timers module actually includes three timers, each timer with upto
13 * three match comparators. Timer #0 is used here in free-running mode as
14 * the clock source, and match comparator #1 used as clock event device.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/interrupt.h>
24 #include <linux/clockchips.h>
26 #include <linux/io.h>
27 #include <linux/irq.h>
28 #include <linux/sched.h>
29 #include <linux/cnt32_to_63.h>
31 #include <mach/addr-map.h>
32 #include <mach/regs-timers.h>
33 #include <mach/regs-apbc.h>
34 #include <mach/irqs.h>
35 #include <mach/cputype.h>
36 #include <asm/mach/time.h>
38 #include "clock.h"
40 #define TIMERS_VIRT_BASE TIMERS1_VIRT_BASE
42 #define MAX_DELTA (0xfffffffe)
43 #define MIN_DELTA (16)
45 #define TCR2NS_SCALE_FACTOR 10
47 static unsigned long tcr2ns_scale;
49 static void __init set_tcr2ns_scale(unsigned long tcr_rate)
51 unsigned long long v = 1000000000ULL << TCR2NS_SCALE_FACTOR;
52 do_div(v, tcr_rate);
53 tcr2ns_scale = v;
55 * We want an even value to automatically clear the top bit
56 * returned by cnt32_to_63() without an additional run time
57 * instruction. So if the LSB is 1 then round it up.
59 if (tcr2ns_scale & 1)
60 tcr2ns_scale++;
63 static inline uint32_t timer_read(void)
65 int delay = 100;
67 __raw_writel(1, TIMERS_VIRT_BASE + TMR_CVWR(0));
69 while (delay--)
70 cpu_relax();
72 return __raw_readl(TIMERS_VIRT_BASE + TMR_CVWR(0));
75 unsigned long long sched_clock(void)
77 unsigned long long v = cnt32_to_63(timer_read());
78 return (v * tcr2ns_scale) >> TCR2NS_SCALE_FACTOR;
81 static irqreturn_t timer_interrupt(int irq, void *dev_id)
83 struct clock_event_device *c = dev_id;
85 /* disable and clear pending interrupt status */
86 __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_IER(0));
87 __raw_writel(0x1, TIMERS_VIRT_BASE + TMR_ICR(0));
88 c->event_handler(c);
89 return IRQ_HANDLED;
92 static int timer_set_next_event(unsigned long delta,
93 struct clock_event_device *dev)
95 unsigned long flags, next;
97 local_irq_save(flags);
99 /* clear pending interrupt status and enable */
100 __raw_writel(0x01, TIMERS_VIRT_BASE + TMR_ICR(0));
101 __raw_writel(0x01, TIMERS_VIRT_BASE + TMR_IER(0));
103 next = timer_read() + delta;
104 __raw_writel(next, TIMERS_VIRT_BASE + TMR_TN_MM(0, 0));
106 local_irq_restore(flags);
107 return 0;
110 static void timer_set_mode(enum clock_event_mode mode,
111 struct clock_event_device *dev)
113 unsigned long flags;
115 local_irq_save(flags);
116 switch (mode) {
117 case CLOCK_EVT_MODE_ONESHOT:
118 case CLOCK_EVT_MODE_UNUSED:
119 case CLOCK_EVT_MODE_SHUTDOWN:
120 /* disable the matching interrupt */
121 __raw_writel(0x00, TIMERS_VIRT_BASE + TMR_IER(0));
122 break;
123 case CLOCK_EVT_MODE_RESUME:
124 case CLOCK_EVT_MODE_PERIODIC:
125 break;
127 local_irq_restore(flags);
130 static struct clock_event_device ckevt = {
131 .name = "clockevent",
132 .features = CLOCK_EVT_FEAT_ONESHOT,
133 .shift = 32,
134 .rating = 200,
135 .set_next_event = timer_set_next_event,
136 .set_mode = timer_set_mode,
139 static cycle_t clksrc_read(struct clocksource *cs)
141 return timer_read();
144 static struct clocksource cksrc = {
145 .name = "clocksource",
146 .shift = 20,
147 .rating = 200,
148 .read = clksrc_read,
149 .mask = CLOCKSOURCE_MASK(32),
150 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
153 static void __init timer_config(void)
155 uint32_t ccr = __raw_readl(TIMERS_VIRT_BASE + TMR_CCR);
156 uint32_t cer = __raw_readl(TIMERS_VIRT_BASE + TMR_CER);
157 uint32_t cmr = __raw_readl(TIMERS_VIRT_BASE + TMR_CMR);
159 __raw_writel(cer & ~0x1, TIMERS_VIRT_BASE + TMR_CER); /* disable */
161 ccr &= (cpu_is_mmp2()) ? TMR_CCR_CS_0(0) : TMR_CCR_CS_0(3);
162 __raw_writel(ccr, TIMERS_VIRT_BASE + TMR_CCR);
164 /* free-running mode */
165 __raw_writel(cmr | 0x01, TIMERS_VIRT_BASE + TMR_CMR);
167 __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_PLCR(0)); /* free-running */
168 __raw_writel(0x7, TIMERS_VIRT_BASE + TMR_ICR(0)); /* clear status */
169 __raw_writel(0x0, TIMERS_VIRT_BASE + TMR_IER(0));
171 /* enable timer counter */
172 __raw_writel(cer | 0x01, TIMERS_VIRT_BASE + TMR_CER);
175 static struct irqaction timer_irq = {
176 .name = "timer",
177 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
178 .handler = timer_interrupt,
179 .dev_id = &ckevt,
182 void __init timer_init(int irq)
184 timer_config();
186 set_tcr2ns_scale(CLOCK_TICK_RATE);
188 ckevt.mult = div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC, ckevt.shift);
189 ckevt.max_delta_ns = clockevent_delta2ns(MAX_DELTA, &ckevt);
190 ckevt.min_delta_ns = clockevent_delta2ns(MIN_DELTA, &ckevt);
191 ckevt.cpumask = cpumask_of(0);
193 cksrc.mult = clocksource_hz2mult(CLOCK_TICK_RATE, cksrc.shift);
195 setup_irq(irq, &timer_irq);
197 clocksource_register(&cksrc);
198 clockevents_register_device(&ckevt);