2 * QEMU sun4v Real Time Clock device
4 * The sun4v_rtc device (sun4v tod clock)
6 * Copyright (c) 2016 Artyom Tarasenko
8 * This code is licensed under the GNU GPL v3 or (at your option) any later
12 #include "qemu/osdep.h"
13 #include "hw/sysbus.h"
14 #include "qemu/module.h"
15 #include "qemu/timer.h"
16 #include "hw/rtc/sun4v-rtc.h"
20 #define TYPE_SUN4V_RTC "sun4v_rtc"
21 #define SUN4V_RTC(obj) OBJECT_CHECK(Sun4vRtc, (obj), TYPE_SUN4V_RTC)
23 typedef struct Sun4vRtc
{
24 SysBusDevice parent_obj
;
29 static uint64_t sun4v_rtc_read(void *opaque
, hwaddr addr
,
32 uint64_t val
= get_clock_realtime() / NANOSECONDS_PER_SECOND
;
34 /* accessing the high 32 bits */
37 trace_sun4v_rtc_read(addr
, val
);
41 static void sun4v_rtc_write(void *opaque
, hwaddr addr
,
42 uint64_t val
, unsigned size
)
44 trace_sun4v_rtc_write(addr
, val
);
47 static const MemoryRegionOps sun4v_rtc_ops
= {
48 .read
= sun4v_rtc_read
,
49 .write
= sun4v_rtc_write
,
50 .endianness
= DEVICE_NATIVE_ENDIAN
,
53 void sun4v_rtc_init(hwaddr addr
)
58 dev
= qdev_create(NULL
, TYPE_SUN4V_RTC
);
59 s
= SYS_BUS_DEVICE(dev
);
61 qdev_init_nofail(dev
);
63 sysbus_mmio_map(s
, 0, addr
);
66 static void sun4v_rtc_realize(DeviceState
*dev
, Error
**errp
)
68 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
69 Sun4vRtc
*s
= SUN4V_RTC(dev
);
71 memory_region_init_io(&s
->iomem
, OBJECT(s
), &sun4v_rtc_ops
, s
,
72 "sun4v-rtc", 0x08ULL
);
73 sysbus_init_mmio(sbd
, &s
->iomem
);
76 static void sun4v_rtc_class_init(ObjectClass
*klass
, void *data
)
78 DeviceClass
*dc
= DEVICE_CLASS(klass
);
80 dc
->realize
= sun4v_rtc_realize
;
83 static const TypeInfo sun4v_rtc_info
= {
84 .name
= TYPE_SUN4V_RTC
,
85 .parent
= TYPE_SYS_BUS_DEVICE
,
86 .instance_size
= sizeof(Sun4vRtc
),
87 .class_init
= sun4v_rtc_class_init
,
90 static void sun4v_rtc_register_types(void)
92 type_register_static(&sun4v_rtc_info
);
95 type_init(sun4v_rtc_register_types
)