2021-12-20 14:43:53

by Conor Dooley

[permalink] [raw]
Subject: [PATCH v2 0/1] soc: add polarfire soc system controller

From: Conor Dooley <[email protected]>

Changes since v1:
- system controller is now an mfd
- parentage is now used to get the device node on the system controller
- mpfs_sys_controller_get() now updates the reference count
- "polarfire-soc" in compat string changed to "mpfs"

Depends on [0] to change the compat string in the dt-binding.

@Arnd Bergmann:
I sent the first version of this patch in November & you (along with
requesting referencing counting) wanted me to check if the driver was
bound to the specific device [1]. I have taken another look at this
driver now and I am still none the wiser as to how I should do this.

As I said in the previous thread, I checked other driver but was not
able to find any examples of of_find_device_by_node() where the binding
of the driver was checked. If you could point me towards an example
that would be great.

Thanks,
Conor.

For some extra context, the device tree entry for this driver will look
like:

syscontroller: syscontroller {
compatible = "microchip,mpfs-sys-controller", "simple-mfd";
mboxes = <&mbox 0>;

hwrandom: hwrandom {
compatible = "microchip,mpfs-rng";
};

sysserv: sysserv {
compatible = "microchip,mpfs-generic-service";
};
};

and the mpfs_sys_controller_get() function is called in, for example,
the mpfs-rng driver:

node_pointer = of_get_parent(dev->of_node);
if (!node_pointer) {
dev_err(&pdev->dev,
"Failed to find mpfs system controller node\n");
return -ENODEV;
}

rng_priv->sys_controller = mpfs_sys_controller_get(&pdev->dev, node_pointer);

[0] https://lore.kernel.org/linux-riscv/CAMuHMdWTjrAiHosU0cGyJYkK=9JzNgHb=tjHXPdYxTWmkVzeYQ@mail.gmail.com/T/
[1] https://lore.kernel.org/linux-riscv/CAK8P3a1m_LhOg5JGMqPz6sohJa2hPZ3GN-jQDPxigZ5DaqAGxQ@mail.gmail.com/

Conor Dooley (1):
soc: add polarfire soc system controller

drivers/soc/Kconfig | 1 +
drivers/soc/Makefile | 1 +
drivers/soc/microchip/Kconfig | 10 ++
drivers/soc/microchip/Makefile | 1 +
drivers/soc/microchip/mpfs-sys-controller.c | 169 ++++++++++++++++++++
include/soc/microchip/mpfs.h | 3 +-
6 files changed, 184 insertions(+), 1 deletion(-)
create mode 100644 drivers/soc/microchip/Kconfig
create mode 100644 drivers/soc/microchip/Makefile
create mode 100644 drivers/soc/microchip/mpfs-sys-controller.c

--
2.33.1



2021-12-20 14:46:45

by Conor Dooley

[permalink] [raw]
Subject: [PATCH v2 1/1] soc: add polarfire soc system controller

From: Conor Dooley <[email protected]>

This driver provides an interface for other drivers to access the
functions of the system controller on the Microchip PolarFire SoC.

Signed-off-by: Conor Dooley <[email protected]>
---
drivers/soc/Kconfig | 1 +
drivers/soc/Makefile | 1 +
drivers/soc/microchip/Kconfig | 10 ++
drivers/soc/microchip/Makefile | 1 +
drivers/soc/microchip/mpfs-sys-controller.c | 169 ++++++++++++++++++++
include/soc/microchip/mpfs.h | 3 +-
6 files changed, 184 insertions(+), 1 deletion(-)
create mode 100644 drivers/soc/microchip/Kconfig
create mode 100644 drivers/soc/microchip/Makefile
create mode 100644 drivers/soc/microchip/mpfs-sys-controller.c

