1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c), Vaisala Oyj
8 #include <dm/device_compat.h>
9 #include <reboot-mode/reboot-mode-rtc.h>
10 #include <reboot-mode/reboot-mode.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 static int reboot_mode_get(struct udevice *dev, u32 *buf)
22 struct reboot_mode_rtc_platdata *plat_data;
24 plat_data = dev_get_plat(dev);
28 for (int i = 0; i < plat_data->size; i++) {
29 ret = rtc_read8(plat_data->rtc, plat_data->addr + i);
36 if (plat_data->is_big_endian)
37 *buf = __be32_to_cpu(*buf);
39 *buf = __le32_to_cpu(*buf);
44 static int reboot_mode_set(struct udevice *dev, u32 buf)
48 struct reboot_mode_rtc_platdata *plat_data;
50 plat_data = dev_get_plat(dev);
54 if (plat_data->is_big_endian)
55 buf = __cpu_to_be32(buf);
57 buf = __cpu_to_le32(buf);
61 for (int i = 0; i < plat_data->size; i++) {
62 ret = rtc_write8(plat_data->rtc, (plat_data->addr + i), val[i]);
70 #if CONFIG_IS_ENABLED(OF_CONTROL)
71 static int reboot_mode_ofdata_to_platdata(struct udevice *dev)
73 struct ofnode_phandle_args phandle_args;
74 struct reboot_mode_rtc_platdata *plat_data;
76 plat_data = dev_get_plat(dev);
80 if (dev_read_phandle_with_args(dev, "rtc", NULL, 0, 0, &phandle_args)) {
81 dev_err(dev, "RTC device not specified\n");
85 if (uclass_get_device_by_ofnode(UCLASS_RTC, phandle_args.node,
87 dev_err(dev, "could not get the RTC device\n");
92 dev_read_addr_size_index(dev, 0, (fdt_size_t *)&plat_data->size);
93 if (plat_data->addr == FDT_ADDR_T_NONE) {
94 dev_err(dev, "Invalid RTC address\n");
97 if (plat_data->size > sizeof(u32)) {
98 dev_err(dev, "Invalid reg size\n");
102 plat_data->is_big_endian = ofnode_read_bool(dev_ofnode(dev), "big-endian");
107 static const struct udevice_id reboot_mode_ids[] = {
108 { .compatible = "reboot-mode-rtc", 0 },
113 static const struct reboot_mode_ops reboot_mode_rtc_ops = {
114 .get = reboot_mode_get,
115 .set = reboot_mode_set,
118 U_BOOT_DRIVER(reboot_mode_rtc) = {
119 .name = "reboot-mode-rtc",
120 .id = UCLASS_REBOOT_MODE,
121 #if CONFIG_IS_ENABLED(OF_CONTROL)
122 .of_match = reboot_mode_ids,
123 .of_to_plat = reboot_mode_ofdata_to_platdata,
125 .plat_auto = sizeof(struct reboot_mode_rtc_platdata),
126 .ops = &reboot_mode_rtc_ops,