patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-arm / arch-omap / time.h
1 /*
2  * linux/include/asm-arm/arch-omap/time.h
3  *
4  * 32kHz timer definition
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 #if !defined(__ASM_ARCH_OMAP_TIME_H)
30 #define __ASM_ARCH_OMAP_TIME_H
31
32 #include <linux/config.h>
33 #include <linux/delay.h>
34 #include <asm/system.h>
35 #include <asm/hardware.h>
36 #include <asm/io.h>
37 #include <asm/leds.h>
38 #include <asm/irq.h>
39 #include <asm/mach/irq.h>
40 #include <asm/arch/clocks.h>
41
42 #ifndef __instrument
43 #define __instrument
44 #define __noinstrument __attribute__ ((no_instrument_function))
45 #endif
46
47 typedef struct {
48         u32 cntl;     /* CNTL_TIMER, R/W */
49         u32 load_tim; /* LOAD_TIM,   W */
50         u32 read_tim; /* READ_TIM,   R */
51 } mputimer_regs_t;
52
53 #define mputimer_base(n) \
54     ((volatile mputimer_regs_t*)IO_ADDRESS(OMAP_MPUTIMER_BASE + \
55                                  (n)*OMAP_MPUTIMER_OFFSET))
56
57 static inline unsigned long timer32k_read(int reg) {
58         unsigned long val;
59         val = omap_readw(reg + OMAP_32kHz_TIMER_BASE);
60         return val;
61 }
62 static inline void timer32k_write(int reg,int val) {
63         omap_writew(val, reg + OMAP_32kHz_TIMER_BASE);
64 }
65
66 /*
67  * How long is the timer interval? 100 HZ, right...
68  * IRQ rate = (TVR + 1) / 32768 seconds
69  * TVR = 32768 * IRQ_RATE -1
70  * IRQ_RATE =  1/100
71  * TVR = 326
72  */
73 #define TIMER32k_PERIOD 326
74 //#define TIMER32k_PERIOD 0x7ff
75
76 static inline void start_timer32k(void) {
77         timer32k_write(TIMER32k_CR,
78                        TIMER32k_TSS | TIMER32k_TRB |
79                        TIMER32k_INT | TIMER32k_ARL);
80 }
81
82 #ifdef CONFIG_MACH_OMAP_PERSEUS2
83 /*
84  * After programming PTV with 0 and setting the MPUTIM_CLOCK_ENABLE
85  * (external clock enable)  bit, the timer count rate is 6.5 MHz (13
86  * MHZ input/2). !! The divider by 2 is undocumented !!
87  */
88 #define MPUTICKS_PER_SEC (13000000/2)
89 #else
90 /*
91  * After programming PTV with 0, the timer count rate is 6 MHz.
92  * WARNING! this must be an even number, or machinecycles_to_usecs
93  * below will break.
94  */
95 #define MPUTICKS_PER_SEC  (12000000/2)
96 #endif
97
98 static int mputimer_started[3] = {0,0,0};
99
100 static inline void __noinstrument start_mputimer(int n,
101                                                  unsigned long load_val)
102 {
103         volatile mputimer_regs_t* timer = mputimer_base(n);
104
105         mputimer_started[n] = 0;
106         timer->cntl = MPUTIM_CLOCK_ENABLE;
107         udelay(1);
108
109         timer->load_tim = load_val;
110         udelay(1);
111         timer->cntl = (MPUTIM_CLOCK_ENABLE | MPUTIM_AR | MPUTIM_ST);
112         mputimer_started[n] = 1;
113 }
114
115 static inline unsigned long __noinstrument
116 read_mputimer(int n)
117 {
118         volatile mputimer_regs_t* timer = mputimer_base(n);
119         return (mputimer_started[n] ? timer->read_tim : 0);
120 }
121
122 void __noinstrument start_mputimer1(unsigned long load_val)
123 {
124         start_mputimer(0, load_val);
125 }
126 void __noinstrument start_mputimer2(unsigned long load_val)
127 {
128         start_mputimer(1, load_val);
129 }
130 void __noinstrument start_mputimer3(unsigned long load_val)
131 {
132         start_mputimer(2, load_val);
133 }
134
135 unsigned long __noinstrument read_mputimer1(void)
136 {
137         return read_mputimer(0);
138 }
139 unsigned long __noinstrument read_mputimer2(void)
140 {
141         return read_mputimer(1);
142 }
143 unsigned long __noinstrument read_mputimer3(void)
144 {
145         return read_mputimer(2);
146 }
147
148 unsigned long __noinstrument do_getmachinecycles(void)
149 {
150         return 0 - read_mputimer(0);
151 }
152
153 unsigned long __noinstrument machinecycles_to_usecs(unsigned long mputicks)
154 {
155         /* Round up to nearest usec */
156         return ((mputicks * 1000) / (MPUTICKS_PER_SEC / 2 / 1000) + 1) >> 1;
157 }
158
159 /*
160  * This marks the time of the last system timer interrupt
161  * that was *processed by the ISR* (timer 2).
162  */
163 static unsigned long systimer_mark;
164
165 static unsigned long omap1510_gettimeoffset(void)
166 {
167         /* Return elapsed usecs since last system timer ISR */
168         return machinecycles_to_usecs(do_getmachinecycles() - systimer_mark);
169 }
170
171 static irqreturn_t
172 omap1510_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
173 {
174         unsigned long now, ilatency;
175
176         /*
177          * Mark the time at which the timer interrupt ocurred using
178          * timer1. We need to remove interrupt latency, which we can
179          * retrieve from the current system timer2 counter. Both the
180          * offset timer1 and the system timer2 are counting at 6MHz,
181          * so we're ok.
182          */
183         now = 0 - read_mputimer1();
184         ilatency = MPUTICKS_PER_SEC / 100 - read_mputimer2();
185         systimer_mark = now - ilatency;
186
187         do_leds();
188         do_timer(regs);
189         do_profile(regs);
190
191         return IRQ_HANDLED;
192 }
193
194 void __init time_init(void)
195 {
196         /* Since we don't call request_irq, we must init the structure */
197         gettimeoffset = omap1510_gettimeoffset;
198
199         timer_irq.handler = omap1510_timer_interrupt;
200         timer_irq.flags = SA_INTERRUPT;
201 #ifdef OMAP1510_USE_32KHZ_TIMER
202         timer32k_write(TIMER32k_CR, 0x0);
203         timer32k_write(TIMER32k_TVR,TIMER32k_PERIOD);
204         setup_irq(INT_OS_32kHz_TIMER, &timer_irq);
205         start_timer32k();
206 #else
207         setup_irq(INT_TIMER2, &timer_irq);
208         start_mputimer2(MPUTICKS_PER_SEC / 100 - 1);
209 #endif
210 }
211
212 #endif