18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Actions Semi Owl timer
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2012 Actions Semi Inc.
68c2ecf20Sopenharmony_ci * Author: Actions Semi, Inc.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (c) 2017 SUSE Linux GmbH
98c2ecf20Sopenharmony_ci * Author: Andreas Färber
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
148c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
158c2ecf20Sopenharmony_ci#include <linux/irq.h>
168c2ecf20Sopenharmony_ci#include <linux/irqreturn.h>
178c2ecf20Sopenharmony_ci#include <linux/sched_clock.h>
188c2ecf20Sopenharmony_ci#include <linux/of.h>
198c2ecf20Sopenharmony_ci#include <linux/of_address.h>
208c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define OWL_Tx_CTL		0x0
238c2ecf20Sopenharmony_ci#define OWL_Tx_CMP		0x4
248c2ecf20Sopenharmony_ci#define OWL_Tx_VAL		0x8
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define OWL_Tx_CTL_PD		BIT(0)
278c2ecf20Sopenharmony_ci#define OWL_Tx_CTL_INTEN	BIT(1)
288c2ecf20Sopenharmony_ci#define OWL_Tx_CTL_EN		BIT(2)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic void __iomem *owl_timer_base;
318c2ecf20Sopenharmony_cistatic void __iomem *owl_clksrc_base;
328c2ecf20Sopenharmony_cistatic void __iomem *owl_clkevt_base;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic inline void owl_timer_reset(void __iomem *base)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	writel(0, base + OWL_Tx_CTL);
378c2ecf20Sopenharmony_ci	writel(0, base + OWL_Tx_VAL);
388c2ecf20Sopenharmony_ci	writel(0, base + OWL_Tx_CMP);
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic inline void owl_timer_set_enabled(void __iomem *base, bool enabled)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	u32 ctl = readl(base + OWL_Tx_CTL);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/* PD bit is cleared when set */
468c2ecf20Sopenharmony_ci	ctl &= ~OWL_Tx_CTL_PD;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	if (enabled)
498c2ecf20Sopenharmony_ci		ctl |= OWL_Tx_CTL_EN;
508c2ecf20Sopenharmony_ci	else
518c2ecf20Sopenharmony_ci		ctl &= ~OWL_Tx_CTL_EN;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	writel(ctl, base + OWL_Tx_CTL);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic u64 notrace owl_timer_sched_read(void)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	return (u64)readl(owl_clksrc_base + OWL_Tx_VAL);
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic int owl_timer_set_state_shutdown(struct clock_event_device *evt)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	owl_timer_set_enabled(owl_clkevt_base, false);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return 0;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int owl_timer_set_state_oneshot(struct clock_event_device *evt)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	owl_timer_reset(owl_clkevt_base);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	return 0;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic int owl_timer_tick_resume(struct clock_event_device *evt)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	return 0;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic int owl_timer_set_next_event(unsigned long evt,
818c2ecf20Sopenharmony_ci				    struct clock_event_device *ev)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	void __iomem *base = owl_clkevt_base;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	owl_timer_set_enabled(base, false);
868c2ecf20Sopenharmony_ci	writel(OWL_Tx_CTL_INTEN, base + OWL_Tx_CTL);
878c2ecf20Sopenharmony_ci	writel(0, base + OWL_Tx_VAL);
888c2ecf20Sopenharmony_ci	writel(evt, base + OWL_Tx_CMP);
898c2ecf20Sopenharmony_ci	owl_timer_set_enabled(base, true);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	return 0;
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic struct clock_event_device owl_clockevent = {
958c2ecf20Sopenharmony_ci	.name			= "owl_tick",
968c2ecf20Sopenharmony_ci	.rating			= 200,
978c2ecf20Sopenharmony_ci	.features		= CLOCK_EVT_FEAT_ONESHOT |
988c2ecf20Sopenharmony_ci				  CLOCK_EVT_FEAT_DYNIRQ,
998c2ecf20Sopenharmony_ci	.set_state_shutdown	= owl_timer_set_state_shutdown,
1008c2ecf20Sopenharmony_ci	.set_state_oneshot	= owl_timer_set_state_oneshot,
1018c2ecf20Sopenharmony_ci	.tick_resume		= owl_timer_tick_resume,
1028c2ecf20Sopenharmony_ci	.set_next_event		= owl_timer_set_next_event,
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic irqreturn_t owl_timer1_interrupt(int irq, void *dev_id)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct clock_event_device *evt = (struct clock_event_device *)dev_id;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	writel(OWL_Tx_CTL_PD, owl_clkevt_base + OWL_Tx_CTL);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	evt->event_handler(evt);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic int __init owl_timer_init(struct device_node *node)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct clk *clk;
1198c2ecf20Sopenharmony_ci	unsigned long rate;
1208c2ecf20Sopenharmony_ci	int timer1_irq, ret;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	owl_timer_base = of_io_request_and_map(node, 0, "owl-timer");
1238c2ecf20Sopenharmony_ci	if (IS_ERR(owl_timer_base)) {
1248c2ecf20Sopenharmony_ci		pr_err("Can't map timer registers\n");
1258c2ecf20Sopenharmony_ci		return PTR_ERR(owl_timer_base);
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	owl_clksrc_base = owl_timer_base + 0x08;
1298c2ecf20Sopenharmony_ci	owl_clkevt_base = owl_timer_base + 0x14;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	timer1_irq = of_irq_get_byname(node, "timer1");
1328c2ecf20Sopenharmony_ci	if (timer1_irq <= 0) {
1338c2ecf20Sopenharmony_ci		pr_err("Can't parse timer1 IRQ\n");
1348c2ecf20Sopenharmony_ci		return -EINVAL;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	clk = of_clk_get(node, 0);
1388c2ecf20Sopenharmony_ci	if (IS_ERR(clk)) {
1398c2ecf20Sopenharmony_ci		ret = PTR_ERR(clk);
1408c2ecf20Sopenharmony_ci		pr_err("Failed to get clock for clocksource (%d)\n", ret);
1418c2ecf20Sopenharmony_ci		return ret;
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	rate = clk_get_rate(clk);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	owl_timer_reset(owl_clksrc_base);
1478c2ecf20Sopenharmony_ci	owl_timer_set_enabled(owl_clksrc_base, true);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	sched_clock_register(owl_timer_sched_read, 32, rate);
1508c2ecf20Sopenharmony_ci	ret = clocksource_mmio_init(owl_clksrc_base + OWL_Tx_VAL, node->name,
1518c2ecf20Sopenharmony_ci				    rate, 200, 32, clocksource_mmio_readl_up);
1528c2ecf20Sopenharmony_ci	if (ret) {
1538c2ecf20Sopenharmony_ci		pr_err("Failed to register clocksource (%d)\n", ret);
1548c2ecf20Sopenharmony_ci		return ret;
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	owl_timer_reset(owl_clkevt_base);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	ret = request_irq(timer1_irq, owl_timer1_interrupt, IRQF_TIMER,
1608c2ecf20Sopenharmony_ci			  "owl-timer", &owl_clockevent);
1618c2ecf20Sopenharmony_ci	if (ret) {
1628c2ecf20Sopenharmony_ci		pr_err("failed to request irq %d\n", timer1_irq);
1638c2ecf20Sopenharmony_ci		return ret;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	owl_clockevent.cpumask = cpumask_of(0);
1678c2ecf20Sopenharmony_ci	owl_clockevent.irq = timer1_irq;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	clockevents_config_and_register(&owl_clockevent, rate,
1708c2ecf20Sopenharmony_ci					0xf, 0xffffffff);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	return 0;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(owl_s500, "actions,s500-timer", owl_timer_init);
1758c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(owl_s700, "actions,s700-timer", owl_timer_init);
1768c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(owl_s900, "actions,s900-timer", owl_timer_init);
177