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 <[email protected]>
---
 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
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Reply via email to