1 // SPDX-License-Identifier: GPL-2.0
3 * Ingenic SoCs TCU IRQ driver
4 * Copyright (C) 2019 Paul Cercueil <paul@crapouillou.net>
5 * Copyright (C) 2020 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>
8 #include <linux/bitops.h>
10 #include <linux/clockchips.h>
11 #include <linux/clocksource.h>
12 #include <linux/interrupt.h>
13 #include <linux/mfd/ingenic-tcu.h>
14 #include <linux/mfd/syscon.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/of_platform.h>
19 #include <linux/overflow.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/sched_clock.h>
24 #include <dt-bindings/clock/ingenic,tcu.h>
26 static DEFINE_PER_CPU(call_single_data_t, ingenic_cevt_csd);
28 struct ingenic_soc_info {
29 unsigned int num_channels;
32 struct ingenic_tcu_timer {
35 struct clock_event_device cevt;
42 struct device_node *np;
44 unsigned int cs_channel;
45 struct clocksource cs;
46 unsigned long pwm_channels_mask;
47 struct ingenic_tcu_timer timers[];
50 static struct ingenic_tcu *ingenic_tcu;
52 static u64 notrace ingenic_tcu_timer_read(void)
54 struct ingenic_tcu *tcu = ingenic_tcu;
57 regmap_read(tcu->map, TCU_REG_TCNTc(tcu->cs_channel), &count);
62 static u64 notrace ingenic_tcu_timer_cs_read(struct clocksource *cs)
64 return ingenic_tcu_timer_read();
67 static inline struct ingenic_tcu *
68 to_ingenic_tcu(struct ingenic_tcu_timer *timer)
70 return container_of(timer, struct ingenic_tcu, timers[timer->cpu]);
73 static inline struct ingenic_tcu_timer *
74 to_ingenic_tcu_timer(struct clock_event_device *evt)
76 return container_of(evt, struct ingenic_tcu_timer, cevt);
79 static int ingenic_tcu_cevt_set_state_shutdown(struct clock_event_device *evt)
81 struct ingenic_tcu_timer *timer = to_ingenic_tcu_timer(evt);
82 struct ingenic_tcu *tcu = to_ingenic_tcu(timer);
84 regmap_write(tcu->map, TCU_REG_TECR, BIT(timer->channel));
89 static int ingenic_tcu_cevt_set_next(unsigned long next,
90 struct clock_event_device *evt)
92 struct ingenic_tcu_timer *timer = to_ingenic_tcu_timer(evt);
93 struct ingenic_tcu *tcu = to_ingenic_tcu(timer);
98 regmap_write(tcu->map, TCU_REG_TDFRc(timer->channel), next);
99 regmap_write(tcu->map, TCU_REG_TCNTc(timer->channel), 0);
100 regmap_write(tcu->map, TCU_REG_TESR, BIT(timer->channel));
105 static void ingenic_per_cpu_event_handler(void *info)
107 struct clock_event_device *cevt = (struct clock_event_device *) info;
109 cevt->event_handler(cevt);
112 static irqreturn_t ingenic_tcu_cevt_cb(int irq, void *dev_id)
114 struct ingenic_tcu_timer *timer = dev_id;
115 struct ingenic_tcu *tcu = to_ingenic_tcu(timer);
116 call_single_data_t *csd;
118 regmap_write(tcu->map, TCU_REG_TECR, BIT(timer->channel));
120 if (timer->cevt.event_handler) {
121 csd = &per_cpu(ingenic_cevt_csd, timer->cpu);
122 csd->info = (void *) &timer->cevt;
123 csd->func = ingenic_per_cpu_event_handler;
124 smp_call_function_single_async(timer->cpu, csd);
130 static struct clk *ingenic_tcu_get_clock(struct device_node *np, int id)
132 struct of_phandle_args args;
138 return of_clk_get_from_provider(&args);
141 static int ingenic_tcu_setup_cevt(unsigned int cpu)
143 struct ingenic_tcu *tcu = ingenic_tcu;
144 struct ingenic_tcu_timer *timer = &tcu->timers[cpu];
145 unsigned int timer_virq;
146 struct irq_domain *domain;
150 timer->clk = ingenic_tcu_get_clock(tcu->np, timer->channel);
151 if (IS_ERR(timer->clk))
152 return PTR_ERR(timer->clk);
154 err = clk_prepare_enable(timer->clk);
158 rate = clk_get_rate(timer->clk);
161 goto err_clk_disable;
164 domain = irq_find_host(tcu->np);
167 goto err_clk_disable;
170 timer_virq = irq_create_mapping(domain, timer->channel);
173 goto err_clk_disable;
176 snprintf(timer->name, sizeof(timer->name), "TCU%u", timer->channel);
178 err = request_irq(timer_virq, ingenic_tcu_cevt_cb, IRQF_TIMER,
181 goto err_irq_dispose_mapping;
183 timer->cpu = smp_processor_id();
184 timer->cevt.cpumask = cpumask_of(smp_processor_id());
185 timer->cevt.features = CLOCK_EVT_FEAT_ONESHOT;
186 timer->cevt.name = timer->name;
187 timer->cevt.rating = 200;
188 timer->cevt.set_state_shutdown = ingenic_tcu_cevt_set_state_shutdown;
189 timer->cevt.set_next_event = ingenic_tcu_cevt_set_next;
191 clockevents_config_and_register(&timer->cevt, rate, 10, 0xffff);
195 err_irq_dispose_mapping:
196 irq_dispose_mapping(timer_virq);
198 clk_disable_unprepare(timer->clk);
204 static int __init ingenic_tcu_clocksource_init(struct device_node *np,
205 struct ingenic_tcu *tcu)
207 unsigned int channel = tcu->cs_channel;
208 struct clocksource *cs = &tcu->cs;
212 tcu->cs_clk = ingenic_tcu_get_clock(np, channel);
213 if (IS_ERR(tcu->cs_clk))
214 return PTR_ERR(tcu->cs_clk);
216 err = clk_prepare_enable(tcu->cs_clk);
220 rate = clk_get_rate(tcu->cs_clk);
223 goto err_clk_disable;
227 regmap_update_bits(tcu->map, TCU_REG_TCSRc(channel),
228 0xffff & ~TCU_TCSR_RESERVED_BITS, 0);
231 regmap_write(tcu->map, TCU_REG_TDFRc(channel), 0xffff);
232 regmap_write(tcu->map, TCU_REG_TCNTc(channel), 0);
235 regmap_write(tcu->map, TCU_REG_TESR, BIT(channel));
237 cs->name = "ingenic-timer";
239 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
240 cs->mask = CLOCKSOURCE_MASK(16);
241 cs->read = ingenic_tcu_timer_cs_read;
243 err = clocksource_register_hz(cs, rate);
245 goto err_clk_disable;
250 clk_disable_unprepare(tcu->cs_clk);
252 clk_put(tcu->cs_clk);
256 static const struct ingenic_soc_info jz4740_soc_info = {
260 static const struct ingenic_soc_info jz4725b_soc_info = {
264 static const struct of_device_id ingenic_tcu_of_match[] = {
265 { .compatible = "ingenic,jz4740-tcu", .data = &jz4740_soc_info, },
266 { .compatible = "ingenic,jz4725b-tcu", .data = &jz4725b_soc_info, },
267 { .compatible = "ingenic,jz4760-tcu", .data = &jz4740_soc_info, },
268 { .compatible = "ingenic,jz4770-tcu", .data = &jz4740_soc_info, },
269 { .compatible = "ingenic,x1000-tcu", .data = &jz4740_soc_info, },
273 static int __init ingenic_tcu_init(struct device_node *np)
275 const struct of_device_id *id = of_match_node(ingenic_tcu_of_match, np);
276 const struct ingenic_soc_info *soc_info = id->data;
277 struct ingenic_tcu_timer *timer;
278 struct ingenic_tcu *tcu;
281 int ret, last_bit = -1;
284 of_node_clear_flag(np, OF_POPULATED);
286 map = device_node_to_regmap(np);
290 tcu = kzalloc(struct_size(tcu, timers, num_possible_cpus()),
296 * Enable all TCU channels for PWM use by default except channels 0/1,
297 * and channel 2 if target CPU is JZ4780/X2000 and SMP is selected.
299 tcu->pwm_channels_mask = GENMASK(soc_info->num_channels - 1,
300 num_possible_cpus() + 1);
301 of_property_read_u32(np, "ingenic,pwm-channels-mask",
302 (u32 *)&tcu->pwm_channels_mask);
304 /* Verify that we have at least num_possible_cpus() + 1 free channels */
305 if (hweight8(tcu->pwm_channels_mask) >
306 soc_info->num_channels - num_possible_cpus() + 1) {
307 pr_crit("%s: Invalid PWM channel mask: 0x%02lx\n", __func__,
308 tcu->pwm_channels_mask);
310 goto err_free_ingenic_tcu;
317 for (cpu = 0; cpu < num_possible_cpus(); cpu++) {
318 timer = &tcu->timers[cpu];
321 timer->channel = find_next_zero_bit(&tcu->pwm_channels_mask,
322 soc_info->num_channels,
324 last_bit = timer->channel;
327 tcu->cs_channel = find_next_zero_bit(&tcu->pwm_channels_mask,
328 soc_info->num_channels,
331 ret = ingenic_tcu_clocksource_init(np, tcu);
333 pr_crit("%s: Unable to init clocksource: %d\n", __func__, ret);
334 goto err_free_ingenic_tcu;
337 /* Setup clock events on each CPU core */
338 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "Ingenic XBurst: online",
339 ingenic_tcu_setup_cevt, NULL);
341 pr_crit("%s: Unable to start CPU timers: %d\n", __func__, ret);
342 goto err_tcu_clocksource_cleanup;
345 /* Register the sched_clock at the end as there's no way to undo it */
346 rate = clk_get_rate(tcu->cs_clk);
347 sched_clock_register(ingenic_tcu_timer_read, 16, rate);
351 err_tcu_clocksource_cleanup:
352 clocksource_unregister(&tcu->cs);
353 clk_disable_unprepare(tcu->cs_clk);
354 clk_put(tcu->cs_clk);
355 err_free_ingenic_tcu:
360 TIMER_OF_DECLARE(jz4740_tcu_intc, "ingenic,jz4740-tcu", ingenic_tcu_init);
361 TIMER_OF_DECLARE(jz4725b_tcu_intc, "ingenic,jz4725b-tcu", ingenic_tcu_init);
362 TIMER_OF_DECLARE(jz4760_tcu_intc, "ingenic,jz4760-tcu", ingenic_tcu_init);
363 TIMER_OF_DECLARE(jz4770_tcu_intc, "ingenic,jz4770-tcu", ingenic_tcu_init);
364 TIMER_OF_DECLARE(x1000_tcu_intc, "ingenic,x1000-tcu", ingenic_tcu_init);
366 static int __init ingenic_tcu_probe(struct platform_device *pdev)
368 platform_set_drvdata(pdev, ingenic_tcu);
373 static int __maybe_unused ingenic_tcu_suspend(struct device *dev)
375 struct ingenic_tcu *tcu = dev_get_drvdata(dev);
378 clk_disable(tcu->cs_clk);
380 for (cpu = 0; cpu < num_online_cpus(); cpu++)
381 clk_disable(tcu->timers[cpu].clk);
386 static int __maybe_unused ingenic_tcu_resume(struct device *dev)
388 struct ingenic_tcu *tcu = dev_get_drvdata(dev);
392 for (cpu = 0; cpu < num_online_cpus(); cpu++) {
393 ret = clk_enable(tcu->timers[cpu].clk);
395 goto err_timer_clk_disable;
398 ret = clk_enable(tcu->cs_clk);
400 goto err_timer_clk_disable;
404 err_timer_clk_disable:
405 for (; cpu > 0; cpu--)
406 clk_disable(tcu->timers[cpu - 1].clk);
410 static const struct dev_pm_ops __maybe_unused ingenic_tcu_pm_ops = {
411 /* _noirq: We want the TCU clocks to be gated last / ungated first */
412 .suspend_noirq = ingenic_tcu_suspend,
413 .resume_noirq = ingenic_tcu_resume,
416 static struct platform_driver ingenic_tcu_driver = {
418 .name = "ingenic-tcu-timer",
419 #ifdef CONFIG_PM_SLEEP
420 .pm = &ingenic_tcu_pm_ops,
422 .of_match_table = ingenic_tcu_of_match,
425 builtin_platform_driver_probe(ingenic_tcu_driver, ingenic_tcu_probe);