]>
Commit | Line | Data |
---|---|---|
a0e89303 AT |
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/hw.h" | |
14 | #include "hw/sysbus.h" | |
0b8fa32f | 15 | #include "qemu/module.h" |
a0e89303 AT |
16 | #include "qemu/timer.h" |
17 | #include "hw/timer/sun4v-rtc.h" | |
252bfbde | 18 | #include "trace.h" |
a0e89303 | 19 | |
a0e89303 AT |
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 | { | |
fff54d22 | 33 | uint64_t val = get_clock_realtime() / NANOSECONDS_PER_SECOND; |
a0e89303 AT |
34 | if (!(addr & 4ULL)) { |
35 | /* accessing the high 32 bits */ | |
36 | val >>= 32; | |
37 | } | |
252bfbde | 38 | trace_sun4v_rtc_read(addr, val); |
a0e89303 AT |
39 | return val; |
40 | } | |
41 | ||
42 | static void sun4v_rtc_write(void *opaque, hwaddr addr, | |
43 | uint64_t val, unsigned size) | |
44 | { | |
51809286 | 45 | trace_sun4v_rtc_write(addr, val); |
a0e89303 AT |
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_create(NULL, TYPE_SUN4V_RTC); | |
60 | s = SYS_BUS_DEVICE(dev); | |
61 | ||
62 | qdev_init_nofail(dev); | |
63 | ||
64 | sysbus_mmio_map(s, 0, addr); | |
65 | } | |
66 | ||
e871972e | 67 | static void sun4v_rtc_realize(DeviceState *dev, Error **errp) |
a0e89303 | 68 | { |
e871972e | 69 | SysBusDevice *sbd = SYS_BUS_DEVICE(dev); |
a0e89303 AT |
70 | Sun4vRtc *s = SUN4V_RTC(dev); |
71 | ||
72 | memory_region_init_io(&s->iomem, OBJECT(s), &sun4v_rtc_ops, s, | |
73 | "sun4v-rtc", 0x08ULL); | |
e871972e | 74 | sysbus_init_mmio(sbd, &s->iomem); |
a0e89303 AT |
75 | } |
76 | ||
77 | static void sun4v_rtc_class_init(ObjectClass *klass, void *data) | |
78 | { | |
e871972e | 79 | DeviceClass *dc = DEVICE_CLASS(klass); |
a0e89303 | 80 | |
e871972e | 81 | dc->realize = sun4v_rtc_realize; |
a0e89303 AT |
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) |