vserver 1.9.3
[linux-2.6.git] / arch / arm / mach-s3c2410 / time.c
1 /* linux/include/asm-arm/arch-s3c2410/time.h
2  *
3  *  Copyright (C) 2003 Simtec Electronics <linux@simtec.co.uk>
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 static unsigned long timer_startval;
38 static unsigned long timer_ticks_usec;
39
40
41 /* with an 12MHz clock, we get 12 ticks per-usec
42  */
43
44
45 /***
46  * Returns microsecond  since last clock interrupt.  Note that interrupts
47  * will have been disabled by do_gettimeoffset()
48  * IRQs are disabled before entering here from do_gettimeofday()
49  */
50
51 #define SRCPND_TIMER4 (1<<(IRQ_TIMER4 - IRQ_EINT0))
52
53 static unsigned long s3c2410_gettimeoffset (void)
54 {
55         unsigned long tdone;
56         unsigned long usec;
57         unsigned long irqpend;
58
59         /* work out how many ticks have gone since last timer interrupt */
60
61         tdone = timer_startval - __raw_readl(S3C2410_TCNTO(4));
62
63         /* check to see if there is an interrupt pending */
64
65         irqpend = __raw_readl(S3C2410_SRCPND);
66         if (irqpend & SRCPND_TIMER4) {
67                 /* re-read the timer, and try and fix up for the missed
68                  * interrupt */
69
70                 tdone = timer_startval - __raw_readl(S3C2410_TCNTO(4));
71                 tdone += 1<<16;
72         }
73
74         /* currently, tcnt is in 12MHz units, but this may change
75          * for non-bast machines...
76          */
77
78         usec = tdone / timer_ticks_usec;
79
80         return usec;
81 }
82
83
84 /*
85  * IRQ handler for the timer
86  */
87 static irqreturn_t
88 s3c2410_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
89 {
90         timer_tick(regs);
91
92         return IRQ_HANDLED;
93 }
94
95 static struct irqaction s3c2410_timer_irq = {
96         .name           = "S32410 Timer Tick",
97         .flags          = SA_INTERRUPT,
98         .handler        = s3c2410_timer_interrupt
99 };
100
101 /*
102  * Set up timer interrupt, and return the current time in seconds.
103  *
104  * Currently we only use timer4, as it is the only timer which has no
105  * other function that can be exploited externally
106  */
107 void __init s3c2410_init_time (void)
108 {
109         unsigned long tcon;
110         unsigned long tcnt;
111         unsigned long tcfg1;
112         unsigned long tcfg0;
113
114         gettimeoffset = s3c2410_gettimeoffset;
115
116         tcnt = 0xffff;  /* default value for tcnt */
117
118         /* read the current timer configuration bits */
119
120         tcon = __raw_readl(S3C2410_TCON);
121         tcfg1 = __raw_readl(S3C2410_TCFG1);
122         tcfg0 = __raw_readl(S3C2410_TCFG0);
123
124         /* configure the system for whichever machine is in use */
125
126         if (machine_is_bast() || machine_is_vr1000()) {
127                 timer_ticks_usec = 12;        /* timer is at 12MHz */
128                 tcnt = (timer_ticks_usec * (1000*1000)) / HZ;
129         }
130
131         /* for the h1940, we use the pclk from the core to generate
132          * the timer values. since 67.5MHz is not a value we can directly
133          * generate the timer value from, we need to pre-scale and
134          * divied before using it.
135          *
136          * overall divsior to get 200Hz is 337500
137          *   we can fit tcnt if we pre-scale by 6, producing a tick rate
138          *   of 11.25MHz, and a tcnt of 56250.
139          */
140
141         if (machine_is_h1940() || machine_is_smdk2410() ) {
142                 timer_ticks_usec = s3c2410_pclk / (1000*1000);
143                 timer_ticks_usec /= 6;
144
145                 tcfg1 &= ~S3C2410_TCFG1_MUX4_MASK;
146                 tcfg1 |= S3C2410_TCFG1_MUX4_DIV2;
147
148                 tcfg0 &= ~S3C2410_TCFG_PRESCALER1_MASK;
149                 tcfg0 |= ((6 - 1) / 2) << S3C2410_TCFG_PRESCALER1_SHIFT;
150
151                 tcnt = (s3c2410_pclk / 6) / HZ;
152         }
153
154
155         printk("setup_timer tcon=%08lx, tcnt %04lx, tcfg %08lx,%08lx\n",
156                tcon, tcnt, tcfg0, tcfg1);
157
158         /* check to see if timer is within 16bit range... */
159         if (tcnt > 0xffff) {
160                 panic("setup_timer: HZ is too small, cannot configure timer!");
161                 return;
162         }
163
164         __raw_writel(tcfg1, S3C2410_TCFG1);
165         __raw_writel(tcfg0, S3C2410_TCFG0);
166
167         timer_startval = tcnt;
168         __raw_writel(tcnt, S3C2410_TCNTB(4));
169
170         /* ensure timer is stopped... */
171
172         tcon &= ~(7<<20);
173         tcon |= S3C2410_TCON_T4RELOAD;
174         tcon |= S3C2410_TCON_T4MANUALUPD;
175
176         __raw_writel(tcon, S3C2410_TCON);
177         __raw_writel(tcnt, S3C2410_TCNTB(4));
178         __raw_writel(tcnt, S3C2410_TCMPB(4));
179
180         setup_irq(IRQ_TIMER4, &s3c2410_timer_irq);
181
182         /* start the timer running */
183         tcon |= S3C2410_TCON_T4START;
184         tcon &= ~S3C2410_TCON_T4MANUALUPD;
185         __raw_writel(tcon, S3C2410_TCON);
186 }
187
188
189