diff mbox series

[v2,9/9] clk: imx: Add the pcc reset controller support on imx8ulp

Message ID 20210810062820.1062884-10-ping.bai@nxp.com
State Superseded
Headers show
Series Add imx8ulp clock & reset driver support | expand

Commit Message

Jacky Bai Aug. 10, 2021, 6:28 a.m. UTC
On i.MX8ULP, for some of the PCCs, it has a peripheral SW RST bit
resides in the same registers as the clock controller. So add this
SW RST controller support alongs with the pcc clock initialization.

the reset and clock shared the same register, to avoid  accessing
the same register by reset control and clock control concurrently,
locking is necessary, so reuse the imx_ccm_lock spinlock to simplify
the code.

Suggested-by: Liu Ying <victor.liu@nxp.com>
Signed-off-by: Jacky Bai <ping.bai@nxp.com>
---
  v2 changes:
    - add 'Suggested-by' as suggested by Victor Liu
---
 drivers/clk/imx/Kconfig              |   1 +
 drivers/clk/imx/clk-composite-7ulp.c |  10 +++
 drivers/clk/imx/clk-imx8ulp.c        | 115 ++++++++++++++++++++++++++-
 3 files changed, 123 insertions(+), 3 deletions(-)

Comments

Philipp Zabel Aug. 23, 2021, 11:23 a.m. UTC | #1
Hi Jacky,

On Tue, 2021-08-10 at 14:28 +0800, Jacky Bai wrote:
> On i.MX8ULP, for some of the PCCs, it has a peripheral SW RST bit

> resides in the same registers as the clock controller. So add this

> SW RST controller support alongs with the pcc clock initialization.

> 

> the reset and clock shared the same register, to avoid  accessing

> the same register by reset control and clock control concurrently,

> locking is necessary, so reuse the imx_ccm_lock spinlock to simplify

> the code.

> 

> Suggested-by: Liu Ying <victor.liu@nxp.com>

> Signed-off-by: Jacky Bai <ping.bai@nxp.com>

> ---

>   v2 changes:

>     - add 'Suggested-by' as suggested by Victor Liu

> ---

>  drivers/clk/imx/Kconfig              |   1 +

>  drivers/clk/imx/clk-composite-7ulp.c |  10 +++

>  drivers/clk/imx/clk-imx8ulp.c        | 115 ++++++++++++++++++++++++++-

>  3 files changed, 123 insertions(+), 3 deletions(-)

> 

> diff --git a/drivers/clk/imx/Kconfig b/drivers/clk/imx/Kconfig

> index b81d6437ed95..0d1e3a6ac32a 100644

> --- a/drivers/clk/imx/Kconfig

> +++ b/drivers/clk/imx/Kconfig

> @@ -102,5 +102,6 @@ config CLK_IMX8QXP

>  config CLK_IMX8ULP

>  	tristate "IMX8ULP CCM Clock Driver"

>  	depends on ARCH_MXC || COMPILE_TEST

> +	select RESET_CONTROLLER


This shouldn't be required anymore, devm_reset_controller_register() has
a stub since commit 48a74b1147f7 ("reset: Add compile-test stubs").

[...]
> diff --git a/drivers/clk/imx/clk-imx8ulp.c b/drivers/clk/imx/clk-imx8ulp.c

> index 6aad04114658..ea596cd6855a 100644

> --- a/drivers/clk/imx/clk-imx8ulp.c

> +++ b/drivers/clk/imx/clk-imx8ulp.c

> @@ -9,6 +9,7 @@

>  #include <linux/module.h>

>  #include <linux/of_device.h>

>  #include <linux/platform_device.h>

> +#include <linux/reset-controller.h>

>  #include <linux/slab.h>

>  

>  #include "clk.h"

> @@ -48,6 +49,98 @@ static const char * const nic_per_divplat[] = { "nic_per_divplat" };

>  static const char * const lpav_axi_div[] = { "lpav_axi_div" };

>  static const char * const lpav_bus_div[] = { "lpav_bus_div" };

>  