diff --git a/drivers/soc/Kconfig b/drivers/soc/Kconfig
index e8a30c4c5aec..b33142e020e0 100644
--- a/drivers/soc/Kconfig
+++ b/drivers/soc/Kconfig
@@ -12,6 +12,7 @@ source "drivers/soc/imx/Kconfig"
source "drivers/soc/ixp4xx/Kconfig"
source "drivers/soc/litex/Kconfig"
source "drivers/soc/mediatek/Kconfig"
+source "drivers/soc/microchip/Kconfig"
source "drivers/soc/qcom/Kconfig"
source "drivers/soc/renesas/Kconfig"
source "drivers/soc/rockchip/Kconfig"
diff --git a/drivers/soc/Makefile b/drivers/soc/Makefile
index a05e9fbcd3e0..e3be151e391e 100644
--- a/drivers/soc/Makefile
+++ b/drivers/soc/Makefile
@@ -17,6 +17,7 @@ obj-y += ixp4xx/
obj-$(CONFIG_SOC_XWAY) += lantiq/
obj-$(CONFIG_LITEX_SOC_CONTROLLER) += litex/
obj-y += mediatek/
+obj-y += microchip/
obj-y += amlogic/
obj-y += qcom/
obj-y += renesas/
diff --git a/drivers/soc/microchip/Kconfig b/drivers/soc/microchip/Kconfig
new file mode 100644
index 000000000000..eb656b33156b
--- /dev/null
+++ b/drivers/soc/microchip/Kconfig
@@ -0,0 +1,10 @@
+config POLARFIRE_SOC_SYS_CTRL
+ tristate "POLARFIRE_SOC_SYS_CTRL"
+ depends on POLARFIRE_SOC_MAILBOX
+ help
+ This driver adds support for the PolarFire SoC (MPFS) system controller.
+
+ To compile this driver as a module, choose M here. the
+ module will be called mpfs_system_controller.
+
+ If unsure, say N.
diff --git a/drivers/soc/microchip/Makefile b/drivers/soc/microchip/Makefile
new file mode 100644
index 000000000000..14489919fe4b
--- /dev/null
+++ b/drivers/soc/microchip/Makefile
@@ -0,0 +1 @@
+obj-$(CONFIG_POLARFIRE_SOC_SYS_CTRL) += mpfs-sys-controller.o
diff --git a/drivers/soc/microchip/mpfs-sys-controller.c b/drivers/soc/microchip/mpfs-sys-controller.c
new file mode 100644
index 000000000000..af3b05d8b41e
--- /dev/null
+++ b/drivers/soc/microchip/mpfs-sys-controller.c
@@ -0,0 +1,169 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Microchip PolarFire SoC (MPFS) system controller driver
+ *
+ * Copyright (c) 2020-2021 Microchip Corporation. All rights reserved.
+ *
+ * Author: Conor Dooley <[email protected]>
+ *
+ */
+
+#include <linux/slab.h>
+#include <linux/kref.h>
+#include <linux/module.h>
+#include <linux/interrupt.h>
+#include <linux/of_platform.h>
+#include <linux/mailbox_client.h>
+#include <linux/platform_device.h>
+#include <soc/microchip/mpfs.h>
+
+static DEFINE_MUTEX(transaction_lock);
+
+struct mpfs_sys_controller {
+ struct mbox_client client;
+ struct mbox_chan *chan;
+ struct completion c;
+ struct kref consumers;
+};
+
+int mpfs_blocking_transaction(struct mpfs_sys_controller *sys_controller, void *msg)
+{
+ int ret, err;
+
+ err = mutex_lock_interruptible(&transaction_lock);
+ if (err)
+ return err;
+
+ reinit_completion(&sys_controller->c);
+
+ ret = mbox_send_message(sys_controller->chan, msg);
+ if (ret >= 0) {
+ if (wait_for_completion_timeout(&sys_controller->c, HZ)) {
+ ret = 0;
+ } else {
+ ret = -ETIMEDOUT;
+ dev_warn(sys_controller->client.dev,
+ "MPFS sys controller transaction timeout\n");
+ }
+ } else {
+ dev_err(sys_controller->client.dev,
+ "mpfs sys controller transaction returned %d\n", ret);
+ }
+
+ mutex_unlock(&transaction_lock);
+
+ return ret;
+}
+EXPORT_SYMBOL(mpfs_blocking_transaction);
+
+static void rx_callback(struct mbox_client *client, void *msg)
+{
+ struct mpfs_sys_controller *sys_controller =
+ container_of(client, struct mpfs_sys_controller, client);
+
+ complete(&sys_controller->c);
+}
+
+static void mpfs_sys_controller_delete(struct kref *kref)
+{
+ struct mpfs_sys_controller *sys_controller = container_of(kref, struct mpfs_sys_controller,
+ consumers);
+
+ mbox_free_channel(sys_controller->chan);
+ kfree(sys_controller);
+}
+
+void mpfs_sys_controller_put(void *data)
+{
+ struct mpfs_sys_controller *sys_controller = data;
+
+ kref_put(&sys_controller->consumers, mpfs_sys_controller_delete);
+}
+EXPORT_SYMBOL_GPL(mpfs_sys_controller_put);
+
+static int mpfs_sys_controller_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct mpfs_sys_controller *sys_controller;
+
+ sys_controller = kzalloc(sizeof(*sys_controller), GFP_KERNEL);
+ if (!sys_controller)
+ return -ENOMEM;
+
+ sys_controller->client.dev = dev;
+ sys_controller->client.rx_callback = rx_callback;
+ sys_controller->client.tx_block = 1U;
+
+ sys_controller->chan = mbox_request_channel(&sys_controller->client, 0);
+ if (IS_ERR(sys_controller->chan))
+ return dev_err_probe(dev, PTR_ERR(sys_controller->chan),
+ "Failed to get mbox channel\n");
+
+ init_completion(&sys_controller->c);
+ kref_init(&sys_controller->consumers);
+
+ platform_set_drvdata(pdev, sys_controller);
+
+ dev_info(&pdev->dev, "Registered MPFS system controller driver\n");
+
+ return 0;
+}
+
+static int mpfs_sys_controller_remove(struct platform_device *pdev)
+{
+ struct mpfs_sys_controller *sys_controller = platform_get_drvdata(pdev);
+
+ mpfs_sys_controller_put(sys_controller);
+
+ return 0;
+}
+
+struct mpfs_sys_controller *mpfs_sys_controller_get(struct device *dev,
+ struct device_node *sys_ctrl_node)
+{
+ struct mpfs_sys_controller *sys_controller;
+ struct platform_device *pdev;
+
+ pdev = of_find_device_by_node(sys_ctrl_node);
+ if (!pdev)
+ return NULL;
+
+ sys_controller = platform_get_drvdata(pdev);
+ if (!sys_controller)
+ goto err_put_device;
+
+ if (!kref_get_unless_zero(&sys_controller->consumers))
+ goto err_put_device;
+
+ put_device(&pdev->dev);
+
+ if (devm_add_action_or_reset(dev, mpfs_sys_controller_put, sys_controller))
+ return NULL;
+
+ return sys_controller;
+
+err_put_device:
+ put_device(&pdev->dev);
+ return NULL;
+}
+EXPORT_SYMBOL_GPL(mpfs_sys_controller_get);
+
+static const struct of_device_id mpfs_sys_controller_of_match[] = {
+ {.compatible = "microchip,mpfs-sys-controller", },
+ {},
+};
+MODULE_DEVICE_TABLE(of, mpfs_sys_controller_of_match);
+
+static struct platform_driver mpfs_sys_controller_driver = {
+ .driver = {
+ .name = "mpfs-sys-controller",
+ .of_match_table = mpfs_sys_controller_of_match,
+ },
+ .probe = mpfs_sys_controller_probe,
+ .remove = mpfs_sys_controller_remove,
+};
+module_platform_driver(mpfs_sys_controller_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Conor Dooley <[email protected]>");
+MODULE_DESCRIPTION("MPFS system controller driver");
diff --git a/include/soc/microchip/mpfs.h b/include/soc/microchip/mpfs.h
index 2b64c95f3be5..0a594dfa8094 100644
--- a/include/soc/microchip/mpfs.h
+++ b/include/soc/microchip/mpfs.h
@@ -36,7 +36,8 @@ struct mpfs_mss_response {

int mpfs_blocking_transaction(struct mpfs_sys_controller *mpfs_client, void *msg);

-struct mpfs_sys_controller *mpfs_sys_controller_get(struct device_node *mailbox_node);
+struct mpfs_sys_controller *mpfs_sys_controller_get(struct device *dev,
+ struct device_node *sys_ctrl_node);

#endif /* if IS_ENABLED(CONFIG_POLARFIRE_SOC_SYS_CTRL) */

--
2.33.1


2022-01-10 10:33:53

by Conor Dooley

[permalink] [raw]
Subject: Re: [PATCH v2 0/1] soc: add polarfire soc system controller

On 20/12/2021 14:44, [email protected] wrote:
> From: Conor Dooley <[email protected]>
>
> Changes since v1:
> - system controller is now an mfd
> - parentage is now used to get the device node on the system controller
> - mpfs_sys_controller_get() now updates the reference count
> - "polarfire-soc" in compat string changed to "mpfs"
>
> Depends on [0] to change the compat string in the dt-binding.
>
Hey Arnd, if you could take a look at this it'd be great
> @Arnd Bergmann:
> I sent the first version of this patch in November & you (along with
> requesting referencing counting) wanted me to check if the driver was
> bound to the specific device [1]. I have taken another look at this
> driver now and I am still none the wiser as to how I should do this.
>
> As I said in the previous thread, I checked other drivers but was not
> able to find any examples of of_find_device_by_node() where the binding
> of the driver was checked. If you could point me towards an example
> that would be great.
>
> Thanks,
> Conor.
>
> For some extra context, the device tree entry for this driver will look
> like:
>
> syscontroller: syscontroller {
> compatible = "microchip,mpfs-sys-controller", "simple-mfd";
> mboxes = <&mbox 0>;
>
> hwrandom: hwrandom {
> compatible = "microchip,mpfs-rng";
> };
>
> sysserv: sysserv {
> compatible = "microchip,mpfs-generic-service";
> };
> };
>
> and the mpfs_sys_controller_get() function is called in, for example,
> the mpfs-rng driver:
>
> node_pointer = of_get_parent(dev->of_node);
> if (!node_pointer) {
> dev_err(&pdev->dev,
> "Failed to find mpfs system controller node\n");
> return -ENODEV;
> }
>
> rng_priv->sys_controller = mpfs_sys_controller_get(&pdev->dev, node_pointer);
>
> [0] https://lore.kernel.org/linux-riscv/CAMuHMdWTjrAiHosU0cGyJYkK=9JzNgHb=tjHXPdYxTWmkVzeYQ@mail.gmail.com/T/
> [1] https://lore.kernel.org/linux-riscv/CAK8P3a1m_LhOg5JGMqPz6sohJa2hPZ3GN-jQDPxigZ5DaqAGxQ@mail.gmail.com/
>
> Conor Dooley (1):
> soc: add polarfire soc system controller
>
> drivers/soc/Kconfig | 1 +
> drivers/soc/Makefile | 1 +
> drivers/soc/microchip/Kconfig | 10 ++
> drivers/soc/microchip/Makefile | 1 +
> drivers/soc/microchip/mpfs-sys-controller.c | 169 ++++++++++++++++++++
> include/soc/microchip/mpfs.h | 3 +-
> 6 files changed, 184 insertions(+), 1 deletion(-)
> create mode 100644 drivers/soc/microchip/Kconfig
> create mode 100644 drivers/soc/microchip/Makefile
> create mode 100644 drivers/soc/microchip/mpfs-sys-controller.c
>