pl031.c 7.2 KB
Newer Older
P
pbrook 已提交
1 2 3 4 5 6 7 8 9
/*
 * ARM AMBA PrimeCell PL031 RTC
 *
 * Copyright (c) 2007 CodeSourcery
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
10 11
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
P
pbrook 已提交
12 13
 */

14
#include "hw/sysbus.h"
15
#include "qemu/timer.h"
16
#include "sysemu/sysemu.h"
P
pbrook 已提交
17 18 19 20

//#define DEBUG_PL031

#ifdef DEBUG_PL031
21 22
#define DPRINTF(fmt, ...) \
do { printf("pl031: " fmt , ## __VA_ARGS__); } while (0)
P
pbrook 已提交
23
#else
24
#define DPRINTF(fmt, ...) do {} while(0)
P
pbrook 已提交
25 26 27 28 29 30 31 32 33 34 35
#endif

#define RTC_DR      0x00    /* Data read register */
#define RTC_MR      0x04    /* Match register */
#define RTC_LR      0x08    /* Data load register */
#define RTC_CR      0x0c    /* Control register */
#define RTC_IMSC    0x10    /* Interrupt mask and set register */
#define RTC_RIS     0x14    /* Raw interrupt status register */
#define RTC_MIS     0x18    /* Masked interrupt status register */
#define RTC_ICR     0x1c    /* Interrupt clear register */

A
Andreas Färber 已提交
36 37 38
#define TYPE_PL031 "pl031"
#define PL031(obj) OBJECT_CHECK(PL031State, (obj), TYPE_PL031)

39
typedef struct PL031State {
A
Andreas Färber 已提交
40 41
    SysBusDevice parent_obj;

A
Avi Kivity 已提交
42
    MemoryRegion iomem;
P
pbrook 已提交
43 44 45
    QEMUTimer *timer;
    qemu_irq irq;

46 47 48 49 50
    /* Needed to preserve the tick_count across migration, even if the
     * absolute value of the rtc_clock is different on the source and
     * destination.
     */
    uint32_t tick_offset_vmstate;
P
pbrook 已提交
51 52 53 54 55 56 57
    uint32_t tick_offset;

    uint32_t mr;
    uint32_t lr;
    uint32_t cr;
    uint32_t im;
    uint32_t is;
58
} PL031State;
P
pbrook 已提交
59 60 61 62 63 64

static const unsigned char pl031_id[] = {
    0x31, 0x10, 0x14, 0x00,         /* Device ID        */
    0x0d, 0xf0, 0x05, 0xb1          /* Cell ID      */
};

65
static void pl031_update(PL031State *s)
P
pbrook 已提交
66 67 68 69 70 71
{
    qemu_set_irq(s->irq, s->is & s->im);
}

static void pl031_interrupt(void * opaque)
{
72
    PL031State *s = (PL031State *)opaque;
P
pbrook 已提交
73

74
    s->is = 1;
P
pbrook 已提交
75 76 77 78
    DPRINTF("Alarm raised\n");
    pl031_update(s);
}

79
static uint32_t pl031_get_count(PL031State *s)
P
pbrook 已提交
80
{
81
    int64_t now = qemu_clock_get_ns(rtc_clock);
82
    return s->tick_offset + now / get_ticks_per_sec();
P
pbrook 已提交
83 84
}

85
static void pl031_set_alarm(PL031State *s)
P
pbrook 已提交
86 87 88 89 90
{
    uint32_t ticks;

    /* The timer wraps around.  This subtraction also wraps in the same way,
       and gives correct results when alarm < now_ticks.  */
91
    ticks = s->mr - pl031_get_count(s);
P
pbrook 已提交
92 93 94 95 96
    DPRINTF("Alarm set in %ud ticks\n", ticks);
    if (ticks == 0) {
        qemu_del_timer(s->timer);
        pl031_interrupt(s);
    } else {
97
        int64_t now = qemu_clock_get_ns(rtc_clock);
98
        qemu_mod_timer(s->timer, now + (int64_t)ticks * get_ticks_per_sec());
P
pbrook 已提交
99 100 101
    }
}

A
Avi Kivity 已提交
102
static uint64_t pl031_read(void *opaque, hwaddr offset,
A
Avi Kivity 已提交
103
                           unsigned size)
P
pbrook 已提交
104
{
105
    PL031State *s = (PL031State *)opaque;
P
pbrook 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

    if (offset >= 0xfe0  &&  offset < 0x1000)
        return pl031_id[(offset - 0xfe0) >> 2];

    switch (offset) {
    case RTC_DR:
        return pl031_get_count(s);
    case RTC_MR:
        return s->mr;
    case RTC_IMSC:
        return s->im;
    case RTC_RIS:
        return s->is;
    case RTC_LR:
        return s->lr;
    case RTC_CR:
        /* RTC is permanently enabled.  */
        return 1;
    case RTC_MIS:
        return s->is & s->im;
    case RTC_ICR:
P
Peter Maydell 已提交
127 128 129
        qemu_log_mask(LOG_GUEST_ERROR,
                      "pl031: read of write-only register at offset 0x%x\n",
                      (int)offset);
P
pbrook 已提交
130 131
        break;
    default:
P
Peter Maydell 已提交
132 133
        qemu_log_mask(LOG_GUEST_ERROR,
                      "pl031_read: Bad offset 0x%x\n", (int)offset);
P
pbrook 已提交
134 135 136 137 138 139
        break;
    }

    return 0;
}

A
Avi Kivity 已提交
140
static void pl031_write(void * opaque, hwaddr offset,
A
Avi Kivity 已提交
141
                        uint64_t value, unsigned size)
P
pbrook 已提交
142
{
143
    PL031State *s = (PL031State *)opaque;
P
pbrook 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160


    switch (offset) {
    case RTC_LR:
        s->tick_offset += value - pl031_get_count(s);
        pl031_set_alarm(s);
        break;
    case RTC_MR:
        s->mr = value;
        pl031_set_alarm(s);
        break;
    case RTC_IMSC:
        s->im = value & 1;
        DPRINTF("Interrupt mask %d\n", s->im);
        pl031_update(s);
        break;
    case RTC_ICR:
161
        /* The PL031 documentation (DDI0224B) states that the interrupt is
P
pbrook 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175
           cleared when bit 0 of the written value is set.  However the
           arm926e documentation (DDI0287B) states that the interrupt is
           cleared when any value is written.  */
        DPRINTF("Interrupt cleared");
        s->is = 0;
        pl031_update(s);
        break;
    case RTC_CR:
        /* Written value is ignored.  */
        break;

    case RTC_DR:
    case RTC_MIS:
    case RTC_RIS:
P
Peter Maydell 已提交
176 177 178
        qemu_log_mask(LOG_GUEST_ERROR,
                      "pl031: write to read-only register at offset 0x%x\n",
                      (int)offset);
P
pbrook 已提交
179 180 181
        break;

    default:
P
Peter Maydell 已提交
182 183
        qemu_log_mask(LOG_GUEST_ERROR,
                      "pl031_write: Bad offset 0x%x\n", (int)offset);
P
pbrook 已提交
184 185 186 187
        break;
    }
}

A
Avi Kivity 已提交
188 189 190 191
static const MemoryRegionOps pl031_ops = {
    .read = pl031_read,
    .write = pl031_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
P
pbrook 已提交
192 193
};

194
static int pl031_init(SysBusDevice *dev)
P
pbrook 已提交
195
{
A
Andreas Färber 已提交
196
    PL031State *s = PL031(dev);
197
    struct tm tm;
P
pbrook 已提交
198

199
    memory_region_init_io(&s->iomem, OBJECT(s), &pl031_ops, s, "pl031", 0x1000);
200
    sysbus_init_mmio(dev, &s->iomem);
P
pbrook 已提交
201

P
Paul Brook 已提交
202
    sysbus_init_irq(dev, &s->irq);
203
    qemu_get_timedate(&tm, 0);
204 205
    s->tick_offset = mktimegm(&tm) -
        qemu_clock_get_ns(rtc_clock) / get_ticks_per_sec();
P
pbrook 已提交
206

207
    s->timer = timer_new_ns(rtc_clock, pl031_interrupt, s);
208
    return 0;
P
pbrook 已提交
209
}
P
Paul Brook 已提交
210

211 212
static void pl031_pre_save(void *opaque)
{
213
    PL031State *s = opaque;
214 215 216

    /* tick_offset is base_time - rtc_clock base time.  Instead, we want to
     * store the base time relative to the vm_clock for backwards-compatibility.  */
217
    int64_t delta = qemu_clock_get_ns(rtc_clock) - qemu_get_clock_ns(vm_clock);
218 219 220
    s->tick_offset_vmstate = s->tick_offset + delta / get_ticks_per_sec();
}

221 222
static int pl031_post_load(void *opaque, int version_id)
{
223
    PL031State *s = opaque;
224

225
    int64_t delta = qemu_clock_get_ns(rtc_clock) - qemu_get_clock_ns(vm_clock);
226
    s->tick_offset = s->tick_offset_vmstate - delta / get_ticks_per_sec();
227 228 229 230 231 232 233 234
    pl031_set_alarm(s);
    return 0;
}

static const VMStateDescription vmstate_pl031 = {
    .name = "pl031",
    .version_id = 1,
    .minimum_version_id = 1,
235
    .pre_save = pl031_pre_save,
236 237
    .post_load = pl031_post_load,
    .fields = (VMStateField[]) {
238 239 240 241 242 243
        VMSTATE_UINT32(tick_offset_vmstate, PL031State),
        VMSTATE_UINT32(mr, PL031State),
        VMSTATE_UINT32(lr, PL031State),
        VMSTATE_UINT32(cr, PL031State),
        VMSTATE_UINT32(im, PL031State),
        VMSTATE_UINT32(is, PL031State),
244 245 246 247
        VMSTATE_END_OF_LIST()
    }
};

248 249
static void pl031_class_init(ObjectClass *klass, void *data)
{
250
    DeviceClass *dc = DEVICE_CLASS(klass);
251 252 253
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);

    k->init = pl031_init;
254 255
    dc->no_user = 1;
    dc->vmsd = &vmstate_pl031;
256 257
}

258
static const TypeInfo pl031_info = {
A
Andreas Färber 已提交
259
    .name          = TYPE_PL031,
260
    .parent        = TYPE_SYS_BUS_DEVICE,
261
    .instance_size = sizeof(PL031State),
262
    .class_init    = pl031_class_init,
P
Peter Maydell 已提交
263 264
};

A
Andreas Färber 已提交
265
static void pl031_register_types(void)
P
Paul Brook 已提交
266
{
267
    type_register_static(&pl031_info);
P
Paul Brook 已提交
268 269
}

A
Andreas Färber 已提交
270
type_init(pl031_register_types)