diff mbox series

[v6,2/7] reset: mchp: sparx5: Use the second reg item when cpu-syscon is not present

Message ID 20240930121601.172216-3-herve.codina@bootlin.com (mailing list archive)
State New, archived
Headers show
Series Add support for the LAN966x PCI device using a DT overlay | expand

Commit Message

Herve Codina Sept. 30, 2024, 12:15 p.m. UTC
In the LAN966x PCI device use case, syscon cannot be used as syscon
devices do not support removal [1]. A syscon device is a core "system"
device and not a device available in some addon boards and so, it is not
supposed to be removed.

In order to remove the syscon usage, use a local mapping of a reg
address range when cpu-syscon is not present.

Link: https://lore.kernel.org/all/20240923100741.11277439@bootlin.com/ [1]
Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
 drivers/reset/reset-microchip-sparx5.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

Comments

Geert Uytterhoeven Sept. 30, 2024, 1:23 p.m. UTC | #1
Hi Hervé,

On Mon, Sep 30, 2024 at 2:16 PM Herve Codina <herve.codina@bootlin.com> wrote:
> In the LAN966x PCI device use case, syscon cannot be used as syscon
> devices do not support removal [1]. A syscon device is a core "system"
> device and not a device available in some addon boards and so, it is not
> supposed to be removed.
>
> In order to remove the syscon usage, use a local mapping of a reg
> address range when cpu-syscon is not present.
>
> Link: https://lore.kernel.org/all/20240923100741.11277439@bootlin.com/ [1]
> Signed-off-by: Herve Codina <herve.codina@bootlin.com>

Thanks for your patch!

> --- a/drivers/reset/reset-microchip-sparx5.c
> +++ b/drivers/reset/reset-microchip-sparx5.c
> @@ -114,8 +114,22 @@ static int mchp_sparx5_reset_probe(struct platform_device *pdev)
>                 return -ENOMEM;
>
>         err = mchp_sparx5_map_syscon(pdev, "cpu-syscon", &ctx->cpu_ctrl);
> -       if (err)
> +       switch (err) {
> +       case 0:
> +               break;
> +       case -ENODEV:
> +               /*
> +                * The cpu-syscon device is not available.
> +                * Fall back with IO mapping (i.e. mapping from reg property).
> +                */
> +               err = mchp_sparx5_map_io(pdev, 1, &ctx->cpu_ctrl);
> +               if (err)
> +                       return err;
> +               break;
> +       default:
>                 return err;
> +       }
> +

This can be shortened to:

    if (err == -ENODEV) {
            /*
             * The cpu-syscon device is not available.
             * Fall back with IO mapping (i.e. mapping from reg property).
             */
            err = mchp_sparx5_map_io(pdev, 1, &ctx->cpu_ctrl);
    }
    if (err)
            return err;

>         err = mchp_sparx5_map_io(pdev, 0, &ctx->gcb_ctrl);
>         if (err)
>                 return err;

Gr{oetje,eeting}s,

                        Geert
Arnd Bergmann Sept. 30, 2024, 1:57 p.m. UTC | #2
On Mon, Sep 30, 2024, at 12:15, Herve Codina wrote:
> In the LAN966x PCI device use case, syscon cannot be used as syscon
> devices do not support removal [1]. A syscon device is a core "system"
> device and not a device available in some addon boards and so, it is not
> supposed to be removed.
>
> In order to remove the syscon usage, use a local mapping of a reg
> address range when cpu-syscon is not present.
>
> Link: https://lore.kernel.org/all/20240923100741.11277439@bootlin.com/ [1]
> Signed-off-by: Herve Codina <herve.codina@bootlin.com>
> ---

