xref: /openbmc/qemu/hw/rtc/sun4v-rtc.c (revision 8fa3b702)
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 DECLARE_INSTANCE_CHECKER(Sun4vRtc, SUN4V_RTC,
25                          TYPE_SUN4V_RTC)
26 
27 struct Sun4vRtc {
28     SysBusDevice parent_obj;
29 
30     MemoryRegion iomem;
31 };
32 
33 static uint64_t sun4v_rtc_read(void *opaque, hwaddr addr,
34                                 unsigned size)
35 {
36     uint64_t val = get_clock_realtime() / NANOSECONDS_PER_SECOND;
37     if (!(addr & 4ULL)) {
38         /* accessing the high 32 bits */
39         val >>= 32;
40     }
41     trace_sun4v_rtc_read(addr, val);
42     return val;
43 }
44 
45 static void sun4v_rtc_write(void *opaque, hwaddr addr,
46                              uint64_t val, unsigned size)
47 {
48     trace_sun4v_rtc_write(addr, val);
49 }
50 
51 static const MemoryRegionOps sun4v_rtc_ops = {
52     .read = sun4v_rtc_read,
53     .write = sun4v_rtc_write,
54     .endianness = DEVICE_NATIVE_ENDIAN,
55 };
56 
57 void sun4v_rtc_init(hwaddr addr)
58 {
59     DeviceState *dev;
60     SysBusDevice *s;
61 
62     dev = qdev_new(TYPE_SUN4V_RTC);
63     s = SYS_BUS_DEVICE(dev);
64 
65     sysbus_realize_and_unref(s, &error_fatal);
66 
67     sysbus_mmio_map(s, 0, addr);
68 }
69 
70 static void sun4v_rtc_realize(DeviceState *dev, Error **errp)
71 {
72     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
73     Sun4vRtc *s = SUN4V_RTC(dev);
74 
75     memory_region_init_io(&s->iomem, OBJECT(s), &sun4v_rtc_ops, s,
76                           "sun4v-rtc", 0x08ULL);
77     sysbus_init_mmio(sbd, &s->iomem);
78 }
79 
80 static void sun4v_rtc_class_init(ObjectClass *klass, void *data)
81 {
82     DeviceClass *dc = DEVICE_CLASS(klass);
83 
84     dc->realize = sun4v_rtc_realize;
85 }
86 
87 static const TypeInfo sun4v_rtc_info = {
88     .name          = TYPE_SUN4V_RTC,
89     .parent        = TYPE_SYS_BUS_DEVICE,
90     .instance_size = sizeof(Sun4vRtc),
91     .class_init    = sun4v_rtc_class_init,
92 };
93 
94 static void sun4v_rtc_register_types(void)
95 {
96     type_register_static(&sun4v_rtc_info);
97 }
98 
99 type_init(sun4v_rtc_register_types)
100