Skip to contentSkip to conversation

Add and connect the ZynqMP RTC

PATCH Superseded Alistair Francis 2 participants 9 messages Jan 23, 2018 – Feb 13, 2018 Arm/Zynq architecture

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover Add and connect the ZynqMP RTC

V5:
- Recalculate tick_offset after migration

V4:
- Use the RegisterAccessInfo .unimp functionality

V3:
- Store an offset value
- Use mktimegm()
- Log unimplemented writes

V2:
- Delete unused realise function
- Add cover letter
- Convert DB_PRINT() macro to trace

Alistair Francis (3):
xlnx-zynqmp-rtc: Initial commit
xlnx-zynqmp-rtc: Add basic time support
xlnx-zynqmp: Connect the RTC device

 include/hw/arm/xlnx-zynqmp.h       |   2 +
 include/hw/timer/xlnx-zynqmp-rtc.h |  87 ++++++++++++
 hw/arm/xlnx-zynqmp.c               |  14 ++
 hw/timer/xlnx-zynqmp-rtc.c         | 266 +++++++++++++++++++++++++++++++++++++
 hw/timer/Makefile.objs             |   1 +
 hw/timer/trace-events              |   3 +
 6 files changed, 373 insertions(+)
 create mode 100644 include/hw/timer/xlnx-zynqmp-rtc.h
 create mode 100644 hw/timer/xlnx-zynqmp-rtc.c
Signature · 1 line
2.14.1
#1 lore raw

PATCH v5 1/3 xlnx-zynqmp-rtc: Initial commit

Initial commit of the ZynqMP RTC device.

---

v5:
- Don't use intermediate val
V2:
- Delete unused realise function
- Remove DB_PRINT()

 include/hw/timer/xlnx-zynqmp-rtc.h |  84 +++++++++++++++
 hw/timer/xlnx-zynqmp-rtc.c         | 214 +++++++++++++++++++++++++++++++++++++
 hw/timer/Makefile.objs             |   1 +
 3 files changed, 299 insertions(+)
 create mode 100644 include/hw/timer/xlnx-zynqmp-rtc.h
 create mode 100644 hw/timer/xlnx-zynqmp-rtc.c
