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