From: "Corvin Köhne" <corvin.koehne@gmail.com>
To: qemu-devel@nongnu.org
Cc: "Edgar E. Iglesias" <edgar.iglesias@gmail.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Alistair Francis" <alistair@alistair23.me>,
qemu-arm@nongnu.org, "Peter Maydell" <peter.maydell@linaro.org>,
"Kevin Wolf" <kwolf@redhat.com>,
qemu-block@nongnu.org, "Corvin Köhne" <c.koehne@beckhoff.com>,
"Hanna Reitz" <hreitz@redhat.com>,
"Yannick Voßen" <y.vossen@beckhoff.com>,
YannickV <Y.Vossen@beckhoff.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: [PATCH v5 02/15] hw/timer: Make PERIPHCLK divider configurable
Date: Thu, 4 Dec 2025 10:34:49 +0100 [thread overview]
Message-ID: <20251204093502.50582-3-corvin.koehne@gmail.com> (raw)
In-Reply-To: <20251204093502.50582-1-corvin.koehne@gmail.com>
From: YannickV <Y.Vossen@beckhoff.com>
The A9 global timer and ARM MP timer use PERIPHCLK as
their clock source. The frequency of PERIPHCLK is derived
by dividing the main clock (CLK) by a configurable
divider (must be at least 2). Previously, the PERIPHCLK
divider was not configurable, which could lead to
unexspected behavior if the application exspected a
different PERIPHCLK rate.
The property periphclk-divider specifies by which value
the main clock is divided to generate PERIPHCLK. This
allows flexible configuration of the timer clocks to
match application requirements.
Information can be found in the Zynq 7000 Soc Technical
Reference Manual under Timers.
https://docs.amd.com/r/en-US/ug585-zynq-7000-SoC-TRM
Signed-off-by: YannickV <Y.Vossen@beckhoff.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/timer/a9gtimer.c | 19 ++++++++++++++++++-
hw/timer/arm_mptimer.c | 19 ++++++++++++++++++-
include/hw/timer/a9gtimer.h | 1 +
include/hw/timer/arm_mptimer.h | 2 ++
4 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/hw/timer/a9gtimer.c b/hw/timer/a9gtimer.c
index ad9abcb4bb..8b4c6d7e6a 100644
--- a/hw/timer/a9gtimer.c
+++ b/hw/timer/a9gtimer.c
@@ -27,6 +27,7 @@
#include "hw/timer/a9gtimer.h"
#include "migration/vmstate.h"
#include "qapi/error.h"
+#include "qemu/error-report.h"
#include "qemu/timer.h"
#include "qemu/bitops.h"
#include "qemu/log.h"
@@ -62,9 +63,17 @@ static inline int a9_gtimer_get_current_cpu(A9GTimerState *s)
static inline uint64_t a9_gtimer_get_conv(A9GTimerState *s)
{
+ /*
+ * Referring to the ARM-Cortex-A9 MPCore TRM
+ *
+ * The a9 global timer relies on the PERIPHCLK as its clock source.
+ * The PERIPHCLK clock period must be configured as a multiple of the
+ * main clock CLK. The conversion from the qemu clock (1GHz) to a9
+ * gtimer ticks can be calculated like this:
+ */
uint64_t prescale = extract32(s->control, R_CONTROL_PRESCALER_SHIFT,
R_CONTROL_PRESCALER_LEN) + 1;
- uint64_t scaled_prescaler = prescale * 10;
+ uint64_t scaled_prescaler = prescale * s->periphclk_divider;
return muldiv64(scaled_prescaler, NANOSECONDS_PER_SECOND, s->freq_hz);
}
@@ -312,6 +321,12 @@ static void a9_gtimer_realize(DeviceState *dev, Error **errp)
sysbus_init_mmio(sbd, &s->iomem);
s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, a9_gtimer_update_no_sync, s);
+ if (s->periphclk_divider < 2) {
+ error_setg(errp, "Invalid periphclk-divider (%lu), must be >= 2",
+ s->periphclk_divider);
+ return;
+ }
+
for (i = 0; i < s->num_cpu; i++) {
A9GTimerPerCPU *gtb = &s->per_cpu[i];
@@ -378,6 +393,8 @@ static const Property a9_gtimer_properties[] = {
DEFINE_PROP_UINT64("clock-frequency", A9GTimerState, freq_hz,
NANOSECONDS_PER_SECOND),
DEFINE_PROP_UINT32("num-cpu", A9GTimerState, num_cpu, 0),
+ DEFINE_PROP_UINT64("periphclk-divider", A9GTimerState,
+ periphclk_divider, 10),
};
static void a9_gtimer_class_init(ObjectClass *klass, const void *data)
diff --git a/hw/timer/arm_mptimer.c b/hw/timer/arm_mptimer.c
index 342ca1276a..cf434ca2f4 100644
--- a/hw/timer/arm_mptimer.c
+++ b/hw/timer/arm_mptimer.c
@@ -27,6 +27,7 @@
#include "hw/timer/arm_mptimer.h"
#include "migration/vmstate.h"
#include "qapi/error.h"
+#include "qemu/error-report.h"
#include "qemu/module.h"
#include "hw/core/cpu.h"
@@ -61,8 +62,16 @@ static inline void timerblock_update_irq(TimerBlock *tb)
/* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
static inline uint32_t timerblock_scale(TimerBlock *tb, uint32_t control)
{
+ /*
+ * Referring to the ARM-Cortex-A9 MPCore TRM
+ *
+ * The arm mp timer relies on the PERIPHCLK as its clock source.
+ * The PERIPHCLK clock period must be configured as a multiple of the
+ * main clock CLK. The conversion from the qemu clock (1GHz) to arm mp
+ * timer ticks can be calculated like this:
+ */
uint64_t prescale = (((control >> 8) & 0xff) + 1);
- uint64_t scaled_prescaler = prescale * 10;
+ uint64_t scaled_prescaler = prescale * tb->periphclk_divider;
return muldiv64(scaled_prescaler, NANOSECONDS_PER_SECOND, tb->freq_hz);
}
@@ -273,6 +282,12 @@ static void arm_mptimer_realize(DeviceState *dev, Error **errp)
for (i = 0; i < s->num_cpu; i++) {
TimerBlock *tb = &s->timerblock[i];
tb->freq_hz = s->freq_hz;
+ if (s->periphclk_divider < 2) {
+ error_setg(errp, "Invalid periphclk-divider (%lu), must be >= 2",
+ s->periphclk_divider);
+ return;
+ }
+ tb->periphclk_divider = s->periphclk_divider;
tb->timer = ptimer_init(timerblock_tick, tb, PTIMER_POLICY);
sysbus_init_irq(sbd, &tb->irq);
memory_region_init_io(&tb->iomem, OBJECT(s), &timerblock_ops, tb,
@@ -309,6 +324,8 @@ static const Property arm_mptimer_properties[] = {
DEFINE_PROP_UINT64("clock-frequency", ARMMPTimerState, freq_hz,
NANOSECONDS_PER_SECOND),
DEFINE_PROP_UINT32("num-cpu", ARMMPTimerState, num_cpu, 0),
+ DEFINE_PROP_UINT64("periphclk-divider", ARMMPTimerState,
+ periphclk_divider, 10),
};
static void arm_mptimer_class_init(ObjectClass *klass, const void *data)
diff --git a/include/hw/timer/a9gtimer.h b/include/hw/timer/a9gtimer.h
index 3b63d14927..ff9baf1c77 100644
--- a/include/hw/timer/a9gtimer.h
+++ b/include/hw/timer/a9gtimer.h
@@ -77,6 +77,7 @@ struct A9GTimerState {
MemoryRegion iomem;
/* static props */
uint64_t freq_hz;
+ uint64_t periphclk_divider;
uint32_t num_cpu;
QEMUTimer *timer;
diff --git a/include/hw/timer/arm_mptimer.h b/include/hw/timer/arm_mptimer.h
index da43a3d351..061934e4b5 100644
--- a/include/hw/timer/arm_mptimer.h
+++ b/include/hw/timer/arm_mptimer.h
@@ -32,6 +32,7 @@ typedef struct {
uint32_t status;
struct ptimer_state *timer;
uint64_t freq_hz;
+ uint64_t periphclk_divider;
qemu_irq irq;
MemoryRegion iomem;
} TimerBlock;
@@ -45,6 +46,7 @@ struct ARMMPTimerState {
/*< public >*/
uint64_t freq_hz;
+ uint64_t periphclk_divider;
uint32_t num_cpu;
TimerBlock timerblock[ARM_MPTIMER_MAX_CPUS];
MemoryRegion iomem;
--
2.47.3
next prev parent reply other threads:[~2025-12-04 9:38 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-04 9:34 [PATCH v5 00/15] hw/arm: add Beckhoff CX7200 board Corvin Köhne
2025-12-04 9:34 ` [PATCH v5 01/15] hw/timer: Make frequency configurable Corvin Köhne
2025-12-04 9:34 ` Corvin Köhne [this message]
2025-12-04 9:34 ` [PATCH v5 03/15] hw/dma/zynq-devcfg: Handle bitstream loading via DMA to 0xffffffff Corvin Köhne
2025-12-04 9:34 ` [PATCH v5 04/15] hw/arm/zynq-devcfg: Prevent unintended unlock during initialization Corvin Köhne
2025-12-04 9:34 ` [PATCH v5 05/15] hw/dma/zynq: Ensure PCFG_DONE bit remains set to indicate PL is in user mode Corvin Köhne
2025-12-04 9:34 ` [PATCH v5 06/15] hw/dma/zynq-devcfg: Simulate dummy PL reset Corvin Köhne
2025-12-04 9:34 ` [PATCH v5 07/15] hw/dma/zynq-devcfg: Indicate power-up status of PL Corvin Köhne
2025-12-04 9:34 ` [PATCH v5 08/15] hw/misc: Add dummy ZYNQ DDR controller Corvin Köhne
2025-12-04 9:34 ` [PATCH v5 09/15] hw/misc/zynq_slcr: Add logic for DCI configuration Corvin Köhne
2025-12-04 9:34 ` [PATCH v5 10/15] hw/misc: Add Beckhoff CCAT device Corvin Köhne
2025-12-04 9:34 ` [PATCH v5 11/15] hw/block/m25p80: Add HAS_SR_TB flag for is25lp016d Corvin Köhne
2025-12-04 9:34 ` [PATCH v5 12/15] hw/arm/xilinx_zynq: Split xilinx_zynq into header and implementation files Corvin Köhne
2025-12-04 9:35 ` [PATCH v5 13/15] hw/arm/xilinx_zynq: Add flash-type property Corvin Köhne
2025-12-04 9:35 ` [PATCH v5 14/15] hw/arm: Add new machine based on xilinx-zynq-a9 for Beckhoff CX7200 Corvin Köhne
2025-12-04 9:35 ` [PATCH v5 15/15] docs/system/arm: Add support " Corvin Köhne
2025-12-04 10:47 ` [PATCH v5 00/15] hw/arm: add Beckhoff CX7200 board Thomas Huth
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251204093502.50582-3-corvin.koehne@gmail.com \
--to=corvin.koehne@gmail.com \
--cc=alistair@alistair23.me \
--cc=c.koehne@beckhoff.com \
--cc=edgar.iglesias@gmail.com \
--cc=hreitz@redhat.com \
--cc=kwolf@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=y.vossen@beckhoff.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).