hw/sd/pl181: Replace fprintf(stderr, "*\n") with error_report()
[qemu/ar7.git] / hw / rtc / sun4v-rtc.c
blob52caea86547bf41a93779d09a31d79f7b4605a82
1 /*
2 * QEMU sun4v Real Time Clock device
4 * The sun4v_rtc device (sun4v tod clock)
6 * Copyright (c) 2016 Artyom Tarasenko
8 * This code is licensed under the GNU GPL v3 or (at your option) any later
9 * version.
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"
21 #define TYPE_SUN4V_RTC "sun4v_rtc"
22 #define SUN4V_RTC(obj) OBJECT_CHECK(Sun4vRtc, (obj), TYPE_SUN4V_RTC)
24 typedef struct Sun4vRtc {
25 SysBusDevice parent_obj;
27 MemoryRegion iomem;
28 } Sun4vRtc;
30 static uint64_t sun4v_rtc_read(void *opaque, hwaddr addr,
31 unsigned size)
33 uint64_t val = get_clock_realtime() / NANOSECONDS_PER_SECOND;
34 if (!(addr & 4ULL)) {
35 /* accessing the high 32 bits */
36 val >>= 32;
38 trace_sun4v_rtc_read(addr, val);
39 return val;
42 static void sun4v_rtc_write(void *opaque, hwaddr addr,
43 uint64_t val, unsigned size)
45 trace_sun4v_rtc_write(addr, val);
48 static const MemoryRegionOps sun4v_rtc_ops = {
49 .read = sun4v_rtc_read,
50 .write = sun4v_rtc_write,
51 .endianness = DEVICE_NATIVE_ENDIAN,
54 void sun4v_rtc_init(hwaddr addr)
56 DeviceState *dev;
57 SysBusDevice *s;
59 dev = qdev_new(TYPE_SUN4V_RTC);
60 s = SYS_BUS_DEVICE(dev);
62 sysbus_realize_and_unref(s, &error_fatal);
64 sysbus_mmio_map(s, 0, addr);
67 static void sun4v_rtc_realize(DeviceState *dev, Error **errp)
69 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
70 Sun4vRtc *s = SUN4V_RTC(dev);
72 memory_region_init_io(&s->iomem, OBJECT(s), &sun4v_rtc_ops, s,
73 "sun4v-rtc", 0x08ULL);
74 sysbus_init_mmio(sbd, &s->iomem);
77 static void sun4v_rtc_class_init(ObjectClass *klass, void *data)
79 DeviceClass *dc = DEVICE_CLASS(klass);
81 dc->realize = sun4v_rtc_realize;
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,
91 static void sun4v_rtc_register_types(void)
93 type_register_static(&sun4v_rtc_info);
96 type_init(sun4v_rtc_register_types)