blob: 3e894ba25b0167a2268001ee4fdabbf0c0bbffed [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>
24
25#include <plat/cpu.h>
26
Changhwan Youn30d8bea2011-03-11 10:39:57 +090027#include <mach/map.h>
Changhwan Youn3a062282011-10-04 17:02:58 +090028#include <mach/irqs.h>
Changhwan Youn30d8bea2011-03-11 10:39:57 +090029#include <mach/regs-mct.h>
30#include <asm/mach/time.h>
31
Changhwan Youn4d2e4d72012-03-09 15:09:21 -080032#define TICK_BASE_CNT 1
33
Changhwan Youn3a062282011-10-04 17:02:58 +090034enum {
35 MCT_INT_SPI,
36 MCT_INT_PPI
37};
38
Changhwan Youn30d8bea2011-03-11 10:39:57 +090039static 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{
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800209 unsigned long cycles_per_jiffy;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900210 exynos4_mct_comp0_stop();
211
212 switch (mode) {
213 case CLOCK_EVT_MODE_PERIODIC:
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800214 cycles_per_jiffy =
215 (((unsigned long long) NSEC_PER_SEC / HZ * evt->mult) >> evt->shift);
216 exynos4_mct_comp0_start(mode, cycles_per_jiffy);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900217 break;
218
219 case CLOCK_EVT_MODE_ONESHOT:
220 case CLOCK_EVT_MODE_UNUSED:
221 case CLOCK_EVT_MODE_SHUTDOWN:
222 case CLOCK_EVT_MODE_RESUME:
223 break;
224 }
225}
226
227static struct clock_event_device mct_comp_device = {
228 .name = "mct-comp",
229 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
230 .rating = 250,
231 .set_next_event = exynos4_comp_set_next_event,
232 .set_mode = exynos4_comp_set_mode,
233};
234
235static irqreturn_t exynos4_mct_comp_isr(int irq, void *dev_id)
236{
237 struct clock_event_device *evt = dev_id;
238
239 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_CSTAT);
240
241 evt->event_handler(evt);
242
243 return IRQ_HANDLED;
244}
245
246static struct irqaction mct_comp_event_irq = {
247 .name = "mct_comp_irq",
248 .flags = IRQF_TIMER | IRQF_IRQPOLL,
249 .handler = exynos4_mct_comp_isr,
250 .dev_id = &mct_comp_device,
251};
252
253static void exynos4_clockevent_init(void)
254{
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800255 clockevents_calc_mult_shift(&mct_comp_device, clk_rate, 5);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900256 mct_comp_device.max_delta_ns =
257 clockevent_delta2ns(0xffffffff, &mct_comp_device);
258 mct_comp_device.min_delta_ns =
259 clockevent_delta2ns(0xf, &mct_comp_device);
260 mct_comp_device.cpumask = cpumask_of(0);
261 clockevents_register_device(&mct_comp_device);
262
263 setup_irq(IRQ_MCT_G0, &mct_comp_event_irq);
264}
265
266#ifdef CONFIG_LOCAL_TIMERS
Kukjin Kim991a6c72011-12-08 10:04:49 +0900267
268static DEFINE_PER_CPU(struct mct_clock_event_device, percpu_mct_tick);
269
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900270/* Clock event handling */
271static void exynos4_mct_tick_stop(struct mct_clock_event_device *mevt)
272{
273 unsigned long tmp;
274 unsigned long mask = MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START;
275 void __iomem *addr = mevt->base + MCT_L_TCON_OFFSET;
276
277 tmp = __raw_readl(addr);
278 if (tmp & mask) {
279 tmp &= ~mask;
280 exynos4_mct_write(tmp, addr);
281 }
282}
283
284static void exynos4_mct_tick_start(unsigned long cycles,
285 struct mct_clock_event_device *mevt)
286{
287 unsigned long tmp;
288
289 exynos4_mct_tick_stop(mevt);
290
291 tmp = (1 << 31) | cycles; /* MCT_L_UPDATE_ICNTB */
292
293 /* update interrupt count buffer */
294 exynos4_mct_write(tmp, mevt->base + MCT_L_ICNTB_OFFSET);
295
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300296 /* enable MCT tick interrupt */
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900297 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_ENB_OFFSET);
298
299 tmp = __raw_readl(mevt->base + MCT_L_TCON_OFFSET);
300 tmp |= MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START |
301 MCT_L_TCON_INTERVAL_MODE;
302 exynos4_mct_write(tmp, mevt->base + MCT_L_TCON_OFFSET);
303}
304
305static int exynos4_tick_set_next_event(unsigned long cycles,
306 struct clock_event_device *evt)
307{
Marc Zyngiere700e412011-11-03 11:13:12 +0900308 struct mct_clock_event_device *mevt = this_cpu_ptr(&percpu_mct_tick);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900309
310 exynos4_mct_tick_start(cycles, mevt);
311
312 return 0;
313}
314
315static inline void exynos4_tick_set_mode(enum clock_event_mode mode,
316 struct clock_event_device *evt)
317{
Marc Zyngiere700e412011-11-03 11:13:12 +0900318 struct mct_clock_event_device *mevt = this_cpu_ptr(&percpu_mct_tick);
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800319 unsigned long cycles_per_jiffy;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900320
321 exynos4_mct_tick_stop(mevt);
322
323 switch (mode) {
324 case CLOCK_EVT_MODE_PERIODIC:
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800325 cycles_per_jiffy =
326 (((unsigned long long) NSEC_PER_SEC / HZ * evt->mult) >> evt->shift);
327 exynos4_mct_tick_start(cycles_per_jiffy, mevt);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900328 break;
329
330 case CLOCK_EVT_MODE_ONESHOT:
331 case CLOCK_EVT_MODE_UNUSED:
332 case CLOCK_EVT_MODE_SHUTDOWN:
333 case CLOCK_EVT_MODE_RESUME:
334 break;
335 }
336}
337
Changhwan Younc8987472011-10-04 17:09:26 +0900338static int exynos4_mct_tick_clear(struct mct_clock_event_device *mevt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900339{
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900340 struct clock_event_device *evt = mevt->evt;
341
342 /*
343 * This is for supporting oneshot mode.
344 * Mct would generate interrupt periodically
345 * without explicit stopping.
346 */
347 if (evt->mode != CLOCK_EVT_MODE_PERIODIC)
348 exynos4_mct_tick_stop(mevt);
349
350 /* Clear the MCT tick interrupt */
Changhwan Youn3a062282011-10-04 17:02:58 +0900351 if (__raw_readl(mevt->base + MCT_L_INT_CSTAT_OFFSET) & 1) {
352 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_CSTAT_OFFSET);
353 return 1;
354 } else {
355 return 0;
356 }
357}
358
359static irqreturn_t exynos4_mct_tick_isr(int irq, void *dev_id)
360{
361 struct mct_clock_event_device *mevt = dev_id;
362 struct clock_event_device *evt = mevt->evt;
363
364 exynos4_mct_tick_clear(mevt);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900365
366 evt->event_handler(evt);
367
368 return IRQ_HANDLED;
369}
370
371static struct irqaction mct_tick0_event_irq = {
372 .name = "mct_tick0_irq",
373 .flags = IRQF_TIMER | IRQF_NOBALANCING,
374 .handler = exynos4_mct_tick_isr,
375};
376
377static struct irqaction mct_tick1_event_irq = {
378 .name = "mct_tick1_irq",
379 .flags = IRQF_TIMER | IRQF_NOBALANCING,
380 .handler = exynos4_mct_tick_isr,
381};
382
383static void exynos4_mct_tick_init(struct clock_event_device *evt)
384{
Marc Zyngiere700e412011-11-03 11:13:12 +0900385 struct mct_clock_event_device *mevt;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900386 unsigned int cpu = smp_processor_id();
387
Marc Zyngiere700e412011-11-03 11:13:12 +0900388 mevt = this_cpu_ptr(&percpu_mct_tick);
389 mevt->evt = evt;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900390
Marc Zyngiere700e412011-11-03 11:13:12 +0900391 mevt->base = EXYNOS4_MCT_L_BASE(cpu);
392 sprintf(mevt->name, "mct_tick%d", cpu);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900393
Marc Zyngiere700e412011-11-03 11:13:12 +0900394 evt->name = mevt->name;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900395 evt->cpumask = cpumask_of(cpu);
396 evt->set_next_event = exynos4_tick_set_next_event;
397 evt->set_mode = exynos4_tick_set_mode;
398 evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
399 evt->rating = 450;
400
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800401 clockevents_calc_mult_shift(evt, clk_rate / (TICK_BASE_CNT + 1), 5);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900402 evt->max_delta_ns =
403 clockevent_delta2ns(0x7fffffff, evt);
404 evt->min_delta_ns =
405 clockevent_delta2ns(0xf, evt);
406
407 clockevents_register_device(evt);
408
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800409 exynos4_mct_write(TICK_BASE_CNT, mevt->base + MCT_L_TCNTB_OFFSET);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900410
Changhwan Youn3a062282011-10-04 17:02:58 +0900411 if (mct_int_type == MCT_INT_SPI) {
412 if (cpu == 0) {
Marc Zyngiere700e412011-11-03 11:13:12 +0900413 mct_tick0_event_irq.dev_id = mevt;
Arnd Bergmanna7fadac2011-10-31 23:58:06 +0100414 evt->irq = IRQ_MCT_L0;
Changhwan Youn3a062282011-10-04 17:02:58 +0900415 setup_irq(IRQ_MCT_L0, &mct_tick0_event_irq);
416 } else {
Marc Zyngiere700e412011-11-03 11:13:12 +0900417 mct_tick1_event_irq.dev_id = mevt;
Arnd Bergmanna7fadac2011-10-31 23:58:06 +0100418 evt->irq = IRQ_MCT_L1;
Changhwan Youn3a062282011-10-04 17:02:58 +0900419 setup_irq(IRQ_MCT_L1, &mct_tick1_event_irq);
420 irq_set_affinity(IRQ_MCT_L1, cpumask_of(1));
421 }
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900422 } else {
Marc Zyngiere700e412011-11-03 11:13:12 +0900423 enable_percpu_irq(IRQ_MCT_LOCALTIMER, 0);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900424 }
425}
426
427/* Setup the local clock events for a CPU */
Kukjin Kim4d487d72011-08-24 16:07:39 +0900428int __cpuinit local_timer_setup(struct clock_event_device *evt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900429{
430 exynos4_mct_tick_init(evt);
Kukjin Kim4d487d72011-08-24 16:07:39 +0900431
432 return 0;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900433}
434
Marc Zyngier28af6902011-07-22 12:52:37 +0100435void local_timer_stop(struct clock_event_device *evt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900436{
Amit Daniel Kachhape248cd52011-12-08 10:07:08 +0900437 unsigned int cpu = smp_processor_id();
Marc Zyngier28af6902011-07-22 12:52:37 +0100438 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
Marc Zyngiere700e412011-11-03 11:13:12 +0900439 if (mct_int_type == MCT_INT_SPI)
Amit Daniel Kachhape248cd52011-12-08 10:07:08 +0900440 if (cpu == 0)
441 remove_irq(evt->irq, &mct_tick0_event_irq);
442 else
443 remove_irq(evt->irq, &mct_tick1_event_irq);
Marc Zyngiere700e412011-11-03 11:13:12 +0900444 else
445 disable_percpu_irq(IRQ_MCT_LOCALTIMER);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900446}
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900447#endif /* CONFIG_LOCAL_TIMERS */
448
449static void __init exynos4_timer_resources(void)
450{
451 struct clk *mct_clk;
452 mct_clk = clk_get(NULL, "xtal");
453
454 clk_rate = clk_get_rate(mct_clk);
Marc Zyngiere700e412011-11-03 11:13:12 +0900455
Kukjin Kim991a6c72011-12-08 10:04:49 +0900456#ifdef CONFIG_LOCAL_TIMERS
Marc Zyngiere700e412011-11-03 11:13:12 +0900457 if (mct_int_type == MCT_INT_PPI) {
458 int err;
459
460 err = request_percpu_irq(IRQ_MCT_LOCALTIMER,
461 exynos4_mct_tick_isr, "MCT",
462 &percpu_mct_tick);
463 WARN(err, "MCT: can't request IRQ %d (%d)\n",
464 IRQ_MCT_LOCALTIMER, err);
465 }
Kukjin Kim991a6c72011-12-08 10:04:49 +0900466#endif /* CONFIG_LOCAL_TIMERS */
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900467}
468
469static void __init exynos4_timer_init(void)
470{
Changhwan Youn3a062282011-10-04 17:02:58 +0900471 if (soc_is_exynos4210())
472 mct_int_type = MCT_INT_SPI;
473 else
474 mct_int_type = MCT_INT_PPI;
475
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900476 exynos4_timer_resources();
477 exynos4_clocksource_init();
478 exynos4_clockevent_init();
479}
480
481struct sys_timer exynos4_timer = {
482 .init = exynos4_timer_init,
483};