include/hw/timer/xlnx-zynqmp-rtc.h new file +84 -0
new file mode 100644
index 0000000000..87649836cc
@@ -0,0 +1,84 @@
+/*
+ * QEMU model of the Xilinx ZynqMP Real Time Clock (RTC).
+ *
+ * Copyright (c) 2017 Xilinx Inc.
+ *
+ * Written-by: Alistair Francis <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "hw/register.h"
+
+#define TYPE_XLNX_ZYNQMP_RTC "xlnx-zynmp.rtc"
+
+#define XLNX_ZYNQMP_RTC(obj) \
+     OBJECT_CHECK(XlnxZynqMPRTC, (obj), TYPE_XLNX_ZYNQMP_RTC)
+
+REG32(SET_TIME_WRITE, 0x0)
+REG32(SET_TIME_READ, 0x4)
+REG32(CALIB_WRITE, 0x8)
+    FIELD(CALIB_WRITE, FRACTION_EN, 20, 1)
+    FIELD(CALIB_WRITE, FRACTION_DATA, 16, 4)
+    FIELD(CALIB_WRITE, MAX_TICK, 0, 16)
+REG32(CALIB_READ, 0xc)
+    FIELD(CALIB_READ, FRACTION_EN, 20, 1)
+    FIELD(CALIB_READ, FRACTION_DATA, 16, 4)
+    FIELD(CALIB_READ, MAX_TICK, 0, 16)
+REG32(CURRENT_TIME, 0x10)
+REG32(CURRENT_TICK, 0x14)
+    FIELD(CURRENT_TICK, VALUE, 0, 16)
+REG32(ALARM, 0x18)
+REG32(RTC_INT_STATUS, 0x20)
+    FIELD(RTC_INT_STATUS, ALARM, 1, 1)
+    FIELD(RTC_INT_STATUS, SECONDS, 0, 1)
+REG32(RTC_INT_MASK, 0x24)
+    FIELD(RTC_INT_MASK, ALARM, 1, 1)
+    FIELD(RTC_INT_MASK, SECONDS, 0, 1)
+REG32(RTC_INT_EN, 0x28)
+    FIELD(RTC_INT_EN, ALARM, 1, 1)
+    FIELD(RTC_INT_EN, SECONDS, 0, 1)
+REG32(RTC_INT_DIS, 0x2c)
+    FIELD(RTC_INT_DIS, ALARM, 1, 1)
+    FIELD(RTC_INT_DIS, SECONDS, 0, 1)
+REG32(ADDR_ERROR, 0x30)
+    FIELD(ADDR_ERROR, STATUS, 0, 1)
+REG32(ADDR_ERROR_INT_MASK, 0x34)
+    FIELD(ADDR_ERROR_INT_MASK, MASK, 0, 1)
+REG32(ADDR_ERROR_INT_EN, 0x38)
+    FIELD(ADDR_ERROR_INT_EN, MASK, 0, 1)
+REG32(ADDR_ERROR_INT_DIS, 0x3c)
+    FIELD(ADDR_ERROR_INT_DIS, MASK, 0, 1)
+REG32(CONTROL, 0x40)
+    FIELD(CONTROL, BATTERY_DISABLE, 31, 1)
+    FIELD(CONTROL, OSC_CNTRL, 24, 4)
+    FIELD(CONTROL, SLVERR_ENABLE, 0, 1)
+REG32(SAFETY_CHK, 0x50)
+
+#define XLNX_ZYNQMP_RTC_R_MAX (R_SAFETY_CHK + 1)
+
+typedef struct XlnxZynqMPRTC {
+    SysBusDevice parent_obj;
+    MemoryRegion iomem;
+    qemu_irq irq_rtc_int;
+    qemu_irq irq_addr_error_int;
+
+    uint32_t regs[XLNX_ZYNQMP_RTC_R_MAX];
+    RegisterInfo regs_info[XLNX_ZYNQMP_RTC_R_MAX];
+} XlnxZynqMPRTC;
hw/timer/xlnx-zynqmp-rtc.c new file +214 -0

214 lines not on this page · raw message

hw/timer/Makefile.objs +1 -0
index 8c19eac3b6..8b27a4b7ef 100644
@@ -21,6 +21,7 @@ common-obj-$(CONFIG_IMX) += imx_epit.o
 common-obj-$(CONFIG_IMX) += imx_gpt.o
 common-obj-$(CONFIG_LM32) += lm32_timer.o
 common-obj-$(CONFIG_MILKYMIST) += milkymist-sysctl.o
+common-obj-$(CONFIG_XLNX_ZYNQMP) += xlnx-zynqmp-rtc.o
 
 obj-$(CONFIG_ALTERA_TIMER) += altera_timer.o
 obj-$(CONFIG_EXYNOS4) += exynos4210_mct.o
Signature · 1 line
2.14.1
#2 lore raw

PATCH v5 2/3 xlnx-zynqmp-rtc: Add basic time support

In reply to Alistair Francis

Allow the guest to determine the time set from the QEMU command line.

This includes adding a trace event to debug the new time.

---
V5:
- Recalculate tick_offset after migration
V4:
- Use the .unimp property
V3:
- Store an offset value
- Use mktimegm()
- Log unimplemented writes
V2:
- Convert DB_PRINT() macro to trace

 include/hw/timer/xlnx-zynqmp-rtc.h |  3 +++
 hw/timer/xlnx-zynqmp-rtc.c         | 52 ++++++++++++++++++++++++++++++++++++++
 hw/timer/trace-events              |  3 +++
 3 files changed, 58 insertions(+)
include/hw/timer/xlnx-zynqmp-rtc.h +3 -0
index 87649836cc..2867563bdd 100644
@@ -79,6 +79,9 @@ typedef struct XlnxZynqMPRTC {
     qemu_irq irq_rtc_int;
     qemu_irq irq_addr_error_int;
 
+    struct tm current_tm;
+    uint32_t tick_offset;
+
     uint32_t regs[XLNX_ZYNQMP_RTC_R_MAX];
     RegisterInfo regs_info[XLNX_ZYNQMP_RTC_R_MAX];
 } XlnxZynqMPRTC;
hw/timer/xlnx-zynqmp-rtc.c +52 -0

97 lines not on this page · raw message

hw/timer/trace-events +3 -0
index 640722b5d1..e6e042fddb 100644
@@ -60,3 +60,6 @@ systick_write(uint64_t addr, uint32_t value, unsigned size) "systick write addr
 cmsdk_apb_timer_read(uint64_t offset, uint64_t data, unsigned size) "CMSDK APB timer read: offset 0x%" PRIx64 " data 0x%" PRIx64 " size %u"
 cmsdk_apb_timer_write(uint64_t offset, uint64_t data, unsigned size) "CMSDK APB timer write: offset 0x%" PRIx64 " data 0x%" PRIx64 " size %u"
 cmsdk_apb_timer_reset(void) "CMSDK APB timer: reset"
+
+# hw/timer/xlnx-zynqmp-rtc.c
+xlnx_zynqmp_rtc_gettime(int year, int month, int day, int hour, int min, int sec) "Get time from host: %d-%d-%d %2d:%02d:%02d"
Signature · 1 line
2.14.1
#3 lore raw

Re: [Qemu-devel] [PATCH v5 2/3] xlnx-zynqmp-rtc: Add basic time support

Quoted 10 lines · Alistair Francis

Allow the guest to determine the time set from the QEMU command line.

This includes adding a trace event to debug the new time.

---
static const VMStateDescription vmstate_rtc = {
.name = TYPE_XLNX_ZYNQMP_RTC,
.version_id = 1,
.minimum_version_id = 1,

hw/timer/xlnx-zynqmp-rtc.c static void rtc_init(Object *obj) · line 240

Quoted 10 lines
hw/timer/xlnx-zynqmp-rtc.c +8 -0
+    .post_load = rtc_post_load,
     .fields = (VMStateField[]) {
         VMSTATE_UINT32_ARRAY(regs, XlnxZynqMPRTC, XLNX_ZYNQMP_RTC_R_MAX),
+        VMSTATE_INT32(current_tm.tm_sec, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_min, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_hour, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_wday, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_mday, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_mon, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_year, XlnxZynqMPRTC),

I'm still not sure about having the current_tm struct fields here rather than whatever the hardware's natural representation of the current time is. Can you explain why you think this is the best approach?

thanks
-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH v5 2/3] xlnx-zynqmp-rtc: Add basic time support

Quoted 23 lines · Peter Maydell
Quoted 10 lines · Alistair Francis

Allow the guest to determine the time set from the QEMU command line.

This includes adding a trace event to debug the new time.

---
static const VMStateDescription vmstate_rtc = {
.name = TYPE_XLNX_ZYNQMP_RTC,
.version_id = 1,
.minimum_version_id = 1,

hw/timer/xlnx-zynqmp-rtc.c static void rtc_init(Object *obj) · line 240

Quoted 10 lines
hw/timer/xlnx-zynqmp-rtc.c +8 -0
+    .post_load = rtc_post_load,
     .fields = (VMStateField[]) {
         VMSTATE_UINT32_ARRAY(regs, XlnxZynqMPRTC, XLNX_ZYNQMP_RTC_R_MAX),
+        VMSTATE_INT32(current_tm.tm_sec, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_min, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_hour, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_wday, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_mday, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_mon, XlnxZynqMPRTC),
+        VMSTATE_INT32(current_tm.tm_year, XlnxZynqMPRTC),
Quoted 4 lines

I'm still not sure about having the current_tm struct fields here rather than whatever the hardware's natural representation of the current time is. Can you explain why you think this is the best approach?

This means that the only thing we have to track is the time that the user set, either through the QEMU command line arguments as an override or the original starting time. Then from there we can always calculate the correct offset based on our current time.

I don't see a nicer way, otherwise we need to recalculate the natural representation before and after migration using something more complex. This way it's always the same logic, we just run it at init and after migration.

Alistair

Quoted 3 lines

thanks
-- PMM

#5 lore raw

Re: [Qemu-devel] [PATCH v5 2/3] xlnx-zynqmp-rtc: Add basic time support

Quoted 11 lines · Alistair Francis
Quoted 4 lines · Peter Maydell

I'm still not sure about having the current_tm struct fields here rather than whatever the hardware's natural representation of the current time is. Can you explain why you think this is the best approach?

This means that the only thing we have to track is the time that the user set, either through the QEMU command line arguments as an override or the original starting time. Then from there we can always calculate the correct offset based on our current time.

Quoted 4 lines

I don't see a nicer way, otherwise we need to recalculate the natural representation before and after migration using something more complex. This way it's always the same logic, we just run it at init and after migration.

Doing it this way round means that it gets complicated when the guest writes to the RTC, though. At the moment I can't see anywhere that sets the current_tm fields except for reset and post-migration. Isn't something missing here? I'm wondering if this choice of migration state is going to paint us into a corner for handling guest-writes-to-rtc.

thanks
-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH v5 2/3] xlnx-zynqmp-rtc: Add basic time support

Quoted 18 lines · Peter Maydell
Quoted 11 lines · Alistair Francis
Quoted 4 lines · Peter Maydell

I'm still not sure about having the current_tm struct fields here rather than whatever the hardware's natural representation of the current time is. Can you explain why you think this is the best approach?

This means that the only thing we have to track is the time that the user set, either through the QEMU command line arguments as an override or the original starting time. Then from there we can always calculate the correct offset based on our current time.

Quoted 4 lines

I don't see a nicer way, otherwise we need to recalculate the natural representation before and after migration using something more complex. This way it's always the same logic, we just run it at init and after migration.

Quoted 6 lines

Doing it this way round means that it gets complicated when the guest writes to the RTC, though. At the moment I can't see anywhere that sets the current_tm fields except for reset and post-migration. Isn't something missing here? I'm wondering if this choice of migration state is going to paint us into a corner for handling guest-writes-to-rtc.

I don't have guest write support. The main focus was just allowing users to set the time when they boot QEMU. We haven't seen any request to allow guests to set the time, so I didn't bother.

Wouldn't adding guest write support just be as simple as converting the value written to a current_tm field?

Alistair

Quoted 4 lines

thanks
-- PMM

#7 lore raw

Re: [Qemu-devel] [PATCH v5 2/3] xlnx-zynqmp-rtc: Add basic time support

Quoted 8 lines · Alistair Francis
Quoted 6 lines · Peter Maydell

Doing it this way round means that it gets complicated when the guest writes to the RTC, though. At the moment I can't see anywhere that sets the current_tm fields except for reset and post-migration. Isn't something missing here? I'm wondering if this choice of migration state is going to paint us into a corner for handling guest-writes-to-rtc.

Quoted 6 lines

I don't have guest write support. The main focus was just allowing users to set the time when they boot QEMU. We haven't seen any request to allow guests to set the time, so I didn't bother.

Wouldn't adding guest write support just be as simple as converting the value written to a current_tm field?

Yes, but then why are we using the current_tm format at all? It's not what the guest wants to see for reads, it's not what writes would naturally be, and it's not what you're using at runtime (you convert it immediately to a tick_offset and ignore the current_tm stuff). I guess it can be made to work, it just seems like a weird format to use for migration.

I've also just reread the patch and am a bit confused -- don't you need some code that writes the correct value for the current guest time to the current_tm fields before migration ?

thanks
-- PMM

#8 lore raw

PATCH v5 3/3 xlnx-zynqmp: Connect the RTC device

In reply to Alistair Francis

---

 include/hw/arm/xlnx-zynqmp.h |  2 ++
 hw/arm/xlnx-zynqmp.c         | 14 ++++++++++++++
 2 files changed, 16 insertions(+)
include/hw/arm/xlnx-zynqmp.h +2 -0
index 3e6fb9b7bd..9e8c9e18dd 100644
@@ -28,6 +28,7 @@
 #include "hw/ssi/xilinx_spips.h"
 #include "hw/dma/xlnx_dpdma.h"
 #include "hw/display/xlnx_dp.h"
+#include "hw/timer/xlnx-zynqmp-rtc.h"
 
 #define TYPE_XLNX_ZYNQMP "xlnx,zynqmp"
 #define XLNX_ZYNQMP(obj) OBJECT_CHECK(XlnxZynqMPState, (obj), \
@@ -90,6 +91,7 @@ typedef struct XlnxZynqMPState {
     XlnxZynqMPQSPIPS qspi;
     XlnxDPState dp;
     XlnxDPDMAState dpdma;
+    XlnxZynqMPRTC rtc;
 
     char *boot_cpu;
     ARMCPU *boot_cpu_ptr;
hw/arm/xlnx-zynqmp.c +14 -0
index 325642058b..deef583c2a 100644
@@ -50,6 +50,9 @@
 #define DPDMA_ADDR          0xfd4c0000
 #define DPDMA_IRQ           116
 
+#define RTC_ADDR            0xffa60000
+#define RTC_IRQ             26
+
 static const uint64_t gem_addr[XLNX_ZYNQMP_NUM_GEMS] = {
     0xFF0B0000, 0xFF0C0000, 0xFF0D0000, 0xFF0E0000,
 };
@@ -183,6 +186,9 @@ static void xlnx_zynqmp_init(Object *obj)
 
     object_initialize(&s->dpdma, sizeof(s->dpdma), TYPE_XLNX_DPDMA);
     qdev_set_parent_bus(DEVICE(&s->dpdma), sysbus_get_default());
+
+    object_initialize(&s->rtc, sizeof(s->rtc), TYPE_XLNX_ZYNQMP_RTC);
+    qdev_set_parent_bus(DEVICE(&s->rtc), sysbus_get_default());
 }
 
 static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)
@@ -454,6 +460,14 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp)
                              &error_abort);
     sysbus_mmio_map(SYS_BUS_DEVICE(&s->dpdma), 0, DPDMA_ADDR);
     sysbus_connect_irq(SYS_BUS_DEVICE(&s->dpdma), 0, gic_spi[DPDMA_IRQ]);
+
+    object_property_set_bool(OBJECT(&s->rtc), true, "realized", &err);
+    if (err) {
+        error_propagate(errp, err);
+        return;
+    }
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->rtc), 0, RTC_ADDR);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->rtc), 0, gic_spi[RTC_IRQ]);
 }
 
 static Property xlnx_zynqmp_props[] = {
Signature · 1 line
2.14.1