> +struct pcc_reset_dev {

> +	void __iomem *base;

> +	struct reset_controller_dev rcdev;

> +	const u32 *resets;

> +	spinlock_t *lock;


I'd add a comment to this lock, stating that it is set to imx_ccm_lock
and protects access to registers shared with clock control.

With these addressed,

Reviewed-by: Philipp Zabel <p.zabel@pengutronix.de>


regards
Philipp
Jacky Bai Aug. 23, 2021, 11:58 p.m. UTC | #2
> Subject: Re: [PATCH v2 9/9] clk: imx: Add the pcc reset controller support on

> imx8ulp

> 

> Hi Jacky,

> 

> On Tue, 2021-08-10 at 14:28 +0800, Jacky Bai wrote:

> > On i.MX8ULP, for some of the PCCs, it has a peripheral SW RST bit

> > resides in the same registers as the clock controller. So add this SW

> > RST controller support alongs with the pcc clock initialization.

> >

> > the reset and clock shared the same register, to avoid  accessing the

> > same register by reset control and clock control concurrently, locking

> > is necessary, so reuse the imx_ccm_lock spinlock to simplify the code.

> >

> > Suggested-by: Liu Ying <victor.liu@nxp.com>

> > Signed-off-by: Jacky Bai <ping.bai@nxp.com>

> > ---

> >   v2 changes:

> >     - add 'Suggested-by' as suggested by Victor Liu

> > ---

> >  drivers/clk/imx/Kconfig              |   1 +

> >  drivers/clk/imx/clk-composite-7ulp.c |  10 +++

> >  drivers/clk/imx/clk-imx8ulp.c        | 115

> ++++++++++++++++++++++++++-

> >  3 files changed, 123 insertions(+), 3 deletions(-)

> >

> > diff --git a/drivers/clk/imx/Kconfig b/drivers/clk/imx/Kconfig index

> > b81d6437ed95..0d1e3a6ac32a 100644

> > --- a/drivers/clk/imx/Kconfig

> > +++ b/drivers/clk/imx/Kconfig

> > @@ -102,5 +102,6 @@ config CLK_IMX8QXP  config CLK_IMX8ULP

> >  	tristate "IMX8ULP CCM Clock Driver"

> >  	depends on ARCH_MXC || COMPILE_TEST

> > +	select RESET_CONTROLLER

> 

> This shouldn't be required anymore, devm_reset_controller_register() has a

> stub since commit 48a74b1147f7 ("reset: Add compile-test stubs").

> 


So we don't need to select 'RESET_CONTROLLER' explicitly, right?

> [...]

> > diff --git a/drivers/clk/imx/clk-imx8ulp.c

> > b/drivers/clk/imx/clk-imx8ulp.c index 6aad04114658..ea596cd6855a

> > 100644

> > --- a/drivers/clk/imx/clk-imx8ulp.c

> > +++ b/drivers/clk/imx/clk-imx8ulp.c

> > @@ -9,6 +9,7 @@

> >  #include <linux/module.h>

> >  #include <linux/of_device.h>

> >  #include <linux/platform_device.h>

> > +#include <linux/reset-controller.h>

> >  #include <linux/slab.h>

> >

> >  #include "clk.h"

> > @@ -48,6 +49,98 @@ static const char * const nic_per_divplat[] = {

> > "nic_per_divplat" };  static const char * const lpav_axi_div[] = {

> > "lpav_axi_div" };  static const char * const lpav_bus_div[] = {

> > "lpav_bus_div" };

> >

> > +struct pcc_reset_dev {

> > +	void __iomem *base;

> > +	struct reset_controller_dev rcdev;

> > +	const u32 *resets;

> > +	spinlock_t *lock;

> 

> I'd add a comment to this lock, stating that it is set to imx_ccm_lock and

> protects access to registers shared with clock control.

> 


Ok, will add some comments, thx.

BR
Jacky Bai

> With these addressed,

> 

> Reviewed-by: Philipp Zabel <p.zabel@pengutronix.de>

> 

> regards

> Philipp
Philipp Zabel Aug. 25, 2021, 10:54 a.m. UTC | #3
On Mon, 2021-08-23 at 23:58 +0000, Jacky Bai wrote:
[...]
> > > diff --git a/drivers/clk/imx/Kconfig b/drivers/clk/imx/Kconfig index

> > > b81d6437ed95..0d1e3a6ac32a 100644

> > > --- a/drivers/clk/imx/Kconfig

> > > +++ b/drivers/clk/imx/Kconfig

> > > @@ -102,5 +102,6 @@ config CLK_IMX8QXP  config CLK_IMX8ULP

> > >  	tristate "IMX8ULP CCM Clock Driver"

> > >  	depends on ARCH_MXC || COMPILE_TEST

> > > +	select RESET_CONTROLLER

> > 

> > This shouldn't be required anymore, devm_reset_controller_register() has a

> > stub since commit 48a74b1147f7 ("reset: Add compile-test stubs").

> 

> So we don't need to select 'RESET_CONTROLLER' explicitly, right?


Right.

regards
Philipp
diff mbox series

Patch

diff --git a/drivers/clk/imx/Kconfig b/drivers/clk/imx/Kconfig
index b81d6437ed95..0d1e3a6ac32a 100644
--- a/drivers/clk/imx/Kconfig
+++ b/drivers/clk/imx/Kconfig
@@ -102,5 +102,6 @@  config CLK_IMX8QXP
 config CLK_IMX8ULP
 	tristate "IMX8ULP CCM Clock Driver"
 	depends on ARCH_MXC || COMPILE_TEST
+	select RESET_CONTROLLER
 	help
 	    Build the driver for i.MX8ULP CCM Clock Driver
diff --git a/drivers/clk/imx/clk-composite-7ulp.c b/drivers/clk/imx/clk-composite-7ulp.c
index 99a36a73dcb5..6cc1b8bb8780 100644
--- a/drivers/clk/imx/clk-composite-7ulp.c
+++ b/drivers/clk/imx/clk-composite-7ulp.c
@@ -28,6 +28,7 @@ 
 static int pcc_gate_enable(struct clk_hw *hw)
 {
 	struct clk_gate *gate = to_clk_gate(hw);
+	unsigned long flags;
 	u32 val;
 	int ret;
 
@@ -35,6 +36,7 @@  static int pcc_gate_enable(struct clk_hw *hw)
 	if (ret)
 		return ret;
 
+	spin_lock_irqsave(gate->lock, flags);
 	/*
 	 * release the sw reset for peripherals associated with
 	 * with this pcc clock.
@@ -43,6 +45,8 @@  static int pcc_gate_enable(struct clk_hw *hw)
 	val |= SW_RST;
 	writel(val, gate->reg);
 
+	spin_unlock_irqrestore(gate->lock, flags);
+
 	return 0;
 }
 
@@ -83,6 +87,8 @@  static struct clk_hw *imx_ulp_clk_hw_composite(const char *name,
 		mux->reg = reg;
 		mux->shift = PCG_PCS_SHIFT;
 		mux->mask = PCG_PCS_MASK;
+		if (has_swrst)
+			mux->lock = &imx_ccm_lock;
 	}
 
 	if (rate_present) {
@@ -100,6 +106,8 @@  static struct clk_hw *imx_ulp_clk_hw_composite(const char *name,
 		fd->nwidth = PCG_PCD_WIDTH;
 		fd->nmask = PCG_PCD_MASK;
 		fd->flags = CLK_FRAC_DIVIDER_ZERO_BASED;
+		if (has_swrst)
+			fd->lock = &imx_ccm_lock;
 	}
 
 	if (gate_present) {
@@ -112,6 +120,8 @@  static struct clk_hw *imx_ulp_clk_hw_composite(const char *name,
 		gate_hw = &gate->hw;
 		gate->reg = reg;
 		gate->bit_idx = PCG_CGC_SHIFT;
+		if (has_swrst)
+			gate->lock = &imx_ccm_lock;
 		/*
 		 * make sure clock is gated during clock tree initialization,
 		 * the HW ONLY allow clock parent/rate changed with clock gated,
diff --git a/drivers/clk/imx/clk-imx8ulp.c b/drivers/clk/imx/clk-imx8ulp.c
index 6aad04114658..ea596cd6855a 100644
--- a/drivers/clk/imx/clk-imx8ulp.c
+++ b/drivers/clk/imx/clk-imx8ulp.c
@@ -9,6 +9,7 @@ 
 #include <linux/module.h>
 #include <linux/of_device.h>
 #include <linux/platform_device.h>
+#include <linux/reset-controller.h>
 #include <linux/slab.h>
 
 #include "clk.h"
@@ -48,6 +49,98 @@  static const char * const nic_per_divplat[] = { "nic_per_divplat" };
 static const char * const lpav_axi_div[] = { "lpav_axi_div" };
 static const char * const lpav_bus_div[] = { "lpav_bus_div" };
 
+struct pcc_reset_dev {
+	void __iomem *base;
+	struct reset_controller_dev rcdev;
+	const u32 *resets;
+	spinlock_t *lock;
+};
+
+#define PCC_SW_RST	BIT(28)
+#define to_pcc_reset_dev(_rcdev)	container_of(_rcdev, struct pcc_reset_dev, rcdev)
+
+static const u32 pcc3_resets[] = {
+	0xa8, 0xac, 0xc8, 0xcc, 0xd0,
+	0xd4, 0xd8, 0xdc, 0xe0, 0xe4,
+	0xe8, 0xec, 0xf0
+};
+
+static const u32 pcc4_resets[] = {
+	0x4, 0x8, 0xc, 0x10, 0x14,
+	0x18, 0x1c, 0x20, 0x24, 0x34,
+	0x38, 0x3c, 0x40, 0x44, 0x48,
+	0x4c, 0x54
+};
+
+static const u32 pcc5_resets[] = {
+	0xa0, 0xa4, 0xa8, 0xac, 0xb0,
+	0xb4, 0xbc, 0xc0, 0xc8, 0xcc,
+	0xd0, 0xf0, 0xf4, 0xf8
+};
+
+static int imx8ulp_pcc_assert(struct reset_controller_dev *rcdev, unsigned long id)
+{
+	struct pcc_reset_dev *pcc_reset = to_pcc_reset_dev(rcdev);
+	u32 offset = pcc_reset->resets[id];
+	unsigned long flags;
+	u32 val;
+
+	spin_lock_irqsave(pcc_reset->lock, flags);
+
+	val = readl(pcc_reset->base + offset);
+	val &= ~PCC_SW_RST;
+	writel(val, pcc_reset->base + offset);
+
+	spin_unlock_irqrestore(pcc_reset->lock, flags);
+
+	return 0;
+}
+
+static int imx8ulp_pcc_deassert(struct reset_controller_dev *rcdev, unsigned long id)
+{
+	struct pcc_reset_dev *pcc_reset = to_pcc_reset_dev(rcdev);
+	u32 offset = pcc_reset->resets[id];
+	unsigned long flags;
+	u32 val;
+
+	spin_lock_irqsave(pcc_reset->lock, flags);
+
+	val = readl(pcc_reset->base + offset);
+	val |= PCC_SW_RST;
+	writel(val, pcc_reset->base + offset);
+
+	spin_unlock_irqrestore(pcc_reset->lock, flags);
+
+	return 0;
+}
+
+static const struct reset_control_ops imx8ulp_pcc_reset_ops = {
+	.assert = imx8ulp_pcc_assert,
+	.deassert = imx8ulp_pcc_deassert,
+};
+
+static int imx8ulp_pcc_reset_init(struct platform_device *pdev, void __iomem *base,
+	 const u32 *resets, unsigned int nr_resets)
+{
+	struct device_node *np = pdev->dev.of_node;
+	struct device *dev = &pdev->dev;
+	struct pcc_reset_dev *pcc_reset;
+
+	pcc_reset = devm_kzalloc(dev, sizeof(*pcc_reset), GFP_KERNEL);
+	if (!pcc_reset)
+		return -ENOMEM;
+
+	pcc_reset->base = base;
+	pcc_reset->lock = &imx_ccm_lock;
+	pcc_reset->resets = resets;
+	pcc_reset->rcdev.owner = THIS_MODULE;
+	pcc_reset->rcdev.nr_resets = nr_resets;
+	pcc_reset->rcdev.ops = &imx8ulp_pcc_reset_ops;
+	pcc_reset->rcdev.of_node = np;
+
+	return devm_reset_controller_register(dev, &pcc_reset->rcdev);
+}
+
 static int imx8ulp_clk_cgc1_init(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -288,10 +381,13 @@  static int imx8ulp_clk_pcc3_init(struct platform_device *pdev)
 	imx_check_clk_hws(clks, clk_data->num);
 
 	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
+	if (ret)
+		return ret;
 
 	imx_register_uart_clocks(1);
 
-	return ret;
+	/* register the pcc3 reset controller */
+	return imx8ulp_pcc_reset_init(pdev, base, pcc3_resets, ARRAY_SIZE(pcc3_resets));
 }
 
 static int imx8ulp_clk_pcc4_init(struct platform_device *pdev)
@@ -300,6 +396,7 @@  static int imx8ulp_clk_pcc4_init(struct platform_device *pdev)
 	struct clk_hw_onecell_data *clk_data;
 	struct clk_hw **clks;
 	void __iomem *base;
+	int ret;
 
 	clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, IMX8ULP_CLK_PCC4_END),
 			   GFP_KERNEL);
