18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *  Copyright (C) 2012 John Crispin <john@phrozen.org>
58c2ecf20Sopenharmony_ci *  Copyright (C) 2012 Lantiq GmbH
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
98c2ecf20Sopenharmony_ci#include <linux/ioport.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
128c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <lantiq_soc.h>
158c2ecf20Sopenharmony_ci#include "../clk.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* the magic ID byte of the core */
188c2ecf20Sopenharmony_ci#define GPTU_MAGIC	0x59
198c2ecf20Sopenharmony_ci/* clock control register */
208c2ecf20Sopenharmony_ci#define GPTU_CLC	0x00
218c2ecf20Sopenharmony_ci/* id register */
228c2ecf20Sopenharmony_ci#define GPTU_ID		0x08
238c2ecf20Sopenharmony_ci/* interrupt node enable */
248c2ecf20Sopenharmony_ci#define GPTU_IRNEN	0xf4
258c2ecf20Sopenharmony_ci/* interrupt control register */
268c2ecf20Sopenharmony_ci#define GPTU_IRCR	0xf8
278c2ecf20Sopenharmony_ci/* interrupt capture register */
288c2ecf20Sopenharmony_ci#define GPTU_IRNCR	0xfc
298c2ecf20Sopenharmony_ci/* there are 3 identical blocks of 2 timers. calculate register offsets */
308c2ecf20Sopenharmony_ci#define GPTU_SHIFT(x)	(x % 2 ? 4 : 0)
318c2ecf20Sopenharmony_ci#define GPTU_BASE(x)	(((x >> 1) * 0x20) + 0x10)
328c2ecf20Sopenharmony_ci/* timer control register */
338c2ecf20Sopenharmony_ci#define GPTU_CON(x)	(GPTU_BASE(x) + GPTU_SHIFT(x) + 0x00)
348c2ecf20Sopenharmony_ci/* timer auto reload register */
358c2ecf20Sopenharmony_ci#define GPTU_RUN(x)	(GPTU_BASE(x) + GPTU_SHIFT(x) + 0x08)
368c2ecf20Sopenharmony_ci/* timer manual reload register */
378c2ecf20Sopenharmony_ci#define GPTU_RLD(x)	(GPTU_BASE(x) + GPTU_SHIFT(x) + 0x10)
388c2ecf20Sopenharmony_ci/* timer count register */
398c2ecf20Sopenharmony_ci#define GPTU_CNT(x)	(GPTU_BASE(x) + GPTU_SHIFT(x) + 0x18)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* GPTU_CON(x) */
428c2ecf20Sopenharmony_ci#define CON_CNT		BIT(2)
438c2ecf20Sopenharmony_ci#define CON_EDGE_ANY	(BIT(7) | BIT(6))
448c2ecf20Sopenharmony_ci#define CON_SYNC	BIT(8)
458c2ecf20Sopenharmony_ci#define CON_CLK_INT	BIT(10)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* GPTU_RUN(x) */
488c2ecf20Sopenharmony_ci#define RUN_SEN		BIT(0)
498c2ecf20Sopenharmony_ci#define RUN_RL		BIT(2)
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* set clock to runmode */
528c2ecf20Sopenharmony_ci#define CLC_RMC		BIT(8)
538c2ecf20Sopenharmony_ci/* bring core out of suspend */
548c2ecf20Sopenharmony_ci#define CLC_SUSPEND	BIT(4)
558c2ecf20Sopenharmony_ci/* the disable bit */
568c2ecf20Sopenharmony_ci#define CLC_DISABLE	BIT(0)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define gptu_w32(x, y)	ltq_w32((x), gptu_membase + (y))
598c2ecf20Sopenharmony_ci#define gptu_r32(x)	ltq_r32(gptu_membase + (x))
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cienum gptu_timer {
628c2ecf20Sopenharmony_ci	TIMER1A = 0,
638c2ecf20Sopenharmony_ci	TIMER1B,
648c2ecf20Sopenharmony_ci	TIMER2A,
658c2ecf20Sopenharmony_ci	TIMER2B,
668c2ecf20Sopenharmony_ci	TIMER3A,
678c2ecf20Sopenharmony_ci	TIMER3B
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic void __iomem *gptu_membase;
718c2ecf20Sopenharmony_cistatic struct resource irqres[6];
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic irqreturn_t timer_irq_handler(int irq, void *priv)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	int timer = irq - irqres[0].start;
768c2ecf20Sopenharmony_ci	gptu_w32(1 << timer, GPTU_IRNCR);
778c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic void gptu_hwinit(void)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	gptu_w32(0x00, GPTU_IRNEN);
838c2ecf20Sopenharmony_ci	gptu_w32(0xff, GPTU_IRNCR);
848c2ecf20Sopenharmony_ci	gptu_w32(CLC_RMC | CLC_SUSPEND, GPTU_CLC);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic void gptu_hwexit(void)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	gptu_w32(0x00, GPTU_IRNEN);
908c2ecf20Sopenharmony_ci	gptu_w32(0xff, GPTU_IRNCR);
918c2ecf20Sopenharmony_ci	gptu_w32(CLC_DISABLE, GPTU_CLC);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic int gptu_enable(struct clk *clk)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	int ret = request_irq(irqres[clk->bits].start, timer_irq_handler,
978c2ecf20Sopenharmony_ci		IRQF_TIMER, "gtpu", NULL);
988c2ecf20Sopenharmony_ci	if (ret) {
998c2ecf20Sopenharmony_ci		pr_err("gptu: failed to request irq\n");
1008c2ecf20Sopenharmony_ci		return ret;
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	gptu_w32(CON_CNT | CON_EDGE_ANY | CON_SYNC | CON_CLK_INT,
1048c2ecf20Sopenharmony_ci		GPTU_CON(clk->bits));
1058c2ecf20Sopenharmony_ci	gptu_w32(1, GPTU_RLD(clk->bits));
1068c2ecf20Sopenharmony_ci	gptu_w32(gptu_r32(GPTU_IRNEN) | BIT(clk->bits), GPTU_IRNEN);
1078c2ecf20Sopenharmony_ci	gptu_w32(RUN_SEN | RUN_RL, GPTU_RUN(clk->bits));
1088c2ecf20Sopenharmony_ci	return 0;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic void gptu_disable(struct clk *clk)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	gptu_w32(0, GPTU_RUN(clk->bits));
1148c2ecf20Sopenharmony_ci	gptu_w32(0, GPTU_CON(clk->bits));
1158c2ecf20Sopenharmony_ci	gptu_w32(0, GPTU_RLD(clk->bits));
1168c2ecf20Sopenharmony_ci	gptu_w32(gptu_r32(GPTU_IRNEN) & ~BIT(clk->bits), GPTU_IRNEN);
1178c2ecf20Sopenharmony_ci	free_irq(irqres[clk->bits].start, NULL);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic inline void clkdev_add_gptu(struct device *dev, const char *con,
1218c2ecf20Sopenharmony_ci							unsigned int timer)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (!clk)
1268c2ecf20Sopenharmony_ci		return;
1278c2ecf20Sopenharmony_ci	clk->cl.dev_id = dev_name(dev);
1288c2ecf20Sopenharmony_ci	clk->cl.con_id = con;
1298c2ecf20Sopenharmony_ci	clk->cl.clk = clk;
1308c2ecf20Sopenharmony_ci	clk->enable = gptu_enable;
1318c2ecf20Sopenharmony_ci	clk->disable = gptu_disable;
1328c2ecf20Sopenharmony_ci	clk->bits = timer;
1338c2ecf20Sopenharmony_ci	clkdev_add(&clk->cl);
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic int gptu_probe(struct platform_device *pdev)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	struct clk *clk;
1398c2ecf20Sopenharmony_ci	struct resource *res;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (of_irq_to_resource_table(pdev->dev.of_node, irqres, 6) != 6) {
1428c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to get IRQ list\n");
1438c2ecf20Sopenharmony_ci		return -EINVAL;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* remap gptu register range */
1498c2ecf20Sopenharmony_ci	gptu_membase = devm_ioremap_resource(&pdev->dev, res);
1508c2ecf20Sopenharmony_ci	if (IS_ERR(gptu_membase))
1518c2ecf20Sopenharmony_ci		return PTR_ERR(gptu_membase);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	/* enable our clock */
1548c2ecf20Sopenharmony_ci	clk = clk_get(&pdev->dev, NULL);
1558c2ecf20Sopenharmony_ci	if (IS_ERR(clk)) {
1568c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to get clock\n");
1578c2ecf20Sopenharmony_ci		return -ENOENT;
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci	clk_enable(clk);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	/* power up the core */
1628c2ecf20Sopenharmony_ci	gptu_hwinit();
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* the gptu has a ID register */
1658c2ecf20Sopenharmony_ci	if (((gptu_r32(GPTU_ID) >> 8) & 0xff) != GPTU_MAGIC) {
1668c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to find magic\n");
1678c2ecf20Sopenharmony_ci		gptu_hwexit();
1688c2ecf20Sopenharmony_ci		clk_disable(clk);
1698c2ecf20Sopenharmony_ci		clk_put(clk);
1708c2ecf20Sopenharmony_ci		return -ENAVAIL;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* register the clocks */
1748c2ecf20Sopenharmony_ci	clkdev_add_gptu(&pdev->dev, "timer1a", TIMER1A);
1758c2ecf20Sopenharmony_ci	clkdev_add_gptu(&pdev->dev, "timer1b", TIMER1B);
1768c2ecf20Sopenharmony_ci	clkdev_add_gptu(&pdev->dev, "timer2a", TIMER2A);
1778c2ecf20Sopenharmony_ci	clkdev_add_gptu(&pdev->dev, "timer2b", TIMER2B);
1788c2ecf20Sopenharmony_ci	clkdev_add_gptu(&pdev->dev, "timer3a", TIMER3A);
1798c2ecf20Sopenharmony_ci	clkdev_add_gptu(&pdev->dev, "timer3b", TIMER3B);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "gptu: 6 timers loaded\n");
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	return 0;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic const struct of_device_id gptu_match[] = {
1878c2ecf20Sopenharmony_ci	{ .compatible = "lantiq,gptu-xway" },
1888c2ecf20Sopenharmony_ci	{},
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic struct platform_driver dma_driver = {
1928c2ecf20Sopenharmony_ci	.probe = gptu_probe,
1938c2ecf20Sopenharmony_ci	.driver = {
1948c2ecf20Sopenharmony_ci		.name = "gptu-xway",
1958c2ecf20Sopenharmony_ci		.of_match_table = gptu_match,
1968c2ecf20Sopenharmony_ci	},
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ciint __init gptu_init(void)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	int ret = platform_driver_register(&dma_driver);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	if (ret)
2048c2ecf20Sopenharmony_ci		pr_info("gptu: Error registering platform driver\n");
2058c2ecf20Sopenharmony_ci	return ret;
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ciarch_initcall(gptu_init);
209