* [PATCH 0/2] rtc: driver for Goldfish RTC
@ 2023-12-30 23:52 Heinrich Schuchardt
2023-12-30 23:53 ` [PATCH 1/2] " Heinrich Schuchardt
2023-12-30 23:53 ` [PATCH 2/2] riscv: qemu: imply GOLDFISH_RTC Heinrich Schuchardt
0 siblings, 2 replies; 3+ messages in thread
From: Heinrich Schuchardt @ 2023-12-30 23:52 UTC (permalink / raw)
To: Tom Rini
Cc: Bin Meng, Simon Glass, Sergei Antonov, Chris Packham, u-boot,
Heinrich Schuchardt
The Goldfish RTC is a virtual device which may be supplied by QEMU.
It is enabled by default on QEMU's RISC-V virt machine.
Provide a driver and enable it by default on RISC-V QEMU.
Heinrich Schuchardt (2):
rtc: driver for Goldfish RTC
riscv: qemu: imply GOLDFISH_RTC
board/emulation/qemu-riscv/Kconfig | 2 +
drivers/rtc/Kconfig | 7 ++
drivers/rtc/Makefile | 1 +
drivers/rtc/goldfish_rtc.c | 105 +++++++++++++++++++++++++++++
4 files changed, 115 insertions(+)
create mode 100644 drivers/rtc/goldfish_rtc.c
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/2] rtc: driver for Goldfish RTC
2023-12-30 23:52 [PATCH 0/2] rtc: driver for Goldfish RTC Heinrich Schuchardt
@ 2023-12-30 23:53 ` Heinrich Schuchardt
2023-12-30 23:53 ` [PATCH 2/2] riscv: qemu: imply GOLDFISH_RTC Heinrich Schuchardt
1 sibling, 0 replies; 3+ messages in thread
From: Heinrich Schuchardt @ 2023-12-30 23:53 UTC (permalink / raw)
To: Tom Rini
Cc: Bin Meng, Simon Glass, Sergei Antonov, Chris Packham, u-boot,
Heinrich Schuchardt
The Goldfish RTC is a virtual device which may be supplied by QEMU.
It is enabled by default on QEMU's RISC-V virt machine.
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
---
drivers/rtc/Kconfig | 7 +++
drivers/rtc/Makefile | 1 +
drivers/rtc/goldfish_rtc.c | 105 +++++++++++++++++++++++++++++++++++++
3 files changed, 113 insertions(+)
create mode 100644 drivers/rtc/goldfish_rtc.c
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 23173139e0..7fc53a6d61 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -122,6 +122,13 @@ config RTC_EMULATION
CONFIG_BOOTP_NTPSERVER. The RTC time is advanced according to CPU
ticks.
+config RTC_GOLDFISH
+ bool "Enable Goldfish driver"
+ depends on DM_RTC
+ help
+ The Goldfish RTC is a virtual device which may be supplied by QEMU.
+ It is enabled by default on QEMU's RISC-V virt machine.
+
config RTC_ISL1208
bool "Enable ISL1208 driver"
depends on DM_RTC
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 308fab8da9..03a424c31a 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -15,6 +15,7 @@ obj-$(CONFIG_RTC_DS1374) += ds1374.o
obj-$(CONFIG_RTC_DS3231) += ds3231.o
obj-$(CONFIG_RTC_DS3232) += ds3232.o
obj-$(CONFIG_RTC_EMULATION) += emul_rtc.o
+obj-$(CONFIG_RTC_GOLDFISH) += goldfish_rtc.o
obj-$(CONFIG_RTC_HT1380) += ht1380.o
obj-$(CONFIG_$(SPL_TPL_)RTC_SANDBOX) += i2c_rtc_emul.o
obj-$(CONFIG_RTC_ISL1208) += isl1208.o
diff --git a/drivers/rtc/goldfish_rtc.c b/drivers/rtc/goldfish_rtc.c
new file mode 100644
index 0000000000..99bbe9eaaf
--- /dev/null
+++ b/drivers/rtc/goldfish_rtc.c
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2023, Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
+ *
+ * This driver emulates a real time clock based on timer ticks.
+ */
+
+#include <div64.h>
+#include <dm.h>
+#include <mapmem.h>
+#include <rtc.h>
+#include <linux/io.h>
+
+/**
+ * struct goldfish_rtc - private data for RTC driver
+ */
+struct goldfish_rtc {
+ /**
+ * @base: base address for register file
+ */
+ void __iomem *base;
+ /**
+ * @isdst: daylight saving time
+ */
+ int isdst;
+};
+
+/* Register offsets */
+#define GOLDFISH_TIME_LOW 0x00
+#define GOLDFISH_TIME_HIGH 0x04
+
+static int goldfish_rtc_get(struct udevice *dev, struct rtc_time *time)
+{
+ struct goldfish_rtc *priv = dev_get_priv(dev);
+ void __iomem *base = priv->base;
+ u64 time_high;
+ u64 time_low;
+ u64 now;
+
+ time_low = ioread32(base + GOLDFISH_TIME_LOW);
+ time_high = ioread32(base + GOLDFISH_TIME_HIGH);
+ now = (time_high << 32) | time_low;
+
+ do_div(now, 1000000000U);
+
+ rtc_to_tm(now, time);
+ time->tm_isdst = priv->isdst;
+
+ return 0;
+}
+
+static int goldfish_rtc_set(struct udevice *dev, const struct rtc_time *time)
+{
+ struct goldfish_rtc *priv = dev_get_priv(dev);
+ void __iomem *base = priv->base;
+ u64 now;
+
+ if (time->tm_year < 1970)
+ return -EINVAL;
+
+ now = rtc_mktime(time) * 1000000000ULL;
+ iowrite32(now >> 32, base + GOLDFISH_TIME_HIGH);
+ iowrite32(now, base + GOLDFISH_TIME_LOW);
+
+ if (time->tm_isdst > 0)
+ priv->isdst = 1;
+ else if (time->tm_isdst < 0)
+ priv->isdst = -1;
+ else
+ priv->isdst = 0;
+
+ return 0;
+}
+
+int goldfish_rtc_probe(struct udevice *dev)
+{
+ struct goldfish_rtc *priv = dev_get_priv(dev);
+ fdt_addr_t addr;
+
+ addr = dev_read_addr(dev);
+ if (addr == FDT_ADDR_T_NONE)
+ return -EINVAL;
+ priv->base = map_sysmem(addr, 0x20);
+
+ return 0;
+}
+
+static const struct rtc_ops goldfish_rtc_ops = {
+ .get = goldfish_rtc_get,
+ .set = goldfish_rtc_set,
+};
+
+static const struct udevice_id goldfish_rtc_of_match[] = {
+ { .compatible = "google,goldfish-rtc", },
+ {},
+};
+
+U_BOOT_DRIVER(rtc_goldfish) = {
+ .name = "rtc_goldfish",
+ .id = UCLASS_RTC,
+ .ops = &goldfish_rtc_ops,
+ .probe = goldfish_rtc_probe,
+ .of_match = goldfish_rtc_of_match,
+ .priv_auto = sizeof(struct goldfish_rtc),
+};
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] riscv: qemu: imply GOLDFISH_RTC
2023-12-30 23:52 [PATCH 0/2] rtc: driver for Goldfish RTC Heinrich Schuchardt
2023-12-30 23:53 ` [PATCH 1/2] " Heinrich Schuchardt
@ 2023-12-30 23:53 ` Heinrich Schuchardt
1 sibling, 0 replies; 3+ messages in thread
From: Heinrich Schuchardt @ 2023-12-30 23:53 UTC (permalink / raw)
To: Tom Rini
Cc: Bin Meng, Simon Glass, Sergei Antonov, Chris Packham, u-boot,
Heinrich Schuchardt
QEMU's virt board provides an emulated Goldfish RTC. Enable the driver by
default.
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
---
board/emulation/qemu-riscv/Kconfig | 2 ++
1 file changed, 2 insertions(+)
diff --git a/board/emulation/qemu-riscv/Kconfig b/board/emulation/qemu-riscv/Kconfig
index cdd0d0d95f..e15d23c99b 100644
--- a/board/emulation/qemu-riscv/Kconfig
+++ b/board/emulation/qemu-riscv/Kconfig
@@ -58,6 +58,8 @@ config BOARD_SPECIFIC_OPTIONS # dummy
imply NVME_PCI
imply PCIE_ECAM_GENERIC
imply DM_RNG
+ imply DM_RTC
+ imply RTC_GOLDFISH
imply SCSI
imply SYS_NS16550
imply SIFIVE_SERIAL
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-12-30 23:53 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-30 23:52 [PATCH 0/2] rtc: driver for Goldfish RTC Heinrich Schuchardt
2023-12-30 23:53 ` [PATCH 1/2] " Heinrich Schuchardt
2023-12-30 23:53 ` [PATCH 2/2] riscv: qemu: imply GOLDFISH_RTC Heinrich Schuchardt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).