@@ -339,7 +436,13 @@  static int imx8ulp_clk_pcc4_init(struct platform_device *pdev)
 
 	imx_check_clk_hws(clks, clk_data->num);
 
-	return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
+	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
+	if (ret)
+		return ret;
+
+	/* register the pcc4 reset controller */
+	return imx8ulp_pcc_reset_init(pdev, base, pcc4_resets, ARRAY_SIZE(pcc4_resets));
+
 }
 
 static int imx8ulp_clk_pcc5_init(struct platform_device *pdev)
@@ -348,6 +451,7 @@  static int imx8ulp_clk_pcc5_init(struct platform_device *pdev)
 	struct clk_hw_onecell_data *clk_data;
 	struct clk_hw **clks;
 	void __iomem *base;
+	int ret;
 
 	clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, IMX8ULP_CLK_PCC5_END),
 			   GFP_KERNEL);
@@ -420,7 +524,12 @@  static int imx8ulp_clk_pcc5_init(struct platform_device *pdev)
 
 	imx_check_clk_hws(clks, clk_data->num);
 
-	return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
+	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
+	if (ret)
+		return ret;
+
+	/* register the pcc5 reset controller */
+	return imx8ulp_pcc_reset_init(pdev, base, pcc5_resets, ARRAY_SIZE(pcc5_resets));
 }
 
 static int imx8ulp_clk_probe(struct platform_device *pdev)