]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
reboot-mode: read the boot mode from RTC memory
authorNandor Han <nandor.han@vaisala.com>
Thu, 10 Jun 2021 13:56:45 +0000 (16:56 +0300)
committerTom Rini <trini@konsulko.com>
Fri, 23 Jul 2021 14:16:39 +0000 (10:16 -0400)
RTC devices could provide battery-backed memory that can be used for
storing the reboot mode magic value.

Add a new reboot-mode back-end that uses RTC to store the reboot-mode
magic value. The driver also supports both endianness modes.

Signed-off-by: Nandor Han <nandor.han@vaisala.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
arch/sandbox/dts/test.dts
configs/sandbox_defconfig
doc/device-tree-bindings/reboot-mode/reboot-mode-rtc.txt [new file with mode: 0644]
drivers/reboot-mode/Kconfig
drivers/reboot-mode/Makefile
drivers/reboot-mode/reboot-mode-rtc.c [new file with mode: 0644]
include/reboot-mode/reboot-mode-rtc.h [new file with mode: 0644]
test/dm/reboot-mode.c

index 2347f4cbe4f1e3cdd4ad125934e9a27f3a3afeb9..d5976318d1ccfb4b787284b91d9a3bc1df09cc54 100644 (file)
                mode-download = <0x03>;
        };
 
+       reboot_mode1: reboot-mode@14 {
+               compatible = "reboot-mode-rtc";
+               rtc = <&rtc_0>;
+               reg = <0x30 4>;
+               u-boot,env-variable = "bootstatus";
+               big-endian;
+               mode-test = <0x21969147>;
+               mode-download = <0x51939147>;
+       };
+
        audio: audio-codec {
                compatible = "sandbox,audio-codec";
                #sound-dai-cells = <1>;
index 6931b176ed9f2be270f1d220740e5340923e6453..1766dc018b648146e2a3757ff265b079eee85ae3 100644 (file)
@@ -292,3 +292,4 @@ CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
 CONFIG_DM_REBOOT_MODE=y
 CONFIG_DM_REBOOT_MODE_GPIO=y
+CONFIG_DM_REBOOT_MODE_RTC=y
diff --git a/doc/device-tree-bindings/reboot-mode/reboot-mode-rtc.txt b/doc/device-tree-bindings/reboot-mode/reboot-mode-rtc.txt
new file mode 100644 (file)
index 0000000..23aa12c
--- /dev/null
@@ -0,0 +1,22 @@
+RTC Reboot Mode Configuration
+
+Required Properties:
+- compatible: must be "reboot-mode-rtc".
+- rtc: reference to the rtc device used.
+- reg: start register and the number of bytes used. Maximum 4 bytes supported.
+- modes: list of properties that define the modes and associated unique ids.
+
+Optional Properties:
+- u-boot,env-variable: used to save the reboot mode (default: reboot-mode).
+- big-endian: if the magic value is stored in big-endian. (default: false).
+
+Example:
+       reboot-mode-rtc {
+               compatible = "reboot-mode-rtc";
+               rtc = <&rtc_0>;
+               reg = <0x14 4>;
+               u-boot,env-variable = "bootstatus";
+               big-endian;
+               modes-test = <0x21969147>;
+               modes-download = <0x51939147>;
+       };
index ff65e2031ae6b5d9213e61d50958c7416e16dc13..ac67bfcef623ae76eb800a2bf58b92074b926b37 100644 (file)
@@ -24,4 +24,13 @@ config DM_REBOOT_MODE_GPIO
                a device in a specific mode by using a GPIO that can be controlled
                outside U-Boot.
 
+config DM_REBOOT_MODE_RTC
+       bool "Use RTC as reboot mode backend"
+       depends on DM_REBOOT_MODE
+       default n
+       help
+               Use RTC non volatile memory to control the reboot mode. This will allow users to boot
+               a device in a specific mode by using a register(s) that can be controlled
+               outside U-Boot (e.g. Kernel).
+
 endmenu
index 04917be4f43f186f32996dd76855f02fec71fd9b..2c13780ced478527002b56b46b73760d5fad8baa 100644 (file)
@@ -6,3 +6,4 @@
 
 obj-$(CONFIG_DM_REBOOT_MODE) += reboot-mode-uclass.o
 obj-$(CONFIG_DM_REBOOT_MODE_GPIO) += reboot-mode-gpio.o
+obj-$(CONFIG_DM_REBOOT_MODE_RTC) += reboot-mode-rtc.o
diff --git a/drivers/reboot-mode/reboot-mode-rtc.c b/drivers/reboot-mode/reboot-mode-rtc.c
new file mode 100644 (file)
index 0000000..972d0cd
--- /dev/null
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c), Vaisala Oyj
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <dm/device_compat.h>
+#include <reboot-mode/reboot-mode-rtc.h>
+#include <reboot-mode/reboot-mode.h>
+#include <rtc.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static int reboot_mode_get(struct udevice *dev, u32 *buf)
+{
+       if (!buf)
+               return -EINVAL;
+
+       int ret;
+       u8 *val = (u8 *)buf;
+       struct reboot_mode_rtc_platdata *plat_data;
+
+       plat_data = dev_get_plat(dev);
+       if (!plat_data)
+               return -EINVAL;
+
+       for (int i = 0; i < plat_data->size; i++) {
+               ret = rtc_read8(plat_data->rtc, plat_data->addr + i);
+               if (ret < 0)
+                       return ret;
+
+               val[i] = ret;
+       }
+
+       if (plat_data->is_big_endian)
+               *buf = __be32_to_cpu(*buf);
+       else
+               *buf = __le32_to_cpu(*buf);
+
+       return 0;
+}
+
+static int reboot_mode_set(struct udevice *dev, u32 buf)
+{
+       int ret;
+       u8 *val;
+       struct reboot_mode_rtc_platdata *plat_data;
+
+       plat_data = dev_get_plat(dev);
+       if (!plat_data)
+               return -EINVAL;
+
+       if (plat_data->is_big_endian)
+               buf = __cpu_to_be32(buf);
+       else
+               buf = __cpu_to_le32(buf);
+
+       val = (u8 *)&buf;
+
+       for (int i = 0; i < plat_data->size; i++) {
+               ret = rtc_write8(plat_data->rtc, (plat_data->addr + i), val[i]);
+               if (ret < 0)
+                       return ret;
+       }
+
+       return 0;
+}
+
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+static int reboot_mode_ofdata_to_platdata(struct udevice *dev)
+{
+       struct ofnode_phandle_args phandle_args;
+       struct reboot_mode_rtc_platdata *plat_data;
+
+       plat_data = dev_get_plat(dev);
+       if (!plat_data)
+               return -EINVAL;
+
+       if (dev_read_phandle_with_args(dev, "rtc", NULL, 0, 0, &phandle_args)) {
+               dev_err(dev, "RTC device not specified\n");
+               return -ENOENT;
+       }
+
+       if (uclass_get_device_by_ofnode(UCLASS_RTC, phandle_args.node,
+                                       &plat_data->rtc)) {
+               dev_err(dev, "could not get the RTC device\n");
+               return -ENODEV;
+       }
+
+       plat_data->addr =
+               dev_read_addr_size_index(dev, 0, (fdt_size_t *)&plat_data->size);
+       if (plat_data->addr == FDT_ADDR_T_NONE) {
+               dev_err(dev, "Invalid RTC address\n");
+               return -EINVAL;
+       }
+       if (plat_data->size > sizeof(u32)) {
+               dev_err(dev, "Invalid reg size\n");
+               return -EINVAL;
+       }
+
+       plat_data->is_big_endian = ofnode_read_bool(dev_ofnode(dev), "big-endian");
+
+       return 0;
+}
+
+static const struct udevice_id reboot_mode_ids[] = {
+       { .compatible = "reboot-mode-rtc", 0 },
+       {}
+};
+#endif
+
+static const struct reboot_mode_ops reboot_mode_rtc_ops = {
+       .get = reboot_mode_get,
+       .set = reboot_mode_set,
+};
+
+U_BOOT_DRIVER(reboot_mode_rtc) = {
+       .name = "reboot-mode-rtc",
+       .id = UCLASS_REBOOT_MODE,
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+       .of_match = reboot_mode_ids,
+       .of_to_plat = reboot_mode_ofdata_to_platdata,
+#endif
+       .plat_auto = sizeof(struct reboot_mode_rtc_platdata),
+       .ops = &reboot_mode_rtc_ops,
+};
diff --git a/include/reboot-mode/reboot-mode-rtc.h b/include/reboot-mode/reboot-mode-rtc.h
new file mode 100644 (file)
index 0000000..3613678
--- /dev/null
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c), Vaisala Oyj
+ */
+
+#ifndef REBOOT_MODE_REBOOT_MODE_RTC_H_
+#define REBOOT_MODE_REBOOT_MODE_RTC_H_
+
+struct reboot_mode_rtc_platdata {
+       struct udevice *rtc;
+       bool is_big_endian;
+       int addr;
+       size_t size;
+};
+
+#endif /* REBOOT_MODE_REBOOT_MODE_RTC_H_ */
index 66aa4793f7bc7f89ad3fd35786929b0cf95c3c62..fbb9c3a5426a638704a5ce1bb828fcc6b7553975 100644 (file)
@@ -9,10 +9,13 @@
 #include <env.h>
 #include <log.h>
 #include <asm/gpio.h>
