mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>, rcz@pengutronix.de
Subject: [PATCH 19/20] power: reset: add drivers for generic syscon reset and poweroff
Date: Sun, 14 Mar 2021 13:28:03 +0100	[thread overview]
Message-ID: <20210314122804.4128-20-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20210314122804.4128-1-a.fatoum@pengutronix.de>

Many SoC reset/poweroff mechanisms can be represented using this generic
binding. Of the boards we support, at least the virt machines can
be rebooted and powered off by it. Import the Linux v5.11 state
of the driver to support this.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/power/reset/Kconfig           | 14 ++++
 drivers/power/reset/Makefile          |  2 +
 drivers/power/reset/syscon-poweroff.c | 76 ++++++++++++++++++++++
 drivers/power/reset/syscon-reboot.c   | 92 +++++++++++++++++++++++++++
 4 files changed, 184 insertions(+)
 create mode 100644 drivers/power/reset/syscon-poweroff.c
 create mode 100644 drivers/power/reset/syscon-reboot.c

diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig
index f65e1f67fd59..e60037a6e637 100644
--- a/drivers/power/reset/Kconfig
+++ b/drivers/power/reset/Kconfig
@@ -14,3 +14,17 @@ config SYSCON_REBOOT_MODE
 	  get reboot mode arguments and store it in SYSCON mapped
 	  register, then the bootloader can read it to take different
 	  action according to the mode.
+
+config POWER_RESET_SYSCON
+	bool "Generic SYSCON regmap reset driver"
+	depends on OFDEVICE
+	select MFD_SYSCON
+	help
+	  Reboot support for generic SYSCON mapped register reset.
+
+config POWER_RESET_SYSCON_POWEROFF
+	bool "Generic SYSCON regmap poweroff driver"
+	depends on OFDEVICE
+	select MFD_SYSCON
+	help
+	  Poweroff support for generic SYSCON mapped register poweroff.
diff --git a/drivers/power/reset/Makefile b/drivers/power/reset/Makefile
index 56feec78cf26..a490dce87333 100644
--- a/drivers/power/reset/Makefile
+++ b/drivers/power/reset/Makefile
@@ -1,3 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_REBOOT_MODE) += reboot-mode.o
 obj-$(CONFIG_SYSCON_REBOOT_MODE) += syscon-reboot-mode.o
