1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
5 * This driver emulates a real time clock based on timer ticks.
12 #include <generated/timestamp_autogenerated.h>
16 * struct emul_rtc - private data for emulated RTC driver
20 * @offset_us: microseconds from 1970-01-01 to timer_get_us() base
24 * @isdst: daylight saving time
29 static int emul_rtc_get(struct udevice *dev, struct rtc_time *time)
31 struct emul_rtc *priv = dev_get_priv(dev);
34 now = timer_get_us() + priv->offset_us;
37 time->tm_isdst = priv->isdst;
42 static int emul_rtc_set(struct udevice *dev, const struct rtc_time *time)
44 struct emul_rtc *priv = dev_get_priv(dev);
46 if (time->tm_year < 1970)
49 priv->offset_us = rtc_mktime(time) * 1000000ULL - timer_get_us();
51 if (time->tm_isdst > 0)
53 else if (time->tm_isdst < 0)
61 int emul_rtc_probe(struct udevice *dev)
63 struct emul_rtc *priv = dev_get_priv(dev);
64 const char *epoch_str;
67 epoch_str = env_get("rtc_emul_epoch");
70 epoch = simple_strtoull(epoch_str, NULL, 10);
72 /* Use the build date as initial time */
75 priv->offset_us = epoch * 1000000ULL - timer_get_us();
81 static const struct rtc_ops emul_rtc_ops = {
86 U_BOOT_DRIVER(rtc_emul) = {
90 .probe = emul_rtc_probe,
91 .priv_auto_alloc_size = sizeof(struct emul_rtc),
94 U_BOOT_DEVICE(rtc_emul) = {