arm_timer.c 10.6 KB
Newer Older
1
/*
2 3 4 5 6
 * ARM PrimeCell Timer modules.
 *
 * Copyright (c) 2005-2006 CodeSourcery.
 * Written by Paul Brook
 *
M
Matthew Fernandez 已提交
7
 * This code is licensed under the GPL.
8 9
 */

10
#include "hw/sysbus.h"
11
#include "qemu/timer.h"
12
#include "qemu-common.h"
13 14
#include "hw/qdev.h"
#include "hw/ptimer.h"
15 16 17 18 19 20 21 22 23 24 25 26 27

/* Common timer implementation.  */

#define TIMER_CTRL_ONESHOT      (1 << 0)
#define TIMER_CTRL_32BIT        (1 << 1)
#define TIMER_CTRL_DIV1         (0 << 2)
#define TIMER_CTRL_DIV16        (1 << 2)
#define TIMER_CTRL_DIV256       (2 << 2)
#define TIMER_CTRL_IE           (1 << 5)
#define TIMER_CTRL_PERIODIC     (1 << 6)
#define TIMER_CTRL_ENABLE       (1 << 7)

typedef struct {
P
pbrook 已提交
28
    ptimer_state *timer;
29 30 31 32
    uint32_t control;
    uint32_t limit;
    int freq;
    int int_level;
P
pbrook 已提交
33
    qemu_irq irq;
34 35 36 37
} arm_timer_state;

/* Check all active timers, and schedule the next timer interrupt.  */

P
pbrook 已提交
38
static void arm_timer_update(arm_timer_state *s)
39 40 41
{
    /* Update interrupts.  */
    if (s->int_level && (s->control & TIMER_CTRL_IE)) {
P
pbrook 已提交
42
        qemu_irq_raise(s->irq);
43
    } else {
P
pbrook 已提交
44
        qemu_irq_lower(s->irq);
45 46 47
    }
}

A
Avi Kivity 已提交
48
static uint32_t arm_timer_read(void *opaque, hwaddr offset)
49 50 51 52 53 54 55 56
{
    arm_timer_state *s = (arm_timer_state *)opaque;

    switch (offset >> 2) {
    case 0: /* TimerLoad */
    case 6: /* TimerBGLoad */
        return s->limit;
    case 1: /* TimerValue */
P
pbrook 已提交
57
        return ptimer_get_count(s->timer);
58 59 60 61 62 63 64 65 66
    case 2: /* TimerControl */
        return s->control;
    case 4: /* TimerRIS */
        return s->int_level;
    case 5: /* TimerMIS */
        if ((s->control & TIMER_CTRL_IE) == 0)
            return 0;
        return s->int_level;
    default:
67 68
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: Bad offset %x\n", __func__, (int)offset);
69 70 71 72
        return 0;
    }
}

P
pbrook 已提交
73 74 75 76 77
/* Reset the timer limit after settings have changed.  */
static void arm_timer_recalibrate(arm_timer_state *s, int reload)
{
    uint32_t limit;

R
Rabin Vincent 已提交
78
    if ((s->control & (TIMER_CTRL_PERIODIC | TIMER_CTRL_ONESHOT)) == 0) {
P
pbrook 已提交
79 80 81 82 83 84 85 86 87 88 89 90
        /* Free running.  */
        if (s->control & TIMER_CTRL_32BIT)
            limit = 0xffffffff;
        else
            limit = 0xffff;
    } else {
          /* Periodic.  */
          limit = s->limit;
    }
    ptimer_set_limit(s->timer, limit, reload);
}

