Linux 5.1.15
[linux/fpc-iii.git] / drivers / clocksource / timer-mp-csky.c
blob183a9955160a80dd89bdc2d2c82cd358a487c6e9
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
4 #include <linux/init.h>
5 #include <linux/interrupt.h>
6 #include <linux/sched_clock.h>
7 #include <linux/cpu.h>
8 #include <linux/of_irq.h>
9 #include <asm/reg_ops.h>
11 #include "timer-of.h"
13 #define PTIM_CCVR "cr<3, 14>"
14 #define PTIM_CTLR "cr<0, 14>"
15 #define PTIM_LVR "cr<6, 14>"
16 #define PTIM_TSR "cr<1, 14>"
18 static int csky_mptimer_irq;
20 static int csky_mptimer_set_next_event(unsigned long delta,
21 struct clock_event_device *ce)
23 mtcr(PTIM_LVR, delta);
25 return 0;
28 static int csky_mptimer_shutdown(struct clock_event_device *ce)
30 mtcr(PTIM_CTLR, 0);
32 return 0;
35 static int csky_mptimer_oneshot(struct clock_event_device *ce)
37 mtcr(PTIM_CTLR, 1);
39 return 0;
42 static int csky_mptimer_oneshot_stopped(struct clock_event_device *ce)
44 mtcr(PTIM_CTLR, 0);
46 return 0;
49 static DEFINE_PER_CPU(struct timer_of, csky_to) = {
50 .flags = TIMER_OF_CLOCK,
51 .clkevt = {
52 .rating = 300,
53 .features = CLOCK_EVT_FEAT_PERCPU |
54 CLOCK_EVT_FEAT_ONESHOT,
55 .set_state_shutdown = csky_mptimer_shutdown,
56 .set_state_oneshot = csky_mptimer_oneshot,
57 .set_state_oneshot_stopped = csky_mptimer_oneshot_stopped,
58 .set_next_event = csky_mptimer_set_next_event,
62 static irqreturn_t csky_timer_interrupt(int irq, void *dev)
64 struct timer_of *to = this_cpu_ptr(&csky_to);
66 mtcr(PTIM_TSR, 0);
68 to->clkevt.event_handler(&to->clkevt);
70 return IRQ_HANDLED;
74 * clock event for percpu
76 static int csky_mptimer_starting_cpu(unsigned int cpu)
78 struct timer_of *to = per_cpu_ptr(&csky_to, cpu);
80 to->clkevt.cpumask = cpumask_of(cpu);
82 enable_percpu_irq(csky_mptimer_irq, 0);
84 clockevents_config_and_register(&to->clkevt, timer_of_rate(to),
85 2, ULONG_MAX);
87 return 0;
90 static int csky_mptimer_dying_cpu(unsigned int cpu)
92 disable_percpu_irq(csky_mptimer_irq);
94 return 0;
98 * clock source
100 static u64 notrace sched_clock_read(void)
102 return (u64)mfcr(PTIM_CCVR);
105 static u64 clksrc_read(struct clocksource *c)
107 return (u64)mfcr(PTIM_CCVR);
110 struct clocksource csky_clocksource = {
111 .name = "csky",
112 .rating = 400,
113 .mask = CLOCKSOURCE_MASK(32),
114 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
115 .read = clksrc_read,
118 static int __init csky_mptimer_init(struct device_node *np)
120 int ret, cpu, cpu_rollback;
121 struct timer_of *to = NULL;
124 * Csky_mptimer is designed for C-SKY SMP multi-processors and
125 * every core has it's own private irq and regs for clkevt and
126 * clksrc.
128 * The regs is accessed by cpu instruction: mfcr/mtcr instead of
129 * mmio map style. So we needn't mmio-address in dts, but we still
130 * need to give clk and irq number.
132 * We use private irq for the mptimer and irq number is the same
133 * for every core. So we use request_percpu_irq() in timer_of_init.
135 csky_mptimer_irq = irq_of_parse_and_map(np, 0);
136 if (csky_mptimer_irq <= 0)
137 return -EINVAL;
139 ret = request_percpu_irq(csky_mptimer_irq, csky_timer_interrupt,
140 "csky_mp_timer", &csky_to);
141 if (ret)
142 return -EINVAL;
144 for_each_possible_cpu(cpu) {
145 to = per_cpu_ptr(&csky_to, cpu);
146 ret = timer_of_init(np, to);
147 if (ret)
148 goto rollback;
151 clocksource_register_hz(&csky_clocksource, timer_of_rate(to));
152 sched_clock_register(sched_clock_read, 32, timer_of_rate(to));
154 ret = cpuhp_setup_state(CPUHP_AP_CSKY_TIMER_STARTING,
155 "clockevents/csky/timer:starting",
156 csky_mptimer_starting_cpu,
157 csky_mptimer_dying_cpu);
158 if (ret)
159 return -EINVAL;
161 return 0;
163 rollback:
164 for_each_possible_cpu(cpu_rollback) {
165 if (cpu_rollback == cpu)
166 break;
168 to = per_cpu_ptr(&csky_to, cpu_rollback);
169 timer_of_cleanup(to);
171 return -EINVAL;
173 TIMER_OF_DECLARE(csky_mptimer, "csky,mptimer", csky_mptimer_init);