2020-05-16 10:15:07

by Baolin Wang

[permalink] [raw]
Subject: [PATCH v4 0/2] Add new physical regmap bus support

The Spreadtrum platform uses a special set/clear method to update
registers' bits, thus this patch set exports a weak function to
allow to register a physical regmap bus to support this feature
instead of using the MMIO bus, which is not a physical regmap bus.

Any comments are welcome. Thanks.

Changes from v3:
- Remove vendor specific support from the syscon driver, and export
a weak function to support physical regmap bus.

Changes from v2:
- Fix building errors without enabling CONFIG_ARCH_SPRD.

Changes from v1:
- Add WARN_ONCE() for seting bits and clearing bits at the same time.
- Remove the Spreadtrum SoC syscon driver, instead moving the regmap_bus
instance into syscon.c driver.

Changes from RFC v2:
- Drop regmap change, which was applied by Mark.
- Add more information about how to use set/clear.
- Add checking to ensure the platform is compatible with
using a new physical regmap bus.

Changes from RFC v1:
- Add new helper to registers a physical regmap bus instead of
using the MMIO bus.

Baolin Wang (2):
mfd: syscon: Support physical regmap bus
soc: sprd: Add Spreadtrum special bits updating support

drivers/mfd/syscon.c | 9 +++-
drivers/soc/Kconfig | 1 +
drivers/soc/Makefile | 1 +
drivers/soc/sprd/Kconfig | 16 +++++++
drivers/soc/sprd/Makefile | 2 +
drivers/soc/sprd/sprd_syscon.c | 86 ++++++++++++++++++++++++++++++++++
include/linux/mfd/syscon.h | 11 +++++
7 files changed, 125 insertions(+), 1 deletion(-)
create mode 100644 drivers/soc/sprd/Kconfig
create mode 100644 drivers/soc/sprd/Makefile
create mode 100644 drivers/soc/sprd/sprd_syscon.c

--
2.17.1


2020-05-16 10:15:14

by Baolin Wang

[permalink] [raw]
Subject: [PATCH v4 1/2] mfd: syscon: Support physical regmap bus

Some platforms such as Spreadtrum platform, define a special method to
update bits of the registers instead of reading and writing, which means
we should use a physical regmap bus to define the reg_update_bits()
operation instead of the MMIO regmap bus.

Thus add a a __weak function for the syscon driver to allow to register
a physical regmap bus to support this new requirement.

Signed-off-by: Baolin Wang <[email protected]>
---
drivers/mfd/syscon.c | 9 ++++++++-
include/linux/mfd/syscon.h | 11 +++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/drivers/mfd/syscon.c b/drivers/mfd/syscon.c
index 3a97816d0cba..dc92f3177ceb 100644
--- a/drivers/mfd/syscon.c
+++ b/drivers/mfd/syscon.c
@@ -40,6 +40,13 @@ static const struct regmap_config syscon_regmap_config = {
.reg_stride = 4,
};

+struct regmap * __weak syscon_regmap_init(struct device_node *np,
+ void __iomem *base,
+ struct regmap_config *syscon_config)
+{
+ return regmap_init_mmio(NULL, base, syscon_config);
+}
+
static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)
{
struct clk *clk;
@@ -106,7 +113,7 @@ static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)
syscon_config.val_bits = reg_io_width * 8;
syscon_config.max_register = resource_size(&res) - reg_io_width;

