blob: 48f76bc05da0d8fb5b5515cd6c919a9cf3c373f6 [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>
Stephen Boydee98d272013-02-15 16:40:51 -080019#include <linux/cpu.h>
Changhwan Youn30d8bea2011-03-11 10:39:57 +090020#include <linux/platform_device.h>
21#include <linux/delay.h>
22#include <linux/percpu.h>
Kukjin Kim2edb36c2012-11-15 15:48:56 +090023#include <linux/of.h>
Thomas Abraham36ba5d52013-03-09 16:01:52 +090024#include <linux/of_irq.h>
25#include <linux/of_address.h>
Thomas Abraham9fbf0c82013-03-09 16:10:03 +090026#include <linux/clocksource.h>
Changhwan Youn30d8bea2011-03-11 10:39:57 +090027
Changhwan Youn30d8bea2011-03-11 10:39:57 +090028#include <asm/mach/time.h>
29
Thomas Abrahama1ba7a72013-03-09 16:01:47 +090030#define EXYNOS4_MCTREG(x) (x)
31#define EXYNOS4_MCT_G_CNT_L EXYNOS4_MCTREG(0x100)
32#define EXYNOS4_MCT_G_CNT_U EXYNOS4_MCTREG(0x104)
33#define EXYNOS4_MCT_G_CNT_WSTAT EXYNOS4_MCTREG(0x110)
34#define EXYNOS4_MCT_G_COMP0_L EXYNOS4_MCTREG(0x200)
35#define EXYNOS4_MCT_G_COMP0_U EXYNOS4_MCTREG(0x204)
36#define EXYNOS4_MCT_G_COMP0_ADD_INCR EXYNOS4_MCTREG(0x208)
37#define EXYNOS4_MCT_G_TCON EXYNOS4_MCTREG(0x240)
38#define EXYNOS4_MCT_G_INT_CSTAT EXYNOS4_MCTREG(0x244)
39#define EXYNOS4_MCT_G_INT_ENB EXYNOS4_MCTREG(0x248)
40#define EXYNOS4_MCT_G_WSTAT EXYNOS4_MCTREG(0x24C)
41#define _EXYNOS4_MCT_L_BASE EXYNOS4_MCTREG(0x300)
42#define EXYNOS4_MCT_L_BASE(x) (_EXYNOS4_MCT_L_BASE + (0x100 * x))
43#define EXYNOS4_MCT_L_MASK (0xffffff00)
44
45#define MCT_L_TCNTB_OFFSET (0x00)
46#define MCT_L_ICNTB_OFFSET (0x08)
47#define MCT_L_TCON_OFFSET (0x20)
48#define MCT_L_INT_CSTAT_OFFSET (0x30)
49#define MCT_L_INT_ENB_OFFSET (0x34)
50#define MCT_L_WSTAT_OFFSET (0x40)
51#define MCT_G_TCON_START (1 << 8)
52#define MCT_G_TCON_COMP0_AUTO_INC (1 << 1)
53#define MCT_G_TCON_COMP0_ENABLE (1 << 0)
54#define MCT_L_TCON_INTERVAL_MODE (1 << 2)
55#define MCT_L_TCON_INT_START (1 << 1)
56#define MCT_L_TCON_TIMER_START (1 << 0)
57
Changhwan Youn4d2e4d72012-03-09 15:09:21 -080058#define TICK_BASE_CNT 1
59
Changhwan Youn3a062282011-10-04 17:02:58 +090060enum {
61 MCT_INT_SPI,
62 MCT_INT_PPI
63};
64
Thomas Abrahamc371dc62013-03-09 16:01:50 +090065enum {
66 MCT_G0_IRQ,
67 MCT_G1_IRQ,
68 MCT_G2_IRQ,
69 MCT_G3_IRQ,
70 MCT_L0_IRQ,
71 MCT_L1_IRQ,
72 MCT_L2_IRQ,
73 MCT_L3_IRQ,
Chander Kashyap6c16ded2013-12-02 07:48:23 +090074 MCT_L4_IRQ,
75 MCT_L5_IRQ,
76 MCT_L6_IRQ,
77 MCT_L7_IRQ,
Thomas Abrahamc371dc62013-03-09 16:01:50 +090078 MCT_NR_IRQS,
79};
80
Thomas Abrahama1ba7a72013-03-09 16:01:47 +090081static void __iomem *reg_base;
Changhwan Youn30d8bea2011-03-11 10:39:57 +090082static unsigned long clk_rate;
Changhwan Youn3a062282011-10-04 17:02:58 +090083static unsigned int mct_int_type;
Thomas Abrahamc371dc62013-03-09 16:01:50 +090084static int mct_irqs[MCT_NR_IRQS];
Changhwan Youn30d8bea2011-03-11 10:39:57 +090085
86struct mct_clock_event_device {
Stephen Boydee98d272013-02-15 16:40:51 -080087 struct clock_event_device evt;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +090088 unsigned long base;
Changhwan Younc8987472011-10-04 17:09:26 +090089 char name[10];
Changhwan Youn30d8bea2011-03-11 10:39:57 +090090};
91
Thomas Abrahama1ba7a72013-03-09 16:01:47 +090092static void exynos4_mct_write(unsigned int value, unsigned long offset)
Changhwan Youn30d8bea2011-03-11 10:39:57 +090093{
Thomas Abrahama1ba7a72013-03-09 16:01:47 +090094 unsigned long stat_addr;
Changhwan Youn30d8bea2011-03-11 10:39:57 +090095 u32 mask;
96 u32 i;
97
Thomas Abrahama1ba7a72013-03-09 16:01:47 +090098 __raw_writel(value, reg_base + offset);
Changhwan Youn30d8bea2011-03-11 10:39:57 +090099
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900100 if (likely(offset >= EXYNOS4_MCT_L_BASE(0))) {
101 stat_addr = (offset & ~EXYNOS4_MCT_L_MASK) + MCT_L_WSTAT_OFFSET;
102 switch (offset & EXYNOS4_MCT_L_MASK) {
103 case MCT_L_TCON_OFFSET:
Changhwan Younc8987472011-10-04 17:09:26 +0900104 mask = 1 << 3; /* L_TCON write status */
105 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900106 case MCT_L_ICNTB_OFFSET:
Changhwan Younc8987472011-10-04 17:09:26 +0900107 mask = 1 << 1; /* L_ICNTB write status */
108 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900109 case MCT_L_TCNTB_OFFSET:
Changhwan Younc8987472011-10-04 17:09:26 +0900110 mask = 1 << 0; /* L_TCNTB write status */
111 break;
112 default:
113 return;
114 }
115 } else {
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900116 switch (offset) {
117 case EXYNOS4_MCT_G_TCON:
Changhwan Younc8987472011-10-04 17:09:26 +0900118 stat_addr = EXYNOS4_MCT_G_WSTAT;
119 mask = 1 << 16; /* G_TCON write status */
120 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900121 case EXYNOS4_MCT_G_COMP0_L:
Changhwan Younc8987472011-10-04 17:09:26 +0900122 stat_addr = EXYNOS4_MCT_G_WSTAT;
123 mask = 1 << 0; /* G_COMP0_L write status */
124 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900125 case EXYNOS4_MCT_G_COMP0_U:
Changhwan Younc8987472011-10-04 17:09:26 +0900126 stat_addr = EXYNOS4_MCT_G_WSTAT;
127 mask = 1 << 1; /* G_COMP0_U write status */
128 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900129 case EXYNOS4_MCT_G_COMP0_ADD_INCR:
Changhwan Younc8987472011-10-04 17:09:26 +0900130 stat_addr = EXYNOS4_MCT_G_WSTAT;
131 mask = 1 << 2; /* G_COMP0_ADD_INCR w status */
132 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900133 case EXYNOS4_MCT_G_CNT_L:
Changhwan Younc8987472011-10-04 17:09:26 +0900134 stat_addr = EXYNOS4_MCT_G_CNT_WSTAT;
135 mask = 1 << 0; /* G_CNT_L write status */
136 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900137 case EXYNOS4_MCT_G_CNT_U:
Changhwan Younc8987472011-10-04 17:09:26 +0900138 stat_addr = EXYNOS4_MCT_G_CNT_WSTAT;
139 mask = 1 << 1; /* G_CNT_U write status */
140 break;
141 default:
142 return;
143 }
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900144 }
145
146 /* Wait maximum 1 ms until written values are applied */
147 for (i = 0; i < loops_per_jiffy / 1000 * HZ; i++)
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900148 if (__raw_readl(reg_base + stat_addr) & mask) {
149 __raw_writel(mask, reg_base + stat_addr);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900150 return;
151 }
152
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900153 panic("MCT hangs after writing %d (offset:0x%lx)\n", value, offset);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900154}
155
156/* Clocksource handling */
157static void exynos4_mct_frc_start(u32 hi, u32 lo)
158{
159 u32 reg;
160
161 exynos4_mct_write(lo, EXYNOS4_MCT_G_CNT_L);
162 exynos4_mct_write(hi, EXYNOS4_MCT_G_CNT_U);
163
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900164 reg = __raw_readl(reg_base + EXYNOS4_MCT_G_TCON);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900165 reg |= MCT_G_TCON_START;
166 exynos4_mct_write(reg, EXYNOS4_MCT_G_TCON);
167}
168
169static cycle_t exynos4_frc_read(struct clocksource *cs)
170{
171 unsigned int lo, hi;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900172 u32 hi2 = __raw_readl(reg_base + EXYNOS4_MCT_G_CNT_U);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900173
174 do {
175 hi = hi2;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900176 lo = __raw_readl(reg_base + EXYNOS4_MCT_G_CNT_L);
177 hi2 = __raw_readl(reg_base + EXYNOS4_MCT_G_CNT_U);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900178 } while (hi != hi2);
179
180 return ((cycle_t)hi << 32) | lo;
181}
182
Changhwan Younaa421c12011-09-02 14:10:52 +0900183static void exynos4_frc_resume(struct clocksource *cs)
184{
185 exynos4_mct_frc_start(0, 0);
186}
187
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900188struct clocksource mct_frc = {
189 .name = "mct-frc",
190 .rating = 400,
191 .read = exynos4_frc_read,
192 .mask = CLOCKSOURCE_MASK(64),
193 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Changhwan Younaa421c12011-09-02 14:10:52 +0900194 .resume = exynos4_frc_resume,
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900195};
196
197static void __init exynos4_clocksource_init(void)
198{
199 exynos4_mct_frc_start(0, 0);
200
201 if (clocksource_register_hz(&mct_frc, clk_rate))
202 panic("%s: can't register clocksource\n", mct_frc.name);
203}
204
205static void exynos4_mct_comp0_stop(void)
206{
207 unsigned int tcon;
208
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900209 tcon = __raw_readl(reg_base + EXYNOS4_MCT_G_TCON);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900210 tcon &= ~(MCT_G_TCON_COMP0_ENABLE | MCT_G_TCON_COMP0_AUTO_INC);
211
212 exynos4_mct_write(tcon, EXYNOS4_MCT_G_TCON);
213 exynos4_mct_write(0, EXYNOS4_MCT_G_INT_ENB);
214}
215
216static void exynos4_mct_comp0_start(enum clock_event_mode mode,
217 unsigned long cycles)
218{
219 unsigned int tcon;
220 cycle_t comp_cycle;
221
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900222 tcon = __raw_readl(reg_base + EXYNOS4_MCT_G_TCON);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900223
224 if (mode == CLOCK_EVT_MODE_PERIODIC) {
225 tcon |= MCT_G_TCON_COMP0_AUTO_INC;
226 exynos4_mct_write(cycles, EXYNOS4_MCT_G_COMP0_ADD_INCR);
227 }
228
229 comp_cycle = exynos4_frc_read(&mct_frc) + cycles;
230 exynos4_mct_write((u32)comp_cycle, EXYNOS4_MCT_G_COMP0_L);
231 exynos4_mct_write((u32)(comp_cycle >> 32), EXYNOS4_MCT_G_COMP0_U);
232
233 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_ENB);
234
235 tcon |= MCT_G_TCON_COMP0_ENABLE;
236 exynos4_mct_write(tcon , EXYNOS4_MCT_G_TCON);
237}
238
239static int exynos4_comp_set_next_event(unsigned long cycles,
240 struct clock_event_device *evt)
241{
242 exynos4_mct_comp0_start(evt->mode, cycles);
243
244 return 0;
245}
246
247static void exynos4_comp_set_mode(enum clock_event_mode mode,
248 struct clock_event_device *evt)
249{
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800250 unsigned long cycles_per_jiffy;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900251 exynos4_mct_comp0_stop();
252
253 switch (mode) {
254 case CLOCK_EVT_MODE_PERIODIC:
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800255 cycles_per_jiffy =
256 (((unsigned long long) NSEC_PER_SEC / HZ * evt->mult) >> evt->shift);
257 exynos4_mct_comp0_start(mode, cycles_per_jiffy);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900258 break;
259
260 case CLOCK_EVT_MODE_ONESHOT:
261 case CLOCK_EVT_MODE_UNUSED:
262 case CLOCK_EVT_MODE_SHUTDOWN:
263 case CLOCK_EVT_MODE_RESUME:
264 break;
265 }
266}
267
268static struct clock_event_device mct_comp_device = {
269 .name = "mct-comp",
270 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
271 .rating = 250,
272 .set_next_event = exynos4_comp_set_next_event,
273 .set_mode = exynos4_comp_set_mode,
274};
275
276static irqreturn_t exynos4_mct_comp_isr(int irq, void *dev_id)
277{
278 struct clock_event_device *evt = dev_id;
279
280 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_CSTAT);
281
282 evt->event_handler(evt);
283
284 return IRQ_HANDLED;
285}
286
287static struct irqaction mct_comp_event_irq = {
288 .name = "mct_comp_irq",
289 .flags = IRQF_TIMER | IRQF_IRQPOLL,
290 .handler = exynos4_mct_comp_isr,
291 .dev_id = &mct_comp_device,
292};
293
294static void exynos4_clockevent_init(void)
295{
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900296 mct_comp_device.cpumask = cpumask_of(0);
Shawn Guo838a2ae2013-01-12 11:50:05 +0000297 clockevents_config_and_register(&mct_comp_device, clk_rate,
298 0xf, 0xffffffff);
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900299 setup_irq(mct_irqs[MCT_G0_IRQ], &mct_comp_event_irq);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900300}
301
Kukjin Kim991a6c72011-12-08 10:04:49 +0900302static DEFINE_PER_CPU(struct mct_clock_event_device, percpu_mct_tick);
303
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900304/* Clock event handling */
305static void exynos4_mct_tick_stop(struct mct_clock_event_device *mevt)
306{
307 unsigned long tmp;
308 unsigned long mask = MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900309 unsigned long offset = mevt->base + MCT_L_TCON_OFFSET;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900310
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900311 tmp = __raw_readl(reg_base + offset);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900312 if (tmp & mask) {
313 tmp &= ~mask;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900314 exynos4_mct_write(tmp, offset);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900315 }
316}
317
318static void exynos4_mct_tick_start(unsigned long cycles,
319 struct mct_clock_event_device *mevt)
320{
321 unsigned long tmp;
322
323 exynos4_mct_tick_stop(mevt);
324
325 tmp = (1 << 31) | cycles; /* MCT_L_UPDATE_ICNTB */
326
327 /* update interrupt count buffer */
328 exynos4_mct_write(tmp, mevt->base + MCT_L_ICNTB_OFFSET);
329
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300330 /* enable MCT tick interrupt */
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900331 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_ENB_OFFSET);
332
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900333 tmp = __raw_readl(reg_base + mevt->base + MCT_L_TCON_OFFSET);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900334 tmp |= MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START |
335 MCT_L_TCON_INTERVAL_MODE;
336 exynos4_mct_write(tmp, mevt->base + MCT_L_TCON_OFFSET);
337}
338
339static int exynos4_tick_set_next_event(unsigned long cycles,
340 struct clock_event_device *evt)
341{
Marc Zyngiere700e412011-11-03 11:13:12 +0900342 struct mct_clock_event_device *mevt = this_cpu_ptr(&percpu_mct_tick);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900343
344 exynos4_mct_tick_start(cycles, mevt);
345
346 return 0;
347}
348
349static inline void exynos4_tick_set_mode(enum clock_event_mode mode,
350 struct clock_event_device *evt)
351{
Marc Zyngiere700e412011-11-03 11:13:12 +0900352 struct mct_clock_event_device *mevt = this_cpu_ptr(&percpu_mct_tick);
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800353 unsigned long cycles_per_jiffy;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900354
355 exynos4_mct_tick_stop(mevt);
356
357 switch (mode) {
358 case CLOCK_EVT_MODE_PERIODIC:
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800359 cycles_per_jiffy =
360 (((unsigned long long) NSEC_PER_SEC / HZ * evt->mult) >> evt->shift);
361 exynos4_mct_tick_start(cycles_per_jiffy, mevt);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900362 break;
363
364 case CLOCK_EVT_MODE_ONESHOT:
365 case CLOCK_EVT_MODE_UNUSED:
366 case CLOCK_EVT_MODE_SHUTDOWN:
367 case CLOCK_EVT_MODE_RESUME:
368 break;
369 }
370}
371
Changhwan Younc8987472011-10-04 17:09:26 +0900372static int exynos4_mct_tick_clear(struct mct_clock_event_device *mevt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900373{
Stephen Boydee98d272013-02-15 16:40:51 -0800374 struct clock_event_device *evt = &mevt->evt;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900375
376 /*
377 * This is for supporting oneshot mode.
378 * Mct would generate interrupt periodically
379 * without explicit stopping.
380 */
381 if (evt->mode != CLOCK_EVT_MODE_PERIODIC)
382 exynos4_mct_tick_stop(mevt);
383
384 /* Clear the MCT tick interrupt */
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900385 if (__raw_readl(reg_base + mevt->base + MCT_L_INT_CSTAT_OFFSET) & 1) {
Changhwan Youn3a062282011-10-04 17:02:58 +0900386 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_CSTAT_OFFSET);
387 return 1;
388 } else {
389 return 0;
390 }
391}
392
393static irqreturn_t exynos4_mct_tick_isr(int irq, void *dev_id)
394{
395 struct mct_clock_event_device *mevt = dev_id;
Stephen Boydee98d272013-02-15 16:40:51 -0800396 struct clock_event_device *evt = &mevt->evt;
Changhwan Youn3a062282011-10-04 17:02:58 +0900397
398 exynos4_mct_tick_clear(mevt);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900399
400 evt->event_handler(evt);
401
402 return IRQ_HANDLED;
403}
404
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400405static int exynos4_local_timer_setup(struct clock_event_device *evt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900406{
Marc Zyngiere700e412011-11-03 11:13:12 +0900407 struct mct_clock_event_device *mevt;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900408 unsigned int cpu = smp_processor_id();
409
Stephen Boydee98d272013-02-15 16:40:51 -0800410 mevt = container_of(evt, struct mct_clock_event_device, evt);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900411
Marc Zyngiere700e412011-11-03 11:13:12 +0900412 mevt->base = EXYNOS4_MCT_L_BASE(cpu);
413 sprintf(mevt->name, "mct_tick%d", cpu);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900414
Marc Zyngiere700e412011-11-03 11:13:12 +0900415 evt->name = mevt->name;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900416 evt->cpumask = cpumask_of(cpu);
417 evt->set_next_event = exynos4_tick_set_next_event;
418 evt->set_mode = exynos4_tick_set_mode;
419 evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
420 evt->rating = 450;
Shawn Guo838a2ae2013-01-12 11:50:05 +0000421 clockevents_config_and_register(evt, clk_rate / (TICK_BASE_CNT + 1),
422 0xf, 0x7fffffff);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900423
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800424 exynos4_mct_write(TICK_BASE_CNT, mevt->base + MCT_L_TCNTB_OFFSET);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900425
Changhwan Youn3a062282011-10-04 17:02:58 +0900426 if (mct_int_type == MCT_INT_SPI) {
Chander Kashyap7114cd72013-06-19 00:29:35 +0900427 evt->irq = mct_irqs[MCT_L0_IRQ + cpu];
428 if (request_irq(evt->irq, exynos4_mct_tick_isr,
429 IRQF_TIMER | IRQF_NOBALANCING,
430 evt->name, mevt)) {
431 pr_err("exynos-mct: cannot register IRQ %d\n",
432 evt->irq);
433 return -EIO;
Changhwan Youn3a062282011-10-04 17:02:58 +0900434 }
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900435 } else {
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900436 enable_percpu_irq(mct_irqs[MCT_L0_IRQ], 0);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900437 }
Kukjin Kim4d487d72011-08-24 16:07:39 +0900438
439 return 0;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900440}
441
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000442static void exynos4_local_timer_stop(struct clock_event_device *evt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900443{
Marc Zyngier28af6902011-07-22 12:52:37 +0100444 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
Marc Zyngiere700e412011-11-03 11:13:12 +0900445 if (mct_int_type == MCT_INT_SPI)
Chander Kashyap7114cd72013-06-19 00:29:35 +0900446 free_irq(evt->irq, this_cpu_ptr(&percpu_mct_tick));
Marc Zyngiere700e412011-11-03 11:13:12 +0900447 else
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900448 disable_percpu_irq(mct_irqs[MCT_L0_IRQ]);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900449}
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000450
Olof Johansson47dcd352013-07-23 14:51:34 -0700451static int exynos4_mct_cpu_notify(struct notifier_block *self,
Stephen Boydee98d272013-02-15 16:40:51 -0800452 unsigned long action, void *hcpu)
453{
454 struct mct_clock_event_device *mevt;
Tomasz Figa5df718d2013-09-25 12:00:59 +0200455 unsigned int cpu;
Stephen Boydee98d272013-02-15 16:40:51 -0800456
457 /*
458 * Grab cpu pointer in each case to avoid spurious
459 * preemptible warnings
460 */
461 switch (action & ~CPU_TASKS_FROZEN) {
462 case CPU_STARTING:
463 mevt = this_cpu_ptr(&percpu_mct_tick);
464 exynos4_local_timer_setup(&mevt->evt);
465 break;
Tomasz Figa5df718d2013-09-25 12:00:59 +0200466 case CPU_ONLINE:
467 cpu = (unsigned long)hcpu;
468 if (mct_int_type == MCT_INT_SPI)
469 irq_set_affinity(mct_irqs[MCT_L0_IRQ + cpu],
470 cpumask_of(cpu));
471 break;
Stephen Boydee98d272013-02-15 16:40:51 -0800472 case CPU_DYING:
473 mevt = this_cpu_ptr(&percpu_mct_tick);
474 exynos4_local_timer_stop(&mevt->evt);
475 break;
476 }
477
478 return NOTIFY_OK;
479}
480
Olof Johansson47dcd352013-07-23 14:51:34 -0700481static struct notifier_block exynos4_mct_cpu_nb = {
Stephen Boydee98d272013-02-15 16:40:51 -0800482 .notifier_call = exynos4_mct_cpu_notify,
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000483};
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900484
Arnd Bergmann19ce4f42013-04-09 22:24:06 +0200485static void __init exynos4_timer_resources(struct device_node *np, void __iomem *base)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900486{
Stephen Boydee98d272013-02-15 16:40:51 -0800487 int err;
488 struct mct_clock_event_device *mevt = this_cpu_ptr(&percpu_mct_tick);
Thomas Abrahamca9048e2013-03-09 17:10:37 +0900489 struct clk *mct_clk, *tick_clk;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900490
Thomas Abraham415ac2e2013-03-09 17:10:31 +0900491 tick_clk = np ? of_clk_get_by_name(np, "fin_pll") :
492 clk_get(NULL, "fin_pll");
493 if (IS_ERR(tick_clk))
494 panic("%s: unable to determine tick clock rate\n", __func__);
495 clk_rate = clk_get_rate(tick_clk);
Marc Zyngiere700e412011-11-03 11:13:12 +0900496
Thomas Abrahamca9048e2013-03-09 17:10:37 +0900497 mct_clk = np ? of_clk_get_by_name(np, "mct") : clk_get(NULL, "mct");
498 if (IS_ERR(mct_clk))
499 panic("%s: unable to retrieve mct clock instance\n", __func__);
500 clk_prepare_enable(mct_clk);
Marc Zyngiere700e412011-11-03 11:13:12 +0900501
Arnd Bergmann228e3022013-04-09 22:07:37 +0200502 reg_base = base;
Thomas Abraham36ba5d52013-03-09 16:01:52 +0900503 if (!reg_base)
504 panic("%s: unable to ioremap mct address space\n", __func__);
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900505
Marc Zyngiere700e412011-11-03 11:13:12 +0900506 if (mct_int_type == MCT_INT_PPI) {
Marc Zyngiere700e412011-11-03 11:13:12 +0900507
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900508 err = request_percpu_irq(mct_irqs[MCT_L0_IRQ],
Marc Zyngiere700e412011-11-03 11:13:12 +0900509 exynos4_mct_tick_isr, "MCT",
510 &percpu_mct_tick);
511 WARN(err, "MCT: can't request IRQ %d (%d)\n",
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900512 mct_irqs[MCT_L0_IRQ], err);
Tomasz Figa5df718d2013-09-25 12:00:59 +0200513 } else {
514 irq_set_affinity(mct_irqs[MCT_L0_IRQ], cpumask_of(0));
Marc Zyngiere700e412011-11-03 11:13:12 +0900515 }
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000516
Stephen Boydee98d272013-02-15 16:40:51 -0800517 err = register_cpu_notifier(&exynos4_mct_cpu_nb);
518 if (err)
519 goto out_irq;
520
521 /* Immediately configure the timer on the boot CPU */
522 exynos4_local_timer_setup(&mevt->evt);
523 return;
524
525out_irq:
526 free_percpu_irq(mct_irqs[MCT_L0_IRQ], &percpu_mct_tick);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900527}
528
Arnd Bergmann034c0972013-04-10 11:35:29 +0200529void __init mct_init(void __iomem *base, int irq_g0, int irq_l0, int irq_l1)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900530{
Arnd Bergmann034c0972013-04-10 11:35:29 +0200531 mct_irqs[MCT_G0_IRQ] = irq_g0;
532 mct_irqs[MCT_L0_IRQ] = irq_l0;
533 mct_irqs[MCT_L1_IRQ] = irq_l1;
534 mct_int_type = MCT_INT_SPI;
Kukjin Kim2edb36c2012-11-15 15:48:56 +0900535
Arnd Bergmann034c0972013-04-10 11:35:29 +0200536 exynos4_timer_resources(NULL, base);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900537 exynos4_clocksource_init();
538 exynos4_clockevent_init();
539}
Arnd Bergmann228e3022013-04-09 22:07:37 +0200540
541static void __init mct_init_dt(struct device_node *np, unsigned int int_type)
542{
543 u32 nr_irqs, i;
544
545 mct_int_type = int_type;
546
547 /* This driver uses only one global timer interrupt */
548 mct_irqs[MCT_G0_IRQ] = irq_of_parse_and_map(np, MCT_G0_IRQ);
549
550 /*
551 * Find out the number of local irqs specified. The local
552 * timer irqs are specified after the four global timer
553 * irqs are specified.
554 */
Arnd Bergmannf4636d02013-04-19 22:00:04 +0200555#ifdef CONFIG_OF
Arnd Bergmann228e3022013-04-09 22:07:37 +0200556 nr_irqs = of_irq_count(np);
Arnd Bergmannf4636d02013-04-19 22:00:04 +0200557#else
558 nr_irqs = 0;
559#endif
Arnd Bergmann228e3022013-04-09 22:07:37 +0200560 for (i = MCT_L0_IRQ; i < nr_irqs; i++)
561 mct_irqs[i] = irq_of_parse_and_map(np, i);
562
Arnd Bergmann19ce4f42013-04-09 22:24:06 +0200563 exynos4_timer_resources(np, of_iomap(np, 0));
Arnd Bergmann228e3022013-04-09 22:07:37 +0200564 exynos4_clocksource_init();
565 exynos4_clockevent_init();
566}
567
568
569static void __init mct_init_spi(struct device_node *np)
570{
571 return mct_init_dt(np, MCT_INT_SPI);
572}
573
574static void __init mct_init_ppi(struct device_node *np)
575{
576 return mct_init_dt(np, MCT_INT_PPI);
577}
578CLOCKSOURCE_OF_DECLARE(exynos4210, "samsung,exynos4210-mct", mct_init_spi);
579CLOCKSOURCE_OF_DECLARE(exynos4412, "samsung,exynos4412-mct", mct_init_ppi);