lib: utils/reset: Add syscon based reboot and poweroff
authorAnup Patel <apatel@ventanamicro.com>
Fri, 21 Jul 2023 12:41:52 +0000 (18:11 +0530)
committerAnup Patel <anup@brainfault.org>
Mon, 31 Jul 2023 08:39:21 +0000 (14:09 +0530)
Let us have common FDT based reset driver for syscon reboot and
poweroff. The device tree bindings for syscon reboot and poweroff
are already available in the Linux kernel sources.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Xiang W <wxjstz@126.com>
lib/utils/reset/Kconfig
lib/utils/reset/fdt_reset_syscon.c [new file with mode: 0644]
lib/utils/reset/objects.mk
platform/generic/configs/defconfig

index 4748cc8504a673121e4b9edf8d97786988c55519..c0b964066eefa3398e40dac3de715369b34ba08b 100644 (file)
@@ -33,6 +33,11 @@ config FDT_RESET_SUNXI_WDT
        bool "Sunxi WDT FDT reset driver"
        default n
 
+config FDT_RESET_SYSCON
+       bool "Syscon FDT reset driver"
+       depends on FDT_REGMAP
+       default n
+
 config FDT_RESET_THEAD
        bool "T-HEAD FDT reset driver"
        default n
diff --git a/lib/utils/reset/fdt_reset_syscon.c b/lib/utils/reset/fdt_reset_syscon.c
new file mode 100644 (file)
index 0000000..bae9ec5
--- /dev/null
@@ -0,0 +1,167 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Ventana Micro Systems Inc.
+ *
+ * Authors:
+ *   Anup Patel <apatel@ventanamicro.com>
+ */
+
+#include <libfdt.h>
+#include <sbi/sbi_ecall_interface.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_hart.h>
+#include <sbi/sbi_system.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/regmap/fdt_regmap.h>
+#include <sbi_utils/reset/fdt_reset.h>
+
+struct syscon_reset {
+       struct regmap *rmap;
+       u32 priority;
+       u32 offset;
+       u32 value;
+       u32 mask;
+};
+
+static struct syscon_reset poweroff;
+static struct syscon_reset reboot;
+
+static struct syscon_reset *syscon_reset_get(bool is_reboot, u32 type)
+{
+       struct syscon_reset *reset = NULL;
+
+       switch (type) {
+       case SBI_SRST_RESET_TYPE_SHUTDOWN:
+               if (!is_reboot)
+                       reset = &poweroff;
+               break;
+       case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+       case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+               if (is_reboot)
+                       reset = &reboot;
+               break;
+       }
+
+       if (reset && !reset->rmap)
+               reset = NULL;
+
+       return reset;
+}
+
+static void syscon_reset_exec(struct syscon_reset *reset)
+{
+       /* Issue the reset through regmap */
+       if (reset)
+               regmap_update_bits(reset->rmap, reset->offset,
+                                  reset->mask, reset->value);
+
+       /* hang !!! */
+       sbi_hart_hang();
+}
+
+static int syscon_poweroff_check(u32 type, u32 reason)
+{
+       struct syscon_reset *reset = syscon_reset_get(false, type);
+
+       return (reset) ? reset->priority : 0;
+}
+
+static void syscon_do_poweroff(u32 type, u32 reason)
+{
+       syscon_reset_exec(syscon_reset_get(false, type));
+}
+
+static struct sbi_system_reset_device syscon_poweroff = {
+       .name = "syscon-poweroff",
+       .system_reset_check = syscon_poweroff_check,
+       .system_reset = syscon_do_poweroff
+};
+
+static int syscon_reboot_check(u32 type, u32 reason)
+{
+       struct syscon_reset *reset = syscon_reset_get(true, type);
+
+       return (reset) ? reset->priority : 0;
+}
+
+static void syscon_do_reboot(u32 type, u32 reason)
+{
+       syscon_reset_exec(syscon_reset_get(true, type));
+}
+
+static struct sbi_system_reset_device syscon_reboot = {
+       .name = "syscon-reboot",
+       .system_reset_check = syscon_reboot_check,
+       .system_reset = syscon_do_reboot
+};
+
+static int syscon_reset_init(void *fdt, int nodeoff,
+                          const struct fdt_match *match)
+{
+       int rc, len;
+       const fdt32_t *val, *mask;
+       bool is_reboot = (ulong)match->data;
+       struct syscon_reset *reset = (is_reboot) ? &reboot : &poweroff;
+
+       if (reset->rmap)
+               return SBI_EALREADY;
+
+       rc = fdt_regmap_get(fdt, nodeoff, &reset->rmap);
+       if (rc)
+               return rc;
+
+       val = fdt_getprop(fdt, nodeoff, "priority", &len);
+       reset->priority = (val && len > 0) ? fdt32_to_cpu(*val) : 192;
+
+       val = fdt_getprop(fdt, nodeoff, "offset", &len);
+       if (val && len > 0)
+               reset->offset = fdt32_to_cpu(*val);
+       else
+               return SBI_EINVAL;
+
+       val = fdt_getprop(fdt, nodeoff, "value", &len);
+       mask = fdt_getprop(fdt, nodeoff, "mask", &len);
+       if (!val && !mask)
+               return SBI_EINVAL;
+
+       if (!val) {
+               /* support old binding */
+               reset->value = fdt32_to_cpu(*mask);
+               reset->mask = 0xFFFFFFFF;
+       } else if (!mask) {
+               /* support value without mask */
+               reset->value = fdt32_to_cpu(*val);
+               reset->mask = 0xFFFFFFFF;
+       } else {
+               reset->value = fdt32_to_cpu(*val);
+               reset->mask = fdt32_to_cpu(*mask);
+       }
+
+       if (is_reboot)
+               sbi_system_reset_add_device(&syscon_reboot);
+       else
+               sbi_system_reset_add_device(&syscon_poweroff);
+
+       return 0;
+}
+
+static const struct fdt_match syscon_poweroff_match[] = {
+       { .compatible = "syscon-poweroff", .data = (const void *)false },
+       { },
+};
+
+struct fdt_reset fdt_syscon_poweroff = {
+       .match_table = syscon_poweroff_match,
+       .init = syscon_reset_init,
+};
+
+static const struct fdt_match syscon_reboot_match[] = {
+       { .compatible = "syscon-reboot", .data = (const void *)true },
+       { },
+};
+
+struct fdt_reset fdt_syscon_reboot = {
+       .match_table = syscon_reboot_match,
+       .init = syscon_reset_init,
+};
index c9f851cd1930bfcac23352beca6fbaa052caca1a..17287a5e2be88aec1adc839ffbb7af2056be2221 100644 (file)
@@ -26,6 +26,10 @@ libsbiutils-objs-$(CONFIG_FDT_RESET_SIFIVE_TEST) += reset/fdt_reset_sifive_test.
 carray-fdt_reset_drivers-$(CONFIG_FDT_RESET_SUNXI_WDT) += fdt_reset_sunxi_wdt
 libsbiutils-objs-$(CONFIG_FDT_RESET_SUNXI_WDT) += reset/fdt_reset_sunxi_wdt.o
 
