linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anson Huang <anson.huang@nxp.com>
To: "catalin.marinas@arm.com" <catalin.marinas@arm.com>,
	"will.deacon@arm.com" <will.deacon@arm.com>,
	"shawnguo@kernel.org" <shawnguo@kernel.org>,
	"s.hauer@pengutronix.de" <s.hauer@pengutronix.de>,
	"kernel@pengutronix.de" <kernel@pengutronix.de>,
	"festevam@gmail.com" <festevam@gmail.com>,
	"heiko@sntech.de" <heiko@sntech.de>,
	"horms+renesas@verge.net.au" <horms+renesas@verge.net.au>,
	"olof@lixom.net" <olof@lixom.net>,
	Andy Gross <andy.gross@linaro.org>,
	"bjorn.andersson@linaro.org" <bjorn.andersson@linaro.org>,
	"jagan@amarulasolutions.com" <jagan@amarulasolutions.com>,
	"enric.balletbo@collabora.com" <enric.balletbo@collabora.com>,
	"stefan.wahren@i2se.com" <stefan.wahren@i2se.com>,
	"ezequiel@collabora.com" <ezequiel@collabora.com>,
	"marc.w.gonzalez@free.fr" <marc.w.gonzalez@free.fr>,
	"robh@kernel.org" <robh@kernel.org>,
	"l.stach@pengutronix.de" <l.stach@pengutronix.de>,
	Abel Vesa <abel.vesa@nxp.com>,
	"linux-arm-kernel@lists.infradead.org" 
	<linux-arm-kernel@lists.infradead.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Cc: dl-linux-imx <linux-imx@nxp.com>
Subject: [PATCH 1/2] soc: imx-sc: add i.MX system controller soc driver support
Date: Thu, 11 Apr 2019 06:49:12 +0000	[thread overview]
Message-ID: <1554965048-19450-1-git-send-email-Anson.Huang@nxp.com> (raw)

i.MX8QXP is an ARMv8 SoC which has a Cortex-M4 system controller
inside, the system controller is in charge of controlling power,
clock and fuse etc..

This patch adds i.MX system controller soc driver support,
Linux kernel has to communicate with system controller via MU
(message unit) IPC to get soc revision, uid etc..

With this patch, soc info can be read from sysfs:

i.mx8qxp-mek# cat /sys/devices/soc0/family
Freescale i.MX

i.mx8qxp-mek# cat /sys/devices/soc0/soc_id
i.MX8QXP

i.mx8qxp-mek# cat /sys/devices/soc0/machine
Freescale i.MX8QXP MEK

i.mx8qxp-mek# cat /sys/devices/soc0/revision
1.1

i.mx8qxp-mek# cat /sys/devices/soc0/soc_uid
7B64280B57AC1898

Signed-off-by: Anson Huang <Anson.Huang@nxp.com>
---
 drivers/soc/imx/Kconfig      |   7 ++
 drivers/soc/imx/Makefile     |   1 +
 drivers/soc/imx/soc-imx-sc.c | 220 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 228 insertions(+)
 create mode 100644 drivers/soc/imx/soc-imx-sc.c

diff --git a/drivers/soc/imx/Kconfig b/drivers/soc/imx/Kconfig
index d80f899..c902b89 100644
--- a/drivers/soc/imx/Kconfig
+++ b/drivers/soc/imx/Kconfig
@@ -7,4 +7,11 @@ config IMX_GPCV2_PM_DOMAINS
 	select PM_GENERIC_DOMAINS
 	default y if SOC_IMX7D
 
+config IMX_SC_SOC
+	depends on IMX_SCU || COMPILE_TEST
+	tristate "i.MX System Controller SoC support"
+	help
+	   If you say yes here you get support for the i.MX System
+	   Controller SoC module.
+
 endmenu
diff --git a/drivers/soc/imx/Makefile b/drivers/soc/imx/Makefile
index 506a6f3..d00606d 100644
--- a/drivers/soc/imx/Makefile
+++ b/drivers/soc/imx/Makefile
@@ -1,2 +1,3 @@
 obj-$(CONFIG_HAVE_IMX_GPC) += gpc.o
 obj-$(CONFIG_IMX_GPCV2_PM_DOMAINS) += gpcv2.o
