blob: d35e149f4ebfca0711d35ac14ffab617290be605 [file] [log] [blame]
Ben Dooksa21765a2007-02-11 18:31:01 +01001/* linux/arch/arm/plat-s3c24xx/time.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 2003-2005 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
22#include <linux/sched.h>
23#include <linux/init.h>
24#include <linux/interrupt.h>
Thomas Gleixner544b46d2006-07-01 22:32:39 +010025#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000027#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010028#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/system.h>
31#include <asm/leds.h>
32#include <asm/mach-types.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010035#include <mach/map.h>
Ben Dooksa2b7ba92008-10-07 22:26:09 +010036#include <plat/regs-timer.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/regs-irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/mach/time.h>
Ben Dooks9bc1aae2008-10-21 14:06:35 +010039#include <mach/tick.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Ben Dooksd5120ae2008-10-07 23:09:51 +010041#include <plat/clock.h>
Ben Dooksa2b7ba92008-10-07 22:26:09 +010042#include <plat/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static unsigned long timer_startval;
45static unsigned long timer_usec_ticks;
46
47#define TIMER_USEC_SHIFT 16
48
49/* we use the shifted arithmetic to work out the ratio of timer ticks
50 * to usecs, as often the peripheral clock is not a nice even multiple
51 * of 1MHz.
52 *
53 * shift of 14 and 15 are too low for the 12MHz, 16 seems to be ok
54 * for the current HZ value of 200 without producing overflows.
55 *
56 * Original patch by Dimitry Andric, updated by Ben Dooks
57*/
58
59
60/* timer_mask_usec_ticks
61 *
62 * given a clock and divisor, make the value to pass into timer_ticks_to_usec
63 * to scale the ticks into usecs
64*/
65
66static inline unsigned long
67timer_mask_usec_ticks(unsigned long scaler, unsigned long pclk)
68{
69 unsigned long den = pclk / 1000;
70
71 return ((1000 << TIMER_USEC_SHIFT) * scaler + (den >> 1)) / den;
72}
73
74/* timer_ticks_to_usec
75 *
76 * convert timer ticks to usec.
77*/
78
79static inline unsigned long timer_ticks_to_usec(unsigned long ticks)
80{
81 unsigned long res;
82
83 res = ticks * timer_usec_ticks;
84 res += 1 << (TIMER_USEC_SHIFT - 4); /* round up slightly */
85
86 return res >> TIMER_USEC_SHIFT;
87}
88
89/***
90 * Returns microsecond since last clock interrupt. Note that interrupts
91 * will have been disabled by do_gettimeoffset()
92 * IRQs are disabled before entering here from do_gettimeofday()
93 */
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static unsigned long s3c2410_gettimeoffset (void)
96{
97 unsigned long tdone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned long tval;
99
100 /* work out how many ticks have gone since last timer interrupt */
101
Ben Dooksb915a122008-10-21 14:06:53 +0100102 tval = __raw_readl(S3C2410_TCNTO(4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 tdone = timer_startval - tval;
104
105 /* check to see if there is an interrupt pending */
106
Ben Dooks9bc1aae2008-10-21 14:06:35 +0100107 if (s3c24xx_ostimer_pending()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* re-read the timer, and try and fix up for the missed
109 * interrupt. Note, the interrupt may go off before the
110 * timer has re-loaded from wrapping.
111 */
112
113 tval = __raw_readl(S3C2410_TCNTO(4));
114 tdone = timer_startval - tval;
115
116 if (tval != 0)
117 tdone += timer_startval;
118 }
119
120 return timer_ticks_to_usec(tdone);
121}
122
123
124/*
125 * IRQ handler for the timer
126 */
127static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700128s3c2410_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700130 timer_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return IRQ_HANDLED;
132}
133
134static struct irqaction s3c2410_timer_irq = {
135 .name = "S3C2410 Timer Tick",
Bernhard Walleb30faba2007-05-08 00:35:39 -0700136 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Russell King09b8b5f2005-06-26 17:06:36 +0100137 .handler = s3c2410_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Ben Dooks766636c2006-03-20 17:10:03 +0000140#define use_tclk1_12() ( \
141 machine_is_bast() || \
142 machine_is_vr1000() || \
143 machine_is_anubis() || \
Ben Dooksb915a122008-10-21 14:06:53 +0100144 machine_is_osiris())
Ben Dooks766636c2006-03-20 17:10:03 +0000145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
147 * Set up timer interrupt, and return the current time in seconds.
148 *
149 * Currently we only use timer4, as it is the only timer which has no
150 * other function that can be exploited externally
151 */
152static void s3c2410_timer_setup (void)
153{
154 unsigned long tcon;
155 unsigned long tcnt;
156 unsigned long tcfg1;
157 unsigned long tcfg0;
158
159 tcnt = 0xffff; /* default value for tcnt */
160
161 /* read the current timer configuration bits */
162
163 tcon = __raw_readl(S3C2410_TCON);
164 tcfg1 = __raw_readl(S3C2410_TCFG1);
165 tcfg0 = __raw_readl(S3C2410_TCFG0);
166
167 /* configure the system for whichever machine is in use */
168
Ben Dooks766636c2006-03-20 17:10:03 +0000169 if (use_tclk1_12()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* timer is at 12MHz, scaler is 1 */
171 timer_usec_ticks = timer_mask_usec_ticks(1, 12000000);
172 tcnt = 12000000 / HZ;
173
174 tcfg1 &= ~S3C2410_TCFG1_MUX4_MASK;
175 tcfg1 |= S3C2410_TCFG1_MUX4_TCLK1;
176 } else {
177 unsigned long pclk;
178 struct clk *clk;
179
180 /* for the h1940 (and others), we use the pclk from the core
181 * to generate the timer values. since values around 50 to
182 * 70MHz are not values we can directly generate the timer
183 * value from, we need to pre-scale and divide before using it.
184 *
185 * for instance, using 50.7MHz and dividing by 6 gives 8.45MHz
186 * (8.45 ticks per usec)
187 */
188
189 /* this is used as default if no other timer can be found */
190
191 clk = clk_get(NULL, "timers");
192 if (IS_ERR(clk))
193 panic("failed to get clock for system timer");
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 clk_enable(clk);
196
197 pclk = clk_get_rate(clk);
198
199 /* configure clock tick */
200
201 timer_usec_ticks = timer_mask_usec_ticks(6, pclk);
202
203 tcfg1 &= ~S3C2410_TCFG1_MUX4_MASK;
204 tcfg1 |= S3C2410_TCFG1_MUX4_DIV2;
205
206 tcfg0 &= ~S3C2410_TCFG_PRESCALER1_MASK;
207 tcfg0 |= ((6 - 1) / 2) << S3C2410_TCFG_PRESCALER1_SHIFT;
208
209 tcnt = (pclk / 6) / HZ;
210 }
211
212 /* timers reload after counting zero, so reduce the count by 1 */
213
214 tcnt--;
215
Ben Dooksb915a122008-10-21 14:06:53 +0100216 printk(KERN_DEBUG "timer tcon=%08lx, tcnt %04lx, tcfg %08lx,%08lx, usec %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 tcon, tcnt, tcfg0, tcfg1, timer_usec_ticks);
218
219 /* check to see if timer is within 16bit range... */
220 if (tcnt > 0xffff) {
221 panic("setup_timer: HZ is too small, cannot configure timer!");
222 return;
223 }
224
225 __raw_writel(tcfg1, S3C2410_TCFG1);
226 __raw_writel(tcfg0, S3C2410_TCFG0);
227
228 timer_startval = tcnt;
229 __raw_writel(tcnt, S3C2410_TCNTB(4));
230
231 /* ensure timer is stopped... */
232
233 tcon &= ~(7<<20);
234 tcon |= S3C2410_TCON_T4RELOAD;
235 tcon |= S3C2410_TCON_T4MANUALUPD;
236
237 __raw_writel(tcon, S3C2410_TCON);
238 __raw_writel(tcnt, S3C2410_TCNTB(4));
239 __raw_writel(tcnt, S3C2410_TCMPB(4));
240
241 /* start the timer running */
242 tcon |= S3C2410_TCON_T4START;
243 tcon &= ~S3C2410_TCON_T4MANUALUPD;
244 __raw_writel(tcon, S3C2410_TCON);
245}
246
Ben Dooksb915a122008-10-21 14:06:53 +0100247static void __init s3c2410_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 s3c2410_timer_setup();
250 setup_irq(IRQ_TIMER4, &s3c2410_timer_irq);
251}
252
253struct sys_timer s3c24xx_timer = {
254 .init = s3c2410_timer_init,
255 .offset = s3c2410_gettimeoffset,
256 .resume = s3c2410_timer_setup
257};