blob: 3b27b29da4787e452f76c2c2f5573e3605e06df6 [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>
Ben Dooks9d325f22008-11-21 10:36:05 +000029#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/system.h>
32#include <asm/leds.h>
33#include <asm/mach-types.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010036#include <mach/map.h>
Ben Dooksa2b7ba92008-10-07 22:26:09 +010037#include <plat/regs-timer.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010038#include <mach/regs-irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/mach/time.h>
Ben Dooks9bc1aae2008-10-21 14:06:35 +010040#include <mach/tick.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ben Dooksd5120ae2008-10-07 23:09:51 +010042#include <plat/clock.h>
Ben Dooksa2b7ba92008-10-07 22:26:09 +010043#include <plat/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static unsigned long timer_startval;
46static unsigned long timer_usec_ticks;
47
Ben Dooksc652d2d2008-10-21 14:07:01 +010048#ifndef TICK_MAX
49#define TICK_MAX (0xffff)
50#endif
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define TIMER_USEC_SHIFT 16
53
54/* we use the shifted arithmetic to work out the ratio of timer ticks
55 * to usecs, as often the peripheral clock is not a nice even multiple
56 * of 1MHz.
57 *
58 * shift of 14 and 15 are too low for the 12MHz, 16 seems to be ok
59 * for the current HZ value of 200 without producing overflows.
60 *
61 * Original patch by Dimitry Andric, updated by Ben Dooks
62*/
63
64
65/* timer_mask_usec_ticks
66 *
67 * given a clock and divisor, make the value to pass into timer_ticks_to_usec
68 * to scale the ticks into usecs
69*/
70
71static inline unsigned long
72timer_mask_usec_ticks(unsigned long scaler, unsigned long pclk)
73{
74 unsigned long den = pclk / 1000;
75
76 return ((1000 << TIMER_USEC_SHIFT) * scaler + (den >> 1)) / den;
77}
78
79/* timer_ticks_to_usec
80 *
81 * convert timer ticks to usec.
82*/
83
84static inline unsigned long timer_ticks_to_usec(unsigned long ticks)
85{
86 unsigned long res;
87
88 res = ticks * timer_usec_ticks;
89 res += 1 << (TIMER_USEC_SHIFT - 4); /* round up slightly */
90
91 return res >> TIMER_USEC_SHIFT;
92}
93
94/***
95 * Returns microsecond since last clock interrupt. Note that interrupts
96 * will have been disabled by do_gettimeoffset()
97 * IRQs are disabled before entering here from do_gettimeofday()
98 */
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static unsigned long s3c2410_gettimeoffset (void)
101{
102 unsigned long tdone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long tval;
104
105 /* work out how many ticks have gone since last timer interrupt */
106
Ben Dooksb915a122008-10-21 14:06:53 +0100107 tval = __raw_readl(S3C2410_TCNTO(4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 tdone = timer_startval - tval;
109
110 /* check to see if there is an interrupt pending */
111
Ben Dooks9bc1aae2008-10-21 14:06:35 +0100112 if (s3c24xx_ostimer_pending()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* re-read the timer, and try and fix up for the missed
114 * interrupt. Note, the interrupt may go off before the
115 * timer has re-loaded from wrapping.
116 */
117
118 tval = __raw_readl(S3C2410_TCNTO(4));
119 tdone = timer_startval - tval;
120
121 if (tval != 0)
122 tdone += timer_startval;
123 }
124
125 return timer_ticks_to_usec(tdone);
126}
127
128
129/*
130 * IRQ handler for the timer
131 */
132static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700133s3c2410_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700135 timer_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return IRQ_HANDLED;
137}
138
139static struct irqaction s3c2410_timer_irq = {
140 .name = "S3C2410 Timer Tick",
Bernhard Walleb30faba2007-05-08 00:35:39 -0700141 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Russell King09b8b5f2005-06-26 17:06:36 +0100142 .handler = s3c2410_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
Ben Dooks766636c2006-03-20 17:10:03 +0000145#define use_tclk1_12() ( \
146 machine_is_bast() || \
147 machine_is_vr1000() || \
148 machine_is_anubis() || \
Ben Dooksb915a122008-10-21 14:06:53 +0100149 machine_is_osiris())
Ben Dooks766636c2006-03-20 17:10:03 +0000150
Ben Dooks9d325f22008-11-21 10:36:05 +0000151static struct clk *tin;
152static struct clk *tdiv;
153static struct clk *timerclk;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
156 * Set up timer interrupt, and return the current time in seconds.
157 *
158 * Currently we only use timer4, as it is the only timer which has no
159 * other function that can be exploited externally
160 */
161static void s3c2410_timer_setup (void)
162{
163 unsigned long tcon;
164 unsigned long tcnt;
165 unsigned long tcfg1;
166 unsigned long tcfg0;
167
Ben Dooksc652d2d2008-10-21 14:07:01 +0100168 tcnt = TICK_MAX; /* default value for tcnt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* configure the system for whichever machine is in use */
171
Ben Dooks766636c2006-03-20 17:10:03 +0000172 if (use_tclk1_12()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* timer is at 12MHz, scaler is 1 */
174 timer_usec_ticks = timer_mask_usec_ticks(1, 12000000);
175 tcnt = 12000000 / HZ;
176
Ben Dooks9d325f22008-11-21 10:36:05 +0000177 tcfg1 = __raw_readl(S3C2410_TCFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 tcfg1 &= ~S3C2410_TCFG1_MUX4_MASK;
179 tcfg1 |= S3C2410_TCFG1_MUX4_TCLK1;
Ben Dooks9d325f22008-11-21 10:36:05 +0000180 __raw_writel(tcfg1, S3C2410_TCFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 } else {
182 unsigned long pclk;
Ben Dooks9d325f22008-11-21 10:36:05 +0000183 struct clk *tscaler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /* for the h1940 (and others), we use the pclk from the core
186 * to generate the timer values. since values around 50 to
187 * 70MHz are not values we can directly generate the timer
188 * value from, we need to pre-scale and divide before using it.
189 *
190 * for instance, using 50.7MHz and dividing by 6 gives 8.45MHz
191 * (8.45 ticks per usec)
192 */
193
Ben Dooks9d325f22008-11-21 10:36:05 +0000194 pclk = clk_get_rate(timerclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 /* configure clock tick */
197
198 timer_usec_ticks = timer_mask_usec_ticks(6, pclk);
199
Ben Dooks9d325f22008-11-21 10:36:05 +0000200 tscaler = clk_get_parent(tdiv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Ben Dooks9d325f22008-11-21 10:36:05 +0000202 clk_set_rate(tscaler, pclk / 3);
203 clk_set_rate(tdiv, pclk / 6);
204 clk_set_parent(tin, tdiv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Ben Dooks9d325f22008-11-21 10:36:05 +0000206 tcnt = clk_get_rate(tin) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
Ben Dooks9d325f22008-11-21 10:36:05 +0000209 tcon = __raw_readl(S3C2410_TCON);
210 tcfg0 = __raw_readl(S3C2410_TCFG0);
211 tcfg1 = __raw_readl(S3C2410_TCFG1);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* timers reload after counting zero, so reduce the count by 1 */
214
215 tcnt--;
216
Ben Dooksb915a122008-10-21 14:06:53 +0100217 printk(KERN_DEBUG "timer tcon=%08lx, tcnt %04lx, tcfg %08lx,%08lx, usec %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 tcon, tcnt, tcfg0, tcfg1, timer_usec_ticks);
219
220 /* check to see if timer is within 16bit range... */
Ben Dooksc652d2d2008-10-21 14:07:01 +0100221 if (tcnt > TICK_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 panic("setup_timer: HZ is too small, cannot configure timer!");
223 return;
224 }
225
226 __raw_writel(tcfg1, S3C2410_TCFG1);
227 __raw_writel(tcfg0, S3C2410_TCFG0);
228
229 timer_startval = tcnt;
230 __raw_writel(tcnt, S3C2410_TCNTB(4));
231
232 /* ensure timer is stopped... */
233
234 tcon &= ~(7<<20);
235 tcon |= S3C2410_TCON_T4RELOAD;
236 tcon |= S3C2410_TCON_T4MANUALUPD;
237
238 __raw_writel(tcon, S3C2410_TCON);
239 __raw_writel(tcnt, S3C2410_TCNTB(4));
240 __raw_writel(tcnt, S3C2410_TCMPB(4));
241
242 /* start the timer running */
243 tcon |= S3C2410_TCON_T4START;
244 tcon &= ~S3C2410_TCON_T4MANUALUPD;
245 __raw_writel(tcon, S3C2410_TCON);
246}
247
Ben Dooks9d325f22008-11-21 10:36:05 +0000248static void __init s3c2410_timer_resources(void)
249{
250 struct platform_device tmpdev;
251
252 tmpdev.dev.bus = &platform_bus_type;
253 tmpdev.id = 4;
254
255 timerclk = clk_get(NULL, "timers");
256 if (IS_ERR(timerclk))
257 panic("failed to get clock for system timer");
258
259 clk_enable(timerclk);
260
261 if (!use_tclk1_12()) {
262 tin = clk_get(&tmpdev.dev, "pwm-tin");
263 if (IS_ERR(tin))
264 panic("failed to get pwm-tin clock for system timer");
265
266 tdiv = clk_get(&tmpdev.dev, "pwm-tdiv");
267 if (IS_ERR(tdiv))
268 panic("failed to get pwm-tdiv clock for system timer");
269 }
270
271 clk_enable(tin);
272}
273
Ben Dooksb915a122008-10-21 14:06:53 +0100274static void __init s3c2410_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Ben Dooks9d325f22008-11-21 10:36:05 +0000276 s3c2410_timer_resources();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 s3c2410_timer_setup();
278 setup_irq(IRQ_TIMER4, &s3c2410_timer_irq);
279}
280
281struct sys_timer s3c24xx_timer = {
282 .init = s3c2410_timer_init,
283 .offset = s3c2410_gettimeoffset,
284 .resume = s3c2410_timer_setup
285};