A
Avi Kivity 已提交
91
static void arm_timer_write(void *opaque, hwaddr offset,
92 93 94
                            uint32_t value)
{
    arm_timer_state *s = (arm_timer_state *)opaque;
P
pbrook 已提交
95
    int freq;
96 97 98 99

    switch (offset >> 2) {
    case 0: /* TimerLoad */
        s->limit = value;
P
pbrook 已提交
100
        arm_timer_recalibrate(s, 1);
101 102 103 104 105 106 107 108 109 110
        break;
    case 1: /* TimerValue */
        /* ??? Linux seems to want to write to this readonly register.
           Ignore it.  */
        break;
    case 2: /* TimerControl */
        if (s->control & TIMER_CTRL_ENABLE) {
            /* Pause the timer if it is running.  This may cause some
               inaccuracy dure to rounding, but avoids a whole lot of other
               messyness.  */
P
pbrook 已提交
111
            ptimer_stop(s->timer);
112 113
        }
        s->control = value;
P
pbrook 已提交
114
        freq = s->freq;
115 116
        /* ??? Need to recalculate expiry time after changing divisor.  */
        switch ((value >> 2) & 3) {
P
pbrook 已提交
117 118
        case 1: freq >>= 4; break;
        case 2: freq >>= 8; break;
119
        }
120
        arm_timer_recalibrate(s, s->control & TIMER_CTRL_ENABLE);
P
pbrook 已提交
121
        ptimer_set_freq(s->timer, freq);
122 123
        if (s->control & TIMER_CTRL_ENABLE) {
            /* Restart the timer if still enabled.  */
P
pbrook 已提交
124
            ptimer_run(s->timer, (s->control & TIMER_CTRL_ONESHOT) != 0);
125 126 127 128 129 130 131
        }
        break;
    case 3: /* TimerIntClr */
        s->int_level = 0;
        break;
    case 6: /* TimerBGLoad */
        s->limit = value;
P
pbrook 已提交
132
        arm_timer_recalibrate(s, 0);
133 134
        break;
    default:
135 136
        qemu_log_mask(LOG_GUEST_ERROR,
                      "%s: Bad offset %x\n", __func__, (int)offset);
137
    }
P
pbrook 已提交
138
    arm_timer_update(s);
139 140 141 142
}

static void arm_timer_tick(void *opaque)
{
P
pbrook 已提交
143 144 145
    arm_timer_state *s = (arm_timer_state *)opaque;
    s->int_level = 1;
    arm_timer_update(s);
146 147
}

J
Juan Quintela 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160
static const VMStateDescription vmstate_arm_timer = {
    .name = "arm_timer",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField[]) {
        VMSTATE_UINT32(control, arm_timer_state),
        VMSTATE_UINT32(limit, arm_timer_state),
        VMSTATE_INT32(int_level, arm_timer_state),
        VMSTATE_PTIMER(timer, arm_timer_state),
        VMSTATE_END_OF_LIST()
    }
};
P
pbrook 已提交
161

P
Paul Brook 已提交
162
static arm_timer_state *arm_timer_init(uint32_t freq)
163 164
{
    arm_timer_state *s;
P
pbrook 已提交
165
    QEMUBH *bh;
166

167
    s = (arm_timer_state *)g_malloc0(sizeof(arm_timer_state));
P
pbrook 已提交
168
    s->freq = freq;
169 170
    s->control = TIMER_CTRL_IE;

P
pbrook 已提交
171 172
    bh = qemu_bh_new(arm_timer_tick, s);
    s->timer = ptimer_init(bh);
J
Juan Quintela 已提交
173
    vmstate_register(NULL, -1, &vmstate_arm_timer, s);
174 175 176 177
    return s;
}

/* ARM PrimeCell SP804 dual timer module.
P
Peter Chubb 已提交
178 179 180
 * Docs at
 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0271d/index.html
*/
181 182

typedef struct {
P
Paul Brook 已提交
183
    SysBusDevice busdev;
A
Avi Kivity 已提交
184
    MemoryRegion iomem;
P
Paul Brook 已提交
185
    arm_timer_state *timer[2];
186
    uint32_t freq0, freq1;
187
    int level[2];
P
pbrook 已提交
188
    qemu_irq irq;
189 190
} sp804_state;

P
Peter Chubb 已提交
191 192 193 194 195 196 197
static const uint8_t sp804_ids[] = {
    /* Timer ID */
    0x04, 0x18, 0x14, 0,
    /* PrimeCell ID */
    0xd, 0xf0, 0x05, 0xb1
};

