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