2 * Reset driver for NXP LPC18xx/43xx Reset Generation Unit (RGU).
4 * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
16 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/reboot.h>
20 #include <linux/reset-controller.h>
21 #include <linux/spinlock.h>
23 /* LPC18xx RGU registers */
24 #define LPC18XX_RGU_CTRL0 0x100
25 #define LPC18XX_RGU_CTRL1 0x104
26 #define LPC18XX_RGU_ACTIVE_STATUS0 0x150
27 #define LPC18XX_RGU_ACTIVE_STATUS1 0x154
29 #define LPC18XX_RGU_RESETS_PER_REG 32
31 /* Internal reset outputs */
32 #define LPC18XX_RGU_CORE_RST 0
33 #define LPC43XX_RGU_M0SUB_RST 12
34 #define LPC43XX_RGU_M0APP_RST 56
36 struct lpc18xx_rgu_data
{
37 struct reset_controller_dev rcdev
;
38 struct clk
*clk_delay
;
45 #define to_rgu_data(p) container_of(p, struct lpc18xx_rgu_data, rcdev)
47 static void __iomem
*rgu_base
;
49 static int lpc18xx_rgu_restart(struct notifier_block
*this, unsigned long mode
,
52 writel(BIT(LPC18XX_RGU_CORE_RST
), rgu_base
+ LPC18XX_RGU_CTRL0
);
55 pr_emerg("%s: unable to restart system\n", __func__
);
60 static struct notifier_block lpc18xx_rgu_restart_nb
= {
61 .notifier_call
= lpc18xx_rgu_restart
,
66 * The LPC18xx RGU has mostly self-deasserting resets except for the
67 * two reset lines going to the internal Cortex-M0 cores.
69 * To prevent the M0 core resets from accidentally getting deasserted
70 * status register must be check and bits in control register set to
73 static int lpc18xx_rgu_setclear_reset(struct reset_controller_dev
*rcdev
,
74 unsigned long id
, bool set
)
76 struct lpc18xx_rgu_data
*rc
= to_rgu_data(rcdev
);
77 u32 stat_offset
= LPC18XX_RGU_ACTIVE_STATUS0
;
78 u32 ctrl_offset
= LPC18XX_RGU_CTRL0
;
82 stat_offset
+= (id
/ LPC18XX_RGU_RESETS_PER_REG
) * sizeof(u32
);
83 ctrl_offset
+= (id
/ LPC18XX_RGU_RESETS_PER_REG
) * sizeof(u32
);
84 rst_bit
= 1 << (id
% LPC18XX_RGU_RESETS_PER_REG
);
86 spin_lock_irqsave(&rc
->lock
, flags
);
87 stat
= ~readl(rc
->base
+ stat_offset
);
89 writel(stat
| rst_bit
, rc
->base
+ ctrl_offset
);
91 writel(stat
& ~rst_bit
, rc
->base
+ ctrl_offset
);
92 spin_unlock_irqrestore(&rc
->lock
, flags
);
97 static int lpc18xx_rgu_assert(struct reset_controller_dev
*rcdev
,
100 return lpc18xx_rgu_setclear_reset(rcdev
, id
, true);
103 static int lpc18xx_rgu_deassert(struct reset_controller_dev
*rcdev
,
106 return lpc18xx_rgu_setclear_reset(rcdev
, id
, false);
109 /* Only M0 cores require explicit reset deassert */
110 static int lpc18xx_rgu_reset(struct reset_controller_dev
*rcdev
,
113 struct lpc18xx_rgu_data
*rc
= to_rgu_data(rcdev
);
115 lpc18xx_rgu_assert(rcdev
, id
);
116 udelay(rc
->delay_us
);
119 case LPC43XX_RGU_M0SUB_RST
:
120 case LPC43XX_RGU_M0APP_RST
:
121 lpc18xx_rgu_setclear_reset(rcdev
, id
, false);
127 static int lpc18xx_rgu_status(struct reset_controller_dev
*rcdev
,
130 struct lpc18xx_rgu_data
*rc
= to_rgu_data(rcdev
);
131 u32 bit
, offset
= LPC18XX_RGU_ACTIVE_STATUS0
;
133 offset
+= (id
/ LPC18XX_RGU_RESETS_PER_REG
) * sizeof(u32
);
134 bit
= 1 << (id
% LPC18XX_RGU_RESETS_PER_REG
);
136 return !(readl(rc
->base
+ offset
) & bit
);
139 static struct reset_control_ops lpc18xx_rgu_ops
= {
140 .reset
= lpc18xx_rgu_reset
,
141 .assert = lpc18xx_rgu_assert
,
142 .deassert
= lpc18xx_rgu_deassert
,
143 .status
= lpc18xx_rgu_status
,
146 static int lpc18xx_rgu_probe(struct platform_device
*pdev
)
148 struct lpc18xx_rgu_data
*rc
;
149 struct resource
*res
;
153 rc
= devm_kzalloc(&pdev
->dev
, sizeof(*rc
), GFP_KERNEL
);
157 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
158 rc
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
159 if (IS_ERR(rc
->base
))
160 return PTR_ERR(rc
->base
);
162 rc
->clk_reg
= devm_clk_get(&pdev
->dev
, "reg");
163 if (IS_ERR(rc
->clk_reg
)) {
164 dev_err(&pdev
->dev
, "reg clock not found\n");
165 return PTR_ERR(rc
->clk_reg
);
168 rc
->clk_delay
= devm_clk_get(&pdev
->dev
, "delay");
169 if (IS_ERR(rc
->clk_delay
)) {
170 dev_err(&pdev
->dev
, "delay clock not found\n");
171 return PTR_ERR(rc
->clk_delay
);
174 ret
= clk_prepare_enable(rc
->clk_reg
);
176 dev_err(&pdev
->dev
, "unable to enable reg clock\n");
180 ret
= clk_prepare_enable(rc
->clk_delay
);
182 dev_err(&pdev
->dev
, "unable to enable delay clock\n");
186 fcclk
= clk_get_rate(rc
->clk_reg
) / USEC_PER_SEC
;
187 firc
= clk_get_rate(rc
->clk_delay
) / USEC_PER_SEC
;
188 if (fcclk
== 0 || firc
== 0)
191 rc
->delay_us
= DIV_ROUND_UP(fcclk
, firc
* firc
);
193 spin_lock_init(&rc
->lock
);
195 rc
->rcdev
.owner
= THIS_MODULE
;
196 rc
->rcdev
.nr_resets
= 64;
197 rc
->rcdev
.ops
= &lpc18xx_rgu_ops
;
198 rc
->rcdev
.of_node
= pdev
->dev
.of_node
;
200 platform_set_drvdata(pdev
, rc
);
202 ret
= reset_controller_register(&rc
->rcdev
);
204 dev_err(&pdev
->dev
, "unable to register device\n");
209 ret
= register_restart_handler(&lpc18xx_rgu_restart_nb
);
211 dev_warn(&pdev
->dev
, "failed to register restart handler\n");
216 clk_disable_unprepare(rc
->clk_delay
);
218 clk_disable_unprepare(rc
->clk_reg
);
223 static int lpc18xx_rgu_remove(struct platform_device
*pdev
)
225 struct lpc18xx_rgu_data
*rc
= platform_get_drvdata(pdev
);
228 ret
= unregister_restart_handler(&lpc18xx_rgu_restart_nb
);
230 dev_warn(&pdev
->dev
, "failed to unregister restart handler\n");
232 reset_controller_unregister(&rc
->rcdev
);
234 clk_disable_unprepare(rc
->clk_delay
);
235 clk_disable_unprepare(rc
->clk_reg
);
240 static const struct of_device_id lpc18xx_rgu_match
[] = {
241 { .compatible
= "nxp,lpc1850-rgu" },
244 MODULE_DEVICE_TABLE(of
, lpc18xx_rgu_match
);
246 static struct platform_driver lpc18xx_rgu_driver
= {
247 .probe
= lpc18xx_rgu_probe
,
248 .remove
= lpc18xx_rgu_remove
,
250 .name
= "lpc18xx-reset",
251 .of_match_table
= lpc18xx_rgu_match
,
254 module_platform_driver(lpc18xx_rgu_driver
);
256 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
257 MODULE_DESCRIPTION("Reset driver for LPC18xx/43xx RGU");
258 MODULE_LICENSE("GPL v2");