Linux-Watchdog Archive on lore.kernel.org
 help / color / Atom feed
From: <freddy.hsin@mediatek.com>
To: <sre@kernel.org>, <linux-pm@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <wim@linux-watchdog.org>,
	<linux@roeck-us.net>, <matthias.bgg@gmail.com>,
	<linux-watchdog@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-mediatek@lists.infradead.org>, <robh+dt@kernel.org>,
	<mark.rutland@arm.com>, <devicetree@vger.kernel.org>
Cc: <Chang-An.Chen@mediatek.com>, <wsd_upstream@mediatek.com>,
	Freddy Hsin <freddy.hsin@mediatek.com>
Subject: [PATCH v1 1/4] power: reset: add reboot mode driver
Date: Tue, 26 Nov 2019 13:34:47 +0800
Message-ID: <1574746490-625-2-git-send-email-freddy.hsin@mediatek.com> (raw)
In-Reply-To: <1574746490-625-1-git-send-email-freddy.hsin@mediatek.com>

From: Freddy Hsin <freddy.hsin@mediatek.com>

This driver parses the reboot commands like "reboot bootloader"
and "reboot recovery" to get a boot mode described in the
device tree , then call the write interfae to store the boot
mode in mtk RGU (reset generation unit) non-volatile register,
which can be read by the bootloader after system reboot, then
the bootloader can take different action according to the mode
stored.

Signed-off-by: Freddy Hsin <freddy.hsin@mediatek.com>
---
 drivers/power/reset/Kconfig      |   11 ++++
 drivers/power/reset/Makefile     |    1 +
 drivers/power/reset/mtk-reboot.c |  116 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 128 insertions(+)
 create mode 100644 drivers/power/reset/mtk-reboot.c

diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig
index a564237..31fedb8 100644
--- a/drivers/power/reset/Kconfig
+++ b/drivers/power/reset/Kconfig
@@ -256,5 +256,16 @@ config NVMEM_REBOOT_MODE
 	  then the bootloader can read it and take different
 	  action according to the mode.
 
+config MTK_REBOOT_MODE
+	tristate "Mediatek SoCs reset driver"
+	depends on OF
+	depends on REGMAP
+	select REBOOT_MODE
+	help
+	  Say y here will enable reboot mode driver. This will
+	  get reboot mode arguments and store it in RGU mapped
+	  register, then the bootloader can read it to take different
+	  action according to the mode.
+
 endif
 
diff --git a/drivers/power/reset/Makefile b/drivers/power/reset/Makefile
index 85da3198..2c64104 100644
--- a/drivers/power/reset/Makefile
+++ b/drivers/power/reset/Makefile
@@ -30,3 +30,4 @@ obj-$(CONFIG_REBOOT_MODE) += reboot-mode.o
 obj-$(CONFIG_SYSCON_REBOOT_MODE) += syscon-reboot-mode.o
 obj-$(CONFIG_POWER_RESET_SC27XX) += sc27xx-poweroff.o
 obj-$(CONFIG_NVMEM_REBOOT_MODE) += nvmem-reboot-mode.o
