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>
Subject: [PATCH 2/9] power: reset: add RISC-V/UC Berkely HTIF poweroff driver support
Date: Sat,  8 Jan 2022 18:15:17 +0100	[thread overview]
Message-ID: <20220108171524.587144-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20220108171524.587144-1-a.fatoum@pengutronix.de>

We already have a driver in board code and device tree passed by
emulator already has a node for it. Match against it and create a proper
driver.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/riscv/boards/riscvemu/board.c  | 15 ----------
 arch/riscv/configs/virt32_defconfig |  1 +
 arch/riscv/configs/virt64_defconfig |  1 +
 arch/riscv/include/asm/htif.h       | 31 +++++++++++++++++++
 drivers/power/reset/Kconfig         |  7 +++++
 drivers/power/reset/Makefile        |  1 +
 drivers/power/reset/htif-poweroff.c | 46 +++++++++++++++++++++++++++++
 7 files changed, 87 insertions(+), 15 deletions(-)
 create mode 100644 arch/riscv/include/asm/htif.h
 create mode 100644 drivers/power/reset/htif-poweroff.c

diff --git a/arch/riscv/boards/riscvemu/board.c b/arch/riscv/boards/riscvemu/board.c
index 7dbf9afe4c2d..73d787a8335a 100644
--- a/arch/riscv/boards/riscvemu/board.c
+++ b/arch/riscv/boards/riscvemu/board.c
@@ -16,18 +16,6 @@ struct riscvemu_priv {
 
 };
 
-#define HTIF_BASE_ADDR		IOMEM(0x40008000)
-#define HTIF_TOHOST_LOW		(HTIF_BASE_ADDR + 0)
-#define HTIF_TOHOST_HIGH	(HTIF_BASE_ADDR + 4)
-
-static void __noreturn riscvemu_poweroff(struct poweroff_handler *pwr)
-{
-	writel(1, HTIF_TOHOST_LOW);
-	writel(0, HTIF_TOHOST_HIGH);
-
-	__builtin_unreachable();
-}
-
 static void __noreturn riscvemu_restart(struct restart_handler *rst)
 {
 	struct riscvemu_priv *priv = container_of(rst, struct riscvemu_priv, rst);
@@ -46,9 +34,6 @@ static int riscvemu_probe(struct device_d *dev)
 	struct riscvemu_priv *priv;
 	u64 start;
 
-	if (of_find_compatible_node(NULL, NULL, "ucb,htif0"))
-		poweroff_handler_register_fn(riscvemu_poweroff);
-
 	of_chosen = of_find_node_by_path("/chosen");
 
 	if (of_property_read_u64(of_chosen, "riscv,kernel-start", &start))
diff --git a/arch/riscv/configs/virt32_defconfig b/arch/riscv/configs/virt32_defconfig
index ea7d1de8be68..bfea7771efd8 100644
--- a/arch/riscv/configs/virt32_defconfig
+++ b/arch/riscv/configs/virt32_defconfig
@@ -109,6 +109,7 @@ CONFIG_BLK_DEV_NVME=y
 CONFIG_SYSCON_REBOOT_MODE=y
 CONFIG_POWER_RESET_SYSCON=y
 CONFIG_POWER_RESET_SYSCON_POWEROFF=y
+CONFIG_POWER_RESET_HTIF_POWEROFF=y
 CONFIG_VIRTIO_MMIO=y
 CONFIG_FS_EXT4=y
 CONFIG_FS_TFTP=y
diff --git a/arch/riscv/configs/virt64_defconfig b/arch/riscv/configs/virt64_defconfig
index b1c7a6712ac3..2ddc174b8a65 100644
--- a/arch/riscv/configs/virt64_defconfig
+++ b/arch/riscv/configs/virt64_defconfig
@@ -110,6 +110,7 @@ CONFIG_BLK_DEV_NVME=y
 CONFIG_SYSCON_REBOOT_MODE=y
 CONFIG_POWER_RESET_SYSCON=y
 CONFIG_POWER_RESET_SYSCON_POWEROFF=y
+CONFIG_POWER_RESET_HTIF_POWEROFF=y
 CONFIG_VIRTIO_MMIO=y
 CONFIG_FS_EXT4=y
 CONFIG_FS_TFTP=y
diff --git a/arch/riscv/include/asm/htif.h b/arch/riscv/include/asm/htif.h
new file mode 100644
index 000000000000..7312f09d2e6e
--- /dev/null
+++ b/arch/riscv/include/asm/htif.h
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2021 Ahmad Fatoum, Pengutronix
+ */
+
+#ifndef __ASM_HTIF_LL__
+#define __ASM_HTIF_LL__
+
+#define HTIF_DEFAULT_BASE_ADDR		0x40008000
+
+#define HTIF_DEV_SYSCALL		0
+#define		HTIF_CMD_SYSCALL	0
+
+#ifndef __ASSEMBLY__
+
+#include <linux/types.h>
+#include <io-64-nonatomic-lo-hi.h>
+
+static inline void __htif_tohost(void __iomem *htif, u8 device, u8 command, u64 arg)
+{
+	writeq(((u64)device << 56) | ((u64)command << 48) | arg, htif);
+}
+
+static inline void htif_tohost(u8 device, u8 command, u64 arg)
+{
+	__htif_tohost(IOMEM(HTIF_DEFAULT_BASE_ADDR), device, command, arg);
+}
+
+#endif
+
+#endif
diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig
index e4151d8bc608..968d4b8ba47d 100644
--- a/drivers/power/reset/Kconfig
+++ b/drivers/power/reset/Kconfig
@@ -54,3 +54,10 @@ config POWER_RESET_GPIO_RESTART
 	  This driver supports restarting your board via a GPIO line.
 	  If your board needs a GPIO high/low to restart, say Y and
 	  create a binding in your devicetree.
+
+config POWER_RESET_HTIF_POWEROFF
+	bool "HTIF poweroff driver"
+	depends on RISCV
+	help
+	  Adds poweroff support via the syscall device on systems
+	  supporting the UC Berkely Host/Target Interface (HTIF).
diff --git a/drivers/power/reset/Makefile b/drivers/power/reset/Makefile
index 10d6f2a41e22..c581382be86d 100644
--- a/drivers/power/reset/Makefile
+++ b/drivers/power/reset/Makefile
@@ -6,3 +6,4 @@ obj-$(CONFIG_POWER_RESET_SYSCON) += syscon-reboot.o
 obj-$(CONFIG_POWER_RESET_SYSCON_POWEROFF) += syscon-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO_RESTART) += gpio-restart.o
