2 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com/
5 * samsung - Common hr-timer support (s3c and s5p)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/err.h>
15 #include <linux/clk.h>
16 #include <linux/clockchips.h>
17 #include <linux/platform_device.h>
19 #include <asm/smp_twd.h>
20 #include <asm/mach/time.h>
21 #include <asm/mach/arch.h>
22 #include <asm/mach/map.h>
23 #include <asm/sched_clock.h>
26 #include <plat/devs.h>
27 #include <plat/regs-timer.h>
28 #include <plat/samsung-time.h>
30 static struct clk
*tin_event
;
31 static struct clk
*tin_source
;
32 static struct clk
*tdiv_event
;
33 static struct clk
*tdiv_source
;
34 static struct clk
*timerclk
;
35 static struct samsung_timer_source timer_source
;
36 static unsigned long clock_count_per_tick
;
37 static void samsung_timer_resume(void);
39 static void samsung_time_stop(enum samsung_timer_mode mode
)
43 tcon
= __raw_readl(S3C2410_TCON
);
47 tcon
&= ~S3C2410_TCON_T0START
;
51 tcon
&= ~S3C2410_TCON_T1START
;
55 tcon
&= ~S3C2410_TCON_T2START
;
59 tcon
&= ~S3C2410_TCON_T3START
;
63 tcon
&= ~S3C2410_TCON_T4START
;
67 printk(KERN_ERR
"Invalid Timer %d\n", mode
);
70 __raw_writel(tcon
, S3C2410_TCON
);
73 static void samsung_time_setup(enum samsung_timer_mode mode
, unsigned long tcnt
)
77 tcon
= __raw_readl(S3C2410_TCON
);
84 tcon
|= S3C2410_TCON_T0MANUALUPD
;
89 tcon
|= S3C2410_TCON_T1MANUALUPD
;
93 tcon
&= ~(0x0f << 12);
94 tcon
|= S3C2410_TCON_T2MANUALUPD
;
98 tcon
&= ~(0x0f << 16);
99 tcon
|= S3C2410_TCON_T3MANUALUPD
;
103 tcon
&= ~(0x07 << 20);
104 tcon
|= S3C2410_TCON_T4MANUALUPD
;
108 printk(KERN_ERR
"Invalid Timer %d\n", mode
);
112 __raw_writel(tcnt
, S3C2410_TCNTB(mode
));
113 __raw_writel(tcnt
, S3C2410_TCMPB(mode
));
114 __raw_writel(tcon
, S3C2410_TCON
);
117 static void samsung_time_start(enum samsung_timer_mode mode
, bool periodic
)
121 tcon
= __raw_readl(S3C2410_TCON
);
125 tcon
|= S3C2410_TCON_T0START
;
126 tcon
&= ~S3C2410_TCON_T0MANUALUPD
;
129 tcon
|= S3C2410_TCON_T0RELOAD
;
131 tcon
&= ~S3C2410_TCON_T0RELOAD
;
135 tcon
|= S3C2410_TCON_T1START
;
136 tcon
&= ~S3C2410_TCON_T1MANUALUPD
;
139 tcon
|= S3C2410_TCON_T1RELOAD
;
141 tcon
&= ~S3C2410_TCON_T1RELOAD
;
145 tcon
|= S3C2410_TCON_T2START
;
146 tcon
&= ~S3C2410_TCON_T2MANUALUPD
;
149 tcon
|= S3C2410_TCON_T2RELOAD
;
151 tcon
&= ~S3C2410_TCON_T2RELOAD
;
155 tcon
|= S3C2410_TCON_T3START
;
156 tcon
&= ~S3C2410_TCON_T3MANUALUPD
;
159 tcon
|= S3C2410_TCON_T3RELOAD
;
161 tcon
&= ~S3C2410_TCON_T3RELOAD
;
165 tcon
|= S3C2410_TCON_T4START
;
166 tcon
&= ~S3C2410_TCON_T4MANUALUPD
;
169 tcon
|= S3C2410_TCON_T4RELOAD
;
171 tcon
&= ~S3C2410_TCON_T4RELOAD
;
175 printk(KERN_ERR
"Invalid Timer %d\n", mode
);
178 __raw_writel(tcon
, S3C2410_TCON
);
181 static int samsung_set_next_event(unsigned long cycles
,
182 struct clock_event_device
*evt
)
184 samsung_time_setup(timer_source
.event_id
, cycles
);
185 samsung_time_start(timer_source
.event_id
, NON_PERIODIC
);
190 static void samsung_set_mode(enum clock_event_mode mode
,
191 struct clock_event_device
*evt
)
193 samsung_time_stop(timer_source
.event_id
);
196 case CLOCK_EVT_MODE_PERIODIC
:
197 samsung_time_setup(timer_source
.event_id
, clock_count_per_tick
);
198 samsung_time_start(timer_source
.event_id
, PERIODIC
);
201 case CLOCK_EVT_MODE_ONESHOT
:
204 case CLOCK_EVT_MODE_UNUSED
:
205 case CLOCK_EVT_MODE_SHUTDOWN
:
208 case CLOCK_EVT_MODE_RESUME
:
209 samsung_timer_resume();
214 static void samsung_timer_resume(void)
216 /* event timer restart */
217 samsung_time_setup(timer_source
.event_id
, clock_count_per_tick
);
218 samsung_time_start(timer_source
.event_id
, PERIODIC
);
220 /* source timer restart */
221 samsung_time_setup(timer_source
.source_id
, TCNT_MAX
);
222 samsung_time_start(timer_source
.source_id
, PERIODIC
);
225 void __init
samsung_set_timer_source(enum samsung_timer_mode event
,
226 enum samsung_timer_mode source
)
228 s3c_device_timer
[event
].dev
.bus
= &platform_bus_type
;
229 s3c_device_timer
[source
].dev
.bus
= &platform_bus_type
;
231 timer_source
.event_id
= event
;
232 timer_source
.source_id
= source
;
235 static struct clock_event_device time_event_device
= {
236 .name
= "samsung_event_timer",
237 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
,
239 .set_next_event
= samsung_set_next_event
,
240 .set_mode
= samsung_set_mode
,
243 static irqreturn_t
samsung_clock_event_isr(int irq
, void *dev_id
)
245 struct clock_event_device
*evt
= dev_id
;
247 evt
->event_handler(evt
);
252 static struct irqaction samsung_clock_event_irq
= {
253 .name
= "samsung_time_irq",
254 .flags
= IRQF_DISABLED
| IRQF_TIMER
| IRQF_IRQPOLL
,
255 .handler
= samsung_clock_event_isr
,
256 .dev_id
= &time_event_device
,
259 static void __init
samsung_clockevent_init(void)
262 unsigned long clock_rate
;
263 unsigned int irq_number
;
266 pclk
= clk_get_rate(timerclk
);
268 tscaler
= clk_get_parent(tdiv_event
);
270 clk_set_rate(tscaler
, pclk
/ TSCALER_DIV
);
271 clk_set_rate(tdiv_event
, pclk
/ TDIV
);
272 clk_set_parent(tin_event
, tdiv_event
);
274 clock_rate
= clk_get_rate(tin_event
);
275 clock_count_per_tick
= clock_rate
/ HZ
;
277 time_event_device
.cpumask
= cpumask_of(0);
278 clockevents_config_and_register(&time_event_device
, clock_rate
, 1, -1);
280 irq_number
= timer_source
.event_id
+ IRQ_TIMER0
;
281 setup_irq(irq_number
, &samsung_clock_event_irq
);
284 static void __iomem
*samsung_timer_reg(void)
286 unsigned long offset
= 0;
288 switch (timer_source
.source_id
) {
293 offset
= (timer_source
.source_id
* 0x0c) + 0x14;
301 printk(KERN_ERR
"Invalid Timer %d\n", timer_source
.source_id
);
305 return S3C_TIMERREG(offset
);
309 * Override the global weak sched_clock symbol with this
310 * local implementation which uses the clocksource to get some
311 * better resolution when scheduling the kernel. We accept that
312 * this wraps around for now, since it is just a relative time
313 * stamp. (Inspired by U300 implementation.)
315 static u32 notrace
samsung_read_sched_clock(void)
317 void __iomem
*reg
= samsung_timer_reg();
322 return ~__raw_readl(reg
);
325 static void __init
samsung_clocksource_init(void)
328 unsigned long clock_rate
;
330 pclk
= clk_get_rate(timerclk
);
332 clk_set_rate(tdiv_source
, pclk
/ TDIV
);
333 clk_set_parent(tin_source
, tdiv_source
);
335 clock_rate
= clk_get_rate(tin_source
);
337 samsung_time_setup(timer_source
.source_id
, TCNT_MAX
);
338 samsung_time_start(timer_source
.source_id
, PERIODIC
);
340 setup_sched_clock(samsung_read_sched_clock
, TSIZE
, clock_rate
);
342 if (clocksource_mmio_init(samsung_timer_reg(), "samsung_clocksource_timer",
343 clock_rate
, 250, TSIZE
, clocksource_mmio_readl_down
))
344 panic("samsung_clocksource_timer: can't register clocksource\n");
347 static void __init
samsung_timer_resources(void)
350 unsigned long event_id
= timer_source
.event_id
;
351 unsigned long source_id
= timer_source
.source_id
;
354 timerclk
= clk_get(NULL
, "timers");
355 if (IS_ERR(timerclk
))
356 panic("failed to get timers clock for timer");
358 clk_enable(timerclk
);
360 sprintf(devname
, "s3c24xx-pwm.%lu", event_id
);
361 s3c_device_timer
[event_id
].id
= event_id
;
362 s3c_device_timer
[event_id
].dev
.init_name
= devname
;
364 tin_event
= clk_get(&s3c_device_timer
[event_id
].dev
, "pwm-tin");
365 if (IS_ERR(tin_event
))
366 panic("failed to get pwm-tin clock for event timer");
368 tdiv_event
= clk_get(&s3c_device_timer
[event_id
].dev
, "pwm-tdiv");
369 if (IS_ERR(tdiv_event
))
370 panic("failed to get pwm-tdiv clock for event timer");
372 clk_enable(tin_event
);
374 sprintf(devname
, "s3c24xx-pwm.%lu", source_id
);
375 s3c_device_timer
[source_id
].id
= source_id
;
376 s3c_device_timer
[source_id
].dev
.init_name
= devname
;
378 tin_source
= clk_get(&s3c_device_timer
[source_id
].dev
, "pwm-tin");
379 if (IS_ERR(tin_source
))
380 panic("failed to get pwm-tin clock for source timer");
382 tdiv_source
= clk_get(&s3c_device_timer
[source_id
].dev
, "pwm-tdiv");
383 if (IS_ERR(tdiv_source
))
384 panic("failed to get pwm-tdiv clock for source timer");
386 clk_enable(tin_source
);
389 void __init
samsung_timer_init(void)
391 samsung_timer_resources();
392 samsung_clockevent_init();
393 samsung_clocksource_init();