blob: edc4b9488f2f9a0c4e53d8cf39e6248aad040cb0 [file] [log] [blame]
Changhwan Youn30d8bea2011-03-11 10:39:57 +09001/* linux/arch/arm/mach-exynos4/mct.c
2 *
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
6 * EXYNOS4 MCT(Multi-Core Timer) support
7 *
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.
11*/
12
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>
22
Changhwan Youn3a062282011-10-04 17:02:58 +090023#include <asm/hardware/gic.h>
Marc Zyngiera8cb6042012-01-10 19:44:19 +000024#include <asm/localtimer.h>
Changhwan Youn3a062282011-10-04 17:02:58 +090025
26#include <plat/cpu.h>
27
Changhwan Youn30d8bea2011-03-11 10:39:57 +090028#include <mach/map.h>
Changhwan Youn3a062282011-10-04 17:02:58 +090029#include <mach/irqs.h>
Changhwan Youn30d8bea2011-03-11 10:39:57 +090030#include <mach/regs-mct.h>
31#include <asm/mach/time.h>
32
Changhwan Youn3a062282011-10-04 17:02:58 +090033enum {
34 MCT_INT_SPI,
35 MCT_INT_PPI
36};
37
Changhwan Youn30d8bea2011-03-11 10:39:57 +090038static unsigned long clk_cnt_per_tick;
39static unsigned long clk_rate;
Changhwan Youn3a062282011-10-04 17:02:58 +090040static unsigned int mct_int_type;
Changhwan Youn30d8bea2011-03-11 10:39:57 +090041
42struct mct_clock_event_device {
43 struct clock_event_device *evt;
44 void __iomem *base;
Changhwan Younc8987472011-10-04 17:09:26 +090045 char name[10];
Changhwan Youn30d8bea2011-03-11 10:39:57 +090046};
47
Changhwan Youn30d8bea2011-03-11 10:39:57 +090048static void exynos4_mct_write(unsigned int value, void *addr)
49{
50 void __iomem *stat_addr;
51 u32 mask;
52 u32 i;
53
54 __raw_writel(value, addr);
55
Changhwan Younc8987472011-10-04 17:09:26 +090056 if (likely(addr >= EXYNOS4_MCT_L_BASE(0))) {
57 u32 base = (u32) addr & EXYNOS4_MCT_L_MASK;
58 switch ((u32) addr & ~EXYNOS4_MCT_L_MASK) {
59 case (u32) MCT_L_TCON_OFFSET:
60 stat_addr = (void __iomem *) base + MCT_L_WSTAT_OFFSET;
61 mask = 1 << 3; /* L_TCON write status */
62 break;
63 case (u32) MCT_L_ICNTB_OFFSET:
64 stat_addr = (void __iomem *) base + MCT_L_WSTAT_OFFSET;
65 mask = 1 << 1; /* L_ICNTB write status */
66 break;
67 case (u32) MCT_L_TCNTB_OFFSET:
68 stat_addr = (void __iomem *) base + MCT_L_WSTAT_OFFSET;
69 mask = 1 << 0; /* L_TCNTB write status */
70 break;
71 default:
72 return;
73 }
74 } else {
75 switch ((u32) addr) {
76 case (u32) EXYNOS4_MCT_G_TCON:
77 stat_addr = EXYNOS4_MCT_G_WSTAT;
78 mask = 1 << 16; /* G_TCON write status */
79 break;
80 case (u32) EXYNOS4_MCT_G_COMP0_L:
81 stat_addr = EXYNOS4_MCT_G_WSTAT;
82 mask = 1 << 0; /* G_COMP0_L write status */
83 break;
84 case (u32) EXYNOS4_MCT_G_COMP0_U:
85 stat_addr = EXYNOS4_MCT_G_WSTAT;
86 mask = 1 << 1; /* G_COMP0_U write status */
87 break;
88 case (u32) EXYNOS4_MCT_G_COMP0_ADD_INCR:
89 stat_addr = EXYNOS4_MCT_G_WSTAT;
90 mask = 1 << 2; /* G_COMP0_ADD_INCR w status */
91 break;
92 case (u32) EXYNOS4_MCT_G_CNT_L:
93 stat_addr = EXYNOS4_MCT_G_CNT_WSTAT;
94 mask = 1 << 0; /* G_CNT_L write status */
95 break;
96 case (u32) EXYNOS4_MCT_G_CNT_U:
97 stat_addr = EXYNOS4_MCT_G_CNT_WSTAT;
98 mask = 1 << 1; /* G_CNT_U write status */
99 break;
100 default:
101 return;
102 }
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900103 }
104
105 /* Wait maximum 1 ms until written values are applied */
106 for (i = 0; i < loops_per_jiffy / 1000 * HZ; i++)
107 if (__raw_readl(stat_addr) & mask) {
108 __raw_writel(mask, stat_addr);
109 return;
110 }
111
112 panic("MCT hangs after writing %d (addr:0x%08x)\n", value, (u32)addr);
113}
114
115/* Clocksource handling */
116static void exynos4_mct_frc_start(u32 hi, u32 lo)
117{
118 u32 reg;
119
120 exynos4_mct_write(lo, EXYNOS4_MCT_G_CNT_L);
121 exynos4_mct_write(hi, EXYNOS4_MCT_G_CNT_U);
122
123 reg = __raw_readl(EXYNOS4_MCT_G_TCON);
124 reg |= MCT_G_TCON_START;
125 exynos4_mct_write(reg, EXYNOS4_MCT_G_TCON);
126}
127
128static cycle_t exynos4_frc_read(struct clocksource *cs)
129{
130 unsigned int lo, hi;
131 u32 hi2 = __raw_readl(EXYNOS4_MCT_G_CNT_U);
132
133 do {
134 hi = hi2;
135 lo = __raw_readl(EXYNOS4_MCT_G_CNT_L);
136 hi2 = __raw_readl(EXYNOS4_MCT_G_CNT_U);
137 } while (hi != hi2);
138
139 return ((cycle_t)hi << 32) | lo;
140}
141
Changhwan Younaa421c12011-09-02 14:10:52 +0900142static void exynos4_frc_resume(struct clocksource *cs)
143{
144 exynos4_mct_frc_start(0, 0);
145}
146
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900147struct clocksource mct_frc = {
148 .name = "mct-frc",
149 .rating = 400,
150 .read = exynos4_frc_read,
151 .mask = CLOCKSOURCE_MASK(64),
152 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Changhwan Younaa421c12011-09-02 14:10:52 +0900153 .resume = exynos4_frc_resume,
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900154};
155
156static void __init exynos4_clocksource_init(void)
157{
158 exynos4_mct_frc_start(0, 0);
159
160 if (clocksource_register_hz(&mct_frc, clk_rate))
161 panic("%s: can't register clocksource\n", mct_frc.name);
162}
163
164static void exynos4_mct_comp0_stop(void)
165{
166 unsigned int tcon;
167
168 tcon = __raw_readl(EXYNOS4_MCT_G_TCON);
169 tcon &= ~(MCT_G_TCON_COMP0_ENABLE | MCT_G_TCON_COMP0_AUTO_INC);
170
171 exynos4_mct_write(tcon, EXYNOS4_MCT_G_TCON);
172 exynos4_mct_write(0, EXYNOS4_MCT_G_INT_ENB);
173}
174
175static void exynos4_mct_comp0_start(enum clock_event_mode mode,
176 unsigned long cycles)
177{
178 unsigned int tcon;
179 cycle_t comp_cycle;
180
181 tcon = __raw_readl(EXYNOS4_MCT_G_TCON);
182
183 if (mode == CLOCK_EVT_MODE_PERIODIC) {
184 tcon |= MCT_G_TCON_COMP0_AUTO_INC;
185 exynos4_mct_write(cycles, EXYNOS4_MCT_G_COMP0_ADD_INCR);
186 }
187
188 comp_cycle = exynos4_frc_read(&mct_frc) + cycles;
189 exynos4_mct_write((u32)comp_cycle, EXYNOS4_MCT_G_COMP0_L);
190 exynos4_mct_write((u32)(comp_cycle >> 32), EXYNOS4_MCT_G_COMP0_U);
191
192 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_ENB);
193
194 tcon |= MCT_G_TCON_COMP0_ENABLE;
195 exynos4_mct_write(tcon , EXYNOS4_MCT_G_TCON);
196}
197
198static int exynos4_comp_set_next_event(unsigned long cycles,
199 struct clock_event_device *evt)
200{
201 exynos4_mct_comp0_start(evt->mode, cycles);
202
203 return 0;
204}
205
206static void exynos4_comp_set_mode(enum clock_event_mode mode,
207 struct clock_event_device *evt)
208{
209 exynos4_mct_comp0_stop();
210
211 switch (mode) {
212 case CLOCK_EVT_MODE_PERIODIC:
213 exynos4_mct_comp0_start(mode, clk_cnt_per_tick);
214 break;
215
216 case CLOCK_EVT_MODE_ONESHOT:
217 case CLOCK_EVT_MODE_UNUSED:
218 case CLOCK_EVT_MODE_SHUTDOWN:
219 case CLOCK_EVT_MODE_RESUME:
220 break;
221 }
222}
223
224static struct clock_event_device mct_comp_device = {
225 .name = "mct-comp",
226 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
227 .rating = 250,
228 .set_next_event = exynos4_comp_set_next_event,
229 .set_mode = exynos4_comp_set_mode,
230};
231
232static irqreturn_t exynos4_mct_comp_isr(int irq, void *dev_id)
233{
234 struct clock_event_device *evt = dev_id;
235
236 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_CSTAT);
237
238 evt->event_handler(evt);
239
240 return IRQ_HANDLED;
241}
242
243static struct irqaction mct_comp_event_irq = {
244 .name = "mct_comp_irq",
245 .flags = IRQF_TIMER | IRQF_IRQPOLL,
246 .handler = exynos4_mct_comp_isr,
247 .dev_id = &mct_comp_device,
248};
249
250static void exynos4_clockevent_init(void)
251{
252 clk_cnt_per_tick = clk_rate / 2 / HZ;
253
254 clockevents_calc_mult_shift(&mct_comp_device, clk_rate / 2, 5);
255 mct_comp_device.max_delta_ns =
256 clockevent_delta2ns(0xffffffff, &mct_comp_device);
257 mct_comp_device.min_delta_ns =
258 clockevent_delta2ns(0xf, &mct_comp_device);
259 mct_comp_device.cpumask = cpumask_of(0);
260 clockevents_register_device(&mct_comp_device);
261
262 setup_irq(IRQ_MCT_G0, &mct_comp_event_irq);
263}
264
265#ifdef CONFIG_LOCAL_TIMERS
Kukjin Kim991a6c72011-12-08 10:04:49 +0900266
267static DEFINE_PER_CPU(struct mct_clock_event_device, percpu_mct_tick);
268
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900269/* Clock event handling */
270static void exynos4_mct_tick_stop(struct mct_clock_event_device *mevt)
271{
272 unsigned long tmp;
273 unsigned long mask = MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START;
274 void __iomem *addr = mevt->base + MCT_L_TCON_OFFSET;
275
276 tmp = __raw_readl(addr);
277 if (tmp & mask) {
278 tmp &= ~mask;
279 exynos4_mct_write(tmp, addr);
280 }
281}
282
283static void exynos4_mct_tick_start(unsigned long cycles,
284 struct mct_clock_event_device *mevt)
285{
286 unsigned long tmp;
287
288 exynos4_mct_tick_stop(mevt);
289
290 tmp = (1 << 31) | cycles; /* MCT_L_UPDATE_ICNTB */
291
292 /* update interrupt count buffer */
293 exynos4_mct_write(tmp, mevt->base + MCT_L_ICNTB_OFFSET);
294
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300295 /* enable MCT tick interrupt */
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900296 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_ENB_OFFSET);
297
298 tmp = __raw_readl(mevt->base + MCT_L_TCON_OFFSET);
299 tmp |= MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START |
300 MCT_L_TCON_INTERVAL_MODE;
301 exynos4_mct_write(tmp, mevt->base + MCT_L_TCON_OFFSET);
302}
303
304static int exynos4_tick_set_next_event(unsigned long cycles,
305 struct clock_event_device *evt)
306{
Marc Zyngiere700e412011-11-03 11:13:12 +0900307 struct mct_clock_event_device *mevt = this_cpu_ptr(&percpu_mct_tick);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900308
309 exynos4_mct_tick_start(cycles, mevt);
310
311 return 0;
312}
313
314static inline void exynos4_tick_set_mode(enum clock_event_mode mode,
315 struct clock_event_device *evt)
316{
Marc Zyngiere700e412011-11-03 11:13:12 +0900317 struct mct_clock_event_device *mevt = this_cpu_ptr(&percpu_mct_tick);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900318
319 exynos4_mct_tick_stop(mevt);
320
321 switch (mode) {
322 case CLOCK_EVT_MODE_PERIODIC:
323 exynos4_mct_tick_start(clk_cnt_per_tick, mevt);
324 break;
325
326 case CLOCK_EVT_MODE_ONESHOT:
327 case CLOCK_EVT_MODE_UNUSED:
328 case CLOCK_EVT_MODE_SHUTDOWN:
329 case CLOCK_EVT_MODE_RESUME:
330 break;
331 }
332}
333
Changhwan Younc8987472011-10-04 17:09:26 +0900334static int exynos4_mct_tick_clear(struct mct_clock_event_device *mevt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900335{
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900336 struct clock_event_device *evt = mevt->evt;
337
338 /*
339 * This is for supporting oneshot mode.
340 * Mct would generate interrupt periodically
341 * without explicit stopping.
342 */
343 if (evt->mode != CLOCK_EVT_MODE_PERIODIC)
344 exynos4_mct_tick_stop(mevt);
345
346 /* Clear the MCT tick interrupt */
Changhwan Youn3a062282011-10-04 17:02:58 +0900347 if (__raw_readl(mevt->base + MCT_L_INT_CSTAT_OFFSET) & 1) {
348 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_CSTAT_OFFSET);
349 return 1;
350 } else {
351 return 0;
352 }
353}
354
355static irqreturn_t exynos4_mct_tick_isr(int irq, void *dev_id)
356{
357 struct mct_clock_event_device *mevt = dev_id;
358 struct clock_event_device *evt = mevt->evt;
359
360 exynos4_mct_tick_clear(mevt);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900361
362 evt->event_handler(evt);
363
364 return IRQ_HANDLED;
365}
366
367static struct irqaction mct_tick0_event_irq = {
368 .name = "mct_tick0_irq",
369 .flags = IRQF_TIMER | IRQF_NOBALANCING,
370 .handler = exynos4_mct_tick_isr,
371};
372
373static struct irqaction mct_tick1_event_irq = {
374 .name = "mct_tick1_irq",
375 .flags = IRQF_TIMER | IRQF_NOBALANCING,
376 .handler = exynos4_mct_tick_isr,
377};
378
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000379static int __cpuinit exynos4_local_timer_setup(struct clock_event_device *evt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900380{
Marc Zyngiere700e412011-11-03 11:13:12 +0900381 struct mct_clock_event_device *mevt;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900382 unsigned int cpu = smp_processor_id();
383
Marc Zyngiere700e412011-11-03 11:13:12 +0900384 mevt = this_cpu_ptr(&percpu_mct_tick);
385 mevt->evt = evt;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900386
Marc Zyngiere700e412011-11-03 11:13:12 +0900387 mevt->base = EXYNOS4_MCT_L_BASE(cpu);
388 sprintf(mevt->name, "mct_tick%d", cpu);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900389
Marc Zyngiere700e412011-11-03 11:13:12 +0900390 evt->name = mevt->name;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900391 evt->cpumask = cpumask_of(cpu);
392 evt->set_next_event = exynos4_tick_set_next_event;
393 evt->set_mode = exynos4_tick_set_mode;
394 evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
395 evt->rating = 450;
396
397 clockevents_calc_mult_shift(evt, clk_rate / 2, 5);
398 evt->max_delta_ns =
399 clockevent_delta2ns(0x7fffffff, evt);
400 evt->min_delta_ns =
401 clockevent_delta2ns(0xf, evt);
402
403 clockevents_register_device(evt);
404
Marc Zyngiere700e412011-11-03 11:13:12 +0900405 exynos4_mct_write(0x1, mevt->base + MCT_L_TCNTB_OFFSET);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900406
Changhwan Youn3a062282011-10-04 17:02:58 +0900407 if (mct_int_type == MCT_INT_SPI) {
408 if (cpu == 0) {
Marc Zyngiere700e412011-11-03 11:13:12 +0900409 mct_tick0_event_irq.dev_id = mevt;
Arnd Bergmanna7fadac2011-10-31 23:58:06 +0100410 evt->irq = IRQ_MCT_L0;
Changhwan Youn3a062282011-10-04 17:02:58 +0900411 setup_irq(IRQ_MCT_L0, &mct_tick0_event_irq);
412 } else {
Marc Zyngiere700e412011-11-03 11:13:12 +0900413 mct_tick1_event_irq.dev_id = mevt;
Arnd Bergmanna7fadac2011-10-31 23:58:06 +0100414 evt->irq = IRQ_MCT_L1;
Changhwan Youn3a062282011-10-04 17:02:58 +0900415 setup_irq(IRQ_MCT_L1, &mct_tick1_event_irq);
416 irq_set_affinity(IRQ_MCT_L1, cpumask_of(1));
417 }
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900418 } else {
Marc Zyngiere700e412011-11-03 11:13:12 +0900419 enable_percpu_irq(IRQ_MCT_LOCALTIMER, 0);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900420 }
Kukjin Kim4d487d72011-08-24 16:07:39 +0900421
422 return 0;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900423}
424
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000425static void exynos4_local_timer_stop(struct clock_event_device *evt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900426{
Amit Daniel Kachhape248cd52011-12-08 10:07:08 +0900427 unsigned int cpu = smp_processor_id();
Marc Zyngier28af6902011-07-22 12:52:37 +0100428 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
Marc Zyngiere700e412011-11-03 11:13:12 +0900429 if (mct_int_type == MCT_INT_SPI)
Amit Daniel Kachhape248cd52011-12-08 10:07:08 +0900430 if (cpu == 0)
431 remove_irq(evt->irq, &mct_tick0_event_irq);
432 else
433 remove_irq(evt->irq, &mct_tick1_event_irq);
Marc Zyngiere700e412011-11-03 11:13:12 +0900434 else
435 disable_percpu_irq(IRQ_MCT_LOCALTIMER);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900436}
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000437
438static struct local_timer_ops exynos4_mct_tick_ops __cpuinitdata = {
439 .setup = exynos4_local_timer_setup,
440 .stop = exynos4_local_timer_stop,
441};
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900442#endif /* CONFIG_LOCAL_TIMERS */
443
444static void __init exynos4_timer_resources(void)
445{
446 struct clk *mct_clk;
447 mct_clk = clk_get(NULL, "xtal");
448
449 clk_rate = clk_get_rate(mct_clk);
Marc Zyngiere700e412011-11-03 11:13:12 +0900450
Kukjin Kim991a6c72011-12-08 10:04:49 +0900451#ifdef CONFIG_LOCAL_TIMERS
Marc Zyngiere700e412011-11-03 11:13:12 +0900452 if (mct_int_type == MCT_INT_PPI) {
453 int err;
454
455 err = request_percpu_irq(IRQ_MCT_LOCALTIMER,
456 exynos4_mct_tick_isr, "MCT",
457 &percpu_mct_tick);
458 WARN(err, "MCT: can't request IRQ %d (%d)\n",
459 IRQ_MCT_LOCALTIMER, err);
460 }
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000461
462 local_timer_register(&exynos4_mct_tick_ops);
Kukjin Kim991a6c72011-12-08 10:04:49 +0900463#endif /* CONFIG_LOCAL_TIMERS */
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900464}
465
466static void __init exynos4_timer_init(void)
467{
Changhwan Youn3a062282011-10-04 17:02:58 +0900468 if (soc_is_exynos4210())
469 mct_int_type = MCT_INT_SPI;
470 else
471 mct_int_type = MCT_INT_PPI;
472
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900473 exynos4_timer_resources();
474 exynos4_clocksource_init();
475 exynos4_clockevent_init();
476}
477
478struct sys_timer exynos4_timer = {
479 .init = exynos4_timer_init,
480};