18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2008 STMicroelectronics
48c2ecf20Sopenharmony_ci * Copyright (C) 2010 Alessandro Rubini
58c2ecf20Sopenharmony_ci * Copyright (C) 2010 Linus Walleij for ST-Ericsson
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/init.h>
88c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
98c2ecf20Sopenharmony_ci#include <linux/irq.h>
108c2ecf20Sopenharmony_ci#include <linux/io.h>
118c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
128c2ecf20Sopenharmony_ci#include <linux/clocksource.h>
138c2ecf20Sopenharmony_ci#include <linux/of_address.h>
148c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
158c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
168c2ecf20Sopenharmony_ci#include <linux/clk.h>
178c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
188c2ecf20Sopenharmony_ci#include <linux/delay.h>
198c2ecf20Sopenharmony_ci#include <linux/err.h>
208c2ecf20Sopenharmony_ci#include <linux/sched_clock.h>
218c2ecf20Sopenharmony_ci#include <asm/mach/time.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/*
248c2ecf20Sopenharmony_ci * The MTU device hosts four different counters, with 4 set of
258c2ecf20Sopenharmony_ci * registers. These are register names.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define MTU_IMSC	0x00	/* Interrupt mask set/clear */
298c2ecf20Sopenharmony_ci#define MTU_RIS		0x04	/* Raw interrupt status */
308c2ecf20Sopenharmony_ci#define MTU_MIS		0x08	/* Masked interrupt status */
318c2ecf20Sopenharmony_ci#define MTU_ICR		0x0C	/* Interrupt clear register */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* per-timer registers take 0..3 as argument */
348c2ecf20Sopenharmony_ci#define MTU_LR(x)	(0x10 + 0x10 * (x) + 0x00)	/* Load value */
358c2ecf20Sopenharmony_ci#define MTU_VAL(x)	(0x10 + 0x10 * (x) + 0x04)	/* Current value */
368c2ecf20Sopenharmony_ci#define MTU_CR(x)	(0x10 + 0x10 * (x) + 0x08)	/* Control reg */
378c2ecf20Sopenharmony_ci#define MTU_BGLR(x)	(0x10 + 0x10 * (x) + 0x0c)	/* At next overflow */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/* bits for the control register */
408c2ecf20Sopenharmony_ci#define MTU_CRn_ENA		0x80
418c2ecf20Sopenharmony_ci#define MTU_CRn_PERIODIC	0x40	/* if 0 = free-running */
428c2ecf20Sopenharmony_ci#define MTU_CRn_PRESCALE_MASK	0x0c
438c2ecf20Sopenharmony_ci#define MTU_CRn_PRESCALE_1		0x00
448c2ecf20Sopenharmony_ci#define MTU_CRn_PRESCALE_16		0x04
458c2ecf20Sopenharmony_ci#define MTU_CRn_PRESCALE_256		0x08
468c2ecf20Sopenharmony_ci#define MTU_CRn_32BITS		0x02
478c2ecf20Sopenharmony_ci#define MTU_CRn_ONESHOT		0x01	/* if 0 = wraps reloading from BGLR*/
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* Other registers are usual amba/primecell registers, currently not used */
508c2ecf20Sopenharmony_ci#define MTU_ITCR	0xff0
518c2ecf20Sopenharmony_ci#define MTU_ITOP	0xff4
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define MTU_PERIPH_ID0	0xfe0
548c2ecf20Sopenharmony_ci#define MTU_PERIPH_ID1	0xfe4
558c2ecf20Sopenharmony_ci#define MTU_PERIPH_ID2	0xfe8
568c2ecf20Sopenharmony_ci#define MTU_PERIPH_ID3	0xfeC
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define MTU_PCELL0	0xff0
598c2ecf20Sopenharmony_ci#define MTU_PCELL1	0xff4
608c2ecf20Sopenharmony_ci#define MTU_PCELL2	0xff8
618c2ecf20Sopenharmony_ci#define MTU_PCELL3	0xffC
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic void __iomem *mtu_base;
648c2ecf20Sopenharmony_cistatic bool clkevt_periodic;
658c2ecf20Sopenharmony_cistatic u32 clk_prescale;
668c2ecf20Sopenharmony_cistatic u32 nmdk_cycle;		/* write-once */
678c2ecf20Sopenharmony_cistatic struct delay_timer mtu_delay_timer;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/*
708c2ecf20Sopenharmony_ci * Override the global weak sched_clock symbol with this
718c2ecf20Sopenharmony_ci * local implementation which uses the clocksource to get some
728c2ecf20Sopenharmony_ci * better resolution when scheduling the kernel.
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_cistatic u64 notrace nomadik_read_sched_clock(void)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	if (unlikely(!mtu_base))
778c2ecf20Sopenharmony_ci		return 0;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	return -readl(mtu_base + MTU_VAL(0));
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic unsigned long nmdk_timer_read_current_timer(void)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	return ~readl_relaxed(mtu_base + MTU_VAL(0));
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* Clockevent device: use one-shot mode */
888c2ecf20Sopenharmony_cistatic int nmdk_clkevt_next(unsigned long evt, struct clock_event_device *ev)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	writel(1 << 1, mtu_base + MTU_IMSC);
918c2ecf20Sopenharmony_ci	writel(evt, mtu_base + MTU_LR(1));
928c2ecf20Sopenharmony_ci	/* Load highest value, enable device, enable interrupts */
938c2ecf20Sopenharmony_ci	writel(MTU_CRn_ONESHOT | clk_prescale |
948c2ecf20Sopenharmony_ci	       MTU_CRn_32BITS | MTU_CRn_ENA,
958c2ecf20Sopenharmony_ci	       mtu_base + MTU_CR(1));
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	return 0;
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic void nmdk_clkevt_reset(void)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	if (clkevt_periodic) {
1038c2ecf20Sopenharmony_ci		/* Timer: configure load and background-load, and fire it up */
1048c2ecf20Sopenharmony_ci		writel(nmdk_cycle, mtu_base + MTU_LR(1));
1058c2ecf20Sopenharmony_ci		writel(nmdk_cycle, mtu_base + MTU_BGLR(1));
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		writel(MTU_CRn_PERIODIC | clk_prescale |
1088c2ecf20Sopenharmony_ci		       MTU_CRn_32BITS | MTU_CRn_ENA,
1098c2ecf20Sopenharmony_ci		       mtu_base + MTU_CR(1));
1108c2ecf20Sopenharmony_ci		writel(1 << 1, mtu_base + MTU_IMSC);
1118c2ecf20Sopenharmony_ci	} else {
1128c2ecf20Sopenharmony_ci		/* Generate an interrupt to start the clockevent again */
1138c2ecf20Sopenharmony_ci		(void) nmdk_clkevt_next(nmdk_cycle, NULL);
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int nmdk_clkevt_shutdown(struct clock_event_device *evt)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	writel(0, mtu_base + MTU_IMSC);
1208c2ecf20Sopenharmony_ci	/* disable timer */
1218c2ecf20Sopenharmony_ci	writel(0, mtu_base + MTU_CR(1));
1228c2ecf20Sopenharmony_ci	/* load some high default value */
1238c2ecf20Sopenharmony_ci	writel(0xffffffff, mtu_base + MTU_LR(1));
1248c2ecf20Sopenharmony_ci	return 0;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic int nmdk_clkevt_set_oneshot(struct clock_event_device *evt)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	clkevt_periodic = false;
1308c2ecf20Sopenharmony_ci	return 0;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic int nmdk_clkevt_set_periodic(struct clock_event_device *evt)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	clkevt_periodic = true;
1368c2ecf20Sopenharmony_ci	nmdk_clkevt_reset();
1378c2ecf20Sopenharmony_ci	return 0;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic void nmdk_clksrc_reset(void)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	/* Disable */
1438c2ecf20Sopenharmony_ci	writel(0, mtu_base + MTU_CR(0));
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* ClockSource: configure load and background-load, and fire it up */
1468c2ecf20Sopenharmony_ci	writel(nmdk_cycle, mtu_base + MTU_LR(0));
1478c2ecf20Sopenharmony_ci	writel(nmdk_cycle, mtu_base + MTU_BGLR(0));
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	writel(clk_prescale | MTU_CRn_32BITS | MTU_CRn_ENA,
1508c2ecf20Sopenharmony_ci	       mtu_base + MTU_CR(0));
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic void nmdk_clkevt_resume(struct clock_event_device *cedev)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	nmdk_clkevt_reset();
1568c2ecf20Sopenharmony_ci	nmdk_clksrc_reset();
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic struct clock_event_device nmdk_clkevt = {
1608c2ecf20Sopenharmony_ci	.name			= "mtu_1",
1618c2ecf20Sopenharmony_ci	.features		= CLOCK_EVT_FEAT_ONESHOT |
1628c2ecf20Sopenharmony_ci				  CLOCK_EVT_FEAT_PERIODIC |
1638c2ecf20Sopenharmony_ci				  CLOCK_EVT_FEAT_DYNIRQ,
1648c2ecf20Sopenharmony_ci	.rating			= 200,
1658c2ecf20Sopenharmony_ci	.set_state_shutdown	= nmdk_clkevt_shutdown,
1668c2ecf20Sopenharmony_ci	.set_state_periodic	= nmdk_clkevt_set_periodic,
1678c2ecf20Sopenharmony_ci	.set_state_oneshot	= nmdk_clkevt_set_oneshot,
1688c2ecf20Sopenharmony_ci	.set_next_event		= nmdk_clkevt_next,
1698c2ecf20Sopenharmony_ci	.resume			= nmdk_clkevt_resume,
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/*
1738c2ecf20Sopenharmony_ci * IRQ Handler for timer 1 of the MTU block.
1748c2ecf20Sopenharmony_ci */
1758c2ecf20Sopenharmony_cistatic irqreturn_t nmdk_timer_interrupt(int irq, void *dev_id)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct clock_event_device *evdev = dev_id;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	writel(1 << 1, mtu_base + MTU_ICR); /* Interrupt clear reg */
1808c2ecf20Sopenharmony_ci	evdev->event_handler(evdev);
1818c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic int __init nmdk_timer_init(void __iomem *base, int irq,
1858c2ecf20Sopenharmony_ci				   struct clk *pclk, struct clk *clk)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	unsigned long rate;
1888c2ecf20Sopenharmony_ci	int ret;
1898c2ecf20Sopenharmony_ci	int min_ticks;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	mtu_base = base;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	BUG_ON(clk_prepare_enable(pclk));
1948c2ecf20Sopenharmony_ci	BUG_ON(clk_prepare_enable(clk));
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	/*
1978c2ecf20Sopenharmony_ci	 * Tick rate is 2.4MHz for Nomadik and 2.4Mhz, 100MHz or 133 MHz
1988c2ecf20Sopenharmony_ci	 * for ux500, and in one specific Ux500 case 32768 Hz.
1998c2ecf20Sopenharmony_ci	 *
2008c2ecf20Sopenharmony_ci	 * Use a divide-by-16 counter if the tick rate is more than 32MHz.
2018c2ecf20Sopenharmony_ci	 * At 32 MHz, the timer (with 32 bit counter) can be programmed
2028c2ecf20Sopenharmony_ci	 * to wake-up at a max 127s a head in time. Dividing a 2.4 MHz timer
2038c2ecf20Sopenharmony_ci	 * with 16 gives too low timer resolution.
2048c2ecf20Sopenharmony_ci	 */
2058c2ecf20Sopenharmony_ci	rate = clk_get_rate(clk);
2068c2ecf20Sopenharmony_ci	if (rate > 32000000) {
2078c2ecf20Sopenharmony_ci		rate /= 16;
2088c2ecf20Sopenharmony_ci		clk_prescale = MTU_CRn_PRESCALE_16;
2098c2ecf20Sopenharmony_ci	} else {
2108c2ecf20Sopenharmony_ci		clk_prescale = MTU_CRn_PRESCALE_1;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/* Cycles for periodic mode */
2148c2ecf20Sopenharmony_ci	nmdk_cycle = DIV_ROUND_CLOSEST(rate, HZ);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* Timer 0 is the free running clocksource */
2188c2ecf20Sopenharmony_ci	nmdk_clksrc_reset();
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	ret = clocksource_mmio_init(mtu_base + MTU_VAL(0), "mtu_0",
2218c2ecf20Sopenharmony_ci				    rate, 200, 32, clocksource_mmio_readl_down);
2228c2ecf20Sopenharmony_ci	if (ret) {
2238c2ecf20Sopenharmony_ci		pr_err("timer: failed to initialize clock source %s\n", "mtu_0");
2248c2ecf20Sopenharmony_ci		return ret;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	sched_clock_register(nomadik_read_sched_clock, 32, rate);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* Timer 1 is used for events, register irq and clockevents */
2308c2ecf20Sopenharmony_ci	if (request_irq(irq, nmdk_timer_interrupt, IRQF_TIMER,
2318c2ecf20Sopenharmony_ci			"Nomadik Timer Tick", &nmdk_clkevt))
2328c2ecf20Sopenharmony_ci		pr_err("%s: request_irq() failed\n", "Nomadik Timer Tick");
2338c2ecf20Sopenharmony_ci	nmdk_clkevt.cpumask = cpumask_of(0);
2348c2ecf20Sopenharmony_ci	nmdk_clkevt.irq = irq;
2358c2ecf20Sopenharmony_ci	if (rate < 100000)
2368c2ecf20Sopenharmony_ci		min_ticks = 5;
2378c2ecf20Sopenharmony_ci	else
2388c2ecf20Sopenharmony_ci		min_ticks = 2;
2398c2ecf20Sopenharmony_ci	clockevents_config_and_register(&nmdk_clkevt, rate, min_ticks,
2408c2ecf20Sopenharmony_ci					0xffffffffU);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	mtu_delay_timer.read_current_timer = &nmdk_timer_read_current_timer;
2438c2ecf20Sopenharmony_ci	mtu_delay_timer.freq = rate;
2448c2ecf20Sopenharmony_ci	register_current_timer_delay(&mtu_delay_timer);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	return 0;
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic int __init nmdk_timer_of_init(struct device_node *node)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	struct clk *pclk;
2528c2ecf20Sopenharmony_ci	struct clk *clk;
2538c2ecf20Sopenharmony_ci	void __iomem *base;
2548c2ecf20Sopenharmony_ci	int irq;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	base = of_iomap(node, 0);
2578c2ecf20Sopenharmony_ci	if (!base) {
2588c2ecf20Sopenharmony_ci		pr_err("Can't remap registers\n");
2598c2ecf20Sopenharmony_ci		return -ENXIO;
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	pclk = of_clk_get_by_name(node, "apb_pclk");
2638c2ecf20Sopenharmony_ci	if (IS_ERR(pclk)) {
2648c2ecf20Sopenharmony_ci		pr_err("could not get apb_pclk\n");
2658c2ecf20Sopenharmony_ci		return PTR_ERR(pclk);
2668c2ecf20Sopenharmony_ci	}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	clk = of_clk_get_by_name(node, "timclk");
2698c2ecf20Sopenharmony_ci	if (IS_ERR(clk)) {
2708c2ecf20Sopenharmony_ci		pr_err("could not get timclk\n");
2718c2ecf20Sopenharmony_ci		return PTR_ERR(clk);
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	irq = irq_of_parse_and_map(node, 0);
2758c2ecf20Sopenharmony_ci	if (irq <= 0) {
2768c2ecf20Sopenharmony_ci		pr_err("Can't parse IRQ\n");
2778c2ecf20Sopenharmony_ci		return -EINVAL;
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	return nmdk_timer_init(base, irq, pclk, clk);
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(nomadik_mtu, "st,nomadik-mtu",
2838c2ecf20Sopenharmony_ci		       nmdk_timer_of_init);
284