diff mbox series

[2/5] hw/timer/avr_timer16: Use the Clock API

Message ID 20200814163924.11662-3-f4bug@amsat.org
State New
Headers show
Series hw/avr: Start using the Clock API | expand

Commit Message

Philippe Mathieu-Daudé Aug. 14, 2020, 4:39 p.m. UTC
Expose the 'clkt' clock source. Connect the MCU I/O clock to it.
Drop the now unused 'cpu-frequency-hz' static property.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 include/hw/timer/avr_timer16.h |  3 ++-
 hw/avr/atmega.c                |  3 +--
 hw/timer/avr_timer16.c         | 12 ++++--------
 3 files changed, 7 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/include/hw/timer/avr_timer16.h b/include/hw/timer/avr_timer16.h
index 982019d242..fb1ef5d3be 100644
--- a/include/hw/timer/avr_timer16.h
+++ b/include/hw/timer/avr_timer16.h
@@ -31,6 +31,7 @@ 
 #include "hw/sysbus.h"
 #include "qemu/timer.h"
 #include "hw/hw.h"
+#include "hw/clock.h"
 
 enum NextInterrupt {
     OVERFLOW,
@@ -52,6 +53,7 @@  typedef struct AVRTimer16State {
     MemoryRegion iomem;
     MemoryRegion imsk_iomem;
     MemoryRegion ifr_iomem;
+    Clock *clkin;
     QEMUTimer *timer;
     qemu_irq capt_irq;
     qemu_irq compa_irq;
@@ -84,7 +86,6 @@  typedef struct AVRTimer16State {
     uint8_t ifr;
 
     uint8_t id;
-    uint64_t cpu_freq_hz;
     uint64_t freq_hz;
     uint64_t period_ns;
     uint64_t reset_time_ns;
diff --git a/hw/avr/atmega.c b/hw/avr/atmega.c
index 9d814de499..f14b558140 100644
--- a/hw/avr/atmega.c
+++ b/hw/avr/atmega.c
@@ -332,8 +332,7 @@  static void atmega_realize(DeviceState *dev, Error **errp)
         devname = g_strdup_printf("timer%zu", i);
         object_initialize_child(OBJECT(dev), devname, &s->timer[i],
                                 TYPE_AVR_TIMER16);
-        object_property_set_uint(OBJECT(&s->timer[i]), "cpu-frequency-hz",
-                                 s->xtal_freq_hz, &error_abort);
+        qdev_connect_clock_in(DEVICE(&s->timer[i]), "clkt", s->ioclk);
         sbd = SYS_BUS_DEVICE(&s->timer[i]);
         sysbus_realize(sbd, &error_abort);
         sysbus_mmio_map(sbd, 0, OFFSET_DATA + mc->dev[idx].addr);
diff --git a/hw/timer/avr_timer16.c b/hw/timer/avr_timer16.c
index c48555da52..7634fe6587 100644
--- a/hw/timer/avr_timer16.c
+++ b/hw/timer/avr_timer16.c
@@ -35,6 +35,7 @@ 
 #include "qapi/error.h"
 #include "qemu/log.h"
 #include "hw/irq.h"
+#include "hw/qdev-clock.h"
 #include "hw/qdev-properties.h"
 #include "hw/timer/avr_timer16.h"
 #include "trace.h"
@@ -167,7 +168,7 @@  static void avr_timer16_clksrc_update(AVRTimer16State *t16)
         break;
     }
     if (divider) {
-        t16->freq_hz = t16->cpu_freq_hz / divider;
+        t16->freq_hz = clock_get_hz(t16->clkin) / divider;
         t16->period_ns = NANOSECONDS_PER_SECOND / t16->freq_hz;
         trace_avr_timer16_clksrc_update(t16->freq_hz, t16->period_ns,
                                         (uint64_t)(1e6 / t16->freq_hz));
@@ -544,8 +545,6 @@  static const MemoryRegionOps avr_timer16_ifr_ops = {
 
 static Property avr_timer16_properties[] = {
     DEFINE_PROP_UINT8("id", struct AVRTimer16State, id, 0),
-    DEFINE_PROP_UINT64("cpu-frequency-hz", struct AVRTimer16State,
-                       cpu_freq_hz, 0),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -564,6 +563,8 @@  static void avr_timer16_init(Object *obj)
 {
     AVRTimer16State *s = AVR_TIMER16(obj);
 
+    s->clkin = qdev_init_clock_in(DEVICE(obj), "clkt", NULL, s);
+
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->capt_irq);
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->compa_irq);
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->compb_irq);
@@ -587,11 +588,6 @@  static void avr_timer16_realize(DeviceState *dev, Error **errp)
 {
     AVRTimer16State *s = AVR_TIMER16(dev);
 
-    if (s->cpu_freq_hz == 0) {
-        error_setg(errp, "AVR timer16: cpu-frequency-hz property must be set");
-        return;
-    }
-
     s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, avr_timer16_interrupt, s);
     s->enabled = true;
 }