18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  linux/drivers/clocksource/zevio-timer.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2013 Daniel Tang <tangrs@tangrs.id.au>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/io.h>
98c2ecf20Sopenharmony_ci#include <linux/irq.h>
108c2ecf20Sopenharmony_ci#include <linux/of.h>
118c2ecf20Sopenharmony_ci#include <linux/of_address.h>
128c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
138c2ecf20Sopenharmony_ci#include <linux/clk.h>
148c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
158c2ecf20Sopenharmony_ci#include <linux/cpumask.h>
168c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define IO_CURRENT_VAL	0x00
208c2ecf20Sopenharmony_ci#define IO_DIVIDER	0x04
218c2ecf20Sopenharmony_ci#define IO_CONTROL	0x08
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define IO_TIMER1	0x00
248c2ecf20Sopenharmony_ci#define IO_TIMER2	0x0C
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define IO_MATCH_BEGIN	0x18
278c2ecf20Sopenharmony_ci#define IO_MATCH(x)	(IO_MATCH_BEGIN + ((x) << 2))
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define IO_INTR_STS	0x00
308c2ecf20Sopenharmony_ci#define IO_INTR_ACK	0x00
318c2ecf20Sopenharmony_ci#define IO_INTR_MSK	0x04
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define CNTL_STOP_TIMER	(1 << 4)
348c2ecf20Sopenharmony_ci#define CNTL_RUN_TIMER	(0 << 4)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define CNTL_INC	(1 << 3)
378c2ecf20Sopenharmony_ci#define CNTL_DEC	(0 << 3)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define CNTL_TOZERO	0
408c2ecf20Sopenharmony_ci#define CNTL_MATCH(x)	((x) + 1)
418c2ecf20Sopenharmony_ci#define CNTL_FOREVER	7
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* There are 6 match registers but we only use one. */
448c2ecf20Sopenharmony_ci#define TIMER_MATCH	0
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define TIMER_INTR_MSK	(1 << (TIMER_MATCH))
478c2ecf20Sopenharmony_ci#define TIMER_INTR_ALL	0x3F
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistruct zevio_timer {
508c2ecf20Sopenharmony_ci	void __iomem *base;
518c2ecf20Sopenharmony_ci	void __iomem *timer1, *timer2;
528c2ecf20Sopenharmony_ci	void __iomem *interrupt_regs;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	struct clk *clk;
558c2ecf20Sopenharmony_ci	struct clock_event_device clkevt;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	char clocksource_name[64];
588c2ecf20Sopenharmony_ci	char clockevent_name[64];
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic int zevio_timer_set_event(unsigned long delta,
628c2ecf20Sopenharmony_ci				 struct clock_event_device *dev)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct zevio_timer *timer = container_of(dev, struct zevio_timer,
658c2ecf20Sopenharmony_ci						 clkevt);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	writel(delta, timer->timer1 + IO_CURRENT_VAL);
688c2ecf20Sopenharmony_ci	writel(CNTL_RUN_TIMER | CNTL_DEC | CNTL_MATCH(TIMER_MATCH),
698c2ecf20Sopenharmony_ci			timer->timer1 + IO_CONTROL);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	return 0;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic int zevio_timer_shutdown(struct clock_event_device *dev)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct zevio_timer *timer = container_of(dev, struct zevio_timer,
778c2ecf20Sopenharmony_ci						 clkevt);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	/* Disable timer interrupts */
808c2ecf20Sopenharmony_ci	writel(0, timer->interrupt_regs + IO_INTR_MSK);
818c2ecf20Sopenharmony_ci	writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK);
828c2ecf20Sopenharmony_ci	/* Stop timer */
838c2ecf20Sopenharmony_ci	writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL);
848c2ecf20Sopenharmony_ci	return 0;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int zevio_timer_set_oneshot(struct clock_event_device *dev)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct zevio_timer *timer = container_of(dev, struct zevio_timer,
908c2ecf20Sopenharmony_ci						 clkevt);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/* Enable timer interrupts */
938c2ecf20Sopenharmony_ci	writel(TIMER_INTR_MSK, timer->interrupt_regs + IO_INTR_MSK);
948c2ecf20Sopenharmony_ci	writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK);
958c2ecf20Sopenharmony_ci	return 0;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic irqreturn_t zevio_timer_interrupt(int irq, void *dev_id)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	struct zevio_timer *timer = dev_id;
1018c2ecf20Sopenharmony_ci	u32 intr;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	intr = readl(timer->interrupt_regs + IO_INTR_ACK);
1048c2ecf20Sopenharmony_ci	if (!(intr & TIMER_INTR_MSK))
1058c2ecf20Sopenharmony_ci		return IRQ_NONE;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	writel(TIMER_INTR_MSK, timer->interrupt_regs + IO_INTR_ACK);
1088c2ecf20Sopenharmony_ci	writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (timer->clkevt.event_handler)
1118c2ecf20Sopenharmony_ci		timer->clkevt.event_handler(&timer->clkevt);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic int __init zevio_timer_add(struct device_node *node)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct zevio_timer *timer;
1198c2ecf20Sopenharmony_ci	struct resource res;
1208c2ecf20Sopenharmony_ci	int irqnr, ret;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	timer = kzalloc(sizeof(*timer), GFP_KERNEL);
1238c2ecf20Sopenharmony_ci	if (!timer)
1248c2ecf20Sopenharmony_ci		return -ENOMEM;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	timer->base = of_iomap(node, 0);
1278c2ecf20Sopenharmony_ci	if (!timer->base) {
1288c2ecf20Sopenharmony_ci		ret = -EINVAL;
1298c2ecf20Sopenharmony_ci		goto error_free;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci	timer->timer1 = timer->base + IO_TIMER1;
1328c2ecf20Sopenharmony_ci	timer->timer2 = timer->base + IO_TIMER2;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	timer->clk = of_clk_get(node, 0);
1358c2ecf20Sopenharmony_ci	if (IS_ERR(timer->clk)) {
1368c2ecf20Sopenharmony_ci		ret = PTR_ERR(timer->clk);
1378c2ecf20Sopenharmony_ci		pr_err("Timer clock not found! (error %d)\n", ret);
1388c2ecf20Sopenharmony_ci		goto error_unmap;
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	timer->interrupt_regs = of_iomap(node, 1);
1428c2ecf20Sopenharmony_ci	irqnr = irq_of_parse_and_map(node, 0);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	of_address_to_resource(node, 0, &res);
1458c2ecf20Sopenharmony_ci	scnprintf(timer->clocksource_name, sizeof(timer->clocksource_name),
1468c2ecf20Sopenharmony_ci			"%llx.%pOFn_clocksource",
1478c2ecf20Sopenharmony_ci			(unsigned long long)res.start, node);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	scnprintf(timer->clockevent_name, sizeof(timer->clockevent_name),
1508c2ecf20Sopenharmony_ci			"%llx.%pOFn_clockevent",
1518c2ecf20Sopenharmony_ci			(unsigned long long)res.start, node);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	if (timer->interrupt_regs && irqnr) {
1548c2ecf20Sopenharmony_ci		timer->clkevt.name		= timer->clockevent_name;
1558c2ecf20Sopenharmony_ci		timer->clkevt.set_next_event	= zevio_timer_set_event;
1568c2ecf20Sopenharmony_ci		timer->clkevt.set_state_shutdown = zevio_timer_shutdown;
1578c2ecf20Sopenharmony_ci		timer->clkevt.set_state_oneshot = zevio_timer_set_oneshot;
1588c2ecf20Sopenharmony_ci		timer->clkevt.tick_resume	= zevio_timer_set_oneshot;
1598c2ecf20Sopenharmony_ci		timer->clkevt.rating		= 200;
1608c2ecf20Sopenharmony_ci		timer->clkevt.cpumask		= cpu_possible_mask;
1618c2ecf20Sopenharmony_ci		timer->clkevt.features		= CLOCK_EVT_FEAT_ONESHOT;
1628c2ecf20Sopenharmony_ci		timer->clkevt.irq		= irqnr;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL);
1658c2ecf20Sopenharmony_ci		writel(0, timer->timer1 + IO_DIVIDER);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci		/* Start with timer interrupts disabled */
1688c2ecf20Sopenharmony_ci		writel(0, timer->interrupt_regs + IO_INTR_MSK);
1698c2ecf20Sopenharmony_ci		writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci		/* Interrupt to occur when timer value matches 0 */
1728c2ecf20Sopenharmony_ci		writel(0, timer->base + IO_MATCH(TIMER_MATCH));
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci		if (request_irq(irqnr, zevio_timer_interrupt,
1758c2ecf20Sopenharmony_ci				IRQF_TIMER | IRQF_IRQPOLL,
1768c2ecf20Sopenharmony_ci				timer->clockevent_name, timer)) {
1778c2ecf20Sopenharmony_ci			pr_err("%s: request_irq() failed\n",
1788c2ecf20Sopenharmony_ci			       timer->clockevent_name);
1798c2ecf20Sopenharmony_ci		}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci		clockevents_config_and_register(&timer->clkevt,
1828c2ecf20Sopenharmony_ci				clk_get_rate(timer->clk), 0x0001, 0xffff);
1838c2ecf20Sopenharmony_ci		pr_info("Added %s as clockevent\n", timer->clockevent_name);
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	writel(CNTL_STOP_TIMER, timer->timer2 + IO_CONTROL);
1878c2ecf20Sopenharmony_ci	writel(0, timer->timer2 + IO_CURRENT_VAL);
1888c2ecf20Sopenharmony_ci	writel(0, timer->timer2 + IO_DIVIDER);
1898c2ecf20Sopenharmony_ci	writel(CNTL_RUN_TIMER | CNTL_FOREVER | CNTL_INC,
1908c2ecf20Sopenharmony_ci			timer->timer2 + IO_CONTROL);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	clocksource_mmio_init(timer->timer2 + IO_CURRENT_VAL,
1938c2ecf20Sopenharmony_ci			timer->clocksource_name,
1948c2ecf20Sopenharmony_ci			clk_get_rate(timer->clk),
1958c2ecf20Sopenharmony_ci			200, 16,
1968c2ecf20Sopenharmony_ci			clocksource_mmio_readw_up);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	pr_info("Added %s as clocksource\n", timer->clocksource_name);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return 0;
2018c2ecf20Sopenharmony_cierror_unmap:
2028c2ecf20Sopenharmony_ci	iounmap(timer->base);
2038c2ecf20Sopenharmony_cierror_free:
2048c2ecf20Sopenharmony_ci	kfree(timer);
2058c2ecf20Sopenharmony_ci	return ret;
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic int __init zevio_timer_init(struct device_node *node)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	return zevio_timer_add(node);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(zevio_timer, "lsi,zevio-timer", zevio_timer_init);
214