18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2014-2018 Nuvoton Technologies tomer.maimon@nuvoton.com
48c2ecf20Sopenharmony_ci * All rights reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright 2017 Google, Inc.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/sched.h>
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/err.h>
148c2ecf20Sopenharmony_ci#include <linux/clk.h>
158c2ecf20Sopenharmony_ci#include <linux/io.h>
168c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
178c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
188c2ecf20Sopenharmony_ci#include <linux/of_address.h>
198c2ecf20Sopenharmony_ci#include "timer-of.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* Timers registers */
228c2ecf20Sopenharmony_ci#define NPCM7XX_REG_TCSR0	0x0 /* Timer 0 Control and Status Register */
238c2ecf20Sopenharmony_ci#define NPCM7XX_REG_TICR0	0x8 /* Timer 0 Initial Count Register */
248c2ecf20Sopenharmony_ci#define NPCM7XX_REG_TCSR1	0x4 /* Timer 1 Control and Status Register */
258c2ecf20Sopenharmony_ci#define NPCM7XX_REG_TICR1	0xc /* Timer 1 Initial Count Register */
268c2ecf20Sopenharmony_ci#define NPCM7XX_REG_TDR1	0x14 /* Timer 1 Data Register */
278c2ecf20Sopenharmony_ci#define NPCM7XX_REG_TISR	0x18 /* Timer Interrupt Status Register */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* Timers control */
308c2ecf20Sopenharmony_ci#define NPCM7XX_Tx_RESETINT		0x1f
318c2ecf20Sopenharmony_ci#define NPCM7XX_Tx_PERIOD		BIT(27)
328c2ecf20Sopenharmony_ci#define NPCM7XX_Tx_INTEN		BIT(29)
338c2ecf20Sopenharmony_ci#define NPCM7XX_Tx_COUNTEN		BIT(30)
348c2ecf20Sopenharmony_ci#define NPCM7XX_Tx_ONESHOT		0x0
358c2ecf20Sopenharmony_ci#define NPCM7XX_Tx_OPER			GENMASK(28, 27)
368c2ecf20Sopenharmony_ci#define NPCM7XX_Tx_MIN_PRESCALE		0x1
378c2ecf20Sopenharmony_ci#define NPCM7XX_Tx_TDR_MASK_BITS	24
388c2ecf20Sopenharmony_ci#define NPCM7XX_Tx_MAX_CNT		0xFFFFFF
398c2ecf20Sopenharmony_ci#define NPCM7XX_T0_CLR_INT		0x1
408c2ecf20Sopenharmony_ci#define NPCM7XX_Tx_CLR_CSR		0x0
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* Timers operating mode */
438c2ecf20Sopenharmony_ci#define NPCM7XX_START_PERIODIC_Tx (NPCM7XX_Tx_PERIOD | NPCM7XX_Tx_COUNTEN | \
448c2ecf20Sopenharmony_ci					NPCM7XX_Tx_INTEN | \
458c2ecf20Sopenharmony_ci					NPCM7XX_Tx_MIN_PRESCALE)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define NPCM7XX_START_ONESHOT_Tx (NPCM7XX_Tx_ONESHOT | NPCM7XX_Tx_COUNTEN | \
488c2ecf20Sopenharmony_ci					NPCM7XX_Tx_INTEN | \
498c2ecf20Sopenharmony_ci					NPCM7XX_Tx_MIN_PRESCALE)
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define NPCM7XX_START_Tx (NPCM7XX_Tx_COUNTEN | NPCM7XX_Tx_PERIOD | \
528c2ecf20Sopenharmony_ci				NPCM7XX_Tx_MIN_PRESCALE)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define NPCM7XX_DEFAULT_CSR (NPCM7XX_Tx_CLR_CSR | NPCM7XX_Tx_MIN_PRESCALE)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int npcm7xx_timer_resume(struct clock_event_device *evt)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct timer_of *to = to_timer_of(evt);
598c2ecf20Sopenharmony_ci	u32 val;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	val = readl(timer_of_base(to) + NPCM7XX_REG_TCSR0);
628c2ecf20Sopenharmony_ci	val |= NPCM7XX_Tx_COUNTEN;
638c2ecf20Sopenharmony_ci	writel(val, timer_of_base(to) + NPCM7XX_REG_TCSR0);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return 0;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int npcm7xx_timer_shutdown(struct clock_event_device *evt)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct timer_of *to = to_timer_of(evt);
718c2ecf20Sopenharmony_ci	u32 val;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	val = readl(timer_of_base(to) + NPCM7XX_REG_TCSR0);
748c2ecf20Sopenharmony_ci	val &= ~NPCM7XX_Tx_COUNTEN;
758c2ecf20Sopenharmony_ci	writel(val, timer_of_base(to) + NPCM7XX_REG_TCSR0);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return 0;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic int npcm7xx_timer_oneshot(struct clock_event_device *evt)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	struct timer_of *to = to_timer_of(evt);
838c2ecf20Sopenharmony_ci	u32 val;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	val = readl(timer_of_base(to) + NPCM7XX_REG_TCSR0);
868c2ecf20Sopenharmony_ci	val &= ~NPCM7XX_Tx_OPER;
878c2ecf20Sopenharmony_ci	val |= NPCM7XX_START_ONESHOT_Tx;
888c2ecf20Sopenharmony_ci	writel(val, timer_of_base(to) + NPCM7XX_REG_TCSR0);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	return 0;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int npcm7xx_timer_periodic(struct clock_event_device *evt)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct timer_of *to = to_timer_of(evt);
968c2ecf20Sopenharmony_ci	u32 val;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	writel(timer_of_period(to), timer_of_base(to) + NPCM7XX_REG_TICR0);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	val = readl(timer_of_base(to) + NPCM7XX_REG_TCSR0);
1018c2ecf20Sopenharmony_ci	val &= ~NPCM7XX_Tx_OPER;
1028c2ecf20Sopenharmony_ci	val |= NPCM7XX_START_PERIODIC_Tx;
1038c2ecf20Sopenharmony_ci	writel(val, timer_of_base(to) + NPCM7XX_REG_TCSR0);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	return 0;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int npcm7xx_clockevent_set_next_event(unsigned long evt,
1098c2ecf20Sopenharmony_ci		struct clock_event_device *clk)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	struct timer_of *to = to_timer_of(clk);
1128c2ecf20Sopenharmony_ci	u32 val;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	writel(evt, timer_of_base(to) + NPCM7XX_REG_TICR0);
1158c2ecf20Sopenharmony_ci	val = readl(timer_of_base(to) + NPCM7XX_REG_TCSR0);
1168c2ecf20Sopenharmony_ci	val |= NPCM7XX_START_Tx;
1178c2ecf20Sopenharmony_ci	writel(val, timer_of_base(to) + NPCM7XX_REG_TCSR0);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return 0;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic irqreturn_t npcm7xx_timer0_interrupt(int irq, void *dev_id)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct clock_event_device *evt = (struct clock_event_device *)dev_id;
1258c2ecf20Sopenharmony_ci	struct timer_of *to = to_timer_of(evt);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	writel(NPCM7XX_T0_CLR_INT, timer_of_base(to) + NPCM7XX_REG_TISR);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	evt->event_handler(evt);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic struct timer_of npcm7xx_to = {
1358c2ecf20Sopenharmony_ci	.flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	.clkevt = {
1388c2ecf20Sopenharmony_ci		.name		    = "npcm7xx-timer0",
1398c2ecf20Sopenharmony_ci		.features	    = CLOCK_EVT_FEAT_PERIODIC |
1408c2ecf20Sopenharmony_ci				      CLOCK_EVT_FEAT_ONESHOT,
1418c2ecf20Sopenharmony_ci		.set_next_event	    = npcm7xx_clockevent_set_next_event,
1428c2ecf20Sopenharmony_ci		.set_state_shutdown = npcm7xx_timer_shutdown,
1438c2ecf20Sopenharmony_ci		.set_state_periodic = npcm7xx_timer_periodic,
1448c2ecf20Sopenharmony_ci		.set_state_oneshot  = npcm7xx_timer_oneshot,
1458c2ecf20Sopenharmony_ci		.tick_resume	    = npcm7xx_timer_resume,
1468c2ecf20Sopenharmony_ci		.rating		    = 300,
1478c2ecf20Sopenharmony_ci	},
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	.of_irq = {
1508c2ecf20Sopenharmony_ci		.handler = npcm7xx_timer0_interrupt,
1518c2ecf20Sopenharmony_ci		.flags = IRQF_TIMER | IRQF_IRQPOLL,
1528c2ecf20Sopenharmony_ci	},
1538c2ecf20Sopenharmony_ci};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic void __init npcm7xx_clockevents_init(void)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	writel(NPCM7XX_DEFAULT_CSR,
1588c2ecf20Sopenharmony_ci		timer_of_base(&npcm7xx_to) + NPCM7XX_REG_TCSR0);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	writel(NPCM7XX_Tx_RESETINT,
1618c2ecf20Sopenharmony_ci		timer_of_base(&npcm7xx_to) + NPCM7XX_REG_TISR);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	npcm7xx_to.clkevt.cpumask = cpumask_of(0);
1648c2ecf20Sopenharmony_ci	clockevents_config_and_register(&npcm7xx_to.clkevt,
1658c2ecf20Sopenharmony_ci					timer_of_rate(&npcm7xx_to),
1668c2ecf20Sopenharmony_ci					0x1, NPCM7XX_Tx_MAX_CNT);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic void __init npcm7xx_clocksource_init(void)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	u32 val;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	writel(NPCM7XX_DEFAULT_CSR,
1748c2ecf20Sopenharmony_ci		timer_of_base(&npcm7xx_to) + NPCM7XX_REG_TCSR1);
1758c2ecf20Sopenharmony_ci	writel(NPCM7XX_Tx_MAX_CNT,
1768c2ecf20Sopenharmony_ci		timer_of_base(&npcm7xx_to) + NPCM7XX_REG_TICR1);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	val = readl(timer_of_base(&npcm7xx_to) + NPCM7XX_REG_TCSR1);
1798c2ecf20Sopenharmony_ci	val |= NPCM7XX_START_Tx;
1808c2ecf20Sopenharmony_ci	writel(val, timer_of_base(&npcm7xx_to) + NPCM7XX_REG_TCSR1);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	clocksource_mmio_init(timer_of_base(&npcm7xx_to) +
1838c2ecf20Sopenharmony_ci				NPCM7XX_REG_TDR1,
1848c2ecf20Sopenharmony_ci				"npcm7xx-timer1", timer_of_rate(&npcm7xx_to),
1858c2ecf20Sopenharmony_ci				200, (unsigned int)NPCM7XX_Tx_TDR_MASK_BITS,
1868c2ecf20Sopenharmony_ci				clocksource_mmio_readl_down);
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic int __init npcm7xx_timer_init(struct device_node *np)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	int ret;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	ret = timer_of_init(np, &npcm7xx_to);
1948c2ecf20Sopenharmony_ci	if (ret)
1958c2ecf20Sopenharmony_ci		return ret;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* Clock input is divided by PRESCALE + 1 before it is fed */
1988c2ecf20Sopenharmony_ci	/* to the counter */
1998c2ecf20Sopenharmony_ci	npcm7xx_to.of_clk.rate = npcm7xx_to.of_clk.rate /
2008c2ecf20Sopenharmony_ci		(NPCM7XX_Tx_MIN_PRESCALE + 1);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	npcm7xx_clocksource_init();
2038c2ecf20Sopenharmony_ci	npcm7xx_clockevents_init();
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	pr_info("Enabling NPCM7xx clocksource timer base: %px, IRQ: %d ",
2068c2ecf20Sopenharmony_ci		timer_of_base(&npcm7xx_to), timer_of_irq(&npcm7xx_to));
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	return 0;
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(npcm7xx, "nuvoton,npcm750-timer", npcm7xx_timer_init);
2128c2ecf20Sopenharmony_ci
213