+carray-fdt_reset_drivers-$(CONFIG_FDT_RESET_SYSCON) += fdt_syscon_poweroff
+carray-fdt_reset_drivers-$(CONFIG_FDT_RESET_SYSCON) += fdt_syscon_reboot
+libsbiutils-objs-$(CONFIG_FDT_RESET_SYSCON) += reset/fdt_reset_syscon.o
+
 carray-fdt_reset_drivers-$(CONFIG_FDT_RESET_THEAD) += fdt_reset_thead
 libsbiutils-objs-$(CONFIG_FDT_RESET_THEAD) += reset/fdt_reset_thead.o
 libsbiutils-objs-$(CONFIG_FDT_RESET_THEAD) += reset/fdt_reset_thead_asm.o
index b76729077dfafe0fc8ddc49b1181514fc394f669..5008c7e6cea85048f5098b5d8a9aa52750fdfddf 100644 (file)
@@ -26,6 +26,7 @@ CONFIG_FDT_RESET_GPIO=y
 CONFIG_FDT_RESET_HTIF=y
 CONFIG_FDT_RESET_SIFIVE_TEST=y
 CONFIG_FDT_RESET_SUNXI_WDT=y
+CONFIG_FDT_RESET_SYSCON=y
 CONFIG_FDT_RESET_THEAD=y
 CONFIG_FDT_SERIAL=y
 CONFIG_FDT_SERIAL_CADENCE=y