P
pbrook 已提交
198
/* Merge the IRQs from the two component devices.  */
199 200 201 202 203
static void sp804_set_irq(void *opaque, int irq, int level)
{
    sp804_state *s = (sp804_state *)opaque;

    s->level[irq] = level;
P
pbrook 已提交
204
    qemu_set_irq(s->irq, s->level[0] || s->level[1]);
205 206
}

A
Avi Kivity 已提交
207
static uint64_t sp804_read(void *opaque, hwaddr offset,
A
Avi Kivity 已提交
208
                           unsigned size)
209 210 211 212 213
{
    sp804_state *s = (sp804_state *)opaque;

    if (offset < 0x20) {
        return arm_timer_read(s->timer[0], offset);
P
Peter Chubb 已提交
214 215
    }
    if (offset < 0x40) {
216 217
        return arm_timer_read(s->timer[1], offset - 0x20);
    }
P
Peter Chubb 已提交
218 219 220 221 222 223 224 225 226 227

    /* TimerPeriphID */
    if (offset >= 0xfe0 && offset <= 0xffc) {
        return sp804_ids[(offset - 0xfe0) >> 2];
    }

    switch (offset) {
    /* Integration Test control registers, which we won't support */
    case 0xf00: /* TimerITCR */
    case 0xf04: /* TimerITOP (strictly write only but..) */
228 229 230
        qemu_log_mask(LOG_UNIMP,
                      "%s: integration test registers unimplemented\n",
                      __func__);
P
Peter Chubb 已提交
231 232 233
        return 0;
    }

234 235
    qemu_log_mask(LOG_GUEST_ERROR,
                  "%s: Bad offset %x\n", __func__, (int)offset);
P
Peter Chubb 已提交
236
    return 0;
237 238
}

A
Avi Kivity 已提交
239
static void sp804_write(void *opaque, hwaddr offset,
A
Avi Kivity 已提交
240
                        uint64_t value, unsigned size)
241 242 243 244 245
{
    sp804_state *s = (sp804_state *)opaque;

    if (offset < 0x20) {
        arm_timer_write(s->timer[0], offset, value);
P
Peter Chubb 已提交
246 247 248 249
        return;
    }

    if (offset < 0x40) {
250
        arm_timer_write(s->timer[1], offset - 0x20, value);
P
Peter Chubb 已提交
251
        return;
252
    }
P
Peter Chubb 已提交
253 254

    /* Technically we could be writing to the Test Registers, but not likely */
255 256
    qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %x\n",
                  __func__, (int)offset);
257 258
}

A
Avi Kivity 已提交
259 260 261 262
static const MemoryRegionOps sp804_ops = {
    .read = sp804_read,
    .write = sp804_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
263 264
};

J
Juan Quintela 已提交
265 266 267 268 269 270 271 272 273 274
static const VMStateDescription vmstate_sp804 = {
    .name = "sp804",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField[]) {
        VMSTATE_INT32_ARRAY(level, sp804_state, 2),
        VMSTATE_END_OF_LIST()
    }
};
P
pbrook 已提交
275

276
static int sp804_init(SysBusDevice *dev)
277
{
P
Paul Brook 已提交
278
    sp804_state *s = FROM_SYSBUS(sp804_state, dev);
P
pbrook 已提交
279
    qemu_irq *qi;
280

P
pbrook 已提交
281
    qi = qemu_allocate_irqs(sp804_set_irq, s, 2);
P
Paul Brook 已提交
282
    sysbus_init_irq(dev, &s->irq);
283 284
    s->timer[0] = arm_timer_init(s->freq0);
    s->timer[1] = arm_timer_init(s->freq1);
P
Paul Brook 已提交
285 286
    s->timer[0]->irq = qi[0];
    s->timer[1]->irq = qi[1];
A
Avi Kivity 已提交
287
    memory_region_init_io(&s->iomem, &sp804_ops, s, "sp804", 0x1000);
288
    sysbus_init_mmio(dev, &s->iomem);
J
Juan Quintela 已提交
289
    vmstate_register(&dev->qdev, -1, &vmstate_sp804, s);
290
    return 0;
291 292 293 294 295
}

/* Integrator/CP timer module.  */