- regmap = regmap_init_mmio(NULL, base, &syscon_config);
+ regmap = syscon_regmap_init(np, base, &syscon_config);
if (IS_ERR(regmap)) {
pr_err("regmap init failed\n");
ret = PTR_ERR(regmap);
diff --git a/include/linux/mfd/syscon.h b/include/linux/mfd/syscon.h
index 7f20e9b502a5..85088e44fe7c 100644
--- a/include/linux/mfd/syscon.h
+++ b/include/linux/mfd/syscon.h
@@ -13,6 +13,7 @@

#include <linux/err.h>
#include <linux/errno.h>
+#include <linux/regmap.h>

struct device_node;

@@ -28,6 +29,9 @@ extern struct regmap *syscon_regmap_lookup_by_phandle_args(
const char *property,
int arg_count,
unsigned int *out_args);
+extern struct regmap *syscon_regmap_init(struct device_node *np,
+ void __iomem *base,
+ struct regmap_config *syscon_config);
#else
static inline struct regmap *device_node_to_regmap(struct device_node *np)
{
@@ -59,6 +63,13 @@ static inline struct regmap *syscon_regmap_lookup_by_phandle_args(
{
return ERR_PTR(-ENOTSUPP);
}
+
+static inline struct regmap *syscon_regmap_init(struct device_node *np,
+ void __iomem *base,
+ struct regmap_config *syscon_config)
+{
+ return ERR_PTR(-ENOTSUPP);
+}
#endif

#endif /* __LINUX_MFD_SYSCON_H__ */
--
2.17.1

2020-05-16 10:15:52

by Baolin Wang

[permalink] [raw]
Subject: [PATCH v4 2/2] soc: sprd: Add Spreadtrum special bits updating support

The spreadtrum platform uses a special set/clear method to update
registers' bits, which can remove the race of updating the global
registers between the multiple subsystems. Thus we can register
a physical regmap bus into syscon core to support this.

Signed-off-by: Baolin Wang <[email protected]>
---
drivers/soc/Kconfig | 1 +
drivers/soc/Makefile | 1 +
drivers/soc/sprd/Kconfig | 16 +++++++
drivers/soc/sprd/Makefile | 2 +
drivers/soc/sprd/sprd_syscon.c | 86 ++++++++++++++++++++++++++++++++++
5 files changed, 106 insertions(+)
create mode 100644 drivers/soc/sprd/Kconfig
create mode 100644 drivers/soc/sprd/Makefile
create mode 100644 drivers/soc/sprd/sprd_syscon.c

diff --git a/drivers/soc/Kconfig b/drivers/soc/Kconfig
index 425ab6f7e375..8cfbf2dc518d 100644
--- a/drivers/soc/Kconfig
+++ b/drivers/soc/Kconfig
@@ -23,5 +23,6 @@ source "drivers/soc/versatile/Kconfig"
source "drivers/soc/xilinx/Kconfig"
source "drivers/soc/zte/Kconfig"
source "drivers/soc/kendryte/Kconfig"
+source "drivers/soc/sprd/Kconfig"

endmenu
diff --git a/drivers/soc/Makefile b/drivers/soc/Makefile
index 36452bed86ef..7d156a6dd536 100644
--- a/drivers/soc/Makefile
+++ b/drivers/soc/Makefile
@@ -29,3 +29,4 @@ obj-$(CONFIG_PLAT_VERSATILE) += versatile/
obj-y += xilinx/
obj-$(CONFIG_ARCH_ZX) += zte/
obj-$(CONFIG_SOC_KENDRYTE) += kendryte/
+obj-$(CONFIG_ARCH_SPRD) += sprd/
diff --git a/drivers/soc/sprd/Kconfig b/drivers/soc/sprd/Kconfig
new file mode 100644
index 000000000000..38d1f5971a28
--- /dev/null
+++ b/drivers/soc/sprd/Kconfig
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# SPRD SoC drivers
+#
+
+menu "Spreadtrum SoC drivers"
+ depends on ARCH_SPRD || COMPILE_TEST
+
+config SPRD_SYSCON
+ tristate "Spreadtrum syscon support"
+ depends on ARCH_SPRD || COMPILE_TEST
+ help
+ Say yes here to add support for the Spreadtrum syscon driver,
+ which is used to implement the atomic method of bits updating.
+
+endmenu
diff --git a/drivers/soc/sprd/Makefile b/drivers/soc/sprd/Makefile
new file mode 100644
index 000000000000..4d7715553cf6
--- /dev/null
+++ b/drivers/soc/sprd/Makefile
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_SPRD_SYSCON) += sprd_syscon.o
diff --git a/drivers/soc/sprd/sprd_syscon.c b/drivers/soc/sprd/sprd_syscon.c
new file mode 100644
index 000000000000..cdc326ef94e5
--- /dev/null
+++ b/drivers/soc/sprd/sprd_syscon.c
@@ -0,0 +1,86 @@
+//SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 Spreadtrum Communications Inc.
+ */
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mfd/syscon.h>
+#include <linux/of.h>
+#include <linux/regmap.h>
+
+#define SPRD_REG_SET_OFFSET 0x1000
+#define SPRD_REG_CLR_OFFSET 0x2000
+
+/*
+ * The Spreadtrum platform defines a special set/clear method to update
+ * registers' bits, which means it can write values to the register's SET
+ * address (offset is 0x1000) to set bits, and write values to the register's
+ * CLEAR address (offset is 0x2000) to clear bits.
+ *
+ * This set/clear method can help to remove the race of accessing the global
+ * registers between the multiple subsystems instead of using hardware
+ * spinlocks.
+ *
+ * Note: there is a potential risk when users want to set and clear bits
+ * at the same time, since the set/clear method will always do bits setting
+ * before bits clearing, which may cause some unexpected results if the
+ * operation sequence is strict. Thus we recommend that do not set and
+ * clear bits at the same time if you are not sure about the results.
+ */
+static int sprd_syscon_update_bits(void *context, unsigned int reg,
+ unsigned int mask, unsigned int val)
+{
+ void __iomem *base = context;
+ unsigned int set, clr;
+
+ set = val & mask;
+ clr = ~set & mask;
+
+ if (set)
+ writel(set, base + reg + SPRD_REG_SET_OFFSET);
+
+ if (clr)
+ writel(clr, base + reg + SPRD_REG_CLR_OFFSET);
+
+ WARN_ONCE(set && clr, "%s: non-atomic update", __func__);
+ return 0;
+}
+
+static int sprd_syscon_read(void *context, unsigned int reg, unsigned int *val)
+{
+ void __iomem *base = context;
+
+ *val = readl(base + reg);
+ return 0;
+}
+
+static int sprd_syscon_write(void *context, unsigned int reg, unsigned int val)
+{
+ void __iomem *base = context;
+
+ writel(val, base + reg);
+ return 0;
+}
+
+static struct regmap_bus sprd_syscon_regmap = {
+ .fast_io = true,
+ .reg_write = sprd_syscon_write,
+ .reg_read = sprd_syscon_read,
+ .reg_update_bits = sprd_syscon_update_bits,
+ .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
+};
+
+struct regmap *syscon_regmap_init(struct device_node *np, void __iomem *base,
+ struct regmap_config *syscon_config)
+{
+ if (of_device_is_compatible(np, "sprd,atomic-syscon"))
+ return regmap_init(NULL, &sprd_syscon_regmap, base,
+ syscon_config);
+
+ return regmap_init_mmio(NULL, base, syscon_config);
+}
+
+MODULE_DESCRIPTION("Spreadtrum syscon support");
+MODULE_AUTHOR("Baolin Wang <[email protected]>");
+MODULE_LICENSE("GPL v2");
--
2.17.1

2020-05-17 00:13:25

by Orson Zhai

[permalink] [raw]
Subject: Re: [PATCH v4 1/2] mfd: syscon: Support physical regmap bus

On Sat, May 16, 2020 at 6:13 PM Baolin Wang <[email protected]> wrote:
>
> Some platforms such as Spreadtrum platform, define a special method to
> update bits of the registers instead of reading and writing, which means
> we should use a physical regmap bus to define the reg_update_bits()
> operation instead of the MMIO regmap bus.
>
> Thus add a a __weak function for the syscon driver to allow to register

Typo -- duplicated "a".

It seems to be a better idea than before.

-Orson

> a physical regmap bus to support this new requirement.
>
> Signed-off-by: Baolin Wang <[email protected]>
> ---
> drivers/mfd/syscon.c | 9 ++++++++-
> include/linux/mfd/syscon.h | 11 +++++++++++
> 2 files changed, 19 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mfd/syscon.c b/drivers/mfd/syscon.c
> index 3a97816d0cba..dc92f3177ceb 100644
> --- a/drivers/mfd/syscon.c
> +++ b/drivers/mfd/syscon.c
> @@ -40,6 +40,13 @@ static const struct regmap_config syscon_regmap_config = {
> .reg_stride = 4,
> };
>
> +struct regmap * __weak syscon_regmap_init(struct device_node *np,
> + void __iomem *base,
> + struct regmap_config *syscon_config)
> +{
> + return regmap_init_mmio(NULL, base, syscon_config);
> +}
> +
> static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)
> {
> struct clk *clk;
> @@ -106,7 +113,7 @@ static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)
> syscon_config.val_bits = reg_io_width * 8;
> syscon_config.max_register = resource_size(&res) - reg_io_width;
>
> - regmap = regmap_init_mmio(NULL, base, &syscon_config);
> + regmap = syscon_regmap_init(np, base, &syscon_config);
> if (IS_ERR(regmap)) {
> pr_err("regmap init failed\n");
> ret = PTR_ERR(regmap);
> diff --git a/include/linux/mfd/syscon.h b/include/linux/mfd/syscon.h
> index 7f20e9b502a5..85088e44fe7c 100644
> --- a/include/linux/mfd/syscon.h
> +++ b/include/linux/mfd/syscon.h
> @@ -13,6 +13,7 @@
>
> #include <linux/err.h>
> #include <linux/errno.h>
> +#include <linux/regmap.h>
>
> struct device_node;
>
> @@ -28,6 +29,9 @@ extern struct regmap *syscon_regmap_lookup_by_phandle_args(
> const char *property,
> int arg_count,
> unsigned int *out_args);
> +extern struct regmap *syscon_regmap_init(struct device_node *np,
> + void __iomem *base,
> + struct regmap_config *syscon_config);
> #else
> static inline struct regmap *device_node_to_regmap(struct device_node *np)
> {
> @@ -59,6 +63,13 @@ static inline struct regmap *syscon_regmap_lookup_by_phandle_args(
> {
> return ERR_PTR(-ENOTSUPP);
> }
> +
> +static inline struct regmap *syscon_regmap_init(struct device_node *np,
> + void __iomem *base,
> + struct regmap_config *syscon_config)
> +{
> + return ERR_PTR(-ENOTSUPP);
> +}
> #endif
>
> #endif /* __LINUX_MFD_SYSCON_H__ */
> --
> 2.17.1
>

2020-05-17 08:58:58

by Baolin Wang

[permalink] [raw]
Subject: Re: [PATCH v4 1/2] mfd: syscon: Support physical regmap bus

On Sun, May 17, 2020 at 8:08 AM Orson Zhai <[email protected]> wrote:
>
> On Sat, May 16, 2020 at 6:13 PM Baolin Wang <[email protected]> wrote:
> >
> > Some platforms such as Spreadtrum platform, define a special method to
> > update bits of the registers instead of reading and writing, which means
> > we should use a physical regmap bus to define the reg_update_bits()
> > operation instead of the MMIO regmap bus.
> >
> > Thus add a a __weak function for the syscon driver to allow to register
>
> Typo -- duplicated "a".

Ah, right, will fix.

>
> It seems to be a better idea than before.

Let's see Arnd and Lee's opinion.

>
> -Orson
>
> > a physical regmap bus to support this new requirement.
> >
> > Signed-off-by: Baolin Wang <[email protected]>
> > ---
> > drivers/mfd/syscon.c | 9 ++++++++-
> > include/linux/mfd/syscon.h | 11 +++++++++++
> > 2 files changed, 19 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/mfd/syscon.c b/drivers/mfd/syscon.c
> > index 3a97816d0cba..dc92f3177ceb 100644
> > --- a/drivers/mfd/syscon.c
> > +++ b/drivers/mfd/syscon.c
> > @@ -40,6 +40,13 @@ static const struct regmap_config syscon_regmap_config = {
> > .reg_stride = 4,
> > };
> >
> > +struct regmap * __weak syscon_regmap_init(struct device_node *np,
> > + void __iomem *base,
> > + struct regmap_config *syscon_config)
> > +{
> > + return regmap_init_mmio(NULL, base, syscon_config);
> > +}
> > +
> > static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)
> > {
> > struct clk *clk;
> > @@ -106,7 +113,7 @@ static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)
> > syscon_config.val_bits = reg_io_width * 8;
> > syscon_config.max_register = resource_size(&res) - reg_io_width;
> >
> > - regmap = regmap_init_mmio(NULL, base, &syscon_config);
> > + regmap = syscon_regmap_init(np, base, &syscon_config);
> > if (IS_ERR(regmap)) {
> > pr_err("regmap init failed\n");
> > ret = PTR_ERR(regmap);
> > diff --git a/include/linux/mfd/syscon.h b/include/linux/mfd/syscon.h
> > index 7f20e9b502a5..85088e44fe7c 100644
> > --- a/include/linux/mfd/syscon.h
> > +++ b/include/linux/mfd/syscon.h
> > @@ -13,6 +13,7 @@
> >
> > #include <linux/err.h>
> > #include <linux/errno.h>
> > +#include <linux/regmap.h>
> >
> > struct device_node;
> >
> > @@ -28,6 +29,9 @@ extern struct regmap *syscon_regmap_lookup_by_phandle_args(
> > const char *property,
> > int arg_count,
> > unsigned int *out_args);
> > +extern struct regmap *syscon_regmap_init(struct device_node *np,
> > + void __iomem *base,
> > + struct regmap_config *syscon_config);
> > #else
> > static inline struct regmap *device_node_to_regmap(struct device_node *np)
> > {
> > @@ -59,6 +63,13 @@ static inline struct regmap *syscon_regmap_lookup_by_phandle_args(
> > {
> > return ERR_PTR(-ENOTSUPP);
> > }
> > +
> > +static inline struct regmap *syscon_regmap_init(struct device_node *np,
> > + void __iomem *base,
> > + struct regmap_config *syscon_config)
> > +{
> > + return ERR_PTR(-ENOTSUPP);
> > +}
> > #endif
> >
> > #endif /* __LINUX_MFD_SYSCON_H__ */
> > --
> > 2.17.1
> >



--
Baolin Wang

2020-05-19 13:23:13

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [PATCH v4 1/2] mfd: syscon: Support physical regmap bus

On Sat, May 16, 2020 at 12:13 PM Baolin Wang <[email protected]> wrote:
>
> Some platforms such as Spreadtrum platform, define a special method to
> update bits of the registers instead of reading and writing, which means
> we should use a physical regmap bus to define the reg_update_bits()
> operation instead of the MMIO regmap bus.
>
> Thus add a a __weak function for the syscon driver to allow to register
> a physical regmap bus to support this new requirement.
>
> };
>
> +struct regmap * __weak syscon_regmap_init(struct device_node *np,
> + void __iomem *base,
> + struct regmap_config *syscon_config)
> +{
> + return regmap_init_mmio(NULL, base, syscon_config);
> +}
> +

Sorry, I don't think the __weak function is going to help here. I'm not
sure whether it actually does what you want when both syscon and
sprd_syscon are loadable modules (I would guess not), but it clearly
won't work when syscon is built-in and sprd_syscon is a module, and
even if the module loader knows how to resolve __weak symbols,
I would not want to rely on module load ordering to make it behave
the right way.

Arnd

2020-05-21 01:12:43

by Orson Zhai

[permalink] [raw]
Subject: Re: [PATCH v4 1/2] mfd: syscon: Support physical regmap bus

On Tue, May 19, 2020 at 9:19 PM Arnd Bergmann <[email protected]> wrote:
>
> On Sat, May 16, 2020 at 12:13 PM Baolin Wang <[email protected]> wrote:
> >
> > Some platforms such as Spreadtrum platform, define a special method to
> > update bits of the registers instead of reading and writing, which means
> > we should use a physical regmap bus to define the reg_update_bits()
> > operation instead of the MMIO regmap bus.
> >
> > Thus add a a __weak function for the syscon driver to allow to register
> > a physical regmap bus to support this new requirement.
> >
> > };
> >
> > +struct regmap * __weak syscon_regmap_init(struct device_node *np,
> > + void __iomem *base,
> > + struct regmap_config *syscon_config)
> > +{
> > + return regmap_init_mmio(NULL, base, syscon_config);
> > +}
> > +
>
> Sorry, I don't think the __weak function is going to help here. I'm not
> sure whether it actually does what you want when both syscon and
> sprd_syscon are loadable modules (I would guess not), but it clearly
> won't work when syscon is built-in and sprd_syscon is a module, and
> even if the module loader knows how to resolve __weak symbols,


I happened to see module.c last week. It seems there's some code to handle
weak symbols by checking about STB_WEAK. But it is not in this case.
When syscon.c is built-in and sprd_syscon is module, this would not
work because the
__weak one has been built into kernel already. Maybe live patch
feature may work it out.
But it seems over expensive to do things like this.

> I would not want to rely on module load ordering to make it behave
> the right way.

Agree. All other sprd modules would must be loaded behind sprd_syscon
and this will not be
handled by modprobe.

Thanks to pointing out this.

Best,
Orson

>
> Arnd