blob: 1016515dc9a83a4ed444eb7acf09dd91f290936c [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 Youn3a062282011-10-04 17:02:58 +090032enum {
33 MCT_INT_SPI,
34 MCT_INT_PPI
35};
36
Changhwan Youn30d8bea2011-03-11 10:39:57 +090037static unsigned long clk_cnt_per_tick;
38static unsigned long clk_rate;
Changhwan Youn3a062282011-10-04 17:02:58 +090039static unsigned int mct_int_type;
Changhwan Youn30d8bea2011-03-11 10:39:57 +090040
41struct mct_clock_event_device {
42 struct clock_event_device *evt;
43 void __iomem *base;
Changhwan Younc8987472011-10-04 17:09:26 +090044 char name[10];
Changhwan Youn30d8bea2011-03-11 10:39:57 +090045};
46
Changhwan Youn30d8bea2011-03-11 10:39:57 +090047static void exynos4_mct_write(unsigned int value, void *addr)
48{
49 void __iomem *stat_addr;
50 u32 mask;
51 u32 i;
52
53 __raw_writel(value, addr);
54
Changhwan Younc8987472011-10-04 17:09:26 +090055 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 */
61 break;
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 */
65 break;
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 */
69 break;
70 default:
71 return;
72 }
73 } else {
74 switch ((u32) addr) {
75 case (u32) EXYNOS4_MCT_G_TCON:
76 stat_addr = EXYNOS4_MCT_G_WSTAT;
77 mask = 1 << 16; /* G_TCON write status */
78 break;
79 case (u32) EXYNOS4_MCT_G_COMP0_L:
80 stat_addr = EXYNOS4_MCT_G_WSTAT;
81 mask = 1 << 0; /* G_COMP0_L write status */
82 break;
83 case (u32) EXYNOS4_MCT_G_COMP0_U:
84 stat_addr = EXYNOS4_MCT_G_WSTAT;
85 mask = 1 << 1; /* G_COMP0_U write status */
86 break;
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 */
90 break;
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 */
94 break;
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 */
98 break;
99 default:
100 return;
101 }
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900102 }
103
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);
108 return;
109 }
110
111 panic("MCT hangs after writing %d (addr:0x%08x)\n", value, (u32)addr);
112}
113
114/* Clocksource handling */
115static void exynos4_mct_frc_start(u32 hi, u32 lo)
116{
117 u32 reg;
118
119 exynos4_mct_write(lo, EXYNOS4_MCT_G_CNT_L);
120 exynos4_mct_write(hi, EXYNOS4_MCT_G_CNT_U);
121
122 reg = __raw_readl(EXYNOS4_MCT_G_TCON);
123 reg |= MCT_G_TCON_START;
124 exynos4_mct_write(reg, EXYNOS4_MCT_G_TCON);
125}
126
127static cycle_t exynos4_frc_read(struct clocksource *cs)
128{
129 unsigned int lo, hi;
130 u32 hi2 = __raw_readl(EXYNOS4_MCT_G_CNT_U);
131
132 do {
133 hi = hi2;
134 lo = __raw_readl(EXYNOS4_MCT_G_CNT_L);
135 hi2 = __raw_readl(EXYNOS4_MCT_G_CNT_U);
136 } while (hi != hi2);
137
138 return ((cycle_t)hi << 32) | lo;
139}
140
Changhwan Younaa421c12011-09-02 14:10:52 +0900141static void exynos4_frc_resume(struct clocksource *cs)
142{
143 exynos4_mct_frc_start(0, 0);
144}
145
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900146struct clocksource mct_frc = {
147 .name = "mct-frc",
148 .rating = 400,
149 .read = exynos4_frc_read,
150 .mask = CLOCKSOURCE_MASK(64),
151 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Changhwan Younaa421c12011-09-02 14:10:52 +0900152 .resume = exynos4_frc_resume,
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900153};
154
155static void __init exynos4_clocksource_init(void)
156{
157 exynos4_mct_frc_start(0, 0);
158
159 if (clocksource_register_hz(&mct_frc, clk_rate))
160 panic("%s: can't register clocksource\n", mct_frc.name);
161}
162
163static void exynos4_mct_comp0_stop(void)
164{
165 unsigned int tcon;
166
167 tcon = __raw_readl(EXYNOS4_MCT_G_TCON);
168 tcon &= ~(MCT_G_TCON_COMP0_ENABLE | MCT_G_TCON_COMP0_AUTO_INC);
169
170 exynos4_mct_write(tcon, EXYNOS4_MCT_G_TCON);
171 exynos4_mct_write(0, EXYNOS4_MCT_G_INT_ENB);
172}
173
174static void exynos4_mct_comp0_start(enum clock_event_mode mode,
175 unsigned long cycles)
176{
177 unsigned int tcon;
178 cycle_t comp_cycle;
179
180 tcon = __raw_readl(EXYNOS4_MCT_G_TCON);
181
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);
185 }
186
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);
190
191 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_ENB);
192
193 tcon |= MCT_G_TCON_COMP0_ENABLE;
194 exynos4_mct_write(tcon , EXYNOS4_MCT_G_TCON);
195}
196
197static int exynos4_comp_set_next_event(unsigned long cycles,
198 struct clock_event_device *evt)
199{
200 exynos4_mct_comp0_start(evt->mode, cycles);
201
202 return 0;
203}
204
205static void exynos4_comp_set_mode(enum clock_event_mode mode,
206 struct clock_event_device *evt)
207{
208 exynos4_mct_comp0_stop();
209
210 switch (mode) {
211 case CLOCK_EVT_MODE_PERIODIC:
212 exynos4_mct_comp0_start(mode, clk_cnt_per_tick);
213 break;
214
215 case CLOCK_EVT_MODE_ONESHOT:
216 case CLOCK_EVT_MODE_UNUSED:
217 case CLOCK_EVT_MODE_SHUTDOWN:
218 case CLOCK_EVT_MODE_RESUME:
219 break;
220 }
221}
222
223static struct clock_event_device mct_comp_device = {
224 .name = "mct-comp",
225 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
226 .rating = 250,
227 .set_next_event = exynos4_comp_set_next_event,
228 .set_mode = exynos4_comp_set_mode,
229};
230
231static irqreturn_t exynos4_mct_comp_isr(int irq, void *dev_id)
232{
233 struct clock_event_device *evt = dev_id;
234
235 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_CSTAT);
236
237 evt->event_handler(evt);
238
239 return IRQ_HANDLED;
240}
241
242static 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,
247};
248
249static void exynos4_clockevent_init(void)
250{
251 clk_cnt_per_tick = clk_rate / 2 / HZ;
252
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);
260
Kukjin Kimbb19a752012-01-25 13:48:11 +0900261 if (soc_is_exynos5250())
262 setup_irq(EXYNOS5_IRQ_MCT_G0, &mct_comp_event_irq);
263 else
264 setup_irq(EXYNOS4_IRQ_MCT_G0, &mct_comp_event_irq);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900265}
266
267#ifdef CONFIG_LOCAL_TIMERS
Kukjin Kim991a6c72011-12-08 10:04:49 +0900268
269static DEFINE_PER_CPU(struct mct_clock_event_device, percpu_mct_tick);
270
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900271/* Clock event handling */
272static void exynos4_mct_tick_stop(struct mct_clock_event_device *mevt)
273{
274 unsigned long tmp;
275 unsigned long mask = MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START;
276 void __iomem *addr = mevt->base + MCT_L_TCON_OFFSET;
277
278 tmp = __raw_readl(addr);
279 if (tmp & mask) {
280 tmp &= ~mask;
281 exynos4_mct_write(tmp, addr);
282 }
283}
284
285static void exynos4_mct_tick_start(unsigned long cycles,
286 struct mct_clock_event_device *mevt)
287{
288 unsigned long tmp;
289
290 exynos4_mct_tick_stop(mevt);
291
292 tmp = (1 << 31) | cycles; /* MCT_L_UPDATE_ICNTB */
293
294 /* update interrupt count buffer */
295 exynos4_mct_write(tmp, mevt->base + MCT_L_ICNTB_OFFSET);
296
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300297 /* enable MCT tick interrupt */
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900298 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_ENB_OFFSET);
299
300 tmp = __raw_readl(mevt->base + MCT_L_TCON_OFFSET);
301 tmp |= MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START |
302 MCT_L_TCON_INTERVAL_MODE;
303 exynos4_mct_write(tmp, mevt->base + MCT_L_TCON_OFFSET);
304}
305
306static int exynos4_tick_set_next_event(unsigned long cycles,
307 struct clock_event_device *evt)
308{
Marc Zyngiere700e412011-11-03 11:13:12 +0900309 struct mct_clock_event_device *mevt = this_cpu_ptr(&percpu_mct_tick);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900310
311 exynos4_mct_tick_start(cycles, mevt);
312
313 return 0;
314}
315
316static inline void exynos4_tick_set_mode(enum clock_event_mode mode,
317 struct clock_event_device *evt)
318{
Marc Zyngiere700e412011-11-03 11:13:12 +0900319 struct mct_clock_event_device *mevt = this_cpu_ptr(&percpu_mct_tick);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900320
321 exynos4_mct_tick_stop(mevt);
322
323 switch (mode) {
324 case CLOCK_EVT_MODE_PERIODIC:
325 exynos4_mct_tick_start(clk_cnt_per_tick, mevt);
326 break;
327
328 case CLOCK_EVT_MODE_ONESHOT:
329 case CLOCK_EVT_MODE_UNUSED:
330 case CLOCK_EVT_MODE_SHUTDOWN:
331 case CLOCK_EVT_MODE_RESUME:
332 break;
333 }
334}
335
Changhwan Younc8987472011-10-04 17:09:26 +0900336static int exynos4_mct_tick_clear(struct mct_clock_event_device *mevt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900337{
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900338 struct clock_event_device *evt = mevt->evt;
339
340 /*
341 * This is for supporting oneshot mode.
342 * Mct would generate interrupt periodically
343 * without explicit stopping.
344 */
345 if (evt->mode != CLOCK_EVT_MODE_PERIODIC)
346 exynos4_mct_tick_stop(mevt);
347
348 /* Clear the MCT tick interrupt */
Changhwan Youn3a062282011-10-04 17:02:58 +0900349 if (__raw_readl(mevt->base + MCT_L_INT_CSTAT_OFFSET) & 1) {
350 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_CSTAT_OFFSET);
351 return 1;
352 } else {
353 return 0;
354 }
355}
356
357static irqreturn_t exynos4_mct_tick_isr(int irq, void *dev_id)
358{
359 struct mct_clock_event_device *mevt = dev_id;
360 struct clock_event_device *evt = mevt->evt;
361
362 exynos4_mct_tick_clear(mevt);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900363
364 evt->event_handler(evt);
365
366 return IRQ_HANDLED;
367}
368
369static struct irqaction mct_tick0_event_irq = {
370 .name = "mct_tick0_irq",
371 .flags = IRQF_TIMER | IRQF_NOBALANCING,
372 .handler = exynos4_mct_tick_isr,
373};
374
375static struct irqaction mct_tick1_event_irq = {
376 .name = "mct_tick1_irq",
377 .flags = IRQF_TIMER | IRQF_NOBALANCING,
378 .handler = exynos4_mct_tick_isr,
379};
380
381static void exynos4_mct_tick_init(struct clock_event_device *evt)
382{
Marc Zyngiere700e412011-11-03 11:13:12 +0900383 struct mct_clock_event_device *mevt;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900384 unsigned int cpu = smp_processor_id();
385
Marc Zyngiere700e412011-11-03 11:13:12 +0900386 mevt = this_cpu_ptr(&percpu_mct_tick);
387 mevt->evt = evt;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900388
Marc Zyngiere700e412011-11-03 11:13:12 +0900389 mevt->base = EXYNOS4_MCT_L_BASE(cpu);
390 sprintf(mevt->name, "mct_tick%d", cpu);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900391
Marc Zyngiere700e412011-11-03 11:13:12 +0900392 evt->name = mevt->name;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900393 evt->cpumask = cpumask_of(cpu);
394 evt->set_next_event = exynos4_tick_set_next_event;
395 evt->set_mode = exynos4_tick_set_mode;
396 evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
397 evt->rating = 450;
398
399 clockevents_calc_mult_shift(evt, clk_rate / 2, 5);
400 evt->max_delta_ns =
401 clockevent_delta2ns(0x7fffffff, evt);
402 evt->min_delta_ns =
403 clockevent_delta2ns(0xf, evt);
404
405 clockevents_register_device(evt);
406
Marc Zyngiere700e412011-11-03 11:13:12 +0900407 exynos4_mct_write(0x1, mevt->base + MCT_L_TCNTB_OFFSET);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900408
Changhwan Youn3a062282011-10-04 17:02:58 +0900409 if (mct_int_type == MCT_INT_SPI) {
410 if (cpu == 0) {
Marc Zyngiere700e412011-11-03 11:13:12 +0900411 mct_tick0_event_irq.dev_id = mevt;
Kukjin Kimbb19a752012-01-25 13:48:11 +0900412 evt->irq = EXYNOS4_IRQ_MCT_L0;
413 setup_irq(EXYNOS4_IRQ_MCT_L0, &mct_tick0_event_irq);
Changhwan Youn3a062282011-10-04 17:02:58 +0900414 } else {
Marc Zyngiere700e412011-11-03 11:13:12 +0900415 mct_tick1_event_irq.dev_id = mevt;
Kukjin Kimbb19a752012-01-25 13:48:11 +0900416 evt->irq = EXYNOS4_IRQ_MCT_L1;
417 setup_irq(EXYNOS4_IRQ_MCT_L1, &mct_tick1_event_irq);
418 irq_set_affinity(EXYNOS4_IRQ_MCT_L1, cpumask_of(1));
Changhwan Youn3a062282011-10-04 17:02:58 +0900419 }
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900420 } else {
Kukjin Kimbb19a752012-01-25 13:48:11 +0900421 enable_percpu_irq(EXYNOS_IRQ_MCT_LOCALTIMER, 0);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900422 }
423}
424
425/* Setup the local clock events for a CPU */
Kukjin Kim4d487d72011-08-24 16:07:39 +0900426int __cpuinit local_timer_setup(struct clock_event_device *evt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900427{
428 exynos4_mct_tick_init(evt);
Kukjin Kim4d487d72011-08-24 16:07:39 +0900429
430 return 0;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900431}
432
Marc Zyngier28af6902011-07-22 12:52:37 +0100433void local_timer_stop(struct clock_event_device *evt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900434{
Amit Daniel Kachhape248cd52011-12-08 10:07:08 +0900435 unsigned int cpu = smp_processor_id();
Marc Zyngier28af6902011-07-22 12:52:37 +0100436 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
Marc Zyngiere700e412011-11-03 11:13:12 +0900437 if (mct_int_type == MCT_INT_SPI)
Amit Daniel Kachhape248cd52011-12-08 10:07:08 +0900438 if (cpu == 0)
439 remove_irq(evt->irq, &mct_tick0_event_irq);
440 else
441 remove_irq(evt->irq, &mct_tick1_event_irq);
Marc Zyngiere700e412011-11-03 11:13:12 +0900442 else
Kukjin Kimbb19a752012-01-25 13:48:11 +0900443 disable_percpu_irq(EXYNOS_IRQ_MCT_LOCALTIMER);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900444}
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900445#endif /* CONFIG_LOCAL_TIMERS */
446
447static void __init exynos4_timer_resources(void)
448{
449 struct clk *mct_clk;
450 mct_clk = clk_get(NULL, "xtal");
451
452 clk_rate = clk_get_rate(mct_clk);
Marc Zyngiere700e412011-11-03 11:13:12 +0900453
Kukjin Kim991a6c72011-12-08 10:04:49 +0900454#ifdef CONFIG_LOCAL_TIMERS
Marc Zyngiere700e412011-11-03 11:13:12 +0900455 if (mct_int_type == MCT_INT_PPI) {
456 int err;
457
Kukjin Kimbb19a752012-01-25 13:48:11 +0900458 err = request_percpu_irq(EXYNOS_IRQ_MCT_LOCALTIMER,
Marc Zyngiere700e412011-11-03 11:13:12 +0900459 exynos4_mct_tick_isr, "MCT",
460 &percpu_mct_tick);
461 WARN(err, "MCT: can't request IRQ %d (%d)\n",
Kukjin Kimbb19a752012-01-25 13:48:11 +0900462 EXYNOS_IRQ_MCT_LOCALTIMER, err);
Marc Zyngiere700e412011-11-03 11:13:12 +0900463 }
Kukjin Kim991a6c72011-12-08 10:04:49 +0900464#endif /* CONFIG_LOCAL_TIMERS */
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900465}
466
467static void __init exynos4_timer_init(void)
468{
Changhwan Youn3a062282011-10-04 17:02:58 +0900469 if (soc_is_exynos4210())
470 mct_int_type = MCT_INT_SPI;
471 else
472 mct_int_type = MCT_INT_PPI;
473
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900474 exynos4_timer_resources();
475 exynos4_clocksource_init();
476 exynos4_clockevent_init();
477}
478
479struct sys_timer exynos4_timer = {
480 .init = exynos4_timer_init,
481};