[PATCH v2 0/2] rtc: provide an emulated RTC

On a board without hardware clock this software real time clock can be used. The build time is used to initialize the RTC. So you will have to adjust the time either manually using the 'date' command or use the 'sntp' to update the RTC with the time from a network time server. See CONFIG_CMD_SNTP and CONFIG_BOOTP_NTPSERVER. The RTC time is advanced according to CPU ticks.
v2: more elaborate Kconfig message adjust device name properties use build time as initial time
Heinrich Schuchardt (2): Makefile: provide constant with seconds since epoch rtc: provide an emulated RTC
MAINTAINERS | 1 + Makefile | 2 ++ drivers/rtc/Kconfig | 11 ++++++ drivers/rtc/Makefile | 1 + drivers/rtc/emul_rtc.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 drivers/rtc/emul_rtc.c
-- 2.28.0

Provide a constant U_BOOT_EPOCH with the number of seconds since 1970-01-01. This constant can be used to initialize a software real time clock until it is updated via the 'sntp' command.
Signed-off-by: Heinrich Schuchardt xypron.glpk@gmx.de --- v2: new patch --- Makefile | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/Makefile b/Makefile index 5a0ef18668..c21729cf52 100644 --- a/Makefile +++ b/Makefile @@ -1870,6 +1870,7 @@ define filechk_timestamp.h LC_ALL=C $${DATE} -u -d "$${SOURCE_DATE}" +'#define U_BOOT_TZ "%z"'; \ LC_ALL=C $${DATE} -u -d "$${SOURCE_DATE}" +'#define U_BOOT_DMI_DATE "%m/%d/%Y"'; \ LC_ALL=C $${DATE} -u -d "$${SOURCE_DATE}" +'#define U_BOOT_BUILD_DATE 0x%Y%m%d'; \ + LC_ALL=C $${DATE} -u -d "$${SOURCE_DATE}" +'#define U_BOOT_EPOCH %s'; \ else \ return 42; \ fi; \ @@ -1879,6 +1880,7 @@ define filechk_timestamp.h LC_ALL=C date +'#define U_BOOT_TZ "%z"'; \ LC_ALL=C date +'#define U_BOOT_DMI_DATE "%m/%d/%Y"'; \ LC_ALL=C date +'#define U_BOOT_BUILD_DATE 0x%Y%m%d'; \ + LC_ALL=C date +'#define U_BOOT_EPOCH %s'; \ fi) endef
-- 2.28.0

On Sun, 25 Oct 2020 at 01:13, Heinrich Schuchardt xypron.glpk@gmx.de wrote:
Provide a constant U_BOOT_EPOCH with the number of seconds since 1970-01-01. This constant can be used to initialize a software real time clock until it is updated via the 'sntp' command.
Signed-off-by: Heinrich Schuchardt xypron.glpk@gmx.de
v2: new patch
Makefile | 2 ++ 1 file changed, 2 insertions(+)
Reviewed-by: Simon Glass sjg@chromium.org