>>  	err = mchp_sparx5_map_syscon(pdev, "cpu-syscon", &ctx->cpu_ctrl);
> -	if (err)
> +	switch (err) {
> +	case 0:
> +		break;
> +	case -ENODEV:

I was expecting a patch that would read the phandle and map the
syscon node to keep the behavior unchanged, but I guess this one
works as well.

The downside of your approach is that it requires an different
DT binding, which only works as long as there are no other
users of the syscon registers.

     Arnd
Herve Codina Sept. 30, 2024, 2:26 p.m. UTC | #3
On Mon, 30 Sep 2024 13:57:01 +0000
"Arnd Bergmann" <arnd@arndb.de> wrote:

> On Mon, Sep 30, 2024, at 12:15, Herve Codina wrote:
> > In the LAN966x PCI device use case, syscon cannot be used as syscon
> > devices do not support removal [1]. A syscon device is a core "system"
> > device and not a device available in some addon boards and so, it is not
> > supposed to be removed.
> >
> > In order to remove the syscon usage, use a local mapping of a reg
> > address range when cpu-syscon is not present.
> >
> > Link: https://lore.kernel.org/all/20240923100741.11277439@bootlin.com/ [1]
> > Signed-off-by: Herve Codina <herve.codina@bootlin.com>
> > ---  
> 
> >>  	err = mchp_sparx5_map_syscon(pdev, "cpu-syscon", &ctx->cpu_ctrl);  
> > -	if (err)
> > +	switch (err) {
> > +	case 0:
> > +		break;
> > +	case -ENODEV:  
> 
> I was expecting a patch that would read the phandle and map the
> syscon node to keep the behavior unchanged, but I guess this one
> works as well.
> 
> The downside of your approach is that it requires an different
> DT binding, which only works as long as there are no other
> users of the syscon registers.

Yes, I knwow but keeping the binding with the syscon device (i.e. compatible
= "...", "syscon";) leads to confusion.
Indeed, the syscon API cannot be used because using this API leads issues
when the syscon device is removed.
That means the you have a "syscon" node (compatible = "syscon") but we cannot
use the syscon API (include/linux/mfd/syscon.h) with this node.

Also, in order to share resources between several consumers of the "syscon"
registers, we need exactly what is done in syscon. I mean we need to map
resources only once, provide this resource throught a regmap an share this
regmap between the consumers. Indeed a lock needs to be shared in order to
protect against registers RMW accesses done by several consumers.
In other word, we need to copy/paste syscon code with support for removal
implemented (feature needed in the LAN966x PCI device use case).

So, I found really simpler and less confusing to fully discard the syscon node
and handle registers directly in the only one consumer.

With all of these, do you thing my approach can be acceptable ?

Best regards,
Hervé
Herve Codina Oct. 1, 2024, 4:30 p.m. UTC | #4
Hi Arnd,

On Mon, 30 Sep 2024 16:26:16 +0200
Herve Codina <herve.codina@bootlin.com> wrote:

> On Mon, 30 Sep 2024 13:57:01 +0000
> "Arnd Bergmann" <arnd@arndb.de> wrote:
> 
> > On Mon, Sep 30, 2024, at 12:15, Herve Codina wrote:  
> > > In the LAN966x PCI device use case, syscon cannot be used as syscon
> > > devices do not support removal [1]. A syscon device is a core "system"
> > > device and not a device available in some addon boards and so, it is not
> > > supposed to be removed.
> > >
> > > In order to remove the syscon usage, use a local mapping of a reg
> > > address range when cpu-syscon is not present.
> > >
> > > Link: https://lore.kernel.org/all/20240923100741.11277439@bootlin.com/ [1]
> > > Signed-off-by: Herve Codina <herve.codina@bootlin.com>
> > > ---    
> >   
> > >>  	err = mchp_sparx5_map_syscon(pdev, "cpu-syscon", &ctx->cpu_ctrl);    
> > > -	if (err)
> > > +	switch (err) {
> > > +	case 0:
> > > +		break;
> > > +	case -ENODEV:    
> > 
> > I was expecting a patch that would read the phandle and map the
> > syscon node to keep the behavior unchanged, but I guess this one
> > works as well.
> > 
> > The downside of your approach is that it requires an different
> > DT binding, which only works as long as there are no other
> > users of the syscon registers.  
> 
> Yes, I knwow but keeping the binding with the syscon device (i.e. compatible
> = "...", "syscon";) leads to confusion.
> Indeed, the syscon API cannot be used because using this API leads issues
> when the syscon device is removed.
> That means the you have a "syscon" node (compatible = "syscon") but we cannot
> use the syscon API (include/linux/mfd/syscon.h) with this node.
> 
> Also, in order to share resources between several consumers of the "syscon"
> registers, we need exactly what is done in syscon. I mean we need to map
> resources only once, provide this resource throught a regmap an share this
> regmap between the consumers. Indeed a lock needs to be shared in order to
> protect against registers RMW accesses done by several consumers.
> In other word, we need to copy/paste syscon code with support for removal
> implemented (feature needed in the LAN966x PCI device use case).
> 
> So, I found really simpler and less confusing to fully discard the syscon node
> and handle registers directly in the only one consumer.
> 
> With all of these, do you thing my approach can be acceptable ?
> 

Well, the related binding has been rejected.

In the next iteration, I will keep the syscon node and implement what you
suggested (i.e. read the phandle and map the syscon node).

This will look like this:
--- 8< ---
static const struct regmap_config mchp_lan966x_syscon_regmap_config = {
       .reg_bits = 32,
       .val_bits = 32,
       .reg_stride = 4,
};

static struct regmap *mchp_lan966x_syscon_to_regmap(struct device *dev,
       	                                           struct device_node *syscon_np)
{
       struct regmap_config regmap_config = mchp_lan966x_syscon_regmap_config;
       resource_size_t size;
       void __iomem *base;

       base = devm_of_iomap(dev, syscon_np, 0, &size);
       if (IS_ERR(base))
               return ERR_CAST(base);

       regmap_config.max_register = size - 4;

       return devm_regmap_init_mmio(dev, base, &regmap_config);
}
--- 8< ---

In mchp_sparx5_map_syscon(), I will call the syscon API or the local
function based on the device compatible string:
	--- 8< ---
	if (of_device_is_compatible(pdev->dev.of_node, "microchip,lan966x-switch-reset"))
		regmap = mchp_lan966x_syscon_to_regmap(&pdev->dev, syscon_np);
	else
		regmap = syscon_node_to_regmap(syscon_np);
	--- 8< ---

Is this kind of solution you were expecting?
If you have thought about something different, can you give me some pointers?

Best regards,
Hervé
Arnd Bergmann Oct. 2, 2024, 9:29 a.m. UTC | #5
On Tue, Oct 1, 2024, at 16:30, Herve Codina wrote:
> On Mon, 30 Sep 2024 16:26:16 +0200
> Herve Codina <herve.codina@bootlin.com> wrote:
> --- 8< ---
>
> In mchp_sparx5_map_syscon(), I will call the syscon API or the local
> function based on the device compatible string:
> 	--- 8< ---
> 	if (of_device_is_compatible(pdev->dev.of_node, 
> "microchip,lan966x-switch-reset"))
> 		regmap = mchp_lan966x_syscon_to_regmap(&pdev->dev, syscon_np);
> 	else
> 		regmap = syscon_node_to_regmap(syscon_np);
> 	--- 8< ---
>
> Is this kind of solution you were expecting?
> If you have thought about something different, can you give me some pointers?

Hi Hervé,

The way I had imagined this was to not need an if() check
at all but unconditionally map the syscon registers in the
reset driver.

The most important part here is to have sensible bindings
that don't need to describe the difference between PCI
and SoC mode. This seems fine for the lan966x case, but
I'm not sure why you need to handle sparx5 differently here.
Do you expect the syscon to be shared with other drivers
on sparx5 but not lan966x?

I don't thinkt this bit matters too much and what you suggest
works fine, I just want to be sure I understand what you are
doing.

      Arnd
Herve Codina Oct. 2, 2024, 10:19 a.m. UTC | #6
Hi Arnd,

On Wed, 02 Oct 2024 09:29:35 +0000
"Arnd Bergmann" <arnd@arndb.de> wrote:

> On Tue, Oct 1, 2024, at 16:30, Herve Codina wrote:
> > On Mon, 30 Sep 2024 16:26:16 +0200
> > Herve Codina <herve.codina@bootlin.com> wrote:
> > --- 8< ---
> >
> > In mchp_sparx5_map_syscon(), I will call the syscon API or the local
> > function based on the device compatible string:
> > 	--- 8< ---
> > 	if (of_device_is_compatible(pdev->dev.of_node, 
> > "microchip,lan966x-switch-reset"))
> > 		regmap = mchp_lan966x_syscon_to_regmap(&pdev->dev, syscon_np);
> > 	else
> > 		regmap = syscon_node_to_regmap(syscon_np);
> > 	--- 8< ---
> >
> > Is this kind of solution you were expecting?
> > If you have thought about something different, can you give me some pointers?  
> 
> Hi Hervé,
> 
> The way I had imagined this was to not need an if() check
> at all but unconditionally map the syscon registers in the
> reset driver.
> 
> The most important part here is to have sensible bindings
> that don't need to describe the difference between PCI
> and SoC mode. This seems fine for the lan966x case, but
> I'm not sure why you need to handle sparx5 differently here.
> Do you expect the syscon to be shared with other drivers
> on sparx5 but not lan966x?

Thanks for this reply.

Exactly, on sparx5 syscon is shared...
$ git grep 'microchip,sparx5-cpu-syscon'
...
arch/arm64/boot/dts/microchip/sparx5.dtsi:                      compatible = "microchip,sparx5-cpu-syscon", "syscon",
drivers/mmc/host/sdhci-of-sparx5.c:     const char *syscon = "microchip,sparx5-cpu-syscon";
drivers/power/reset/ocelot-reset.c:     .syscon          = "microchip,sparx5-cpu-syscon",
drivers/spi/spi-dw-mmio.c:      const char *syscon_name = "microchip,sparx5-cpu-syscon";
$

> 
> I don't thinkt this bit matters too much and what you suggest
> works fine, I just want to be sure I understand what you are
> doing.
> 
>       Arnd

Best regards,
Hervé
Arnd Bergmann Oct. 2, 2024, 10:58 a.m. UTC | #7
On Wed, Oct 2, 2024, at 10:19, Herve Codina wrote:
> On Wed, 02 Oct 2024 09:29:35 +0000

> Thanks for this reply.
>
> Exactly, on sparx5 syscon is shared...
> $ git grep 'microchip,sparx5-cpu-syscon'
> ...
> arch/arm64/boot/dts/microchip/sparx5.dtsi:                      
> compatible = "microchip,sparx5-cpu-syscon", "syscon",
> drivers/mmc/host/sdhci-of-sparx5.c:     const char *syscon = 
> "microchip,sparx5-cpu-syscon";
> drivers/power/reset/ocelot-reset.c:     .syscon          = 
> "microchip,sparx5-cpu-syscon",
> drivers/spi/spi-dw-mmio.c:      const char *syscon_name = 
> "microchip,sparx5-cpu-syscon";
> $

Ok, got it. In that case, your suggestion looks fine.

       Arnd
diff mbox series

Patch

diff --git a/drivers/reset/reset-microchip-sparx5.c b/drivers/reset/reset-microchip-sparx5.c
index 636e85c388b0..1c095fa41d69 100644
--- a/drivers/reset/reset-microchip-sparx5.c
+++ b/drivers/reset/reset-microchip-sparx5.c
@@ -114,8 +114,22 @@  static int mchp_sparx5_reset_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	err = mchp_sparx5_map_syscon(pdev, "cpu-syscon", &ctx->cpu_ctrl);
-	if (err)
+	switch (err) {
+	case 0:
+		break;
+	case -ENODEV:
+		/*
+		 * The cpu-syscon device is not available.
+		 * Fall back with IO mapping (i.e. mapping from reg property).
+		 */
+		err = mchp_sparx5_map_io(pdev, 1, &ctx->cpu_ctrl);
+		if (err)
+			return err;
+		break;
+	default:
 		return err;
+	}
+
 	err = mchp_sparx5_map_io(pdev, 0, &ctx->gcb_ctrl);
 	if (err)
 		return err;