sun4v-rtc.c 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * QEMU sun4v Real Time Clock device
 *
 * The sun4v_rtc device (sun4v tod clock)
 *
 * Copyright (c) 2016 Artyom Tarasenko
 *
 * This code is licensed under the GNU GPL v3 or (at your option) any later
 * version.
 */

#include "qemu/osdep.h"
#include "hw/hw.h"
#include "hw/sysbus.h"
#include "qemu/timer.h"
#include "hw/timer/sun4v-rtc.h"
17
#include "trace.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31


#define TYPE_SUN4V_RTC "sun4v_rtc"
#define SUN4V_RTC(obj) OBJECT_CHECK(Sun4vRtc, (obj), TYPE_SUN4V_RTC)

typedef struct Sun4vRtc {
    SysBusDevice parent_obj;

    MemoryRegion iomem;
} Sun4vRtc;

static uint64_t sun4v_rtc_read(void *opaque, hwaddr addr,
                                unsigned size)
{
32
    uint64_t val = get_clock_realtime() / NANOSECONDS_PER_SECOND;
33 34 35 36
    if (!(addr & 4ULL)) {
        /* accessing the high 32 bits */
        val >>= 32;
    }
37
    trace_sun4v_rtc_read(addr, val);
38 39 40 41 42 43
    return val;
}

static void sun4v_rtc_write(void *opaque, hwaddr addr,
                             uint64_t val, unsigned size)
{
44
    trace_sun4v_rtc_read(addr, val);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}

static const MemoryRegionOps sun4v_rtc_ops = {
    .read = sun4v_rtc_read,
    .write = sun4v_rtc_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
};

void sun4v_rtc_init(hwaddr addr)
{
    DeviceState *dev;
    SysBusDevice *s;

    dev = qdev_create(NULL, TYPE_SUN4V_RTC);
    s = SYS_BUS_DEVICE(dev);

    qdev_init_nofail(dev);

    sysbus_mmio_map(s, 0, addr);
}

66
static void sun4v_rtc_realize(DeviceState *dev, Error **errp)
67
{
68
    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
69 70 71 72
    Sun4vRtc *s = SUN4V_RTC(dev);

    memory_region_init_io(&s->iomem, OBJECT(s), &sun4v_rtc_ops, s,
                          "sun4v-rtc", 0x08ULL);
73
    sysbus_init_mmio(sbd, &s->iomem);
74 75 76 77
}

static void sun4v_rtc_class_init(ObjectClass *klass, void *data)
{
78
    DeviceClass *dc = DEVICE_CLASS(klass);
79

80
    dc->realize = sun4v_rtc_realize;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

static const TypeInfo sun4v_rtc_info = {
    .name          = TYPE_SUN4V_RTC,
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(Sun4vRtc),
    .class_init    = sun4v_rtc_class_init,
};

static void sun4v_rtc_register_types(void)
{
    type_register_static(&sun4v_rtc_info);
}

type_init(sun4v_rtc_register_types)