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