18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * System timer for CSR SiRFprimaII
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
108c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
118c2ecf20Sopenharmony_ci#include <linux/clocksource.h>
128c2ecf20Sopenharmony_ci#include <linux/cpu.h>
138c2ecf20Sopenharmony_ci#include <linux/bitops.h>
148c2ecf20Sopenharmony_ci#include <linux/irq.h>
158c2ecf20Sopenharmony_ci#include <linux/clk.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/of.h>
188c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
198c2ecf20Sopenharmony_ci#include <linux/of_address.h>
208c2ecf20Sopenharmony_ci#include <linux/sched_clock.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_32COUNTER_0_CTRL			0x0000
238c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_32COUNTER_1_CTRL			0x0004
248c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_MATCH_0				0x0018
258c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_MATCH_1				0x001c
268c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_COUNTER_0				0x0048
278c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_COUNTER_1				0x004c
288c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_INTR_STATUS			0x0060
298c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_WATCHDOG_EN			0x0064
308c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_64COUNTER_CTRL			0x0068
318c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_64COUNTER_LO			0x006c
328c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_64COUNTER_HI			0x0070
338c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_64COUNTER_LOAD_LO			0x0074
348c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_64COUNTER_LOAD_HI			0x0078
358c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_64COUNTER_RLATCHED_LO		0x007c
368c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_64COUNTER_RLATCHED_HI		0x0080
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define SIRFSOC_TIMER_REG_CNT 6
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic unsigned long atlas7_timer_rate;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic const u32 sirfsoc_timer_reg_list[SIRFSOC_TIMER_REG_CNT] = {
438c2ecf20Sopenharmony_ci	SIRFSOC_TIMER_WATCHDOG_EN,
448c2ecf20Sopenharmony_ci	SIRFSOC_TIMER_32COUNTER_0_CTRL,
458c2ecf20Sopenharmony_ci	SIRFSOC_TIMER_32COUNTER_1_CTRL,
468c2ecf20Sopenharmony_ci	SIRFSOC_TIMER_64COUNTER_CTRL,
478c2ecf20Sopenharmony_ci	SIRFSOC_TIMER_64COUNTER_RLATCHED_LO,
488c2ecf20Sopenharmony_ci	SIRFSOC_TIMER_64COUNTER_RLATCHED_HI,
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic u32 sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT];
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic void __iomem *sirfsoc_timer_base;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* disable count and interrupt */
568c2ecf20Sopenharmony_cistatic inline void sirfsoc_timer_count_disable(int idx)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) & ~0x7,
598c2ecf20Sopenharmony_ci		sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* enable count and interrupt */
638c2ecf20Sopenharmony_cistatic inline void sirfsoc_timer_count_enable(int idx)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) | 0x3,
668c2ecf20Sopenharmony_ci		sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx);
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* timer interrupt handler */
708c2ecf20Sopenharmony_cistatic irqreturn_t sirfsoc_timer_interrupt(int irq, void *dev_id)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct clock_event_device *ce = dev_id;
738c2ecf20Sopenharmony_ci	int cpu = smp_processor_id();
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	/* clear timer interrupt */
768c2ecf20Sopenharmony_ci	writel_relaxed(BIT(cpu), sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (clockevent_state_oneshot(ce))
798c2ecf20Sopenharmony_ci		sirfsoc_timer_count_disable(cpu);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	ce->event_handler(ce);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* read 64-bit timer counter */
878c2ecf20Sopenharmony_cistatic u64 sirfsoc_timer_read(struct clocksource *cs)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	u64 cycles;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	writel_relaxed((readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
928c2ecf20Sopenharmony_ci			BIT(0)) & ~BIT(1), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_HI);
958c2ecf20Sopenharmony_ci	cycles = (cycles << 32) | readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_LO);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	return cycles;
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic int sirfsoc_timer_set_next_event(unsigned long delta,
1018c2ecf20Sopenharmony_ci	struct clock_event_device *ce)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	int cpu = smp_processor_id();
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/* disable timer first, then modify the related registers */
1068c2ecf20Sopenharmony_ci	sirfsoc_timer_count_disable(cpu);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0 +
1098c2ecf20Sopenharmony_ci		4 * cpu);
1108c2ecf20Sopenharmony_ci	writel_relaxed(delta, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0 +
1118c2ecf20Sopenharmony_ci		4 * cpu);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* enable the tick */
1148c2ecf20Sopenharmony_ci	sirfsoc_timer_count_enable(cpu);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	return 0;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/* Oneshot is enabled in set_next_event */
1208c2ecf20Sopenharmony_cistatic int sirfsoc_timer_shutdown(struct clock_event_device *evt)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	sirfsoc_timer_count_disable(smp_processor_id());
1238c2ecf20Sopenharmony_ci	return 0;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic void sirfsoc_clocksource_suspend(struct clocksource *cs)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	int i;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
1318c2ecf20Sopenharmony_ci		sirfsoc_timer_reg_val[i] = readl_relaxed(sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic void sirfsoc_clocksource_resume(struct clocksource *cs)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	int i;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	for (i = 0; i < SIRFSOC_TIMER_REG_CNT - 2; i++)
1398c2ecf20Sopenharmony_ci		writel_relaxed(sirfsoc_timer_reg_val[i], sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 2],
1428c2ecf20Sopenharmony_ci		sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
1438c2ecf20Sopenharmony_ci	writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 1],
1448c2ecf20Sopenharmony_ci		sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
1478c2ecf20Sopenharmony_ci		BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic struct clock_event_device __percpu *sirfsoc_clockevent;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic struct clocksource sirfsoc_clocksource = {
1538c2ecf20Sopenharmony_ci	.name = "sirfsoc_clocksource",
1548c2ecf20Sopenharmony_ci	.rating = 200,
1558c2ecf20Sopenharmony_ci	.mask = CLOCKSOURCE_MASK(64),
1568c2ecf20Sopenharmony_ci	.flags = CLOCK_SOURCE_IS_CONTINUOUS,
1578c2ecf20Sopenharmony_ci	.read = sirfsoc_timer_read,
1588c2ecf20Sopenharmony_ci	.suspend = sirfsoc_clocksource_suspend,
1598c2ecf20Sopenharmony_ci	.resume = sirfsoc_clocksource_resume,
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic unsigned int sirfsoc_timer_irq, sirfsoc_timer1_irq;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic int sirfsoc_local_timer_starting_cpu(unsigned int cpu)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct clock_event_device *ce = per_cpu_ptr(sirfsoc_clockevent, cpu);
1678c2ecf20Sopenharmony_ci	unsigned int irq;
1688c2ecf20Sopenharmony_ci	const char *name;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	if (cpu == 0) {
1718c2ecf20Sopenharmony_ci		irq = sirfsoc_timer_irq;
1728c2ecf20Sopenharmony_ci		name = "sirfsoc_timer0";
1738c2ecf20Sopenharmony_ci	} else {
1748c2ecf20Sopenharmony_ci		irq = sirfsoc_timer1_irq;
1758c2ecf20Sopenharmony_ci		name = "sirfsoc_timer1";
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	ce->irq = irq;
1798c2ecf20Sopenharmony_ci	ce->name = "local_timer";
1808c2ecf20Sopenharmony_ci	ce->features = CLOCK_EVT_FEAT_ONESHOT;
1818c2ecf20Sopenharmony_ci	ce->rating = 200;
1828c2ecf20Sopenharmony_ci	ce->set_state_shutdown = sirfsoc_timer_shutdown;
1838c2ecf20Sopenharmony_ci	ce->set_state_oneshot = sirfsoc_timer_shutdown;
1848c2ecf20Sopenharmony_ci	ce->tick_resume = sirfsoc_timer_shutdown;
1858c2ecf20Sopenharmony_ci	ce->set_next_event = sirfsoc_timer_set_next_event;
1868c2ecf20Sopenharmony_ci	clockevents_calc_mult_shift(ce, atlas7_timer_rate, 60);
1878c2ecf20Sopenharmony_ci	ce->max_delta_ns = clockevent_delta2ns(-2, ce);
1888c2ecf20Sopenharmony_ci	ce->max_delta_ticks = (unsigned long)-2;
1898c2ecf20Sopenharmony_ci	ce->min_delta_ns = clockevent_delta2ns(2, ce);
1908c2ecf20Sopenharmony_ci	ce->min_delta_ticks = 2;
1918c2ecf20Sopenharmony_ci	ce->cpumask = cpumask_of(cpu);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	BUG_ON(request_irq(ce->irq, sirfsoc_timer_interrupt,
1948c2ecf20Sopenharmony_ci			   IRQF_TIMER | IRQF_NOBALANCING, name, ce));
1958c2ecf20Sopenharmony_ci	irq_force_affinity(ce->irq, cpumask_of(cpu));
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	clockevents_register_device(ce);
1988c2ecf20Sopenharmony_ci	return 0;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic int sirfsoc_local_timer_dying_cpu(unsigned int cpu)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	struct clock_event_device *ce = per_cpu_ptr(sirfsoc_clockevent, cpu);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	sirfsoc_timer_count_disable(1);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	if (cpu == 0)
2088c2ecf20Sopenharmony_ci		free_irq(sirfsoc_timer_irq, ce);
2098c2ecf20Sopenharmony_ci	else
2108c2ecf20Sopenharmony_ci		free_irq(sirfsoc_timer1_irq, ce);
2118c2ecf20Sopenharmony_ci	return 0;
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic int __init sirfsoc_clockevent_init(void)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	sirfsoc_clockevent = alloc_percpu(struct clock_event_device);
2178c2ecf20Sopenharmony_ci	BUG_ON(!sirfsoc_clockevent);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	/* Install and invoke hotplug callbacks */
2208c2ecf20Sopenharmony_ci	return cpuhp_setup_state(CPUHP_AP_MARCO_TIMER_STARTING,
2218c2ecf20Sopenharmony_ci				 "clockevents/marco:starting",
2228c2ecf20Sopenharmony_ci				 sirfsoc_local_timer_starting_cpu,
2238c2ecf20Sopenharmony_ci				 sirfsoc_local_timer_dying_cpu);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/* initialize the kernel jiffy timer source */
2278c2ecf20Sopenharmony_cistatic int __init sirfsoc_atlas7_timer_init(struct device_node *np)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	struct clk *clk;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	clk = of_clk_get(np, 0);
2328c2ecf20Sopenharmony_ci	BUG_ON(IS_ERR(clk));
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	BUG_ON(clk_prepare_enable(clk));
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	atlas7_timer_rate = clk_get_rate(clk);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	/* timer dividers: 0, not divided */
2398c2ecf20Sopenharmony_ci	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
2408c2ecf20Sopenharmony_ci	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL);
2418c2ecf20Sopenharmony_ci	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_1_CTRL);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/* Initialize timer counters to 0 */
2448c2ecf20Sopenharmony_ci	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
2458c2ecf20Sopenharmony_ci	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
2468c2ecf20Sopenharmony_ci	writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
2478c2ecf20Sopenharmony_ci		BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
2488c2ecf20Sopenharmony_ci	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0);
2498c2ecf20Sopenharmony_ci	writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_1);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	/* Clear all interrupts */
2528c2ecf20Sopenharmony_ci	writel_relaxed(0xFFFF, sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	BUG_ON(clocksource_register_hz(&sirfsoc_clocksource, atlas7_timer_rate));
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	return sirfsoc_clockevent_init();
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cistatic int __init sirfsoc_of_timer_init(struct device_node *np)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	sirfsoc_timer_base = of_iomap(np, 0);
2628c2ecf20Sopenharmony_ci	if (!sirfsoc_timer_base) {
2638c2ecf20Sopenharmony_ci		pr_err("unable to map timer cpu registers\n");
2648c2ecf20Sopenharmony_ci		return -ENXIO;
2658c2ecf20Sopenharmony_ci	}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	sirfsoc_timer_irq = irq_of_parse_and_map(np, 0);
2688c2ecf20Sopenharmony_ci	if (!sirfsoc_timer_irq) {
2698c2ecf20Sopenharmony_ci		pr_err("No irq passed for timer0 via DT\n");
2708c2ecf20Sopenharmony_ci		return -EINVAL;
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	sirfsoc_timer1_irq = irq_of_parse_and_map(np, 1);
2748c2ecf20Sopenharmony_ci	if (!sirfsoc_timer1_irq) {
2758c2ecf20Sopenharmony_ci		pr_err("No irq passed for timer1 via DT\n");
2768c2ecf20Sopenharmony_ci		return -EINVAL;
2778c2ecf20Sopenharmony_ci	}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	return sirfsoc_atlas7_timer_init(np);
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(sirfsoc_atlas7_timer, "sirf,atlas7-tick", sirfsoc_of_timer_init);
282