On a board without hardware clock this software real time clock can be used. The build time is used to initialize the RTC. So you will have to adjust the time either manually using the 'date' command or use the 'sntp' to update the RTC with the time from a network time server. See CONFIG_CMD_SNTP and CONFIG_BOOTP_NTPSERVER. The RTC time is advanced according to CPU ticks.
Signed-off-by: Heinrich Schuchardt xypron.glpk@gmx.de --- v2: more elaborate Kconfig message adjust device name properties use build time as initial time --- MAINTAINERS | 1 + drivers/rtc/Kconfig | 11 ++++++ drivers/rtc/Makefile | 1 + drivers/rtc/emul_rtc.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 drivers/rtc/emul_rtc.c
diff --git a/MAINTAINERS b/MAINTAINERS index fc4fad46ee..a98e0c5b76 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -678,6 +678,7 @@ S: Maintained T: git https://gitlab.denx.de/u-boot/custodians/u-boot-efi.git F: doc/api/efi.rst F: doc/uefi/* +F: drivers/rtc/emul_rtc.c F: include/capitalization.h F: include/charset.h F: include/cp1250.h diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 63662001c2..d06d272e14 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -63,6 +63,17 @@ config RTC_DS3232 Support for Dallas Semiconductor (now Maxim) DS3232 compatible Real Time Clock devices.
+config RTC_EMULATION + bool "Enable emulated RTC" + depends on DM_RTC + help + On a board without hardware clock this software real time clock can be + used. The build time is used to initialize the RTC. So you will have + to adjust the time either manually using the 'date' command or use + the 'sntp' to update the RTC with the time from a network time server. + See CONFIG_CMD_SNTP and CONFIG_BOOTP_NTPSERVER. The RTC time is + advanced according to CPU ticks. + config RTC_ISL1208 bool "Enable ISL1208 driver" depends on DM_RTC diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 12eb449583..ef66dc4bf0 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -22,6 +22,7 @@ obj-$(CONFIG_RTC_DS164x) += ds164x.o obj-$(CONFIG_RTC_DS174x) += ds174x.o obj-$(CONFIG_RTC_DS3231) += ds3231.o obj-$(CONFIG_RTC_DS3232) += ds3232.o +obj-$(CONFIG_RTC_EMULATION) += emul_rtc.o obj-$(CONFIG_RTC_FTRTC010) += ftrtc010.o obj-$(CONFIG_SANDBOX) += i2c_rtc_emul.o obj-$(CONFIG_RTC_IMXDI) += imxdi.o diff --git a/drivers/rtc/emul_rtc.c b/drivers/rtc/emul_rtc.c new file mode 100644 index 0000000000..c98c24bbb3 --- /dev/null +++ b/drivers/rtc/emul_rtc.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2020, Heinrich Schuchardt xypron.glpk@gmx.de + * + * This driver emulates a real time clock based on timer ticks. + */ + +#include <common.h> +#include <div64.h> +#include <dm.h> +#include <generated/timestamp_autogenerated.h> +#include <rtc.h> + +/** + * struct emul_rtc - private data for emulated RTC driver + */ +struct emul_rtc { + /** + * @offset_us: microseconds from 1970-01-01 to timer_get_us() base + */ + u64 offset_us; + /** + * @isdst: daylight saving time + */ + int isdst; +}; + +static int emul_rtc_get(struct udevice *dev, struct rtc_time *time) +{ + struct emul_rtc *priv = dev_get_priv(dev); + u64 now; + + if (!priv->offset_us) { + /* Use the build date as initial time */ + priv->offset_us = U_BOOT_EPOCH * 1000000ULL - timer_get_us(); + priv->isdst = -1; + } + + now = timer_get_us() + priv->offset_us; + do_div(now, 1000000); + rtc_to_tm(now, time); + time->tm_isdst = priv->isdst; + + return 0; +} + +static int emul_rtc_set(struct udevice *dev, const struct rtc_time *time) +{ + struct emul_rtc *priv = dev_get_priv(dev); + + if (time->tm_year < 1970) + return -EINVAL; + + priv->offset_us = rtc_mktime(time) * 1000000ULL - timer_get_us(); + + if (time->tm_isdst > 0) + priv->isdst = 1; + else if (time->tm_isdst < 0) + priv->isdst = -1; + else + priv->isdst = 0; + + return 0; +} + +static const struct rtc_ops emul_rtc_ops = { + .get = emul_rtc_get, + .set = emul_rtc_set, +}; + +U_BOOT_DRIVER(rtc_emul) = { + .name = "rtc_emul", + .id = UCLASS_RTC, + .ops = &emul_rtc_ops, + .priv_auto_alloc_size = sizeof(struct emul_rtc), +}; + +U_BOOT_DEVICE(rtc_emul) = { + .name = "rtc_emul", +}; -- 2.28.0

