http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / kernel / linux / arch / arm / mach-omap / time.c
1 /*
2  * arch/arm/mach-omap/time.c
3  *
4  * OMAP Timer Tick 
5  *
6  * Copyright (C) 2000 RidgeRun, Inc.
7  * Author: Greg Lonnon <glonnon@ridgerun.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; either version 2 of the License, or (at your
12  * option) any later version.
13  *
14  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
20  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * You should have received a copy of the  GNU General Public License along
26  * with this program; if not, write  to the Free Software Foundation, Inc.,
27  * 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 #include <linux/config.h>
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/interrupt.h>
35 #include <linux/sched.h>
36
37 #include <asm/system.h>
38 #include <asm/hardware.h>
39 #include <asm/io.h>
40 #include <asm/leds.h>
41 #include <asm/irq.h>
42 #include <asm/mach/irq.h>
43 #include <asm/mach/time.h>
44 #include <asm/arch/clocks.h>
45
46 #ifndef __instrument
47 #define __instrument
48 #define __noinstrument __attribute__ ((no_instrument_function))
49 #endif
50
51 typedef struct {
52         u32 cntl;     /* CNTL_TIMER, R/W */
53         u32 load_tim; /* LOAD_TIM,   W */
54         u32 read_tim; /* READ_TIM,   R */
55 } mputimer_regs_t;
56
57 #define mputimer_base(n) \
58     ((volatile mputimer_regs_t*)IO_ADDRESS(OMAP_MPUTIMER_BASE + \
59                                  (n)*OMAP_MPUTIMER_OFFSET))
60
61 static inline unsigned long timer32k_read(int reg) {
62         unsigned long val;
63         val = omap_readw(reg + OMAP_32kHz_TIMER_BASE);
64         return val;
65 }
66 static inline void timer32k_write(int reg,int val) {
67         omap_writew(val, reg + OMAP_32kHz_TIMER_BASE);
68 }
69
70 /*
71  * How long is the timer interval? 100 HZ, right...
72  * IRQ rate = (TVR + 1) / 32768 seconds
73  * TVR = 32768 * IRQ_RATE -1
74  * IRQ_RATE =  1/100
75  * TVR = 326
76  */
77 #define TIMER32k_PERIOD 326
78 //#define TIMER32k_PERIOD 0x7ff
79
80 static inline void start_timer32k(void) {
81         timer32k_write(TIMER32k_CR,
82                        TIMER32k_TSS | TIMER32k_TRB |
83                        TIMER32k_INT | TIMER32k_ARL);
84 }
85
86 #ifdef CONFIG_MACH_OMAP_PERSEUS2
87 /*
88  * After programming PTV with 0 and setting the MPUTIM_CLOCK_ENABLE
89  * (external clock enable)  bit, the timer count rate is 6.5 MHz (13
90  * MHZ input/2). !! The divider by 2 is undocumented !!
91  */
92 #define MPUTICKS_PER_SEC (13000000/2)
93 #else
94 /*
95  * After programming PTV with 0, the timer count rate is 6 MHz.
96  * WARNING! this must be an even number, or machinecycles_to_usecs
97  * below will break.
98  */
99 #define MPUTICKS_PER_SEC  (12000000/2)
100 #endif
101
102 static int mputimer_started[3] = {0,0,0};
103
104 static inline void __noinstrument start_mputimer(int n,
105                                                  unsigned long load_val)
106 {
107         volatile mputimer_regs_t* timer = mputimer_base(n);
108
109         mputimer_started[n] = 0;
110         timer->cntl = MPUTIM_CLOCK_ENABLE;
111         udelay(1);
112
113         timer->load_tim = load_val;
114         udelay(1);
115         timer->cntl = (MPUTIM_CLOCK_ENABLE | MPUTIM_AR | MPUTIM_ST);
116         mputimer_started[n] = 1;
117 }
118
119 static inline unsigned long __noinstrument
120 read_mputimer(int n)
121 {
122         volatile mputimer_regs_t* timer = mputimer_base(n);
123         return (mputimer_started[n] ? timer->read_tim : 0);
124 }
125
126 void __noinstrument start_mputimer1(unsigned long load_val)
127 {
128         start_mputimer(0, load_val);
129 }
130 void __noinstrument start_mputimer2(unsigned long load_val)
131 {
132         start_mputimer(1, load_val);
133 }
134 void __noinstrument start_mputimer3(unsigned long load_val)
135 {
136         start_mputimer(2, load_val);
137 }
138
139 unsigned long __noinstrument read_mputimer1(void)
140 {
141         return read_mputimer(0);
142 }
143 unsigned long __noinstrument read_mputimer2(void)
144 {
145         return read_mputimer(1);
146 }
147 unsigned long __noinstrument read_mputimer3(void)
148 {
149         return read_mputimer(2);
150 }
151
152 unsigned long __noinstrument do_getmachinecycles(void)
153 {
154         return 0 - read_mputimer(0);
155 }
156
157 unsigned long __noinstrument machinecycles_to_usecs(unsigned long mputicks)
158 {
159         /* Round up to nearest usec */
160         return ((mputicks * 1000) / (MPUTICKS_PER_SEC / 2 / 1000) + 1) >> 1;
161 }
162
163 /*
164  * This marks the time of the last system timer interrupt
165  * that was *processed by the ISR* (timer 2).
166  */
167 static unsigned long systimer_mark;
168
169 static unsigned long omap_gettimeoffset(void)
170 {
171         /* Return elapsed usecs since last system timer ISR */
172         return machinecycles_to_usecs(do_getmachinecycles() - systimer_mark);
173 }
174
175 static irqreturn_t
176 omap_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
177 {
178         unsigned long now, ilatency;
179
180         /*
181          * Mark the time at which the timer interrupt ocurred using
182          * timer1. We need to remove interrupt latency, which we can
183          * retrieve from the current system timer2 counter. Both the
184          * offset timer1 and the system timer2 are counting at 6MHz,
185          * so we're ok.
186          */
187         now = 0 - read_mputimer1();
188         ilatency = MPUTICKS_PER_SEC / 100 - read_mputimer2();
189         systimer_mark = now - ilatency;
190
191         timer_tick(regs);
192
193         return IRQ_HANDLED;
194 }
195
196 static struct irqaction omap_timer_irq = {
197         .name           = "OMAP Timer Tick",
198         .flags          = SA_INTERRUPT,
199         .handler        = omap_timer_interrupt
200 };
201
202 void __init omap_init_time(void)
203 {
204         /* Since we don't call request_irq, we must init the structure */
205         gettimeoffset = omap_gettimeoffset;
206
207 #ifdef OMAP1510_USE_32KHZ_TIMER
208         timer32k_write(TIMER32k_CR, 0x0);
209         timer32k_write(TIMER32k_TVR,TIMER32k_PERIOD);
210         setup_irq(INT_OS_32kHz_TIMER, &omap_timer_irq);
211         start_timer32k();
212 #else
213         setup_irq(INT_TIMER2, &omap_timer_irq);
214         start_mputimer2(MPUTICKS_PER_SEC / 100 - 1);
215 #endif
216 }
217