+obj-$(CONFIG_POWER_RESET_SYSCON) += syscon-reboot.o
+obj-$(CONFIG_POWER_RESET_SYSCON_POWEROFF) += syscon-poweroff.o
diff --git a/drivers/power/reset/syscon-poweroff.c b/drivers/power/reset/syscon-poweroff.c
new file mode 100644
index 000000000000..3664c4d8bd08
--- /dev/null
+++ b/drivers/power/reset/syscon-poweroff.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Generic Syscon Poweroff Driver
+ *
+ * Copyright (c) 2015, National Instruments Corp.
+ * Author: Moritz Fischer <moritz.fischer@ettus.com>
+ */
+
+#include <common.h>
+#include <init.h>
+#include <poweroff.h>
+#include <mfd/syscon.h>
+
+static struct regmap *map;
+static u32 offset;
+static u32 value;
+static u32 mask;
+
+static void syscon_poweroff(struct poweroff_handler *handler)
+{
+	/* Issue the poweroff */
+	regmap_update_bits(map, offset, mask, value);
+
+	mdelay(1000);
+
+	pr_emerg("Unable to poweroff system\n");
+}
+
+static int syscon_poweroff_probe(struct device_d *dev)
+{
+	int mask_err, value_err;
+
+	map = syscon_regmap_lookup_by_phandle(dev->device_node, "regmap");
+	if (IS_ERR(map)) {
+		dev_err(dev, "unable to get syscon");
+		return PTR_ERR(map);
+	}
+
+	if (of_property_read_u32(dev->device_node, "offset", &offset)) {
+		dev_err(dev, "unable to read 'offset'");
+		return -EINVAL;
+	}
+
+	value_err = of_property_read_u32(dev->device_node, "value", &value);
+	mask_err = of_property_read_u32(dev->device_node, "mask", &mask);
+	if (value_err && mask_err) {
+		dev_err(dev, "unable to read 'value' and 'mask'");
+		return -EINVAL;
+	}
+
+	if (value_err) {
+		/* support old binding */
+		value = mask;
+		mask = 0xFFFFFFFF;
+	} else if (mask_err) {
+		/* support value without mask*/
+		mask = 0xFFFFFFFF;
+	}
+
+	poweroff_handler_register_fn(syscon_poweroff);
+
+	return 0;
+}
+
+static const struct of_device_id syscon_poweroff_of_match[] = {
+	{ .compatible = "syscon-poweroff" },
+	{}
+};
+
+static struct driver_d syscon_poweroff_driver = {
+	.name = "syscon-poweroff",
+	.of_compatible = syscon_poweroff_of_match,
+	.probe = syscon_poweroff_probe,
+};
+
+coredevice_platform_driver(syscon_poweroff_driver);
diff --git a/drivers/power/reset/syscon-reboot.c b/drivers/power/reset/syscon-reboot.c
new file mode 100644
index 000000000000..2dbb6c1ddcb8
--- /dev/null
+++ b/drivers/power/reset/syscon-reboot.c
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Generic Syscon Reboot Driver
+ *
+ * Copyright (c) 2013, Applied Micro Circuits Corporation
+ * Author: Feng Kan <fkan@apm.com>
+ */
+#include <common.h>
+#include <init.h>
+#include <restart.h>
+#include <mfd/syscon.h>
+
+struct syscon_reboot_context {
+	struct regmap *map;
+	u32 offset;
+	u32 value;
+	u32 mask;
+	struct restart_handler restart_handler;
+};
+
+static void __noreturn syscon_restart_handle(struct restart_handler *this)
+{
+	struct syscon_reboot_context *ctx =
+			container_of(this, struct syscon_reboot_context,
+					restart_handler);
+
+	/* Issue the reboot */
+	regmap_update_bits(ctx->map, ctx->offset, ctx->mask, ctx->value);
+
+	mdelay(1000);
+
+	panic("Unable to restart system\n");
+}
+
+static int syscon_reboot_probe(struct device_d *dev)
+{
+	struct syscon_reboot_context *ctx;
+	int mask_err, value_err;
+	int err;
+
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+	if (!ctx)
+		return -ENOMEM;
+
+	ctx->map = syscon_regmap_lookup_by_phandle(dev->device_node, "regmap");
+	if (IS_ERR(ctx->map)) {
+		ctx->map = syscon_node_to_regmap(dev->parent->device_node);
+		if (IS_ERR(ctx->map))
+			return PTR_ERR(ctx->map);
+	}
+
+	if (of_property_read_u32(dev->device_node, "offset", &ctx->offset))
+		return -EINVAL;
+
+	value_err = of_property_read_u32(dev->device_node, "value", &ctx->value);
+	mask_err = of_property_read_u32(dev->device_node, "mask", &ctx->mask);
+	if (value_err && mask_err) {
+		dev_err(dev, "unable to read 'value' and 'mask'");
+		return -EINVAL;
+	}
+
+	if (value_err) {
+		/* support old binding */
+		ctx->value = ctx->mask;
+		ctx->mask = 0xFFFFFFFF;
+	} else if (mask_err) {
+		/* support value without mask*/
+		ctx->mask = 0xFFFFFFFF;
+	}
+
+	ctx->restart_handler.name = "syscon-reboot";
+	ctx->restart_handler.restart = syscon_restart_handle;
+	ctx->restart_handler.priority = 192;
+
+	err = restart_handler_register(&ctx->restart_handler);
+	if (err)
+		dev_err(dev, "can't register restart notifier\n");
+
+	return err;
+}
+
+static const struct of_device_id syscon_reboot_of_match[] = {
+	{ .compatible = "syscon-reboot" },
+	{}
+};
+
+static struct driver_d syscon_reboot_driver = {
+	.probe = syscon_reboot_probe,
+	.name = "syscon-reboot",
+	.of_compatible = syscon_reboot_of_match,
+};
+coredevice_platform_driver(syscon_reboot_driver);
-- 
2.29.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  parent reply	other threads:[~2021-03-14 12:30 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-14 12:27 [PATCH 00/20] RISC-V: rework for PBL, VIRT and 64-Bit support Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 01/20] partitions: don't allocate dma capable memory Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 02/20] images: make BOARD_ARM_GENERIC_DT available for other arches Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 03/20] ARM: rename CONFIG_ARM_USE_COMPRESSED_DTB to CONFIG_USE_COMPRESSED_DTB Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 04/20] ARM: aarch64: ommit unused label in assembly Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 05/20] serial: virtio-console: depend on, but don't select VIRTIO Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 06/20] filetype: detect RISC-V Linux kernel image Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 07/20] asm: unaligned: don't do unaligned accesses Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 08/20] RISC-V: debug_ll: ns16550: align C access size with assembly's Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 09/20] RISC-V: drop duplicate or unneeded cflags Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 10/20] RISC-V: add cacheless HAS_DMA support Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 11/20] RISC-V: erizo: move to arch/riscv/boards/erizo Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 12/20] RISC-V: import Linux' optimized string functions Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 13/20] RISC-V: implement PBL and relocation support Ahmad Fatoum
2021-03-15  7:43   ` Ahmad Fatoum
2021-03-14 12:27 ` [PATCH 14/20] RISC-V: erizo: migrate to PBL Ahmad Fatoum
2021-03-15  8:43   ` MIPS RELOCATABLE: " Antony Pavlov
2021-03-15 11:45     ` Oleksij Rempel
2021-03-15 12:08       ` Antony Pavlov
2021-03-15 12:40         ` Ahmad Fatoum
2021-03-15 15:37           ` Antony Pavlov
2021-03-15 15:46             ` Ahmad Fatoum
2021-03-15 17:03               ` Oleksij Rempel
2021-03-14 12:27 ` [PATCH 15/20] RISC-V: support symbol names in barebox image Ahmad Fatoum
2021-03-14 12:28 ` [PATCH 16/20] RISC-V: add 64-bit support Ahmad Fatoum
2021-03-14 12:51   ` Rouven Czerwinski
2021-03-14 13:03   ` [PATCH 1/2] fixup! " Ahmad Fatoum
2021-03-14 13:03     ` [PATCH 2/2] " Ahmad Fatoum
2021-03-15  8:38       ` Antony Pavlov
2021-03-14 12:28 ` [PATCH 17/20] RISC-V: add generic DT image Ahmad Fatoum
2021-03-14 12:28 ` [PATCH 18/20] clocksource: add driver for RISC-V CLINT timer Ahmad Fatoum
2021-03-14 12:28 ` Ahmad Fatoum [this message]
2021-03-14 12:28 ` [PATCH 20/20] RISC-V: add Qemu virt support Ahmad Fatoum
2021-03-14 16:30   ` [PATCH] fixup! " Ahmad Fatoum
2021-03-15  8:22 ` [PATCH 00/20] RISC-V: rework for PBL, VIRT and 64-Bit support Antony Pavlov
2021-03-15 10:30   ` Ahmad Fatoum

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=20210314122804.4128-20-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=rcz@pengutronix.de \
    /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