1 // SPDX-License-Identifier: GPL-2.0-only
2 /* linux/arch/arm/mach-exynos4/mct.c
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
7 * Exynos4 MCT(Multi-Core Timer) support
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/err.h>
13 #include <linux/clk.h>
14 #include <linux/clockchips.h>
15 #include <linux/cpu.h>
16 #include <linux/delay.h>
17 #include <linux/percpu.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_address.h>
21 #include <linux/clocksource.h>
22 #include <linux/sched_clock.h>
24 #define EXYNOS4_MCTREG(x) (x)
25 #define EXYNOS4_MCT_G_CNT_L EXYNOS4_MCTREG(0x100)
26 #define EXYNOS4_MCT_G_CNT_U EXYNOS4_MCTREG(0x104)
27 #define EXYNOS4_MCT_G_CNT_WSTAT EXYNOS4_MCTREG(0x110)
28 #define EXYNOS4_MCT_G_COMP0_L EXYNOS4_MCTREG(0x200)
29 #define EXYNOS4_MCT_G_COMP0_U EXYNOS4_MCTREG(0x204)
30 #define EXYNOS4_MCT_G_COMP0_ADD_INCR EXYNOS4_MCTREG(0x208)
31 #define EXYNOS4_MCT_G_TCON EXYNOS4_MCTREG(0x240)
32 #define EXYNOS4_MCT_G_INT_CSTAT EXYNOS4_MCTREG(0x244)
33 #define EXYNOS4_MCT_G_INT_ENB EXYNOS4_MCTREG(0x248)
34 #define EXYNOS4_MCT_G_WSTAT EXYNOS4_MCTREG(0x24C)
35 #define _EXYNOS4_MCT_L_BASE EXYNOS4_MCTREG(0x300)
36 #define EXYNOS4_MCT_L_BASE(x) (_EXYNOS4_MCT_L_BASE + (0x100 * (x)))
37 #define EXYNOS4_MCT_L_MASK (0xffffff00)
39 #define MCT_L_TCNTB_OFFSET (0x00)
40 #define MCT_L_ICNTB_OFFSET (0x08)
41 #define MCT_L_TCON_OFFSET (0x20)
42 #define MCT_L_INT_CSTAT_OFFSET (0x30)
43 #define MCT_L_INT_ENB_OFFSET (0x34)
44 #define MCT_L_WSTAT_OFFSET (0x40)
45 #define MCT_G_TCON_START (1 << 8)
46 #define MCT_G_TCON_COMP0_AUTO_INC (1 << 1)
47 #define MCT_G_TCON_COMP0_ENABLE (1 << 0)
48 #define MCT_L_TCON_INTERVAL_MODE (1 << 2)
49 #define MCT_L_TCON_INT_START (1 << 1)
50 #define MCT_L_TCON_TIMER_START (1 << 0)
52 #define TICK_BASE_CNT 1
55 /* Use values higher than ARM arch timer. See 6282edb72bed. */
56 #define MCT_CLKSOURCE_RATING 450
57 #define MCT_CLKEVENTS_RATING 500
59 #define MCT_CLKSOURCE_RATING 350
60 #define MCT_CLKEVENTS_RATING 350
63 /* There are four Global timers starting with 0 offset */
65 /* Local timers count starts after global timer count */
67 /* Max number of IRQ as per DT binding document */
68 #define MCT_NR_IRQS 20
69 /* Max number of local timers */
70 #define MCT_NR_LOCAL (MCT_NR_IRQS - MCT_L0_IRQ)
77 static void __iomem *reg_base;
78 static unsigned long clk_rate;
79 static unsigned int mct_int_type;
80 static int mct_irqs[MCT_NR_IRQS];
82 struct mct_clock_event_device {
83 struct clock_event_device evt;
86 * The length of the name must be adjusted if number of
87 * local timer interrupts grow over two digits
92 static void exynos4_mct_write(unsigned int value, unsigned long offset)
94 unsigned long stat_addr;
98 writel_relaxed(value, reg_base + offset);
100 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:
104 mask = 1 << 3; /* L_TCON write status */
106 case MCT_L_ICNTB_OFFSET:
107 mask = 1 << 1; /* L_ICNTB write status */
109 case MCT_L_TCNTB_OFFSET:
110 mask = 1 << 0; /* L_TCNTB write status */
117 case EXYNOS4_MCT_G_TCON:
118 stat_addr = EXYNOS4_MCT_G_WSTAT;
119 mask = 1 << 16; /* G_TCON write status */
121 case EXYNOS4_MCT_G_COMP0_L:
122 stat_addr = EXYNOS4_MCT_G_WSTAT;
123 mask = 1 << 0; /* G_COMP0_L write status */
125 case EXYNOS4_MCT_G_COMP0_U:
126 stat_addr = EXYNOS4_MCT_G_WSTAT;
127 mask = 1 << 1; /* G_COMP0_U write status */
129 case EXYNOS4_MCT_G_COMP0_ADD_INCR:
130 stat_addr = EXYNOS4_MCT_G_WSTAT;
131 mask = 1 << 2; /* G_COMP0_ADD_INCR w status */
133 case EXYNOS4_MCT_G_CNT_L:
134 stat_addr = EXYNOS4_MCT_G_CNT_WSTAT;
135 mask = 1 << 0; /* G_CNT_L write status */
137 case EXYNOS4_MCT_G_CNT_U:
138 stat_addr = EXYNOS4_MCT_G_CNT_WSTAT;
139 mask = 1 << 1; /* G_CNT_U write status */
146 /* Wait maximum 1 ms until written values are applied */
147 for (i = 0; i < loops_per_jiffy / 1000 * HZ; i++)
148 if (readl_relaxed(reg_base + stat_addr) & mask) {
149 writel_relaxed(mask, reg_base + stat_addr);
153 panic("MCT hangs after writing %d (offset:0x%lx)\n", value, offset);
156 /* Clocksource handling */
157 static void exynos4_mct_frc_start(void)
161 reg = readl_relaxed(reg_base + EXYNOS4_MCT_G_TCON);
162 reg |= MCT_G_TCON_START;
163 exynos4_mct_write(reg, EXYNOS4_MCT_G_TCON);
167 * exynos4_read_count_64 - Read all 64-bits of the global counter
169 * This will read all 64-bits of the global counter taking care to make sure
170 * that the upper and lower half match. Note that reading the MCT can be quite
171 * slow (hundreds of nanoseconds) so you should use the 32-bit (lower half
172 * only) version when possible.
174 * Returns the number of cycles in the global counter.
176 static u64 exynos4_read_count_64(void)
179 u32 hi2 = readl_relaxed(reg_base + EXYNOS4_MCT_G_CNT_U);
183 lo = readl_relaxed(reg_base + EXYNOS4_MCT_G_CNT_L);
184 hi2 = readl_relaxed(reg_base + EXYNOS4_MCT_G_CNT_U);
187 return ((u64)hi << 32) | lo;
191 * exynos4_read_count_32 - Read the lower 32-bits of the global counter
193 * This will read just the lower 32-bits of the global counter. This is marked
194 * as notrace so it can be used by the scheduler clock.
196 * Returns the number of cycles in the global counter (lower 32 bits).
198 static u32 notrace exynos4_read_count_32(void)
200 return readl_relaxed(reg_base + EXYNOS4_MCT_G_CNT_L);
203 static u64 exynos4_frc_read(struct clocksource *cs)
205 return exynos4_read_count_32();
208 static void exynos4_frc_resume(struct clocksource *cs)
210 exynos4_mct_frc_start();
213 static struct clocksource mct_frc = {
215 .rating = MCT_CLKSOURCE_RATING,
216 .read = exynos4_frc_read,
217 .mask = CLOCKSOURCE_MASK(32),
218 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
219 .resume = exynos4_frc_resume,
222 static u64 notrace exynos4_read_sched_clock(void)
224 return exynos4_read_count_32();
227 #if defined(CONFIG_ARM)
228 static struct delay_timer exynos4_delay_timer;
230 static cycles_t exynos4_read_current_timer(void)
232 BUILD_BUG_ON_MSG(sizeof(cycles_t) != sizeof(u32),
233 "cycles_t needs to move to 32-bit for ARM64 usage");
234 return exynos4_read_count_32();
238 static int __init exynos4_clocksource_init(bool frc_shared)
241 * When the frc is shared, the main processer should have already
242 * turned it on and we shouldn't be writing to TCON.
245 mct_frc.resume = NULL;
247 exynos4_mct_frc_start();
249 #if defined(CONFIG_ARM)
250 exynos4_delay_timer.read_current_timer = &exynos4_read_current_timer;
251 exynos4_delay_timer.freq = clk_rate;
252 register_current_timer_delay(&exynos4_delay_timer);
255 if (clocksource_register_hz(&mct_frc, clk_rate))
256 panic("%s: can't register clocksource\n", mct_frc.name);
258 sched_clock_register(exynos4_read_sched_clock, 32, clk_rate);
263 static void exynos4_mct_comp0_stop(void)
267 tcon = readl_relaxed(reg_base + EXYNOS4_MCT_G_TCON);
268 tcon &= ~(MCT_G_TCON_COMP0_ENABLE | MCT_G_TCON_COMP0_AUTO_INC);
270 exynos4_mct_write(tcon, EXYNOS4_MCT_G_TCON);
271 exynos4_mct_write(0, EXYNOS4_MCT_G_INT_ENB);
274 static void exynos4_mct_comp0_start(bool periodic, unsigned long cycles)
279 tcon = readl_relaxed(reg_base + EXYNOS4_MCT_G_TCON);
282 tcon |= MCT_G_TCON_COMP0_AUTO_INC;
283 exynos4_mct_write(cycles, EXYNOS4_MCT_G_COMP0_ADD_INCR);
286 comp_cycle = exynos4_read_count_64() + cycles;
287 exynos4_mct_write((u32)comp_cycle, EXYNOS4_MCT_G_COMP0_L);
288 exynos4_mct_write((u32)(comp_cycle >> 32), EXYNOS4_MCT_G_COMP0_U);
290 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_ENB);
292 tcon |= MCT_G_TCON_COMP0_ENABLE;
293 exynos4_mct_write(tcon , EXYNOS4_MCT_G_TCON);
296 static int exynos4_comp_set_next_event(unsigned long cycles,
297 struct clock_event_device *evt)
299 exynos4_mct_comp0_start(false, cycles);
304 static int mct_set_state_shutdown(struct clock_event_device *evt)
306 exynos4_mct_comp0_stop();
310 static int mct_set_state_periodic(struct clock_event_device *evt)
312 unsigned long cycles_per_jiffy;
314 cycles_per_jiffy = (((unsigned long long)NSEC_PER_SEC / HZ * evt->mult)
316 exynos4_mct_comp0_stop();
317 exynos4_mct_comp0_start(true, cycles_per_jiffy);
321 static struct clock_event_device mct_comp_device = {
323 .features = CLOCK_EVT_FEAT_PERIODIC |
324 CLOCK_EVT_FEAT_ONESHOT,
326 .set_next_event = exynos4_comp_set_next_event,
327 .set_state_periodic = mct_set_state_periodic,
328 .set_state_shutdown = mct_set_state_shutdown,
329 .set_state_oneshot = mct_set_state_shutdown,
330 .set_state_oneshot_stopped = mct_set_state_shutdown,
331 .tick_resume = mct_set_state_shutdown,
334 static irqreturn_t exynos4_mct_comp_isr(int irq, void *dev_id)
336 struct clock_event_device *evt = dev_id;
338 exynos4_mct_write(0x1, EXYNOS4_MCT_G_INT_CSTAT);
340 evt->event_handler(evt);
345 static int exynos4_clockevent_init(void)
347 mct_comp_device.cpumask = cpumask_of(0);
348 clockevents_config_and_register(&mct_comp_device, clk_rate,
350 if (request_irq(mct_irqs[MCT_G0_IRQ], exynos4_mct_comp_isr,
351 IRQF_TIMER | IRQF_IRQPOLL, "mct_comp_irq",
353 pr_err("%s: request_irq() failed\n", "mct_comp_irq");
358 static DEFINE_PER_CPU(struct mct_clock_event_device, percpu_mct_tick);
360 /* Clock event handling */
361 static void exynos4_mct_tick_stop(struct mct_clock_event_device *mevt)
364 unsigned long mask = MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START;
365 unsigned long offset = mevt->base + MCT_L_TCON_OFFSET;
367 tmp = readl_relaxed(reg_base + offset);
370 exynos4_mct_write(tmp, offset);
374 static void exynos4_mct_tick_start(unsigned long cycles,
375 struct mct_clock_event_device *mevt)
379 exynos4_mct_tick_stop(mevt);
381 tmp = (1 << 31) | cycles; /* MCT_L_UPDATE_ICNTB */
383 /* update interrupt count buffer */
384 exynos4_mct_write(tmp, mevt->base + MCT_L_ICNTB_OFFSET);
386 /* enable MCT tick interrupt */
387 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_ENB_OFFSET);
389 tmp = readl_relaxed(reg_base + mevt->base + MCT_L_TCON_OFFSET);
390 tmp |= MCT_L_TCON_INT_START | MCT_L_TCON_TIMER_START |
391 MCT_L_TCON_INTERVAL_MODE;
392 exynos4_mct_write(tmp, mevt->base + MCT_L_TCON_OFFSET);
395 static void exynos4_mct_tick_clear(struct mct_clock_event_device *mevt)
397 /* Clear the MCT tick interrupt */
398 if (readl_relaxed(reg_base + mevt->base + MCT_L_INT_CSTAT_OFFSET) & 1)
399 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_CSTAT_OFFSET);
402 static int exynos4_tick_set_next_event(unsigned long cycles,
403 struct clock_event_device *evt)
405 struct mct_clock_event_device *mevt;
407 mevt = container_of(evt, struct mct_clock_event_device, evt);
408 exynos4_mct_tick_start(cycles, mevt);
412 static int set_state_shutdown(struct clock_event_device *evt)
414 struct mct_clock_event_device *mevt;
416 mevt = container_of(evt, struct mct_clock_event_device, evt);
417 exynos4_mct_tick_stop(mevt);
418 exynos4_mct_tick_clear(mevt);
422 static int set_state_periodic(struct clock_event_device *evt)
424 struct mct_clock_event_device *mevt;
425 unsigned long cycles_per_jiffy;
427 mevt = container_of(evt, struct mct_clock_event_device, evt);
428 cycles_per_jiffy = (((unsigned long long)NSEC_PER_SEC / HZ * evt->mult)
430 exynos4_mct_tick_stop(mevt);
431 exynos4_mct_tick_start(cycles_per_jiffy, mevt);
435 static irqreturn_t exynos4_mct_tick_isr(int irq, void *dev_id)
437 struct mct_clock_event_device *mevt = dev_id;
438 struct clock_event_device *evt = &mevt->evt;
441 * This is for supporting oneshot mode.
442 * Mct would generate interrupt periodically
443 * without explicit stopping.
445 if (!clockevent_state_periodic(&mevt->evt))
446 exynos4_mct_tick_stop(mevt);
448 exynos4_mct_tick_clear(mevt);
450 evt->event_handler(evt);
455 static int exynos4_mct_starting_cpu(unsigned int cpu)
457 struct mct_clock_event_device *mevt =
458 per_cpu_ptr(&percpu_mct_tick, cpu);
459 struct clock_event_device *evt = &mevt->evt;
461 snprintf(mevt->name, sizeof(mevt->name), "mct_tick%d", cpu);
463 evt->name = mevt->name;
464 evt->cpumask = cpumask_of(cpu);
465 evt->set_next_event = exynos4_tick_set_next_event;
466 evt->set_state_periodic = set_state_periodic;
467 evt->set_state_shutdown = set_state_shutdown;
468 evt->set_state_oneshot = set_state_shutdown;
469 evt->set_state_oneshot_stopped = set_state_shutdown;
470 evt->tick_resume = set_state_shutdown;
471 evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
472 CLOCK_EVT_FEAT_PERCPU;
473 evt->rating = MCT_CLKEVENTS_RATING;
475 exynos4_mct_write(TICK_BASE_CNT, mevt->base + MCT_L_TCNTB_OFFSET);
477 if (mct_int_type == MCT_INT_SPI) {
482 irq_force_affinity(evt->irq, cpumask_of(cpu));
483 enable_irq(evt->irq);
485 enable_percpu_irq(mct_irqs[MCT_L0_IRQ], 0);
487 clockevents_config_and_register(evt, clk_rate / (TICK_BASE_CNT + 1),
493 static int exynos4_mct_dying_cpu(unsigned int cpu)
495 struct mct_clock_event_device *mevt =
496 per_cpu_ptr(&percpu_mct_tick, cpu);
497 struct clock_event_device *evt = &mevt->evt;
499 evt->set_state_shutdown(evt);
500 if (mct_int_type == MCT_INT_SPI) {
502 disable_irq_nosync(evt->irq);
503 exynos4_mct_write(0x1, mevt->base + MCT_L_INT_CSTAT_OFFSET);
505 disable_percpu_irq(mct_irqs[MCT_L0_IRQ]);
510 static int __init exynos4_timer_resources(struct device_node *np)
512 struct clk *mct_clk, *tick_clk;
514 reg_base = of_iomap(np, 0);
516 panic("%s: unable to ioremap mct address space\n", __func__);
518 tick_clk = of_clk_get_by_name(np, "fin_pll");
519 if (IS_ERR(tick_clk))
520 panic("%s: unable to determine tick clock rate\n", __func__);
521 clk_rate = clk_get_rate(tick_clk);
523 mct_clk = of_clk_get_by_name(np, "mct");
525 panic("%s: unable to retrieve mct clock instance\n", __func__);
526 clk_prepare_enable(mct_clk);
532 * exynos4_timer_interrupts - initialize MCT interrupts
533 * @np: device node for MCT
534 * @int_type: interrupt type, MCT_INT_PPI or MCT_INT_SPI
535 * @local_idx: array mapping CPU numbers to local timer indices
536 * @nr_local: size of @local_idx array
538 static int __init exynos4_timer_interrupts(struct device_node *np,
539 unsigned int int_type,
540 const u32 *local_idx,
543 int nr_irqs, i, err, cpu;
545 mct_int_type = int_type;
547 /* This driver uses only one global timer interrupt */
548 mct_irqs[MCT_G0_IRQ] = irq_of_parse_and_map(np, MCT_G0_IRQ);
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.
555 nr_irqs = of_irq_count(np);
556 if (nr_irqs > ARRAY_SIZE(mct_irqs)) {
557 pr_err("exynos-mct: too many (%d) interrupts configured in DT\n",
559 nr_irqs = ARRAY_SIZE(mct_irqs);
561 for (i = MCT_L0_IRQ; i < nr_irqs; i++)
562 mct_irqs[i] = irq_of_parse_and_map(np, i);
564 if (mct_int_type == MCT_INT_PPI) {
566 err = request_percpu_irq(mct_irqs[MCT_L0_IRQ],
567 exynos4_mct_tick_isr, "MCT",
569 WARN(err, "MCT: can't request IRQ %d (%d)\n",
570 mct_irqs[MCT_L0_IRQ], err);
572 for_each_possible_cpu(cpu) {
574 unsigned int irq_idx;
575 struct mct_clock_event_device *pcpu_mevt =
576 per_cpu_ptr(&percpu_mct_tick, cpu);
578 if (cpu >= nr_local) {
583 irq_idx = MCT_L0_IRQ + local_idx[cpu];
585 pcpu_mevt->evt.irq = -1;
586 if (irq_idx >= ARRAY_SIZE(mct_irqs))
588 mct_irq = mct_irqs[irq_idx];
590 irq_set_status_flags(mct_irq, IRQ_NOAUTOEN);
591 if (request_irq(mct_irq,
592 exynos4_mct_tick_isr,
593 IRQF_TIMER | IRQF_NOBALANCING,
594 pcpu_mevt->name, pcpu_mevt)) {
595 pr_err("exynos-mct: cannot register IRQ (cpu%d)\n",
600 pcpu_mevt->evt.irq = mct_irq;
604 for_each_possible_cpu(cpu) {
605 struct mct_clock_event_device *mevt = per_cpu_ptr(&percpu_mct_tick, cpu);
607 if (cpu >= nr_local) {
612 mevt->base = EXYNOS4_MCT_L_BASE(local_idx[cpu]);
615 /* Install hotplug callbacks which configure the timer on this CPU */
616 err = cpuhp_setup_state(CPUHP_AP_EXYNOS4_MCT_TIMER_STARTING,
617 "clockevents/exynos4/mct_timer:starting",
618 exynos4_mct_starting_cpu,
619 exynos4_mct_dying_cpu);
626 if (mct_int_type == MCT_INT_PPI) {
627 free_percpu_irq(mct_irqs[MCT_L0_IRQ], &percpu_mct_tick);
629 for_each_possible_cpu(cpu) {
630 struct mct_clock_event_device *pcpu_mevt =
631 per_cpu_ptr(&percpu_mct_tick, cpu);
633 if (pcpu_mevt->evt.irq != -1) {
634 free_irq(pcpu_mevt->evt.irq, pcpu_mevt);
635 pcpu_mevt->evt.irq = -1;
642 static int __init mct_init_dt(struct device_node *np, unsigned int int_type)
644 bool frc_shared = of_property_read_bool(np, "samsung,frc-shared");
645 u32 local_idx[MCT_NR_LOCAL] = {0};
649 nr_local = of_property_count_u32_elems(np, "samsung,local-timers");
653 if (nr_local > ARRAY_SIZE(local_idx))
656 ret = of_property_read_u32_array(np, "samsung,local-timers",
657 local_idx, nr_local);
663 nr_local = ARRAY_SIZE(local_idx);
664 for (i = 0; i < nr_local; i++)
668 ret = exynos4_timer_resources(np);
672 ret = exynos4_timer_interrupts(np, int_type, local_idx, nr_local);
676 ret = exynos4_clocksource_init(frc_shared);
681 * When the FRC is shared with a main processor, this secondary
682 * processor cannot use the global comparator.
687 return exynos4_clockevent_init();
691 static int __init mct_init_spi(struct device_node *np)
693 return mct_init_dt(np, MCT_INT_SPI);
696 static int __init mct_init_ppi(struct device_node *np)
698 return mct_init_dt(np, MCT_INT_PPI);
700 TIMER_OF_DECLARE(exynos4210, "samsung,exynos4210-mct", mct_init_spi);
701 TIMER_OF_DECLARE(exynos4412, "samsung,exynos4412-mct", mct_init_ppi);