typedef struct {
P
Paul Brook 已提交
296
    SysBusDevice busdev;
A
Avi Kivity 已提交
297
    MemoryRegion iomem;
P
Paul Brook 已提交
298
    arm_timer_state *timer[3];
299 300
} icp_pit_state;

A
Avi Kivity 已提交
301
static uint64_t icp_pit_read(void *opaque, hwaddr offset,
A
Avi Kivity 已提交
302
                             unsigned size)
303 304 305 306 307 308
{
    icp_pit_state *s = (icp_pit_state *)opaque;
    int n;

    /* ??? Don't know the PrimeCell ID for this device.  */
    n = offset >> 8;
309
    if (n > 2) {
310
        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad timer %d\n", __func__, n);
P
Paul Brook 已提交
311
    }
312 313 314 315

    return arm_timer_read(s->timer[n], offset & 0xff);
}

A
Avi Kivity 已提交
316
static void icp_pit_write(void *opaque, hwaddr offset,
A
Avi Kivity 已提交
317
                          uint64_t value, unsigned size)
318 319 320 321 322
{
    icp_pit_state *s = (icp_pit_state *)opaque;
    int n;

    n = offset >> 8;
323
    if (n > 2) {
324
        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad timer %d\n", __func__, n);
P
Paul Brook 已提交
325
    }
326 327 328 329

    arm_timer_write(s->timer[n], offset & 0xff, value);
}

A
Avi Kivity 已提交
330 331 332 333
static const MemoryRegionOps icp_pit_ops = {
    .read = icp_pit_read,
    .write = icp_pit_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
334 335
};

336
static int icp_pit_init(SysBusDevice *dev)
337
{
P
Paul Brook 已提交
338
    icp_pit_state *s = FROM_SYSBUS(icp_pit_state, dev);
339 340

    /* Timer 0 runs at the system clock speed (40MHz).  */
P
Paul Brook 已提交
341
    s->timer[0] = arm_timer_init(40000000);
342
    /* The other two timers run at 1MHz.  */
P
Paul Brook 已提交
343 344 345 346 347 348
    s->timer[1] = arm_timer_init(1000000);
    s->timer[2] = arm_timer_init(1000000);

    sysbus_init_irq(dev, &s->timer[0]->irq);
    sysbus_init_irq(dev, &s->timer[1]->irq);
    sysbus_init_irq(dev, &s->timer[2]->irq);
349

A
Avi Kivity 已提交
350
    memory_region_init_io(&s->iomem, &icp_pit_ops, s, "icp_pit", 0x1000);
351
    sysbus_init_mmio(dev, &s->iomem);
P
pbrook 已提交
352 353
    /* This device has no state to save/restore.  The component timers will
       save themselves.  */
354
    return 0;
355
}
P
Paul Brook 已提交
356

357 358 359 360 361 362 363
static void icp_pit_class_init(ObjectClass *klass, void *data)
{
    SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);

    sdc->init = icp_pit_init;
}

364
static const TypeInfo icp_pit_info = {
365 366 367 368 369 370 371 372 373 374
    .name          = "integrator_pit",
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(icp_pit_state),
    .class_init    = icp_pit_class_init,
};

static Property sp804_properties[] = {
    DEFINE_PROP_UINT32("freq0", sp804_state, freq0, 1000000),
    DEFINE_PROP_UINT32("freq1", sp804_state, freq1, 1000000),
    DEFINE_PROP_END_OF_LIST(),
375 376 377 378 379
};

static void sp804_class_init(ObjectClass *klass, void *data)
{
    SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
380
    DeviceClass *k = DEVICE_CLASS(klass);
381 382

    sdc->init = sp804_init;
383
    k->props = sp804_properties;
384 385
}

386
static const TypeInfo sp804_info = {
387 388 389 390
    .name          = "sp804",
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(sp804_state),
    .class_init    = sp804_class_init,
391 392
};

A
Andreas Färber 已提交
393
static void arm_timer_register_types(void)
P
Paul Brook 已提交
394
{
395 396
    type_register_static(&icp_pit_info);
    type_register_static(&sp804_info);
P
Paul Brook 已提交
397 398
}

A
Andreas Färber 已提交
399
type_init(arm_timer_register_types)