1 // SPDX-License-Identifier: GPL-2.0-only
3 * Reset driver for NXP LPC18xx/43xx Reset Generation Unit (RGU).
5 * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
9 #include <linux/delay.h>
10 #include <linux/err.h>
12 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/reboot.h>
16 #include <linux/reset-controller.h>
17 #include <linux/spinlock.h>
19 /* LPC18xx RGU registers */
20 #define LPC18XX_RGU_CTRL0 0x100
21 #define LPC18XX_RGU_CTRL1 0x104
22 #define LPC18XX_RGU_ACTIVE_STATUS0 0x150
23 #define LPC18XX_RGU_ACTIVE_STATUS1 0x154
25 #define LPC18XX_RGU_RESETS_PER_REG 32
27 /* Internal reset outputs */
28 #define LPC18XX_RGU_CORE_RST 0
29 #define LPC43XX_RGU_M0SUB_RST 12
30 #define LPC43XX_RGU_M0APP_RST 56
32 struct lpc18xx_rgu_data {
33 struct reset_controller_dev rcdev;
34 struct notifier_block restart_nb;
35 struct clk *clk_delay;
42 #define to_rgu_data(p) container_of(p, struct lpc18xx_rgu_data, rcdev)
44 static int lpc18xx_rgu_restart(struct notifier_block *nb, unsigned long mode,
47 struct lpc18xx_rgu_data *rc = container_of(nb, struct lpc18xx_rgu_data,
50 writel(BIT(LPC18XX_RGU_CORE_RST), rc->base + LPC18XX_RGU_CTRL0);
53 pr_emerg("%s: unable to restart system\n", __func__);
59 * The LPC18xx RGU has mostly self-deasserting resets except for the
60 * two reset lines going to the internal Cortex-M0 cores.
62 * To prevent the M0 core resets from accidentally getting deasserted
63 * status register must be check and bits in control register set to
66 static int lpc18xx_rgu_setclear_reset(struct reset_controller_dev *rcdev,
67 unsigned long id, bool set)
69 struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
70 u32 stat_offset = LPC18XX_RGU_ACTIVE_STATUS0;
71 u32 ctrl_offset = LPC18XX_RGU_CTRL0;
75 stat_offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
76 ctrl_offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
77 rst_bit = 1 << (id % LPC18XX_RGU_RESETS_PER_REG);
79 spin_lock_irqsave(&rc->lock, flags);
80 stat = ~readl(rc->base + stat_offset);
82 writel(stat | rst_bit, rc->base + ctrl_offset);
84 writel(stat & ~rst_bit, rc->base + ctrl_offset);
85 spin_unlock_irqrestore(&rc->lock, flags);
90 static int lpc18xx_rgu_assert(struct reset_controller_dev *rcdev,
93 return lpc18xx_rgu_setclear_reset(rcdev, id, true);
96 static int lpc18xx_rgu_deassert(struct reset_controller_dev *rcdev,
99 return lpc18xx_rgu_setclear_reset(rcdev, id, false);
102 /* Only M0 cores require explicit reset deassert */
103 static int lpc18xx_rgu_reset(struct reset_controller_dev *rcdev,
106 struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
108 lpc18xx_rgu_assert(rcdev, id);
109 udelay(rc->delay_us);
112 case LPC43XX_RGU_M0SUB_RST:
113 case LPC43XX_RGU_M0APP_RST:
114 lpc18xx_rgu_setclear_reset(rcdev, id, false);
120 static int lpc18xx_rgu_status(struct reset_controller_dev *rcdev,
123 struct lpc18xx_rgu_data *rc = to_rgu_data(rcdev);
124 u32 bit, offset = LPC18XX_RGU_ACTIVE_STATUS0;
126 offset += (id / LPC18XX_RGU_RESETS_PER_REG) * sizeof(u32);
127 bit = 1 << (id % LPC18XX_RGU_RESETS_PER_REG);
129 return !(readl(rc->base + offset) & bit);
132 static const struct reset_control_ops lpc18xx_rgu_ops = {
133 .reset = lpc18xx_rgu_reset,
134 .assert = lpc18xx_rgu_assert,
135 .deassert = lpc18xx_rgu_deassert,
136 .status = lpc18xx_rgu_status,
139 static int lpc18xx_rgu_probe(struct platform_device *pdev)
141 struct lpc18xx_rgu_data *rc;
142 struct resource *res;
146 rc = devm_kzalloc(&pdev->dev, sizeof(*rc), GFP_KERNEL);
150 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
151 rc->base = devm_ioremap_resource(&pdev->dev, res);
152 if (IS_ERR(rc->base))
153 return PTR_ERR(rc->base);
155 rc->clk_reg = devm_clk_get(&pdev->dev, "reg");
156 if (IS_ERR(rc->clk_reg)) {
157 dev_err(&pdev->dev, "reg clock not found\n");
158 return PTR_ERR(rc->clk_reg);
161 rc->clk_delay = devm_clk_get(&pdev->dev, "delay");
162 if (IS_ERR(rc->clk_delay)) {
163 dev_err(&pdev->dev, "delay clock not found\n");
164 return PTR_ERR(rc->clk_delay);
167 ret = clk_prepare_enable(rc->clk_reg);
169 dev_err(&pdev->dev, "unable to enable reg clock\n");
173 ret = clk_prepare_enable(rc->clk_delay);
175 dev_err(&pdev->dev, "unable to enable delay clock\n");
179 fcclk = clk_get_rate(rc->clk_reg) / USEC_PER_SEC;
180 firc = clk_get_rate(rc->clk_delay) / USEC_PER_SEC;
181 if (fcclk == 0 || firc == 0)
184 rc->delay_us = DIV_ROUND_UP(fcclk, firc * firc);
186 spin_lock_init(&rc->lock);
188 rc->rcdev.owner = THIS_MODULE;
189 rc->rcdev.nr_resets = 64;
190 rc->rcdev.ops = &lpc18xx_rgu_ops;
191 rc->rcdev.of_node = pdev->dev.of_node;
193 platform_set_drvdata(pdev, rc);
195 ret = reset_controller_register(&rc->rcdev);
197 dev_err(&pdev->dev, "unable to register device\n");
201 rc->restart_nb.priority = 192,
202 rc->restart_nb.notifier_call = lpc18xx_rgu_restart,
203 ret = register_restart_handler(&rc->restart_nb);
205 dev_warn(&pdev->dev, "failed to register restart handler\n");
210 clk_disable_unprepare(rc->clk_delay);
212 clk_disable_unprepare(rc->clk_reg);
217 static const struct of_device_id lpc18xx_rgu_match[] = {
218 { .compatible = "nxp,lpc1850-rgu" },
222 static struct platform_driver lpc18xx_rgu_driver = {
223 .probe = lpc18xx_rgu_probe,
225 .name = "lpc18xx-reset",
226 .of_match_table = lpc18xx_rgu_match,
227 .suppress_bind_attrs = true,
230 builtin_platform_driver(lpc18xx_rgu_driver);