vserver 1.9.5.x5
[linux-2.6.git] / arch / arm / mach-s3c2410 / time.c
1 /* linux/arch/arm/mach-s3c2410/time.c
2  *
3  * Copyright (C) 2003,2004 Simtec Electronics
4  *      Ben Dooks, <ben@simtec.co.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <linux/config.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <asm/system.h>
27 #include <asm/leds.h>
28 #include <asm/mach-types.h>
29
30 #include <asm/io.h>
31 #include <asm/irq.h>
32 #include <asm/arch/map.h>
33 #include <asm/arch/regs-timer.h>
34 #include <asm/arch/regs-irq.h>
35 #include <asm/mach/time.h>
36
37 #include "clock.h"
38
39 static unsigned long timer_startval;
40 static unsigned long timer_usec_ticks;
41
42 #define TIMER_USEC_SHIFT 16
43
44 /* we use the shifted arithmetic to work out the ratio of timer ticks
45  * to usecs, as often the peripheral clock is not a nice even multiple
46  * of 1MHz.
47  *
48  * shift of 14 and 15 are too low for the 12MHz, 16 seems to be ok
49  * for the current HZ value of 200 without producing overflows.
50  *
51  * Original patch by Dimitry Andric, updated by Ben Dooks
52 */
53
54
55 /* timer_mask_usec_ticks
56  *
57  * given a clock and divisor, make the value to pass into timer_ticks_to_usec
58  * to scale the ticks into usecs
59 */
60
61 static inline unsigned long
62 timer_mask_usec_ticks(unsigned long scaler, unsigned long pclk)
63 {
64         unsigned long den = pclk / 1000;
65
66         return ((1000 << TIMER_USEC_SHIFT) * scaler + (den >> 1)) / den;
67 }
68
69 /* timer_ticks_to_usec
70  *
71  * convert timer ticks to usec.
72 */
73
74 static inline unsigned long timer_ticks_to_usec(unsigned long ticks)
75 {
76         unsigned long res;
77
78         res = ticks * timer_usec_ticks;
79         res += 1 << (TIMER_USEC_SHIFT - 4);     /* round up slightly */
80
81         return res >> TIMER_USEC_SHIFT;
82 }
83
84 /***
85  * Returns microsecond  since last clock interrupt.  Note that interrupts
86  * will have been disabled by do_gettimeoffset()
87  * IRQs are disabled before entering here from do_gettimeofday()
88  */
89
90 #define SRCPND_TIMER4 (1<<(IRQ_TIMER4 - IRQ_EINT0))
91
92 static unsigned long s3c2410_gettimeoffset (void)
93 {
94         unsigned long tdone;
95         unsigned long irqpend;
96         unsigned long tval;
97
98         /* work out how many ticks have gone since last timer interrupt */
99
100         tval =  __raw_readl(S3C2410_TCNTO(4));
101         tdone = timer_startval - tval;
102
103         /* check to see if there is an interrupt pending */
104
105         irqpend = __raw_readl(S3C2410_SRCPND);
106         if (irqpend & SRCPND_TIMER4) {
107                 /* re-read the timer, and try and fix up for the missed
108                  * interrupt. Note, the interrupt may go off before the
109                  * timer has re-loaded from wrapping.
110                  */
111
112                 tval =  __raw_readl(S3C2410_TCNTO(4));
113                 tdone = timer_startval - tval;
114
115                 if (tval != 0)
116                         tdone += timer_startval;
117         }
118
119         return timer_ticks_to_usec(tdone);
120 }
121
122
123 /*
124  * IRQ handler for the timer
125  */
126 static irqreturn_t
127 s3c2410_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
128 {
129         write_seqlock(&xtime_lock);
130         timer_tick(regs);
131         write_sequnlock(&xtime_lock);
132         return IRQ_HANDLED;
133 }
134
135 static struct irqaction s3c2410_timer_irq = {
136         .name           = "S3C2410 Timer Tick",
137         .flags          = SA_INTERRUPT,
138         .handler        = s3c2410_timer_interrupt
139 };
140
141 /*
142  * Set up timer interrupt, and return the current time in seconds.
143  *
144  * Currently we only use timer4, as it is the only timer which has no
145  * other function that can be exploited externally
146  */
147 static void s3c2410_timer_setup (void)
148 {
149         unsigned long tcon;
150         unsigned long tcnt;
151         unsigned long tcfg1;
152         unsigned long tcfg0;
153
154         tcnt = 0xffff;  /* default value for tcnt */
155
156         /* read the current timer configuration bits */
157
158         tcon = __raw_readl(S3C2410_TCON);
159         tcfg1 = __raw_readl(S3C2410_TCFG1);
160         tcfg0 = __raw_readl(S3C2410_TCFG0);
161
162         /* configure the system for whichever machine is in use */
163
164         if (machine_is_bast() || machine_is_vr1000()) {
165                 /* timer is at 12MHz, scaler is 1 */
166                 timer_usec_ticks = timer_mask_usec_ticks(1, 12000000);
167                 tcnt = 12000000 / HZ;
168
169                 tcfg1 &= ~S3C2410_TCFG1_MUX4_MASK;
170                 tcfg1 |= S3C2410_TCFG1_MUX4_TCLK1;
171         } else {
172                 /* for the h1940 (and others), we use the pclk from the core
173                  * to generate the timer values. since values around 50 to
174                  * 70MHz are not values we can directly generate the timer
175                  * value from, we need to pre-scale and divide before using it.
176                  *
177                  * for instance, using 50.7MHz and dividing by 6 gives 8.45MHz
178                  * (8.45 ticks per usec)
179                  */
180
181                 /* this is used as default if no other timer can be found */
182
183                 timer_usec_ticks = timer_mask_usec_ticks(6, s3c24xx_pclk);
184
185                 tcfg1 &= ~S3C2410_TCFG1_MUX4_MASK;
186                 tcfg1 |= S3C2410_TCFG1_MUX4_DIV2;
187
188                 tcfg0 &= ~S3C2410_TCFG_PRESCALER1_MASK;
189                 tcfg0 |= ((6 - 1) / 2) << S3C2410_TCFG_PRESCALER1_SHIFT;
190
191                 tcnt = (s3c24xx_pclk / 6) / HZ;
192         }
193
194         /* timers reload after counting zero, so reduce the count by 1 */
195
196         tcnt--;
197
198         printk("timer tcon=%08lx, tcnt %04lx, tcfg %08lx,%08lx, usec %08lx\n",
199                tcon, tcnt, tcfg0, tcfg1, timer_usec_ticks);
200
201         /* check to see if timer is within 16bit range... */
202         if (tcnt > 0xffff) {
203                 panic("setup_timer: HZ is too small, cannot configure timer!");
204                 return;
205         }
206
207         __raw_writel(tcfg1, S3C2410_TCFG1);
208         __raw_writel(tcfg0, S3C2410_TCFG0);
209
210         timer_startval = tcnt;
211         __raw_writel(tcnt, S3C2410_TCNTB(4));
212
213         /* ensure timer is stopped... */
214
215         tcon &= ~(7<<20);
216         tcon |= S3C2410_TCON_T4RELOAD;
217         tcon |= S3C2410_TCON_T4MANUALUPD;
218
219         __raw_writel(tcon, S3C2410_TCON);
220         __raw_writel(tcnt, S3C2410_TCNTB(4));
221         __raw_writel(tcnt, S3C2410_TCMPB(4));
222
223         /* start the timer running */
224         tcon |= S3C2410_TCON_T4START;
225         tcon &= ~S3C2410_TCON_T4MANUALUPD;
226         __raw_writel(tcon, S3C2410_TCON);
227 }
228
229 static void __init s3c2410_timer_init (void)
230 {
231         s3c2410_timer_setup();
232         setup_irq(IRQ_TIMER4, &s3c2410_timer_irq);
233 }
234
235 struct sys_timer s3c24xx_timer = {
236         .init           = s3c2410_timer_init,
237         .offset         = s3c2410_gettimeoffset,
238         .resume         = s3c2410_timer_setup
239 };