blob: 203ac05e2b3d9b4631dd5050f2cebfaf0d705d48 [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>
Kukjin Kim2edb36c2012-11-15 15:48:56 +090022#include <linux/of.h>
Thomas Abraham36ba5d52013-03-09 16:01:52 +090023#include <linux/of_irq.h>
24#include <linux/of_address.h>
Thomas Abraham9fbf0c82013-03-09 16:10:03 +090025#include <linux/clocksource.h>
Changhwan Youn30d8bea2011-03-11 10:39:57 +090026
Kukjin Kim2edb36c2012-11-15 15:48:56 +090027#include <asm/arch_timer.h>
Marc Zyngiera8cb6042012-01-10 19:44:19 +000028#include <asm/localtimer.h>
Changhwan Youn3a062282011-10-04 17:02:58 +090029
30#include <plat/cpu.h>
31
Changhwan Youn30d8bea2011-03-11 10:39:57 +090032#include <mach/map.h>
Changhwan Youn3a062282011-10-04 17:02:58 +090033#include <mach/irqs.h>
Changhwan Youn30d8bea2011-03-11 10:39:57 +090034#include <asm/mach/time.h>
35
Thomas Abrahama1ba7a72013-03-09 16:01:47 +090036#define EXYNOS4_MCTREG(x) (x)
37#define EXYNOS4_MCT_G_CNT_L EXYNOS4_MCTREG(0x100)
38#define EXYNOS4_MCT_G_CNT_U EXYNOS4_MCTREG(0x104)
39#define EXYNOS4_MCT_G_CNT_WSTAT EXYNOS4_MCTREG(0x110)
40#define EXYNOS4_MCT_G_COMP0_L EXYNOS4_MCTREG(0x200)
41#define EXYNOS4_MCT_G_COMP0_U EXYNOS4_MCTREG(0x204)
42#define EXYNOS4_MCT_G_COMP0_ADD_INCR EXYNOS4_MCTREG(0x208)
43#define EXYNOS4_MCT_G_TCON EXYNOS4_MCTREG(0x240)
44#define EXYNOS4_MCT_G_INT_CSTAT EXYNOS4_MCTREG(0x244)
45#define EXYNOS4_MCT_G_INT_ENB EXYNOS4_MCTREG(0x248)
46#define EXYNOS4_MCT_G_WSTAT EXYNOS4_MCTREG(0x24C)
47#define _EXYNOS4_MCT_L_BASE EXYNOS4_MCTREG(0x300)
48#define EXYNOS4_MCT_L_BASE(x) (_EXYNOS4_MCT_L_BASE + (0x100 * x))
49#define EXYNOS4_MCT_L_MASK (0xffffff00)
50
51#define MCT_L_TCNTB_OFFSET (0x00)
52#define MCT_L_ICNTB_OFFSET (0x08)
53#define MCT_L_TCON_OFFSET (0x20)
54#define MCT_L_INT_CSTAT_OFFSET (0x30)
55#define MCT_L_INT_ENB_OFFSET (0x34)
56#define MCT_L_WSTAT_OFFSET (0x40)
57#define MCT_G_TCON_START (1 << 8)
58#define MCT_G_TCON_COMP0_AUTO_INC (1 << 1)
59#define MCT_G_TCON_COMP0_ENABLE (1 << 0)
60#define MCT_L_TCON_INTERVAL_MODE (1 << 2)
61#define MCT_L_TCON_INT_START (1 << 1)
62#define MCT_L_TCON_TIMER_START (1 << 0)
63
Changhwan Youn4d2e4d72012-03-09 15:09:21 -080064#define TICK_BASE_CNT 1
65
Changhwan Youn3a062282011-10-04 17:02:58 +090066enum {
67 MCT_INT_SPI,
68 MCT_INT_PPI
69};
70
Thomas Abrahamc371dc62013-03-09 16:01:50 +090071enum {
72 MCT_G0_IRQ,
73 MCT_G1_IRQ,
74 MCT_G2_IRQ,
75 MCT_G3_IRQ,
76 MCT_L0_IRQ,
77 MCT_L1_IRQ,
78 MCT_L2_IRQ,
79 MCT_L3_IRQ,
80 MCT_NR_IRQS,
81};
82
Thomas Abrahama1ba7a72013-03-09 16:01:47 +090083static void __iomem *reg_base;
Changhwan Youn30d8bea2011-03-11 10:39:57 +090084static unsigned long clk_rate;
Changhwan Youn3a062282011-10-04 17:02:58 +090085static unsigned int mct_int_type;
Thomas Abrahamc371dc62013-03-09 16:01:50 +090086static int mct_irqs[MCT_NR_IRQS];
Changhwan Youn30d8bea2011-03-11 10:39:57 +090087
88struct mct_clock_event_device {
89 struct clock_event_device *evt;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +090090 unsigned long base;
Changhwan Younc8987472011-10-04 17:09:26 +090091 char name[10];
Changhwan Youn30d8bea2011-03-11 10:39:57 +090092};
93
Thomas Abrahama1ba7a72013-03-09 16:01:47 +090094static void exynos4_mct_write(unsigned int value, unsigned long offset)
Changhwan Youn30d8bea2011-03-11 10:39:57 +090095{
Thomas Abrahama1ba7a72013-03-09 16:01:47 +090096 unsigned long stat_addr;
Changhwan Youn30d8bea2011-03-11 10:39:57 +090097 u32 mask;
98 u32 i;
99
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900100 __raw_writel(value, reg_base + offset);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900101
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900102 if (likely(offset >= EXYNOS4_MCT_L_BASE(0))) {
103 stat_addr = (offset & ~EXYNOS4_MCT_L_MASK) + MCT_L_WSTAT_OFFSET;
104 switch (offset & EXYNOS4_MCT_L_MASK) {
105 case MCT_L_TCON_OFFSET:
Changhwan Younc8987472011-10-04 17:09:26 +0900106 mask = 1 << 3; /* L_TCON write status */
107 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900108 case MCT_L_ICNTB_OFFSET:
Changhwan Younc8987472011-10-04 17:09:26 +0900109 mask = 1 << 1; /* L_ICNTB write status */
110 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900111 case MCT_L_TCNTB_OFFSET:
Changhwan Younc8987472011-10-04 17:09:26 +0900112 mask = 1 << 0; /* L_TCNTB write status */
113 break;
114 default:
115 return;
116 }
117 } else {
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900118 switch (offset) {
119 case EXYNOS4_MCT_G_TCON:
Changhwan Younc8987472011-10-04 17:09:26 +0900120 stat_addr = EXYNOS4_MCT_G_WSTAT;
121 mask = 1 << 16; /* G_TCON write status */
122 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900123 case EXYNOS4_MCT_G_COMP0_L:
Changhwan Younc8987472011-10-04 17:09:26 +0900124 stat_addr = EXYNOS4_MCT_G_WSTAT;
125 mask = 1 << 0; /* G_COMP0_L write status */
126 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900127 case EXYNOS4_MCT_G_COMP0_U:
Changhwan Younc8987472011-10-04 17:09:26 +0900128 stat_addr = EXYNOS4_MCT_G_WSTAT;
129 mask = 1 << 1; /* G_COMP0_U write status */
130 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900131 case EXYNOS4_MCT_G_COMP0_ADD_INCR:
Changhwan Younc8987472011-10-04 17:09:26 +0900132 stat_addr = EXYNOS4_MCT_G_WSTAT;
133 mask = 1 << 2; /* G_COMP0_ADD_INCR w status */
134 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900135 case EXYNOS4_MCT_G_CNT_L:
Changhwan Younc8987472011-10-04 17:09:26 +0900136 stat_addr = EXYNOS4_MCT_G_CNT_WSTAT;
137 mask = 1 << 0; /* G_CNT_L write status */
138 break;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900139 case EXYNOS4_MCT_G_CNT_U:
Changhwan Younc8987472011-10-04 17:09:26 +0900140 stat_addr = EXYNOS4_MCT_G_CNT_WSTAT;
141 mask = 1 << 1; /* G_CNT_U write status */
142 break;
143 default:
144 return;
145 }
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900146 }
147
148 /* Wait maximum 1 ms until written values are applied */
149 for (i = 0; i < loops_per_jiffy / 1000 * HZ; i++)
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900150 if (__raw_readl(reg_base + stat_addr) & mask) {
151 __raw_writel(mask, reg_base + stat_addr);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900152 return;
153 }
154
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900155 panic("MCT hangs after writing %d (offset:0x%lx)\n", value, offset);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900156}
157
158/* Clocksource handling */
159static void exynos4_mct_frc_start(u32 hi, u32 lo)
160{
161 u32 reg;
162
163 exynos4_mct_write(lo, EXYNOS4_MCT_G_CNT_L);
164 exynos4_mct_write(hi, EXYNOS4_MCT_G_CNT_U);
165
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900166 reg = __raw_readl(reg_base + EXYNOS4_MCT_G_TCON);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900167 reg |= MCT_G_TCON_START;
168 exynos4_mct_write(reg, EXYNOS4_MCT_G_TCON);
169}
170
171static cycle_t exynos4_frc_read(struct clocksource *cs)
172{
173 unsigned int lo, hi;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900174 u32 hi2 = __raw_readl(reg_base + EXYNOS4_MCT_G_CNT_U);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900175
176 do {
177 hi = hi2;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900178 lo = __raw_readl(reg_base + EXYNOS4_MCT_G_CNT_L);
179 hi2 = __raw_readl(reg_base + EXYNOS4_MCT_G_CNT_U);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900180 } while (hi != hi2);
181
182 return ((cycle_t)hi << 32) | lo;
183}
184
Changhwan Younaa421c12011-09-02 14:10:52 +0900185static void exynos4_frc_resume(struct clocksource *cs)
186{
187 exynos4_mct_frc_start(0, 0);
188}
189
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900190struct clocksource mct_frc = {
191 .name = "mct-frc",
192 .rating = 400,
193 .read = exynos4_frc_read,
194 .mask = CLOCKSOURCE_MASK(64),
195 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Changhwan Younaa421c12011-09-02 14:10:52 +0900196 .resume = exynos4_frc_resume,
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900197};
198
199static void __init exynos4_clocksource_init(void)
200{
201 exynos4_mct_frc_start(0, 0);
202
203 if (clocksource_register_hz(&mct_frc, clk_rate))
204 panic("%s: can't register clocksource\n", mct_frc.name);
205}
206
207static void exynos4_mct_comp0_stop(void)
208{
209 unsigned int tcon;
210
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900211 tcon = __raw_readl(reg_base + EXYNOS4_MCT_G_TCON);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900212 tcon &= ~(MCT_G_TCON_COMP0_ENABLE | MCT_G_TCON_COMP0_AUTO_INC);
213
214 exynos4_mct_write(tcon, EXYNOS4_MCT_G_TCON);
215 exynos4_mct_write(0, EXYNOS4_MCT_G_INT_ENB);
216}
217
218static void exynos4_mct_comp0_start(enum clock_event_mode mode,
219 unsigned long cycles)
220{
221 unsigned int tcon;
222 cycle_t comp_cycle;
223
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900224 tcon = __raw_readl(reg_base + EXYNOS4_MCT_G_TCON);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900225
226 if (mode == CLOCK_EVT_MODE_PERIODIC) {
227 tcon |= MCT_G_TCON_COMP0_AUTO_INC;
228 exynos4_mct_write(cycles, EXYNOS4_MCT_G_COMP0_ADD_INCR);
229 }
230
231 comp_cycle = exynos4_frc_read(&mct_frc) + cycles;
232 exynos4_mct_write((u32)comp_cycle, EXYNOS4_MCT_G_COMP0_L);
233 exynos4_mct_write((u32)(comp_cycle >> 32), EXYNOS4_MCT_G_COMP0_U);
234
235 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_ENB);
236
237 tcon |= MCT_G_TCON_COMP0_ENABLE;
238 exynos4_mct_write(tcon , EXYNOS4_MCT_G_TCON);
239}
240
241static int exynos4_comp_set_next_event(unsigned long cycles,
242 struct clock_event_device *evt)
243{
244 exynos4_mct_comp0_start(evt->mode, cycles);
245
246 return 0;
247}
248
249static void exynos4_comp_set_mode(enum clock_event_mode mode,
250 struct clock_event_device *evt)
251{
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800252 unsigned long cycles_per_jiffy;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900253 exynos4_mct_comp0_stop();
254
255 switch (mode) {
256 case CLOCK_EVT_MODE_PERIODIC:
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800257 cycles_per_jiffy =
258 (((unsigned long long) NSEC_PER_SEC / HZ * evt->mult) >> evt->shift);
259 exynos4_mct_comp0_start(mode, cycles_per_jiffy);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900260 break;
261
262 case CLOCK_EVT_MODE_ONESHOT:
263 case CLOCK_EVT_MODE_UNUSED:
264 case CLOCK_EVT_MODE_SHUTDOWN:
265 case CLOCK_EVT_MODE_RESUME:
266 break;
267 }
268}
269
270static struct clock_event_device mct_comp_device = {
271 .name = "mct-comp",
272 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
273 .rating = 250,
274 .set_next_event = exynos4_comp_set_next_event,
275 .set_mode = exynos4_comp_set_mode,
276};
277
278static irqreturn_t exynos4_mct_comp_isr(int irq, void *dev_id)
279{
280 struct clock_event_device *evt = dev_id;
281
282 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_CSTAT);
283
284 evt->event_handler(evt);
285
286 return IRQ_HANDLED;
287}
288
289static struct irqaction mct_comp_event_irq = {
290 .name = "mct_comp_irq",
291 .flags = IRQF_TIMER | IRQF_IRQPOLL,
292 .handler = exynos4_mct_comp_isr,
293 .dev_id = &mct_comp_device,
294};
295
296static void exynos4_clockevent_init(void)
297{
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900298 mct_comp_device.cpumask = cpumask_of(0);
Shawn Guo838a2ae2013-01-12 11:50:05 +0000299 clockevents_config_and_register(&mct_comp_device, clk_rate,
300 0xf, 0xffffffff);
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900301 setup_irq(mct_irqs[MCT_G0_IRQ], &mct_comp_event_irq);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900302}
303
304#ifdef CONFIG_LOCAL_TIMERS
Kukjin Kim991a6c72011-12-08 10:04:49 +0900305
306static DEFINE_PER_CPU(struct mct_clock_event_device, percpu_mct_tick);
307
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900308/* Clock event handling */
309static void exynos4_mct_tick_stop(struct mct_clock_event_device *mevt)
310{
311 unsigned long tmp;
312 unsigned long mask = MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900313 unsigned long offset = mevt->base + MCT_L_TCON_OFFSET;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900314
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900315 tmp = __raw_readl(reg_base + offset);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900316 if (tmp & mask) {
317 tmp &= ~mask;
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900318 exynos4_mct_write(tmp, offset);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900319 }
320}
321
322static void exynos4_mct_tick_start(unsigned long cycles,
323 struct mct_clock_event_device *mevt)
324{
325 unsigned long tmp;
326
327 exynos4_mct_tick_stop(mevt);
328
329 tmp = (1 << 31) | cycles; /* MCT_L_UPDATE_ICNTB */
330
331 /* update interrupt count buffer */
332 exynos4_mct_write(tmp, mevt->base + MCT_L_ICNTB_OFFSET);
333
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300334 /* enable MCT tick interrupt */
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900335 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_ENB_OFFSET);
336
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900337 tmp = __raw_readl(reg_base + mevt->base + MCT_L_TCON_OFFSET);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900338 tmp |= MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START |
339 MCT_L_TCON_INTERVAL_MODE;
340 exynos4_mct_write(tmp, mevt->base + MCT_L_TCON_OFFSET);
341}
342
343static int exynos4_tick_set_next_event(unsigned long cycles,
344 struct clock_event_device *evt)
345{
Marc Zyngiere700e412011-11-03 11:13:12 +0900346 struct mct_clock_event_device *mevt = this_cpu_ptr(&percpu_mct_tick);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900347
348 exynos4_mct_tick_start(cycles, mevt);
349
350 return 0;
351}
352
353static inline void exynos4_tick_set_mode(enum clock_event_mode mode,
354 struct clock_event_device *evt)
355{
Marc Zyngiere700e412011-11-03 11:13:12 +0900356 struct mct_clock_event_device *mevt = this_cpu_ptr(&percpu_mct_tick);
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800357 unsigned long cycles_per_jiffy;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900358
359 exynos4_mct_tick_stop(mevt);
360
361 switch (mode) {
362 case CLOCK_EVT_MODE_PERIODIC:
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800363 cycles_per_jiffy =
364 (((unsigned long long) NSEC_PER_SEC / HZ * evt->mult) >> evt->shift);
365 exynos4_mct_tick_start(cycles_per_jiffy, mevt);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900366 break;
367
368 case CLOCK_EVT_MODE_ONESHOT:
369 case CLOCK_EVT_MODE_UNUSED:
370 case CLOCK_EVT_MODE_SHUTDOWN:
371 case CLOCK_EVT_MODE_RESUME:
372 break;
373 }
374}
375
Changhwan Younc8987472011-10-04 17:09:26 +0900376static int exynos4_mct_tick_clear(struct mct_clock_event_device *mevt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900377{
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900378 struct clock_event_device *evt = mevt->evt;
379
380 /*
381 * This is for supporting oneshot mode.
382 * Mct would generate interrupt periodically
383 * without explicit stopping.
384 */
385 if (evt->mode != CLOCK_EVT_MODE_PERIODIC)
386 exynos4_mct_tick_stop(mevt);
387
388 /* Clear the MCT tick interrupt */
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900389 if (__raw_readl(reg_base + mevt->base + MCT_L_INT_CSTAT_OFFSET) & 1) {
Changhwan Youn3a062282011-10-04 17:02:58 +0900390 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_CSTAT_OFFSET);
391 return 1;
392 } else {
393 return 0;
394 }
395}
396
397static irqreturn_t exynos4_mct_tick_isr(int irq, void *dev_id)
398{
399 struct mct_clock_event_device *mevt = dev_id;
400 struct clock_event_device *evt = mevt->evt;
401
402 exynos4_mct_tick_clear(mevt);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900403
404 evt->event_handler(evt);
405
406 return IRQ_HANDLED;
407}
408
409static struct irqaction mct_tick0_event_irq = {
410 .name = "mct_tick0_irq",
411 .flags = IRQF_TIMER | IRQF_NOBALANCING,
412 .handler = exynos4_mct_tick_isr,
413};
414
415static struct irqaction mct_tick1_event_irq = {
416 .name = "mct_tick1_irq",
417 .flags = IRQF_TIMER | IRQF_NOBALANCING,
418 .handler = exynos4_mct_tick_isr,
419};
420
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000421static int __cpuinit exynos4_local_timer_setup(struct clock_event_device *evt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900422{
Marc Zyngiere700e412011-11-03 11:13:12 +0900423 struct mct_clock_event_device *mevt;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900424 unsigned int cpu = smp_processor_id();
425
Marc Zyngiere700e412011-11-03 11:13:12 +0900426 mevt = this_cpu_ptr(&percpu_mct_tick);
427 mevt->evt = evt;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900428
Marc Zyngiere700e412011-11-03 11:13:12 +0900429 mevt->base = EXYNOS4_MCT_L_BASE(cpu);
430 sprintf(mevt->name, "mct_tick%d", cpu);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900431
Marc Zyngiere700e412011-11-03 11:13:12 +0900432 evt->name = mevt->name;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900433 evt->cpumask = cpumask_of(cpu);
434 evt->set_next_event = exynos4_tick_set_next_event;
435 evt->set_mode = exynos4_tick_set_mode;
436 evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
437 evt->rating = 450;
Shawn Guo838a2ae2013-01-12 11:50:05 +0000438 clockevents_config_and_register(evt, clk_rate / (TICK_BASE_CNT + 1),
439 0xf, 0x7fffffff);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900440
Changhwan Youn4d2e4d72012-03-09 15:09:21 -0800441 exynos4_mct_write(TICK_BASE_CNT, mevt->base + MCT_L_TCNTB_OFFSET);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900442
Changhwan Youn3a062282011-10-04 17:02:58 +0900443 if (mct_int_type == MCT_INT_SPI) {
444 if (cpu == 0) {
Marc Zyngiere700e412011-11-03 11:13:12 +0900445 mct_tick0_event_irq.dev_id = mevt;
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900446 evt->irq = mct_irqs[MCT_L0_IRQ];
447 setup_irq(evt->irq, &mct_tick0_event_irq);
Changhwan Youn3a062282011-10-04 17:02:58 +0900448 } else {
Marc Zyngiere700e412011-11-03 11:13:12 +0900449 mct_tick1_event_irq.dev_id = mevt;
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900450 evt->irq = mct_irqs[MCT_L1_IRQ];
451 setup_irq(evt->irq, &mct_tick1_event_irq);
452 irq_set_affinity(evt->irq, cpumask_of(1));
Changhwan Youn3a062282011-10-04 17:02:58 +0900453 }
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900454 } else {
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900455 enable_percpu_irq(mct_irqs[MCT_L0_IRQ], 0);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900456 }
Kukjin Kim4d487d72011-08-24 16:07:39 +0900457
458 return 0;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900459}
460
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000461static void exynos4_local_timer_stop(struct clock_event_device *evt)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900462{
Amit Daniel Kachhape248cd52011-12-08 10:07:08 +0900463 unsigned int cpu = smp_processor_id();
Marc Zyngier28af6902011-07-22 12:52:37 +0100464 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
Marc Zyngiere700e412011-11-03 11:13:12 +0900465 if (mct_int_type == MCT_INT_SPI)
Amit Daniel Kachhape248cd52011-12-08 10:07:08 +0900466 if (cpu == 0)
467 remove_irq(evt->irq, &mct_tick0_event_irq);
468 else
469 remove_irq(evt->irq, &mct_tick1_event_irq);
Marc Zyngiere700e412011-11-03 11:13:12 +0900470 else
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900471 disable_percpu_irq(mct_irqs[MCT_L0_IRQ]);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900472}
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000473
474static struct local_timer_ops exynos4_mct_tick_ops __cpuinitdata = {
475 .setup = exynos4_local_timer_setup,
476 .stop = exynos4_local_timer_stop,
477};
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900478#endif /* CONFIG_LOCAL_TIMERS */
479
Thomas Abraham36ba5d52013-03-09 16:01:52 +0900480static void __init exynos4_timer_resources(struct device_node *np)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900481{
Thomas Abrahamca9048e2013-03-09 17:10:37 +0900482 struct clk *mct_clk, *tick_clk;
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900483
Thomas Abraham415ac2e2013-03-09 17:10:31 +0900484 tick_clk = np ? of_clk_get_by_name(np, "fin_pll") :
485 clk_get(NULL, "fin_pll");
486 if (IS_ERR(tick_clk))
487 panic("%s: unable to determine tick clock rate\n", __func__);
488 clk_rate = clk_get_rate(tick_clk);
Marc Zyngiere700e412011-11-03 11:13:12 +0900489
Thomas Abrahamca9048e2013-03-09 17:10:37 +0900490 mct_clk = np ? of_clk_get_by_name(np, "mct") : clk_get(NULL, "mct");
491 if (IS_ERR(mct_clk))
492 panic("%s: unable to retrieve mct clock instance\n", __func__);
493 clk_prepare_enable(mct_clk);
494
Thomas Abraham36ba5d52013-03-09 16:01:52 +0900495 reg_base = np ? of_iomap(np, 0) : S5P_VA_SYSTIMER;
496 if (!reg_base)
497 panic("%s: unable to ioremap mct address space\n", __func__);
Thomas Abrahama1ba7a72013-03-09 16:01:47 +0900498
Kukjin Kim991a6c72011-12-08 10:04:49 +0900499#ifdef CONFIG_LOCAL_TIMERS
Marc Zyngiere700e412011-11-03 11:13:12 +0900500 if (mct_int_type == MCT_INT_PPI) {
501 int err;
502
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900503 err = request_percpu_irq(mct_irqs[MCT_L0_IRQ],
Marc Zyngiere700e412011-11-03 11:13:12 +0900504 exynos4_mct_tick_isr, "MCT",
505 &percpu_mct_tick);
506 WARN(err, "MCT: can't request IRQ %d (%d)\n",
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900507 mct_irqs[MCT_L0_IRQ], err);
Marc Zyngiere700e412011-11-03 11:13:12 +0900508 }
Marc Zyngiera8cb6042012-01-10 19:44:19 +0000509
510 local_timer_register(&exynos4_mct_tick_ops);
Kukjin Kim991a6c72011-12-08 10:04:49 +0900511#endif /* CONFIG_LOCAL_TIMERS */
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900512}
513
Thomas Abraham36ba5d52013-03-09 16:01:52 +0900514static const struct of_device_id exynos_mct_ids[] = {
515 { .compatible = "samsung,exynos4210-mct", .data = (void *)MCT_INT_SPI },
516 { .compatible = "samsung,exynos4412-mct", .data = (void *)MCT_INT_PPI },
517};
518
Thomas Abraham9fbf0c82013-03-09 16:10:03 +0900519void __init mct_init(void)
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900520{
Thomas Abraham36ba5d52013-03-09 16:01:52 +0900521 struct device_node *np = NULL;
522 const struct of_device_id *match;
523 u32 nr_irqs, i;
524
Thomas Abraham36ba5d52013-03-09 16:01:52 +0900525#ifdef CONFIG_OF
526 np = of_find_matching_node_and_match(NULL, exynos_mct_ids, &match);
527#endif
528 if (np) {
529 mct_int_type = (u32)(match->data);
530
531 /* This driver uses only one global timer interrupt */
532 mct_irqs[MCT_G0_IRQ] = irq_of_parse_and_map(np, MCT_G0_IRQ);
533
534 /*
535 * Find out the number of local irqs specified. The local
536 * timer irqs are specified after the four global timer
537 * irqs are specified.
538 */
539#ifdef CONFIG_OF
540 nr_irqs = of_irq_count(np);
541#endif
542 for (i = MCT_L0_IRQ; i < nr_irqs; i++)
543 mct_irqs[i] = irq_of_parse_and_map(np, i);
544 } else if (soc_is_exynos4210()) {
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900545 mct_irqs[MCT_G0_IRQ] = EXYNOS4_IRQ_MCT_G0;
546 mct_irqs[MCT_L0_IRQ] = EXYNOS4_IRQ_MCT_L0;
547 mct_irqs[MCT_L1_IRQ] = EXYNOS4_IRQ_MCT_L1;
Changhwan Youn3a062282011-10-04 17:02:58 +0900548 mct_int_type = MCT_INT_SPI;
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900549 } else {
Thomas Abraham36ba5d52013-03-09 16:01:52 +0900550 panic("unable to determine mct controller type\n");
Thomas Abrahamc371dc62013-03-09 16:01:50 +0900551 }
Changhwan Youn3a062282011-10-04 17:02:58 +0900552
Thomas Abraham36ba5d52013-03-09 16:01:52 +0900553 exynos4_timer_resources(np);
Changhwan Youn30d8bea2011-03-11 10:39:57 +0900554 exynos4_clocksource_init();
555 exynos4_clockevent_init();
556}
Thomas Abraham9fbf0c82013-03-09 16:10:03 +0900557CLOCKSOURCE_OF_DECLARE(exynos4210, "samsung,exynos4210-mct", mct_init)
558CLOCKSOURCE_OF_DECLARE(exynos4412, "samsung,exynos4412-mct", mct_init)