On Sun, Oct 25, 2020 at 7:13 AM Heinrich Schuchardt xypron.glpk@gmx.de wrote:
On a board without hardware clock this software real time clock can be used. The build time is used to initialize the RTC. So you will have to adjust the time either manually using the 'date' command or use the 'sntp' to update the RTC with the time from a network time server. See CONFIG_CMD_SNTP and CONFIG_BOOTP_NTPSERVER. The RTC time is advanced according to CPU ticks.
Signed-off-by: Heinrich Schuchardt xypron.glpk@gmx.de
Looks good to me, and tested on RockPro64 w/ UEFI SCT.
Tested-by: Grant Likely grant.likely@arm.com
g.
v2: more elaborate Kconfig message adjust device name properties use build time as initial time
MAINTAINERS | 1 + drivers/rtc/Kconfig | 11 ++++++ drivers/rtc/Makefile | 1 + drivers/rtc/emul_rtc.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 drivers/rtc/emul_rtc.c
diff --git a/MAINTAINERS b/MAINTAINERS index fc4fad46ee..a98e0c5b76 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -678,6 +678,7 @@ S: Maintained T: git https://gitlab.denx.de/u-boot/custodians/u-boot-efi.git F: doc/api/efi.rst F: doc/uefi/* +F: drivers/rtc/emul_rtc.c F: include/capitalization.h F: include/charset.h F: include/cp1250.h diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 63662001c2..d06d272e14 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -63,6 +63,17 @@ config RTC_DS3232 Support for Dallas Semiconductor (now Maxim) DS3232 compatible Real Time Clock devices.
+config RTC_EMULATION
bool "Enable emulated RTC"
depends on DM_RTC
help
On a board without hardware clock this software real time clock can be
used. The build time is used to initialize the RTC. So you will have
to adjust the time either manually using the 'date' command or use
the 'sntp' to update the RTC with the time from a network time server.
See CONFIG_CMD_SNTP and CONFIG_BOOTP_NTPSERVER. The RTC time is
advanced according to CPU ticks.
config RTC_ISL1208 bool "Enable ISL1208 driver" depends on DM_RTC diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 12eb449583..ef66dc4bf0 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -22,6 +22,7 @@ obj-$(CONFIG_RTC_DS164x) += ds164x.o obj-$(CONFIG_RTC_DS174x) += ds174x.o obj-$(CONFIG_RTC_DS3231) += ds3231.o obj-$(CONFIG_RTC_DS3232) += ds3232.o +obj-$(CONFIG_RTC_EMULATION) += emul_rtc.o obj-$(CONFIG_RTC_FTRTC010) += ftrtc010.o obj-$(CONFIG_SANDBOX) += i2c_rtc_emul.o obj-$(CONFIG_RTC_IMXDI) += imxdi.o diff --git a/drivers/rtc/emul_rtc.c b/drivers/rtc/emul_rtc.c new file mode 100644 index 0000000000..c98c24bbb3 --- /dev/null +++ b/drivers/rtc/emul_rtc.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0+ +/*
- Copyright 2020, Heinrich Schuchardt xypron.glpk@gmx.de
- This driver emulates a real time clock based on timer ticks.
- */
+#include <common.h> +#include <div64.h> +#include <dm.h> +#include <generated/timestamp_autogenerated.h> +#include <rtc.h>
+/**
- struct emul_rtc - private data for emulated RTC driver
- */
+struct emul_rtc {
/**
* @offset_us: microseconds from 1970-01-01 to timer_get_us() base
*/
u64 offset_us;
/**
* @isdst: daylight saving time
*/
int isdst;
+};
+static int emul_rtc_get(struct udevice *dev, struct rtc_time *time) +{
struct emul_rtc *priv = dev_get_priv(dev);
u64 now;
if (!priv->offset_us) {
/* Use the build date as initial time */
priv->offset_us = U_BOOT_EPOCH * 1000000ULL - timer_get_us();
priv->isdst = -1;
}
now = timer_get_us() + priv->offset_us;
do_div(now, 1000000);
rtc_to_tm(now, time);
time->tm_isdst = priv->isdst;
return 0;
+}
+static int emul_rtc_set(struct udevice *dev, const struct rtc_time *time) +{
struct emul_rtc *priv = dev_get_priv(dev);
if (time->tm_year < 1970)
return -EINVAL;
priv->offset_us = rtc_mktime(time) * 1000000ULL - timer_get_us();
if (time->tm_isdst > 0)
priv->isdst = 1;
else if (time->tm_isdst < 0)
priv->isdst = -1;
else
priv->isdst = 0;
return 0;
+}
+static const struct rtc_ops emul_rtc_ops = {
.get = emul_rtc_get,
.set = emul_rtc_set,
+};
+U_BOOT_DRIVER(rtc_emul) = {
.name = "rtc_emul",
.id = UCLASS_RTC,
.ops = &emul_rtc_ops,
.priv_auto_alloc_size = sizeof(struct emul_rtc),
+};
+U_BOOT_DEVICE(rtc_emul) = {
.name = "rtc_emul",
+};
2.28.0

