xref: /qemu/hw/rtc/sun4v-rtc.c (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
1 /*
2  * QEMU sun4v Real Time Clock device
3  *
4  * The sun4v_rtc device (sun4v tod clock)
5  *
6  * Copyright (c) 2016 Artyom Tarasenko
7  *
8  * This code is licensed under the GNU GPL v3 or (at your option) any later
9  * version.
10  */
11 
12 #include "qemu/osdep.h"
13 #include "hw/sysbus.h"
14 #include "qapi/error.h"
15 #include "qemu/module.h"
16 #include "qemu/timer.h"
17 #include "hw/rtc/sun4v-rtc.h"
18 #include "trace.h"
19 #include "qom/object.h"
20 
21 
22 #define TYPE_SUN4V_RTC "sun4v_rtc"
23 typedef struct Sun4vRtc Sun4vRtc;
24 #define SUN4V_RTC(obj) OBJECT_CHECK(Sun4vRtc, (obj), TYPE_SUN4V_RTC)
25 
26 struct Sun4vRtc {
27     SysBusDevice parent_obj;
28 
29     MemoryRegion iomem;
30 };
31 
32 static uint64_t sun4v_rtc_read(void *opaque, hwaddr addr,
33                                 unsigned size)
34 {
35     uint64_t val = get_clock_realtime() / NANOSECONDS_PER_SECOND;
36     if (!(addr & 4ULL)) {
37         /* accessing the high 32 bits */
38         val >>= 32;
39     }
40     trace_sun4v_rtc_read(addr, val);
41     return val;
42 }
43 
44 static void sun4v_rtc_write(void *opaque, hwaddr addr,
45                              uint64_t val, unsigned size)
46 {
47     trace_sun4v_rtc_write(addr, val);
48 }
49 
50 static const MemoryRegionOps sun4v_rtc_ops = {
51     .read = sun4v_rtc_read,
52     .write = sun4v_rtc_write,
53     .endianness = DEVICE_NATIVE_ENDIAN,
54 };
55 
56 void sun4v_rtc_init(hwaddr addr)
57 {
58     DeviceState *dev;
59     SysBusDevice *s;
60 
61     dev = qdev_new(TYPE_SUN4V_RTC);
62     s = SYS_BUS_DEVICE(dev);
63 
64     sysbus_realize_and_unref(s, &error_fatal);
65 
66     sysbus_mmio_map(s, 0, addr);
67 }
68 
69 static void sun4v_rtc_realize(DeviceState *dev, Error **errp)
70 {
71     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
72     Sun4vRtc *s = SUN4V_RTC(dev);
73 
74     memory_region_init_io(&s->iomem, OBJECT(s), &sun4v_rtc_ops, s,
75                           "sun4v-rtc", 0x08ULL);
76     sysbus_init_mmio(sbd, &s->iomem);
77 }
78 
79 static void sun4v_rtc_class_init(ObjectClass *klass, void *data)
80 {
81     DeviceClass *dc = DEVICE_CLASS(klass);
82 
83     dc->realize = sun4v_rtc_realize;
84 }
85 
86 static const TypeInfo sun4v_rtc_info = {
87     .name          = TYPE_SUN4V_RTC,
88     .parent        = TYPE_SYS_BUS_DEVICE,
89     .instance_size = sizeof(Sun4vRtc),
90     .class_init    = sun4v_rtc_class_init,
91 };
92 
93 static void sun4v_rtc_register_types(void)
94 {
95     type_register_static(&sun4v_rtc_info);
96 }
97 
98 type_init(sun4v_rtc_register_types)
99