18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Reset driver for NXP LPC18xx/43xx Reset Generation Unit (RGU).
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/clk.h>
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/err.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/of.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci#include <linux/reboot.h>
168c2ecf20Sopenharmony_ci#include <linux/reset-controller.h>
178c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* LPC18xx RGU registers */
208c2ecf20Sopenharmony_ci#define LPC18XX_RGU_CTRL0		0x100
218c2ecf20Sopenharmony_ci#define LPC18XX_RGU_CTRL1		0x104
228c2ecf20Sopenharmony_ci#define LPC18XX_RGU_ACTIVE_STATUS0	0x150
238c2ecf20Sopenharmony_ci#define LPC18XX_RGU_ACTIVE_STATUS1	0x154
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define LPC18XX_RGU_RESETS_PER_REG	32
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* Internal reset outputs */
288c2ecf20Sopenharmony_ci#define LPC18XX_RGU_CORE_RST	0
298c2ecf20Sopenharmony_ci#define LPC43XX_RGU_M0SUB_RST	12
308c2ecf20Sopenharmony_ci#define LPC43XX_RGU_M0APP_RST	56
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistruct lpc18xx_rgu_data {
338c2ecf20Sopenharmony_ci	struct reset_controller_dev rcdev;
348c2ecf20Sopenharmony_ci	struct notifier_block restart_nb;
358c2ecf20Sopenharmony_ci	struct clk *clk_delay;
368c2ecf20Sopenharmony_ci	struct clk *clk_reg;
378c2ecf20Sopenharmony_ci	void __iomem *base;
388c2ecf20Sopenharmony_ci	spinlock_t lock;
398c2ecf20Sopenharmony_ci	u32 delay_us;
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define to_rgu_data(p) container_of(p, struct lpc18xx_rgu_data, rcdev)
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic int lpc18xx_rgu_restart(struct notifier_block *nb, unsigned long mode,
458c2ecf20Sopenharmony_ci			       void *cmd)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	struct lpc18xx_rgu_data *rc = container_of(nb, struct lpc18xx_rgu_data,
488c2ecf20Sopenharmony_ci						   restart_nb);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	writel(BIT(LPC18XX_RGU_CORE_RST), rc->base + LPC18XX_RGU_CTRL0);
518c2ecf20Sopenharmony_ci	mdelay(2000);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	pr_emerg("%s: unable to restart system\n", __func__);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/*
598c2ecf20Sopenharmony_ci * The LPC18xx RGU has mostly self-deasserting resets except for the
608c2ecf20Sopenharmony_ci * two reset lines going to the internal Cortex-M0 cores.
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * To prevent the M0 core resets from accidentally getting deasserted
638c2ecf20Sopenharmony_ci * status register must be check and bits in control register set to
648c2ecf20Sopenharmony_ci * preserve the state.
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_cistatic int lpc18xx_rgu_setclear_reset(struct reset_controller_dev *rcdev,
678c2ecf20Sopenharmony_ci				      unsigned long id, bool set)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
708c2ecf20Sopenharmony_ci	u32 stat_offset = LPC18XX_RGU_ACTIVE_STATUS0;
718c2ecf20Sopenharmony_ci	u32 ctrl_offset = LPC18XX_RGU_CTRL0;
728c2ecf20Sopenharmony_ci	unsigned long flags;
738c2ecf20Sopenharmony_ci	u32 stat, rst_bit;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	stat_offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
768c2ecf20Sopenharmony_ci	ctrl_offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
778c2ecf20Sopenharmony_ci	rst_bit = 1 << (id % LPC18XX_RGU_RESETS_PER_REG);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rc->lock, flags);
808c2ecf20Sopenharmony_ci	stat = ~readl(rc->base + stat_offset);
818c2ecf20Sopenharmony_ci	if (set)
828c2ecf20Sopenharmony_ci		writel(stat | rst_bit, rc->base + ctrl_offset);
838c2ecf20Sopenharmony_ci	else
848c2ecf20Sopenharmony_ci		writel(stat & ~rst_bit, rc->base + ctrl_offset);
858c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rc->lock, flags);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return 0;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic int lpc18xx_rgu_assert(struct reset_controller_dev *rcdev,
918c2ecf20Sopenharmony_ci			      unsigned long id)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	return lpc18xx_rgu_setclear_reset(rcdev, id, true);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic int lpc18xx_rgu_deassert(struct reset_controller_dev *rcdev,
978c2ecf20Sopenharmony_ci				unsigned long id)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	return lpc18xx_rgu_setclear_reset(rcdev, id, false);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/* Only M0 cores require explicit reset deassert */
1038c2ecf20Sopenharmony_cistatic int lpc18xx_rgu_reset(struct reset_controller_dev *rcdev,
1048c2ecf20Sopenharmony_ci			     unsigned long id)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	lpc18xx_rgu_assert(rcdev, id);
1098c2ecf20Sopenharmony_ci	udelay(rc->delay_us);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	switch (id) {
1128c2ecf20Sopenharmony_ci	case LPC43XX_RGU_M0SUB_RST:
1138c2ecf20Sopenharmony_ci	case LPC43XX_RGU_M0APP_RST:
1148c2ecf20Sopenharmony_ci		lpc18xx_rgu_setclear_reset(rcdev, id, false);
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	return 0;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic int lpc18xx_rgu_status(struct reset_controller_dev *rcdev,
1218c2ecf20Sopenharmony_ci			      unsigned long id)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
1248c2ecf20Sopenharmony_ci	u32 bit, offset = LPC18XX_RGU_ACTIVE_STATUS0;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
1278c2ecf20Sopenharmony_ci	bit = 1 << (id % LPC18XX_RGU_RESETS_PER_REG);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return !(readl(rc->base + offset) & bit);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic const struct reset_control_ops lpc18xx_rgu_ops = {
1338c2ecf20Sopenharmony_ci	.reset		= lpc18xx_rgu_reset,
1348c2ecf20Sopenharmony_ci	.assert		= lpc18xx_rgu_assert,
1358c2ecf20Sopenharmony_ci	.deassert	= lpc18xx_rgu_deassert,
1368c2ecf20Sopenharmony_ci	.status		= lpc18xx_rgu_status,
1378c2ecf20Sopenharmony_ci};
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int lpc18xx_rgu_probe(struct platform_device *pdev)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct lpc18xx_rgu_data *rc;
1428c2ecf20Sopenharmony_ci	struct resource *res;
1438c2ecf20Sopenharmony_ci	u32 fcclk, firc;
1448c2ecf20Sopenharmony_ci	int ret;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	rc = devm_kzalloc(&pdev->dev, sizeof(*rc), GFP_KERNEL);
1478c2ecf20Sopenharmony_ci	if (!rc)
1488c2ecf20Sopenharmony_ci		return -ENOMEM;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1518c2ecf20Sopenharmony_ci	rc->base = devm_ioremap_resource(&pdev->dev, res);
1528c2ecf20Sopenharmony_ci	if (IS_ERR(rc->base))
1538c2ecf20Sopenharmony_ci		return PTR_ERR(rc->base);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	rc->clk_reg = devm_clk_get(&pdev->dev, "reg");
1568c2ecf20Sopenharmony_ci	if (IS_ERR(rc->clk_reg)) {
1578c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "reg clock not found\n");
1588c2ecf20Sopenharmony_ci		return PTR_ERR(rc->clk_reg);
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	rc->clk_delay = devm_clk_get(&pdev->dev, "delay");
1628c2ecf20Sopenharmony_ci	if (IS_ERR(rc->clk_delay)) {
1638c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "delay clock not found\n");
1648c2ecf20Sopenharmony_ci		return PTR_ERR(rc->clk_delay);
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(rc->clk_reg);
1688c2ecf20Sopenharmony_ci	if (ret) {
1698c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "unable to enable reg clock\n");
1708c2ecf20Sopenharmony_ci		return ret;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(rc->clk_delay);
1748c2ecf20Sopenharmony_ci	if (ret) {
1758c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "unable to enable delay clock\n");
1768c2ecf20Sopenharmony_ci		goto dis_clk_reg;
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	fcclk = clk_get_rate(rc->clk_reg) / USEC_PER_SEC;
1808c2ecf20Sopenharmony_ci	firc = clk_get_rate(rc->clk_delay) / USEC_PER_SEC;
1818c2ecf20Sopenharmony_ci	if (fcclk == 0 || firc == 0)
1828c2ecf20Sopenharmony_ci		rc->delay_us = 2;
1838c2ecf20Sopenharmony_ci	else
1848c2ecf20Sopenharmony_ci		rc->delay_us = DIV_ROUND_UP(fcclk, firc * firc);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	spin_lock_init(&rc->lock);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	rc->rcdev.owner = THIS_MODULE;
1898c2ecf20Sopenharmony_ci	rc->rcdev.nr_resets = 64;
1908c2ecf20Sopenharmony_ci	rc->rcdev.ops = &lpc18xx_rgu_ops;
1918c2ecf20Sopenharmony_ci	rc->rcdev.of_node = pdev->dev.of_node;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, rc);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	ret = reset_controller_register(&rc->rcdev);
1968c2ecf20Sopenharmony_ci	if (ret) {
1978c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "unable to register device\n");
1988c2ecf20Sopenharmony_ci		goto dis_clks;
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	rc->restart_nb.priority = 192,
2028c2ecf20Sopenharmony_ci	rc->restart_nb.notifier_call = lpc18xx_rgu_restart,
2038c2ecf20Sopenharmony_ci	ret = register_restart_handler(&rc->restart_nb);
2048c2ecf20Sopenharmony_ci	if (ret)
2058c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "failed to register restart handler\n");
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	return 0;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cidis_clks:
2108c2ecf20Sopenharmony_ci	clk_disable_unprepare(rc->clk_delay);
2118c2ecf20Sopenharmony_cidis_clk_reg:
2128c2ecf20Sopenharmony_ci	clk_disable_unprepare(rc->clk_reg);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	return ret;
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic const struct of_device_id lpc18xx_rgu_match[] = {
2188c2ecf20Sopenharmony_ci	{ .compatible = "nxp,lpc1850-rgu" },
2198c2ecf20Sopenharmony_ci	{ }
2208c2ecf20Sopenharmony_ci};
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic struct platform_driver lpc18xx_rgu_driver = {
2238c2ecf20Sopenharmony_ci	.probe	= lpc18xx_rgu_probe,
2248c2ecf20Sopenharmony_ci	.driver	= {
2258c2ecf20Sopenharmony_ci		.name			= "lpc18xx-reset",
2268c2ecf20Sopenharmony_ci		.of_match_table		= lpc18xx_rgu_match,
2278c2ecf20Sopenharmony_ci		.suppress_bind_attrs	= true,
2288c2ecf20Sopenharmony_ci	},
2298c2ecf20Sopenharmony_ci};
2308c2ecf20Sopenharmony_cibuiltin_platform_driver(lpc18xx_rgu_driver);
231