Hi Heinrich,
On Sun, 25 Oct 2020 at 01:13, Heinrich Schuchardt xypron.glpk@gmx.de wrote:
On a board without hardware clock this software real time clock can be used. The build time is used to initialize the RTC. So you will have to adjust the time either manually using the 'date' command or use the 'sntp' to update the RTC with the time from a network time server. See CONFIG_CMD_SNTP and CONFIG_BOOTP_NTPSERVER. The RTC time is advanced according to CPU ticks.
Signed-off-by: Heinrich Schuchardt xypron.glpk@gmx.de
v2: more elaborate Kconfig message adjust device name properties use build time as initial time
MAINTAINERS | 1 + drivers/rtc/Kconfig | 11 ++++++ drivers/rtc/Makefile | 1 + drivers/rtc/emul_rtc.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 drivers/rtc/emul_rtc.c
Reviewed-by: Simon Glass sjg@chromium.org
diff --git a/MAINTAINERS b/MAINTAINERS index fc4fad46ee..a98e0c5b76 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -678,6 +678,7 @@ S: Maintained T: git https://gitlab.denx.de/u-boot/custodians/u-boot-efi.git F: doc/api/efi.rst F: doc/uefi/* +F: drivers/rtc/emul_rtc.c F: include/capitalization.h F: include/charset.h F: include/cp1250.h diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 63662001c2..d06d272e14 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -63,6 +63,17 @@ config RTC_DS3232 Support for Dallas Semiconductor (now Maxim) DS3232 compatible Real Time Clock devices.
+config RTC_EMULATION
bool "Enable emulated RTC"
depends on DM_RTC
help
On a board without hardware clock this software real time clock can be
used. The build time is used to initialize the RTC. So you will have
to adjust the time either manually using the 'date' command or use
the 'sntp' to update the RTC with the time from a network time server.
See CONFIG_CMD_SNTP and CONFIG_BOOTP_NTPSERVER. The RTC time is
advanced according to CPU ticks.
config RTC_ISL1208 bool "Enable ISL1208 driver" depends on DM_RTC diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 12eb449583..ef66dc4bf0 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -22,6 +22,7 @@ obj-$(CONFIG_RTC_DS164x) += ds164x.o obj-$(CONFIG_RTC_DS174x) += ds174x.o obj-$(CONFIG_RTC_DS3231) += ds3231.o obj-$(CONFIG_RTC_DS3232) += ds3232.o +obj-$(CONFIG_RTC_EMULATION) += emul_rtc.o obj-$(CONFIG_RTC_FTRTC010) += ftrtc010.o obj-$(CONFIG_SANDBOX) += i2c_rtc_emul.o obj-$(CONFIG_RTC_IMXDI) += imxdi.o diff --git a/drivers/rtc/emul_rtc.c b/drivers/rtc/emul_rtc.c new file mode 100644 index 0000000000..c98c24bbb3 --- /dev/null +++ b/drivers/rtc/emul_rtc.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0+ +/*
- Copyright 2020, Heinrich Schuchardt xypron.glpk@gmx.de
- This driver emulates a real time clock based on timer ticks.
- */
+#include <common.h> +#include <div64.h> +#include <dm.h> +#include <generated/timestamp_autogenerated.h>
put at end
+#include <rtc.h>
+/**
- struct emul_rtc - private data for emulated RTC driver
- */
+struct emul_rtc {
/**
* @offset_us: microseconds from 1970-01-01 to timer_get_us() base
*/
u64 offset_us;
/**
* @isdst: daylight saving time
*/
why not:
/** @.... */
int isdst;
+};

Can I suggest as a future enhancement adding an option to also be able to read U_BOOT_EPOCH from env? Userspace tools could write the time on shutdown (like fakehwclock). Ok, adding `date xxxxx` to the start script could work too, but it seems messy.
Pablo.
On 25/10/20 04:13, Heinrich Schuchardt wrote:
On a board without hardware clock this software real time clock can be used. The build time is used to initialize the RTC. So you will have to adjust the time either manually using the 'date' command or use the 'sntp' to update the RTC with the time from a network time server. See CONFIG_CMD_SNTP and CONFIG_BOOTP_NTPSERVER. The RTC time is advanced according to CPU ticks.
v2: more elaborate Kconfig message adjust device name properties use build time as initial time
Heinrich Schuchardt (2): Makefile: provide constant with seconds since epoch rtc: provide an emulated RTC
MAINTAINERS | 1 + Makefile | 2 ++ drivers/rtc/Kconfig | 11 ++++++ drivers/rtc/Makefile | 1 + drivers/rtc/emul_rtc.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 drivers/rtc/emul_rtc.c
-- 2.28.0