+obj-$(CONFIG_POWER_RESET_HTIF_POWEROFF) += htif-poweroff.o
diff --git a/drivers/power/reset/htif-poweroff.c b/drivers/power/reset/htif-poweroff.c
new file mode 100644
index 000000000000..8df060d5079f
--- /dev/null
+++ b/drivers/power/reset/htif-poweroff.c
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Ahmad Fatoum, Pengutronix
+ */
+
+#include <common.h>
+#include <driver.h>
+#include <poweroff.h>
+#include <asm/htif.h>
+
+static void __iomem *htif = IOMEM(HTIF_DEFAULT_BASE_ADDR);
+
+static void __noreturn riscvemu_poweroff(struct poweroff_handler *pwr)
+{
+	shutdown_barebox();
+
+	__htif_tohost(htif, HTIF_DEV_SYSCALL, 0, 1);
+
+	__builtin_unreachable();
+}
+
+static int htif_poweroff_probe(struct device_d *dev)
+{
+	struct resource *iores;
+
+	iores = dev_request_mem_resource(dev, 0);
+	if (!IS_ERR(iores))
+		htif = IOMEM(iores->start);
+	else if (PTR_ERR(iores) != -ENOENT)
+		return PTR_ERR(iores);
+
+	return poweroff_handler_register_fn(riscvemu_poweroff);
+}
+
+
+static const struct of_device_id htif_poweroff_of_match[] = {
+	{ .compatible = "ucb,htif0" },
+	{}
+};
+
+static struct driver_d htif_poweroff_driver = {
+	.name = "htif-poweroff",
+	.of_compatible = htif_poweroff_of_match,
+	.probe = htif_poweroff_probe,
+};
+coredevice_platform_driver(htif_poweroff_driver);
-- 
2.30.2


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


  reply	other threads:[~2022-01-08 17:17 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-08 17:15 [PATCH 1/9] RISC-V: virt: activate all consoles Ahmad Fatoum
2022-01-08 17:15 ` Ahmad Fatoum [this message]
2022-01-08 17:15 ` [PATCH 3/9] RISC-V: virt: riscvemu: add HTIF DEBUG_LL support Ahmad Fatoum
2022-01-08 17:15 ` [PATCH 4/9] RISC-V: virt: riscvemu: repurpose 64k low RAM for state/environment Ahmad Fatoum
2022-01-08 17:15 ` [PATCH 5/9] RISC-V: virt: riscvemu: be explicit about HTIF base address Ahmad Fatoum
2022-01-08 17:15 ` [PATCH 6/9] RISC-V: virt: riscvemu: clear frame buffer before jumping to reset vector Ahmad Fatoum
2022-01-08 17:15 ` [PATCH 7/9] RISC-V: don't use x8/s0/fp in assembly Ahmad Fatoum
2022-01-08 18:25   ` Ahmad Fatoum
2022-01-10 10:59     ` Antony Pavlov
2022-01-08 17:15 ` [PATCH 8/9] RISC-V: add stacktrace support via frame pointer walking Ahmad Fatoum
2022-01-08 17:15 ` [PATCH 9/9] RISC-V: Virt: enable more useful options Ahmad Fatoum
2022-01-12 10:23 ` [PATCH 1/9] RISC-V: virt: activate all consoles Sascha Hauer

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