1 /* linux/arch/arm/mach-exynos4/mct.c
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
6 * EXYNOS4 MCT(Multi-Core Timer) support
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/sched.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/err.h>
17 #include <linux/clk.h>
18 #include <linux/clockchips.h>
19 #include <linux/platform_device.h>
20 #include <linux/delay.h>
21 #include <linux/percpu.h>
23 #include <asm/hardware/gic.h>
28 #include <mach/irqs.h>
29 #include <mach/regs-mct.h>
30 #include <asm/mach/time.h>
37 static unsigned long clk_cnt_per_tick
;
38 static unsigned long clk_rate
;
39 static unsigned int mct_int_type
;
41 struct mct_clock_event_device
{
42 struct clock_event_device
*evt
;
47 static void exynos4_mct_write(unsigned int value
, void *addr
)
49 void __iomem
*stat_addr
;
53 __raw_writel(value
, addr
);
55 if (likely(addr
>= EXYNOS4_MCT_L_BASE(0))) {
56 u32 base
= (u32
) addr
& EXYNOS4_MCT_L_MASK
;
57 switch ((u32
) addr
& ~EXYNOS4_MCT_L_MASK
) {
58 case (u32
) MCT_L_TCON_OFFSET
:
59 stat_addr
= (void __iomem
*) base
+ MCT_L_WSTAT_OFFSET
;
60 mask
= 1 << 3; /* L_TCON write status */
62 case (u32
) MCT_L_ICNTB_OFFSET
:
63 stat_addr
= (void __iomem
*) base
+ MCT_L_WSTAT_OFFSET
;
64 mask
= 1 << 1; /* L_ICNTB write status */
66 case (u32
) MCT_L_TCNTB_OFFSET
:
67 stat_addr
= (void __iomem
*) base
+ MCT_L_WSTAT_OFFSET
;
68 mask
= 1 << 0; /* L_TCNTB write status */
75 case (u32
) EXYNOS4_MCT_G_TCON
:
76 stat_addr
= EXYNOS4_MCT_G_WSTAT
;
77 mask
= 1 << 16; /* G_TCON write status */
79 case (u32
) EXYNOS4_MCT_G_COMP0_L
:
80 stat_addr
= EXYNOS4_MCT_G_WSTAT
;
81 mask
= 1 << 0; /* G_COMP0_L write status */
83 case (u32
) EXYNOS4_MCT_G_COMP0_U
:
84 stat_addr
= EXYNOS4_MCT_G_WSTAT
;
85 mask
= 1 << 1; /* G_COMP0_U write status */
87 case (u32
) EXYNOS4_MCT_G_COMP0_ADD_INCR
:
88 stat_addr
= EXYNOS4_MCT_G_WSTAT
;
89 mask
= 1 << 2; /* G_COMP0_ADD_INCR w status */
91 case (u32
) EXYNOS4_MCT_G_CNT_L
:
92 stat_addr
= EXYNOS4_MCT_G_CNT_WSTAT
;
93 mask
= 1 << 0; /* G_CNT_L write status */
95 case (u32
) EXYNOS4_MCT_G_CNT_U
:
96 stat_addr
= EXYNOS4_MCT_G_CNT_WSTAT
;
97 mask
= 1 << 1; /* G_CNT_U write status */
104 /* Wait maximum 1 ms until written values are applied */
105 for (i
= 0; i
< loops_per_jiffy
/ 1000 * HZ
; i
++)
106 if (__raw_readl(stat_addr
) & mask
) {
107 __raw_writel(mask
, stat_addr
);
111 panic("MCT hangs after writing %d (addr:0x%08x)\n", value
, (u32
)addr
);
114 /* Clocksource handling */
115 static void exynos4_mct_frc_start(u32 hi
, u32 lo
)
119 exynos4_mct_write(lo
, EXYNOS4_MCT_G_CNT_L
);
120 exynos4_mct_write(hi
, EXYNOS4_MCT_G_CNT_U
);
122 reg
= __raw_readl(EXYNOS4_MCT_G_TCON
);
123 reg
|= MCT_G_TCON_START
;
124 exynos4_mct_write(reg
, EXYNOS4_MCT_G_TCON
);
127 static cycle_t
exynos4_frc_read(struct clocksource
*cs
)
130 u32 hi2
= __raw_readl(EXYNOS4_MCT_G_CNT_U
);
134 lo
= __raw_readl(EXYNOS4_MCT_G_CNT_L
);
135 hi2
= __raw_readl(EXYNOS4_MCT_G_CNT_U
);
138 return ((cycle_t
)hi
<< 32) | lo
;
141 static void exynos4_frc_resume(struct clocksource
*cs
)
143 exynos4_mct_frc_start(0, 0);
146 struct clocksource mct_frc
= {
149 .read
= exynos4_frc_read
,
150 .mask
= CLOCKSOURCE_MASK(64),
151 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
152 .resume
= exynos4_frc_resume
,
155 static void __init
exynos4_clocksource_init(void)
157 exynos4_mct_frc_start(0, 0);
159 if (clocksource_register_hz(&mct_frc
, clk_rate
))
160 panic("%s: can't register clocksource\n", mct_frc
.name
);
163 static void exynos4_mct_comp0_stop(void)
167 tcon
= __raw_readl(EXYNOS4_MCT_G_TCON
);
168 tcon
&= ~(MCT_G_TCON_COMP0_ENABLE
| MCT_G_TCON_COMP0_AUTO_INC
);
170 exynos4_mct_write(tcon
, EXYNOS4_MCT_G_TCON
);
171 exynos4_mct_write(0, EXYNOS4_MCT_G_INT_ENB
);
174 static void exynos4_mct_comp0_start(enum clock_event_mode mode
,
175 unsigned long cycles
)
180 tcon
= __raw_readl(EXYNOS4_MCT_G_TCON
);
182 if (mode
== CLOCK_EVT_MODE_PERIODIC
) {
183 tcon
|= MCT_G_TCON_COMP0_AUTO_INC
;
184 exynos4_mct_write(cycles
, EXYNOS4_MCT_G_COMP0_ADD_INCR
);
187 comp_cycle
= exynos4_frc_read(&mct_frc
) + cycles
;
188 exynos4_mct_write((u32
)comp_cycle
, EXYNOS4_MCT_G_COMP0_L
);
189 exynos4_mct_write((u32
)(comp_cycle
>> 32), EXYNOS4_MCT_G_COMP0_U
);
191 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_ENB
);
193 tcon
|= MCT_G_TCON_COMP0_ENABLE
;
194 exynos4_mct_write(tcon
, EXYNOS4_MCT_G_TCON
);
197 static int exynos4_comp_set_next_event(unsigned long cycles
,
198 struct clock_event_device
*evt
)
200 exynos4_mct_comp0_start(evt
->mode
, cycles
);
205 static void exynos4_comp_set_mode(enum clock_event_mode mode
,
206 struct clock_event_device
*evt
)
208 exynos4_mct_comp0_stop();
211 case CLOCK_EVT_MODE_PERIODIC
:
212 exynos4_mct_comp0_start(mode
, clk_cnt_per_tick
);
215 case CLOCK_EVT_MODE_ONESHOT
:
216 case CLOCK_EVT_MODE_UNUSED
:
217 case CLOCK_EVT_MODE_SHUTDOWN
:
218 case CLOCK_EVT_MODE_RESUME
:
223 static struct clock_event_device mct_comp_device
= {
225 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
,
227 .set_next_event
= exynos4_comp_set_next_event
,
228 .set_mode
= exynos4_comp_set_mode
,
231 static irqreturn_t
exynos4_mct_comp_isr(int irq
, void *dev_id
)
233 struct clock_event_device
*evt
= dev_id
;
235 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_CSTAT
);
237 evt
->event_handler(evt
);
242 static struct irqaction mct_comp_event_irq
= {
243 .name
= "mct_comp_irq",
244 .flags
= IRQF_TIMER
| IRQF_IRQPOLL
,
245 .handler
= exynos4_mct_comp_isr
,
246 .dev_id
= &mct_comp_device
,
249 static void exynos4_clockevent_init(void)
251 clk_cnt_per_tick
= clk_rate
/ 2 / HZ
;
253 clockevents_calc_mult_shift(&mct_comp_device
, clk_rate
/ 2, 5);
254 mct_comp_device
.max_delta_ns
=
255 clockevent_delta2ns(0xffffffff, &mct_comp_device
);
256 mct_comp_device
.min_delta_ns
=
257 clockevent_delta2ns(0xf, &mct_comp_device
);
258 mct_comp_device
.cpumask
= cpumask_of(0);
259 clockevents_register_device(&mct_comp_device
);
261 setup_irq(IRQ_MCT_G0
, &mct_comp_event_irq
);
264 #ifdef CONFIG_LOCAL_TIMERS
266 static DEFINE_PER_CPU(struct mct_clock_event_device
, percpu_mct_tick
);
268 /* Clock event handling */
269 static void exynos4_mct_tick_stop(struct mct_clock_event_device
*mevt
)
272 unsigned long mask
= MCT_L_TCON_INT_START
| MCT_L_TCON_TIMER_START
;
273 void __iomem
*addr
= mevt
->base
+ MCT_L_TCON_OFFSET
;
275 tmp
= __raw_readl(addr
);
278 exynos4_mct_write(tmp
, addr
);
282 static void exynos4_mct_tick_start(unsigned long cycles
,
283 struct mct_clock_event_device
*mevt
)
287 exynos4_mct_tick_stop(mevt
);
289 tmp
= (1 << 31) | cycles
; /* MCT_L_UPDATE_ICNTB */
291 /* update interrupt count buffer */
292 exynos4_mct_write(tmp
, mevt
->base
+ MCT_L_ICNTB_OFFSET
);
294 /* enable MCT tick interrupt */
295 exynos4_mct_write(0x1, mevt
->base
+ MCT_L_INT_ENB_OFFSET
);
297 tmp
= __raw_readl(mevt
->base
+ MCT_L_TCON_OFFSET
);
298 tmp
|= MCT_L_TCON_INT_START
| MCT_L_TCON_TIMER_START
|
299 MCT_L_TCON_INTERVAL_MODE
;
300 exynos4_mct_write(tmp
, mevt
->base
+ MCT_L_TCON_OFFSET
);
303 static int exynos4_tick_set_next_event(unsigned long cycles
,
304 struct clock_event_device
*evt
)
306 struct mct_clock_event_device
*mevt
= this_cpu_ptr(&percpu_mct_tick
);
308 exynos4_mct_tick_start(cycles
, mevt
);
313 static inline void exynos4_tick_set_mode(enum clock_event_mode mode
,
314 struct clock_event_device
*evt
)
316 struct mct_clock_event_device
*mevt
= this_cpu_ptr(&percpu_mct_tick
);
318 exynos4_mct_tick_stop(mevt
);
321 case CLOCK_EVT_MODE_PERIODIC
:
322 exynos4_mct_tick_start(clk_cnt_per_tick
, mevt
);
325 case CLOCK_EVT_MODE_ONESHOT
:
326 case CLOCK_EVT_MODE_UNUSED
:
327 case CLOCK_EVT_MODE_SHUTDOWN
:
328 case CLOCK_EVT_MODE_RESUME
:
333 static int exynos4_mct_tick_clear(struct mct_clock_event_device
*mevt
)
335 struct clock_event_device
*evt
= mevt
->evt
;
338 * This is for supporting oneshot mode.
339 * Mct would generate interrupt periodically
340 * without explicit stopping.
342 if (evt
->mode
!= CLOCK_EVT_MODE_PERIODIC
)
343 exynos4_mct_tick_stop(mevt
);
345 /* Clear the MCT tick interrupt */
346 if (__raw_readl(mevt
->base
+ MCT_L_INT_CSTAT_OFFSET
) & 1) {
347 exynos4_mct_write(0x1, mevt
->base
+ MCT_L_INT_CSTAT_OFFSET
);
354 static irqreturn_t
exynos4_mct_tick_isr(int irq
, void *dev_id
)
356 struct mct_clock_event_device
*mevt
= dev_id
;
357 struct clock_event_device
*evt
= mevt
->evt
;
359 exynos4_mct_tick_clear(mevt
);
361 evt
->event_handler(evt
);
366 static struct irqaction mct_tick0_event_irq
= {
367 .name
= "mct_tick0_irq",
368 .flags
= IRQF_TIMER
| IRQF_NOBALANCING
,
369 .handler
= exynos4_mct_tick_isr
,
372 static struct irqaction mct_tick1_event_irq
= {
373 .name
= "mct_tick1_irq",
374 .flags
= IRQF_TIMER
| IRQF_NOBALANCING
,
375 .handler
= exynos4_mct_tick_isr
,
378 static void exynos4_mct_tick_init(struct clock_event_device
*evt
)
380 struct mct_clock_event_device
*mevt
;
381 unsigned int cpu
= smp_processor_id();
383 mevt
= this_cpu_ptr(&percpu_mct_tick
);
386 mevt
->base
= EXYNOS4_MCT_L_BASE(cpu
);
387 sprintf(mevt
->name
, "mct_tick%d", cpu
);
389 evt
->name
= mevt
->name
;
390 evt
->cpumask
= cpumask_of(cpu
);
391 evt
->set_next_event
= exynos4_tick_set_next_event
;
392 evt
->set_mode
= exynos4_tick_set_mode
;
393 evt
->features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
;
396 clockevents_calc_mult_shift(evt
, clk_rate
/ 2, 5);
398 clockevent_delta2ns(0x7fffffff, evt
);
400 clockevent_delta2ns(0xf, evt
);
402 clockevents_register_device(evt
);
404 exynos4_mct_write(0x1, mevt
->base
+ MCT_L_TCNTB_OFFSET
);
406 if (mct_int_type
== MCT_INT_SPI
) {
408 mct_tick0_event_irq
.dev_id
= mevt
;
409 evt
->irq
= IRQ_MCT_L0
;
410 setup_irq(IRQ_MCT_L0
, &mct_tick0_event_irq
);
412 mct_tick1_event_irq
.dev_id
= mevt
;
413 evt
->irq
= IRQ_MCT_L1
;
414 setup_irq(IRQ_MCT_L1
, &mct_tick1_event_irq
);
415 irq_set_affinity(IRQ_MCT_L1
, cpumask_of(1));
418 enable_percpu_irq(IRQ_MCT_LOCALTIMER
, 0);
422 /* Setup the local clock events for a CPU */
423 int __cpuinit
local_timer_setup(struct clock_event_device
*evt
)
425 exynos4_mct_tick_init(evt
);
430 void local_timer_stop(struct clock_event_device
*evt
)
432 unsigned int cpu
= smp_processor_id();
433 evt
->set_mode(CLOCK_EVT_MODE_UNUSED
, evt
);
434 if (mct_int_type
== MCT_INT_SPI
)
436 remove_irq(evt
->irq
, &mct_tick0_event_irq
);
438 remove_irq(evt
->irq
, &mct_tick1_event_irq
);
440 disable_percpu_irq(IRQ_MCT_LOCALTIMER
);
442 #endif /* CONFIG_LOCAL_TIMERS */
444 static void __init
exynos4_timer_resources(void)
447 mct_clk
= clk_get(NULL
, "xtal");
449 clk_rate
= clk_get_rate(mct_clk
);
451 #ifdef CONFIG_LOCAL_TIMERS
452 if (mct_int_type
== MCT_INT_PPI
) {
455 err
= request_percpu_irq(IRQ_MCT_LOCALTIMER
,
456 exynos4_mct_tick_isr
, "MCT",
458 WARN(err
, "MCT: can't request IRQ %d (%d)\n",
459 IRQ_MCT_LOCALTIMER
, err
);
461 #endif /* CONFIG_LOCAL_TIMERS */
464 static void __init
exynos4_timer_init(void)
466 if (soc_is_exynos4210())
467 mct_int_type
= MCT_INT_SPI
;
469 mct_int_type
= MCT_INT_PPI
;
471 exynos4_timer_resources();
472 exynos4_clocksource_init();
473 exynos4_clockevent_init();
476 struct sys_timer exynos4_timer
= {
477 .init
= exynos4_timer_init
,