+obj-$(CONFIG_IMX_SC_SOC) += soc-imx-sc.o
diff --git a/drivers/soc/imx/soc-imx-sc.c b/drivers/soc/imx/soc-imx-sc.c
new file mode 100644
index 0000000..029d754
--- /dev/null
+++ b/drivers/soc/imx/soc-imx-sc.c
@@ -0,0 +1,220 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2019 NXP.
+ */
+
+#include <dt-bindings/firmware/imx/rsrc.h>
+#include <linux/firmware/imx/sci.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/sys_soc.h>
+#include <linux/platform_device.h>
+#include <linux/of.h>
+
+#include <soc/imx/revision.h>
+
+#define IMX_SC_SOC_DRIVER_NAME	"imx-sc-soc"
+
+#define SOC_REV_MAJOR_OFFSET	0x4
+#define SOC_REV_MAJOR_MASK	0xf
+#define SOC_REV_MINOR_OFFSET	0x4
+#define SOC_REV_MINOR_MASK	0xf
+
+#define get_soc_rev_major(rev) ((rev >> SOC_REV_MAJOR_OFFSET) & SOC_REV_MAJOR_MASK)
+#define get_soc_rev_minor(rev) ((rev >> SOC_REV_MINOR_OFFSET) & SOC_REV_MINOR_MASK)
+
+static u32 imx_sc_soc_rev = IMX_CHIP_REVISION_UNKNOWN;
+static u64 imx_sc_soc_uid;
+
+static struct imx_sc_ipc *soc_ipc_handle;
+static struct platform_device *imx_sc_soc_pdev;
+
+struct imx_sc_msg_misc_get_soc_id {
+	struct imx_sc_rpc_msg hdr;
+	union {
+		struct {
+			u32 control;
+			u16 resource;
+		} __packed send;
+		struct {
+			u32 id;
+			u16 reserved;
+		} __packed resp;
+	} data;
+};
+
+struct imx_sc_msg_misc_get_soc_uid {
+	struct imx_sc_rpc_msg hdr;
+	u32 id_l;
+	u32 id_h;
+};
+
+static inline void imx_sc_set_soc_revision(u32 rev)
+{
+	imx_sc_soc_rev = rev;
+}
+
+unsigned int imx_get_soc_revision(void)
+{
+	return imx_sc_soc_rev;
+}
+EXPORT_SYMBOL(imx_get_soc_revision);
+
+static u32 imx_init_revision_from_scu(void)
+{
+	struct imx_sc_msg_misc_get_soc_id msg;
+	struct imx_sc_msg_misc_get_soc_uid msg1;
+	struct imx_sc_rpc_msg *hdr = &msg.hdr;
+	struct imx_sc_rpc_msg *hdr1 = &msg1.hdr;
+	u32 id, rev;
+	int ret;
+
+	hdr->ver = IMX_SC_RPC_VERSION;
+	hdr->svc = IMX_SC_RPC_SVC_MISC;
+	hdr->func = IMX_SC_MISC_FUNC_GET_CONTROL;
+	hdr->size = 3;
+
+	msg.data.send.control = IMX_SC_C_ID;
+	msg.data.send.resource = IMX_SC_R_SYSTEM;
+
+	ret = imx_scu_call_rpc(soc_ipc_handle, &msg, true);
+	if (ret) {
+		pr_err("misc get control failed, ret %d\n", ret);
+		return ret;
+	}
+
+	id = msg.data.resp.id;
+
+	rev = (id >> 5) & 0xf;
+	rev = (((rev >> 2) + 1) << 4) | (rev & 0x3);
+
+	imx_sc_set_soc_revision(rev);
+
+	hdr1->ver = IMX_SC_RPC_VERSION;
+	hdr1->svc = IMX_SC_RPC_SVC_MISC;
+	hdr1->func = IMX_SC_MISC_FUNC_UNIQUE_ID;
+	hdr1->size = 1;
+
+	/* the return value of SCU FW is in correct, can NOT check the ret */
+	ret = imx_scu_call_rpc(soc_ipc_handle, &msg1, true);
+
+	imx_sc_soc_uid = msg1.id_h;
+	imx_sc_soc_uid <<= 32;
+	imx_sc_soc_uid |= msg1.id_l;
+
+	return rev;
+}
+
+static ssize_t imx_sc_get_soc_uid(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	return sprintf(buf, "%016llX\n", imx_sc_soc_uid);
+}
+
+static struct device_attribute imx_sc_uid =
+	__ATTR(soc_uid, 0444, imx_sc_get_soc_uid, NULL);
+
+static int imx_sc_soc_probe(struct platform_device *pdev)
+{
+	struct soc_device_attribute *soc_dev_attr;
+	u32 rev = IMX_CHIP_REVISION_UNKNOWN;
+	struct soc_device *soc_dev;
+	u32 soc_rev;
+	int ret;
+
+	ret = imx_scu_get_handle(&soc_ipc_handle);
+	if (ret)
+		return ret;
+
+	soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
+	if (!soc_dev_attr)
+		return -ENOMEM;
+
+	soc_dev_attr->family = "Freescale i.MX";
+
+	if (of_machine_is_compatible("fsl,imx8qxp"))
+		soc_dev_attr->soc_id = "i.MX8QXP";
+	else
+		soc_dev_attr->soc_id = "unknown";
+
+	rev = imx_init_revision_from_scu();
+	if (rev == IMX_CHIP_REVISION_UNKNOWN)
+		dev_info(&pdev->dev, "CPU identified as %s, unknown revision\n",
+			 soc_dev_attr->soc_id);
+	else
+		dev_info(&pdev->dev, "CPU identified as %s, silicon rev %d.%d\n",
+			 soc_dev_attr->soc_id,
+			 get_soc_rev_major(rev),
+			 get_soc_rev_minor(rev));
+
+	soc_rev = imx_get_soc_revision();
+	soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%d.%d",
+					   get_soc_rev_major(rev),
+					   get_soc_rev_minor(rev));
+	if (!soc_dev_attr->revision) {
+		ret = -ENOMEM;
+		goto free_soc;
+	}
+
+	of_property_read_string(of_root, "model", &soc_dev_attr->machine);
+
+	soc_dev = soc_device_register(soc_dev_attr);
+	if (IS_ERR(soc_dev)) {
+		ret = PTR_ERR(soc_dev);
+		goto free_rev;
+	}
+
+	ret = device_create_file(soc_device_to_device(soc_dev), &imx_sc_uid);
+	if (ret)
+		dev_err(&pdev->dev, "could not register sysfs entry\n");
+
+	return ret;
+
+free_rev:
+	kfree(soc_dev_attr->revision);
+free_soc:
+	kfree(soc_dev_attr);
+	return ret;
+}
+
+static struct platform_driver imx_sc_soc_driver = {
+	.driver = {
+		.name = IMX_SC_SOC_DRIVER_NAME,
+	},
+	.probe = imx_sc_soc_probe,
+};
+
+static int __init imx_sc_soc_init(void)
+{
+	int ret;
+
+	ret = platform_driver_register(&imx_sc_soc_driver);
+	if (ret)
+		return ret;
+
+	imx_sc_soc_pdev =
+		platform_device_register_simple(IMX_SC_SOC_DRIVER_NAME,
+						-1,
+						NULL,
+						0);
+	if (IS_ERR(imx_sc_soc_pdev)) {
+		ret = PTR_ERR(imx_sc_soc_pdev);
+		goto unreg_platform_driver;
+	}
+
+	return 0;
+
+unreg_platform_driver:
+	platform_driver_unregister(&imx_sc_soc_driver);
+	return ret;
+}
+
+static void __exit imx_sc_soc_exit(void)
+{
+	platform_device_unregister(imx_sc_soc_pdev);
+	platform_driver_unregister(&imx_sc_soc_driver);
+}
+
+module_init(imx_sc_soc_init);
+module_exit(imx_sc_soc_exit);
-- 
2.7.4


             reply	other threads:[~2019-04-11  6:49 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-11  6:49 Anson Huang [this message]
2019-04-11  6:49 ` [PATCH 2/2] defconfig: arm64: add i.MX system controller SoC support Anson Huang
2019-04-21  7:40 ` [PATCH 1/2] soc: imx-sc: add i.MX system controller soc driver support Shawn Guo
2019-04-21  7:41   ` Shawn Guo
2019-04-22  0:51     ` Anson Huang
2019-04-22  6:46       ` Anson Huang
2019-04-22  8:48         ` Leonard Crestez
2019-05-10  2:42           ` Shawn Guo
2019-05-10  7:30             ` Abel Vesa
2019-05-10  9:17 ` Aisheng Dong
2019-05-10 11:15   ` Anson Huang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1554965048-19450-1-git-send-email-Anson.Huang@nxp.com \
    --to=anson.huang@nxp.com \
    --cc=abel.vesa@nxp.com \
    --cc=andy.gross@linaro.org \
    --cc=bjorn.andersson@linaro.org \
    --cc=catalin.marinas@arm.com \
    --cc=enric.balletbo@collabora.com \
    --cc=ezequiel@collabora.com \
    --cc=festevam@gmail.com \
    --cc=heiko@sntech.de \
    --cc=horms+renesas@verge.net.au \
    --cc=jagan@amarulasolutions.com \
    --cc=kernel@pengutronix.de \
    --cc=l.stach@pengutronix.de \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-imx@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marc.w.gonzalez@free.fr \
    --cc=olof@lixom.net \
    --cc=robh@kernel.org \
    --cc=s.hauer@pengutronix.de \
    --cc=shawnguo@kernel.org \
    --cc=stefan.wahren@i2se.com \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).