On 10/29/20 11:27 AM, Pablo Sebastián Greco wrote:
Can I suggest as a future enhancement adding an option to also be able to read U_BOOT_EPOCH from env? Userspace tools could write the time on shutdown (like fakehwclock).
Thank you for your suggestion.
What does fakehwclock relate to?
Ok, adding `date xxxxx` to the start script could work too, but it seems messy.
If you have network, you should use CONFIG_CMD_SNTP=Y and CONFIG_BOOTP_NTPSERVER=y and add "setenv autoload no && dhcp && sntp" to your script.
This is why I submitted the following patch:
net: sntp: remove CONFIG_TIMESTAMP constraint https://lists.denx.de/pipermail/u-boot/2020-October/430445.html
If we want to follow you idea, we would have a probe() and a remove() method to the driver for managing the environment variable.
One problem with saving the time on shutdown to the environment is that save_env() always saves the complete environment and not a single variable. Saving the complete environment may save changes that you do not want to persist.
So a complete solution will require:
* Create a function to save a single environment variable selectively. This will be the part with the biggest effort. * Implement the probe() and remove() methods * Add a Kconfig option to enable the usage of the environment variable.
But is this worth the effort if you have SNTP?
Best regards
Heinrich
On 25/10/20 04:13, Heinrich Schuchardt wrote:
On a board without hardware clock this software real time clock can be used. The build time is used to initialize the RTC. So you will have to adjust the time either manually using the 'date' command or use the 'sntp' to update the RTC with the time from a network time server. See CONFIG_CMD_SNTP and CONFIG_BOOTP_NTPSERVER. The RTC time is advanced according to CPU ticks.
v2: more elaborate Kconfig message adjust device name properties use build time as initial time
Heinrich Schuchardt (2): Makefile: provide constant with seconds since epoch rtc: provide an emulated RTC
MAINTAINERS | 1 + Makefile | 2 ++ drivers/rtc/Kconfig | 11 ++++++ drivers/rtc/Makefile | 1 + drivers/rtc/emul_rtc.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 drivers/rtc/emul_rtc.c
-- 2.28.0

On 29/10/20 08:36, Heinrich Schuchardt wrote:
On 10/29/20 11:27 AM, Pablo Sebastián Greco wrote:
Can I suggest as a future enhancement adding an option to also be able to read U_BOOT_EPOCH from env? Userspace tools could write the time on shutdown (like fakehwclock).
Thank you for your suggestion.
What does fakehwclock relate to?
fake-hwclock is a userspace linux tool that saves the current time in a file, in case ntp is not available at reboot time, so the clock will not be perfect, but it may be "good enough"
Ok, adding `date xxxxx` to the start script could work too, but it seems messy.
If you have network, you should use CONFIG_CMD_SNTP=Y and CONFIG_BOOTP_NTPSERVER=y and add "setenv autoload no && dhcp && sntp" to your script.
This is why I submitted the following patch:
net: sntp: remove CONFIG_TIMESTAMP constraint https://lists.denx.de/pipermail/u-boot/2020-October/430445.html
If we want to follow you idea, we would have a probe() and a remove() method to the driver for managing the environment variable.
One problem with saving the time on shutdown to the environment is that save_env() always saves the complete environment and not a single variable. Saving the complete environment may save changes that you do not want to persist.
So a complete solution will require:
- Create a function to save a single environment variable selectively. This will be the part with the biggest effort.
That would be nice to have, even if not related to this.
- Implement the probe() and remove() methods
- Add a Kconfig option to enable the usage of the environment variable.
But is this worth the effort if you have SNTP?
If SNTP is available, I agree that it makes no sense. but may not always be the case (as noted before), and some programs don't react too kindly to the clock jumping ahead several years during boot, so it would be nice to have a "good enough" clock before the kernel takes over, Also, as was mentioned in other parts of the thread, some devices may be too close to the size limit, but I'd happily exchange network in uboot for this driver. I know the idea is a bit wacky, but thought it could be useful ;)
Best regards
Heinrich
On 25/10/20 04:13, Heinrich Schuchardt wrote:
On a board without hardware clock this software real time clock can be used. The build time is used to initialize the RTC. So you will have to adjust the time either manually using the 'date' command or use the 'sntp' to update the RTC with the time from a network time server. See CONFIG_CMD_SNTP and CONFIG_BOOTP_NTPSERVER. The RTC time is advanced according to CPU ticks.
v2: more elaborate Kconfig message adjust device name properties use build time as initial time
Heinrich Schuchardt (2): Makefile: provide constant with seconds since epoch rtc: provide an emulated RTC
MAINTAINERS | 1 + Makefile | 2 ++ drivers/rtc/Kconfig | 11 ++++++ drivers/rtc/Makefile | 1 + drivers/rtc/emul_rtc.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 drivers/rtc/emul_rtc.c
-- 2.28.0
Pablo
participants (4)
-
Grant Likely
-
Heinrich Schuchardt
-
Pablo Sebastián Greco
-
Simon Glass