+obj-$(CONFIG_MTK_REBOOT_MODE) += mtk-reboot.o
diff --git a/drivers/power/reset/mtk-reboot.c b/drivers/power/reset/mtk-reboot.c
new file mode 100644
index 0000000..545c427
--- /dev/null
+++ b/drivers/power/reset/mtk-reboot.c
@@ -0,0 +1,116 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019 MediaTek Inc.
+ * Author Freddy Hsin <freddy.hsin@mediatek.com>
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/reboot.h>
+#include <linux/regmap.h>
+#include <linux/of_address.h>
+#include <linux/reboot-mode.h>
+
+static const struct regmap_config mtk_regmap_config = {
+	.reg_bits = 32,
+	.val_bits = 32,
+	.reg_stride = 4,
+};
+
+struct mtk_reboot_mode {
+	struct regmap *map;
+	struct reboot_mode_driver reboot;
+	u32 offset;
+	u32 mask;
+};
+
+static int mtk_reboot_mode_write(struct reboot_mode_driver *reboot,
+				 unsigned int magic)
+{
+	struct mtk_reboot_mode *mtk_rbm;
+	int ret;
+
+	mtk_rbm = container_of(reboot, struct mtk_reboot_mode, reboot);
+
+	ret = regmap_update_bits(mtk_rbm->map, mtk_rbm->offset,
+				 mtk_rbm->mask, magic);
+	if (ret < 0)
+		dev_info(reboot->dev, "update reboot mode bits failed\n");
+
+	return ret;
+}
+
+static int mtk_regmap_lookup_by_phandle(struct device *dev,
+					struct mtk_reboot_mode *mtk_rbm)
+{
+	struct device_node *toprgu_np;
+	struct device_node *np = dev->of_node;
+	void __iomem *base;
+
+	toprgu_np = of_parse_phandle(np, "regmap", 0);
+
+	if (!of_device_is_compatible(toprgu_np, "mediatek,toprgu"))
+		return -EINVAL;
+
+	base = of_iomap(toprgu_np, 0);
+	if (IS_ERR(base))
+		return PTR_ERR(base);
+
+	mtk_rbm->map = devm_regmap_init_mmio(dev, base,
+					     &mtk_regmap_config);
+	return PTR_ERR_OR_ZERO(mtk_rbm->map);
+}
+
+static int mtk_reboot_mode_probe(struct platform_device *pdev)
+{
+	int ret;
+	struct mtk_reboot_mode *mtk_rbm;
+
+	mtk_rbm = devm_kzalloc(&pdev->dev, sizeof(*mtk_rbm), GFP_KERNEL);
+	if (!mtk_rbm)
+		return -ENOMEM;
+
+	mtk_rbm->reboot.dev = &pdev->dev;
+	mtk_rbm->reboot.write = mtk_reboot_mode_write;
+	mtk_rbm->mask = 0xf;
+
+	ret = mtk_regmap_lookup_by_phandle(&pdev->dev, mtk_rbm);
+	if (ret) {
+		dev_info(&pdev->dev, "Couldn't create the toprgu regmap\n");
+		return -EINVAL;
+	}
+
+	if (of_property_read_u32(pdev->dev.of_node, "offset",
+				 &mtk_rbm->offset))
+		return -EINVAL;
+
+	of_property_read_u32(pdev->dev.of_node, "mask", &mtk_rbm->mask);
+
+	ret = devm_reboot_mode_register(&pdev->dev, &mtk_rbm->reboot);
+	if (ret)
+		dev_info(&pdev->dev, "can't register reboot mode\n");
+
+	return ret;
+}
+
+static const struct of_device_id mtk_reboot_mode_of_match[] = {
+	{ .compatible = "toprgu-reboot-mode" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, mtk_reboot_mode_of_match);
+
+static struct platform_driver mtk_reboot_mode_driver = {
+	.probe = mtk_reboot_mode_probe,
+	.driver = {
+		.name = "toprgu-reboot-mode",
+		.of_match_table = mtk_reboot_mode_of_match,
+	},
+};
+module_platform_driver(mtk_reboot_mode_driver);
+
+MODULE_AUTHOR("Freddy Hsin <freddy.hsin@mediatek.com>");
+MODULE_DESCRIPTION("Mediatek reboot mode driver");
+MODULE_LICENSE("GPL v2");
-- 
1.7.9.5

  reply index

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-26  5:34 [PATCH v1 0/4] Support MTK " freddy.hsin
2019-11-26  5:34 ` freddy.hsin [this message]
2019-11-26  5:34 ` [PATCH v1 2/4] watchdog: populate reboot mode node in toprgu node of MTK RGU (Reset Generation Unit) freddy.hsin
2019-11-26 12:18   ` Guenter Roeck
2019-11-26  5:34 ` [PATCH v1 3/4] soc: mediatek: add reboot-mode header freddy.hsin
2019-11-26  5:34 ` [PATCH v1 4/4] dt-binding: power: reset: Add documentation for MTK RGU (Reset Gernation Unit) reboot driver freddy.hsin
2019-12-05 19:08   ` Rob Herring

Reply instructions:

You may reply publically 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=1574746490-625-2-git-send-email-freddy.hsin@mediatek.com \
    --to=freddy.hsin@mediatek.com \
    --cc=Chang-An.Chen@mediatek.com \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=mark.rutland@arm.com \
    --cc=matthias.bgg@gmail.com \
    --cc=robh+dt@kernel.org \
    --cc=sre@kernel.org \
    --cc=wim@linux-watchdog.org \
    --cc=wsd_upstream@mediatek.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

Linux-Watchdog Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-watchdog/0 linux-watchdog/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-watchdog linux-watchdog/ https://lore.kernel.org/linux-watchdog \
		linux-watchdog@vger.kernel.org
	public-inbox-index linux-watchdog

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-watchdog


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git