+#include <asm/rtc.h>
 #include <asm/test.h>
 #include <dm/test.h>
 #include <test/test.h>
 #include <test/ut.h>
+#include <rtc.h>
+#include <linux/byteorder/generic.h>
 
 static int dm_test_reboot_mode_gpio(struct unit_test_state *uts)
 {
@@ -40,3 +43,29 @@ static int dm_test_reboot_mode_gpio(struct unit_test_state *uts)
 
 DM_TEST(dm_test_reboot_mode_gpio,
        UT_TESTF_PROBE_TEST | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
+
+static int dm_test_reboot_mode_rtc(struct unit_test_state *uts)
+{
+       struct udevice *rtc_dev;
+       struct udevice *rm_dev;
+       u32 read_val;
+       u32 test_magic_val = cpu_to_be32(0x21969147);
+
+       uclass_get_device_by_name(UCLASS_RTC, "rtc@43",
+                                 &rtc_dev);
+       dm_rtc_write(rtc_dev, REG_AUX0, (u8 *)&test_magic_val, 4);
+
+       ut_assertok(uclass_get_device_by_name(UCLASS_REBOOT_MODE,
+                                             "reboot-mode@14", &rm_dev));
+       ut_assertok(dm_reboot_mode_update(rm_dev));
+
+       ut_asserteq_str("test", env_get("bootstatus"));
+
+       dm_rtc_read(rtc_dev, REG_AUX0, (u8 *)&read_val, 4);
+       ut_asserteq(read_val, 0);
+
+       return 0;
+}
+
+DM_TEST(dm_test_reboot_mode_rtc,
+       UT_TESTF_PROBE_TEST | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);