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