xref: /linux/drivers/reset/reset-lpc18xx.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c392b65bSJoachim Eastwood /*
3c392b65bSJoachim Eastwood  * Reset driver for NXP LPC18xx/43xx Reset Generation Unit (RGU).
4c392b65bSJoachim Eastwood  *
5c392b65bSJoachim Eastwood  * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
6c392b65bSJoachim Eastwood  */
7c392b65bSJoachim Eastwood 
8c392b65bSJoachim Eastwood #include <linux/clk.h>
9c392b65bSJoachim Eastwood #include <linux/delay.h>
10c392b65bSJoachim Eastwood #include <linux/err.h>
11c392b65bSJoachim Eastwood #include <linux/io.h>
12cdd24f76SPaul Gortmaker #include <linux/init.h>
13c392b65bSJoachim Eastwood #include <linux/of.h>
14c392b65bSJoachim Eastwood #include <linux/platform_device.h>
15c392b65bSJoachim Eastwood #include <linux/reboot.h>
16c392b65bSJoachim Eastwood #include <linux/reset-controller.h>
17c392b65bSJoachim Eastwood #include <linux/spinlock.h>
18c392b65bSJoachim Eastwood 
19c392b65bSJoachim Eastwood /* LPC18xx RGU registers */
20c392b65bSJoachim Eastwood #define LPC18XX_RGU_CTRL0		0x100
21c392b65bSJoachim Eastwood #define LPC18XX_RGU_CTRL1		0x104
22c392b65bSJoachim Eastwood #define LPC18XX_RGU_ACTIVE_STATUS0	0x150
23c392b65bSJoachim Eastwood #define LPC18XX_RGU_ACTIVE_STATUS1	0x154
24c392b65bSJoachim Eastwood 
25c392b65bSJoachim Eastwood #define LPC18XX_RGU_RESETS_PER_REG	32
26c392b65bSJoachim Eastwood 
27c392b65bSJoachim Eastwood /* Internal reset outputs */
28c392b65bSJoachim Eastwood #define LPC18XX_RGU_CORE_RST	0
29c392b65bSJoachim Eastwood #define LPC43XX_RGU_M0SUB_RST	12
30c392b65bSJoachim Eastwood #define LPC43XX_RGU_M0APP_RST	56
31c392b65bSJoachim Eastwood 
32c392b65bSJoachim Eastwood struct lpc18xx_rgu_data {
33c392b65bSJoachim Eastwood 	struct reset_controller_dev rcdev;
34773fe726SJoachim Eastwood 	struct notifier_block restart_nb;
35c392b65bSJoachim Eastwood 	struct clk *clk_delay;
36c392b65bSJoachim Eastwood 	struct clk *clk_reg;
37c392b65bSJoachim Eastwood 	void __iomem *base;
38c392b65bSJoachim Eastwood 	spinlock_t lock;
39c392b65bSJoachim Eastwood 	u32 delay_us;
40c392b65bSJoachim Eastwood };
41c392b65bSJoachim Eastwood 
42c392b65bSJoachim Eastwood #define to_rgu_data(p) container_of(p, struct lpc18xx_rgu_data, rcdev)
43c392b65bSJoachim Eastwood 
lpc18xx_rgu_restart(struct notifier_block * nb,unsigned long mode,void * cmd)44773fe726SJoachim Eastwood static int lpc18xx_rgu_restart(struct notifier_block *nb, unsigned long mode,
45c392b65bSJoachim Eastwood 			       void *cmd)
46c392b65bSJoachim Eastwood {
47773fe726SJoachim Eastwood 	struct lpc18xx_rgu_data *rc = container_of(nb, struct lpc18xx_rgu_data,
48773fe726SJoachim Eastwood 						   restart_nb);
49773fe726SJoachim Eastwood 
50773fe726SJoachim Eastwood 	writel(BIT(LPC18XX_RGU_CORE_RST), rc->base + LPC18XX_RGU_CTRL0);
51c392b65bSJoachim Eastwood 	mdelay(2000);
52c392b65bSJoachim Eastwood 
53c392b65bSJoachim Eastwood 	pr_emerg("%s: unable to restart system\n", __func__);
54c392b65bSJoachim Eastwood 
55c392b65bSJoachim Eastwood 	return NOTIFY_DONE;
56c392b65bSJoachim Eastwood }
57c392b65bSJoachim Eastwood 
58c392b65bSJoachim Eastwood /*
59c392b65bSJoachim Eastwood  * The LPC18xx RGU has mostly self-deasserting resets except for the
60c392b65bSJoachim Eastwood  * two reset lines going to the internal Cortex-M0 cores.
61c392b65bSJoachim Eastwood  *
62c392b65bSJoachim Eastwood  * To prevent the M0 core resets from accidentally getting deasserted
63c392b65bSJoachim Eastwood  * status register must be check and bits in control register set to
64c392b65bSJoachim Eastwood  * preserve the state.
65c392b65bSJoachim Eastwood  */
lpc18xx_rgu_setclear_reset(struct reset_controller_dev * rcdev,unsigned long id,bool set)66c392b65bSJoachim Eastwood static int lpc18xx_rgu_setclear_reset(struct reset_controller_dev *rcdev,
67c392b65bSJoachim Eastwood 				      unsigned long id, bool set)
68c392b65bSJoachim Eastwood {
69c392b65bSJoachim Eastwood 	struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
70c392b65bSJoachim Eastwood 	u32 stat_offset = LPC18XX_RGU_ACTIVE_STATUS0;
71c392b65bSJoachim Eastwood 	u32 ctrl_offset = LPC18XX_RGU_CTRL0;
72c392b65bSJoachim Eastwood 	unsigned long flags;
73c392b65bSJoachim Eastwood 	u32 stat, rst_bit;
74c392b65bSJoachim Eastwood 
75c392b65bSJoachim Eastwood 	stat_offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
76c392b65bSJoachim Eastwood 	ctrl_offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
77c392b65bSJoachim Eastwood 	rst_bit = 1 << (id % LPC18XX_RGU_RESETS_PER_REG);
78c392b65bSJoachim Eastwood 
79c392b65bSJoachim Eastwood 	spin_lock_irqsave(&rc->lock, flags);
80c392b65bSJoachim Eastwood 	stat = ~readl(rc->base + stat_offset);
81c392b65bSJoachim Eastwood 	if (set)
82c392b65bSJoachim Eastwood 		writel(stat | rst_bit, rc->base + ctrl_offset);
83c392b65bSJoachim Eastwood 	else
84c392b65bSJoachim Eastwood 		writel(stat & ~rst_bit, rc->base + ctrl_offset);
85c392b65bSJoachim Eastwood 	spin_unlock_irqrestore(&rc->lock, flags);
86c392b65bSJoachim Eastwood 
87c392b65bSJoachim Eastwood 	return 0;
88c392b65bSJoachim Eastwood }
89c392b65bSJoachim Eastwood 
lpc18xx_rgu_assert(struct reset_controller_dev * rcdev,unsigned long id)90c392b65bSJoachim Eastwood static int lpc18xx_rgu_assert(struct reset_controller_dev *rcdev,
91c392b65bSJoachim Eastwood 			      unsigned long id)
92c392b65bSJoachim Eastwood {
93c392b65bSJoachim Eastwood 	return lpc18xx_rgu_setclear_reset(rcdev, id, true);
94c392b65bSJoachim Eastwood }
95c392b65bSJoachim Eastwood 
lpc18xx_rgu_deassert(struct reset_controller_dev * rcdev,unsigned long id)96c392b65bSJoachim Eastwood static int lpc18xx_rgu_deassert(struct reset_controller_dev *rcdev,
97c392b65bSJoachim Eastwood 				unsigned long id)
98c392b65bSJoachim Eastwood {
99c392b65bSJoachim Eastwood 	return lpc18xx_rgu_setclear_reset(rcdev, id, false);
100c392b65bSJoachim Eastwood }
101c392b65bSJoachim Eastwood 
102c392b65bSJoachim Eastwood /* Only M0 cores require explicit reset deassert */
lpc18xx_rgu_reset(struct reset_controller_dev * rcdev,unsigned long id)103c392b65bSJoachim Eastwood static int lpc18xx_rgu_reset(struct reset_controller_dev *rcdev,
104c392b65bSJoachim Eastwood 			     unsigned long id)
105c392b65bSJoachim Eastwood {
106c392b65bSJoachim Eastwood 	struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
107c392b65bSJoachim Eastwood 
108c392b65bSJoachim Eastwood 	lpc18xx_rgu_assert(rcdev, id);
109c392b65bSJoachim Eastwood 	udelay(rc->delay_us);
110c392b65bSJoachim Eastwood 
111c392b65bSJoachim Eastwood 	switch (id) {
112c392b65bSJoachim Eastwood 	case LPC43XX_RGU_M0SUB_RST:
113c392b65bSJoachim Eastwood 	case LPC43XX_RGU_M0APP_RST:
114c392b65bSJoachim Eastwood 		lpc18xx_rgu_setclear_reset(rcdev, id, false);
115c392b65bSJoachim Eastwood 	}
116c392b65bSJoachim Eastwood 
117c392b65bSJoachim Eastwood 	return 0;
118c392b65bSJoachim Eastwood }
119c392b65bSJoachim Eastwood 
lpc18xx_rgu_status(struct reset_controller_dev * rcdev,unsigned long id)120c392b65bSJoachim Eastwood static int lpc18xx_rgu_status(struct reset_controller_dev *rcdev,
121c392b65bSJoachim Eastwood 			      unsigned long id)
122c392b65bSJoachim Eastwood {
123c392b65bSJoachim Eastwood 	struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
124c392b65bSJoachim Eastwood 	u32 bit, offset = LPC18XX_RGU_ACTIVE_STATUS0;
125c392b65bSJoachim Eastwood 
126c392b65bSJoachim Eastwood 	offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
127c392b65bSJoachim Eastwood 	bit = 1 << (id % LPC18XX_RGU_RESETS_PER_REG);
128c392b65bSJoachim Eastwood 
129c392b65bSJoachim Eastwood 	return !(readl(rc->base + offset) & bit);
130c392b65bSJoachim Eastwood }
131c392b65bSJoachim Eastwood 
1321a55cad1SPhilipp Zabel static const struct reset_control_ops lpc18xx_rgu_ops = {
133c392b65bSJoachim Eastwood 	.reset		= lpc18xx_rgu_reset,
134c392b65bSJoachim Eastwood 	.assert		= lpc18xx_rgu_assert,
135c392b65bSJoachim Eastwood 	.deassert	= lpc18xx_rgu_deassert,
136c392b65bSJoachim Eastwood 	.status		= lpc18xx_rgu_status,
137c392b65bSJoachim Eastwood };
138c392b65bSJoachim Eastwood 
lpc18xx_rgu_probe(struct platform_device * pdev)139c392b65bSJoachim Eastwood static int lpc18xx_rgu_probe(struct platform_device *pdev)
140c392b65bSJoachim Eastwood {
141c392b65bSJoachim Eastwood 	struct lpc18xx_rgu_data *rc;
142c392b65bSJoachim Eastwood 	u32 fcclk, firc;
143c392b65bSJoachim Eastwood 	int ret;
144c392b65bSJoachim Eastwood 
145c392b65bSJoachim Eastwood 	rc = devm_kzalloc(&pdev->dev, sizeof(*rc), GFP_KERNEL);
146c392b65bSJoachim Eastwood 	if (!rc)
147c392b65bSJoachim Eastwood 		return -ENOMEM;
148c392b65bSJoachim Eastwood 
14968fda5a9SYe Xingchen 	rc->base = devm_platform_ioremap_resource(pdev, 0);
150c392b65bSJoachim Eastwood 	if (IS_ERR(rc->base))
151c392b65bSJoachim Eastwood 		return PTR_ERR(rc->base);
152c392b65bSJoachim Eastwood 
153*0fa8ce76SKrzysztof Kozlowski 	rc->clk_reg = devm_clk_get_enabled(&pdev->dev, "reg");
154ece222e9SKrzysztof Kozlowski 	if (IS_ERR(rc->clk_reg))
155ece222e9SKrzysztof Kozlowski 		return dev_err_probe(&pdev->dev, PTR_ERR(rc->clk_reg),
156ece222e9SKrzysztof Kozlowski 				     "reg clock not found\n");
157c392b65bSJoachim Eastwood 
158*0fa8ce76SKrzysztof Kozlowski 	rc->clk_delay = devm_clk_get_enabled(&pdev->dev, "delay");
159ece222e9SKrzysztof Kozlowski 	if (IS_ERR(rc->clk_delay))
160ece222e9SKrzysztof Kozlowski 		return dev_err_probe(&pdev->dev, PTR_ERR(rc->clk_delay),
161ece222e9SKrzysztof Kozlowski 				     "delay clock not found\n");
162c392b65bSJoachim Eastwood 
163c392b65bSJoachim Eastwood 	fcclk = clk_get_rate(rc->clk_reg) / USEC_PER_SEC;
164c392b65bSJoachim Eastwood 	firc = clk_get_rate(rc->clk_delay) / USEC_PER_SEC;
165c392b65bSJoachim Eastwood 	if (fcclk == 0 || firc == 0)
166c392b65bSJoachim Eastwood 		rc->delay_us = 2;
167c392b65bSJoachim Eastwood 	else
168c392b65bSJoachim Eastwood 		rc->delay_us = DIV_ROUND_UP(fcclk, firc * firc);
169c392b65bSJoachim Eastwood 
170c392b65bSJoachim Eastwood 	spin_lock_init(&rc->lock);
171c392b65bSJoachim Eastwood 
172c392b65bSJoachim Eastwood 	rc->rcdev.owner = THIS_MODULE;
173c392b65bSJoachim Eastwood 	rc->rcdev.nr_resets = 64;
174c392b65bSJoachim Eastwood 	rc->rcdev.ops = &lpc18xx_rgu_ops;
175c392b65bSJoachim Eastwood 	rc->rcdev.of_node = pdev->dev.of_node;
176c392b65bSJoachim Eastwood 
177c392b65bSJoachim Eastwood 	ret = reset_controller_register(&rc->rcdev);
178*0fa8ce76SKrzysztof Kozlowski 	if (ret)
179*0fa8ce76SKrzysztof Kozlowski 		return dev_err_probe(&pdev->dev, ret, "unable to register device\n");
180c392b65bSJoachim Eastwood 
181773fe726SJoachim Eastwood 	rc->restart_nb.priority = 192,
182773fe726SJoachim Eastwood 	rc->restart_nb.notifier_call = lpc18xx_rgu_restart,
183773fe726SJoachim Eastwood 	ret = register_restart_handler(&rc->restart_nb);
184c392b65bSJoachim Eastwood 	if (ret)
185c392b65bSJoachim Eastwood 		dev_warn(&pdev->dev, "failed to register restart handler\n");
186c392b65bSJoachim Eastwood 
187c392b65bSJoachim Eastwood 	return 0;
188c392b65bSJoachim Eastwood }
189c392b65bSJoachim Eastwood 
190c392b65bSJoachim Eastwood static const struct of_device_id lpc18xx_rgu_match[] = {
191c392b65bSJoachim Eastwood 	{ .compatible = "nxp,lpc1850-rgu" },
192c392b65bSJoachim Eastwood 	{ }
193c392b65bSJoachim Eastwood };
194c392b65bSJoachim Eastwood 
195c392b65bSJoachim Eastwood static struct platform_driver lpc18xx_rgu_driver = {
196c392b65bSJoachim Eastwood 	.probe	= lpc18xx_rgu_probe,
197c392b65bSJoachim Eastwood 	.driver	= {
198c392b65bSJoachim Eastwood 		.name			= "lpc18xx-reset",
199c392b65bSJoachim Eastwood 		.of_match_table		= lpc18xx_rgu_match,
200cdd24f76SPaul Gortmaker 		.suppress_bind_attrs	= true,
201c392b65bSJoachim Eastwood 	},
202c392b65bSJoachim Eastwood };
203cdd24f76SPaul Gortmaker builtin_platform_driver(lpc18xx_rgu_driver);
204