arm_timer.c 8.8 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
 */

P
Paul Brook 已提交
10
#include "sysbus.h"
P
pbrook 已提交
11
#include "qemu-timer.h"
12 13 14 15 16 17 18 19 20 21 22 23 24

/* 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 已提交
25
    ptimer_state *timer;
26 27 28 29
    uint32_t control;
    uint32_t limit;
    int freq;
    int int_level;
P
pbrook 已提交
30
    qemu_irq irq;
31 32 33 34
} arm_timer_state;

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

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

A
Anthony Liguori 已提交
45
static uint32_t arm_timer_read(void *opaque, target_phys_addr_t offset)
46 47 48 49 50 51 52 53
{
    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 已提交
54
        return ptimer_get_count(s->timer);
55 56 57 58 59 60 61 62 63
    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:
P
Paul Brook 已提交
64
        hw_error("arm_timer_read: Bad offset %x\n", (int)offset);
65 66 67 68
        return 0;
    }
}

P
pbrook 已提交
69 70 71 72 73
/* 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 已提交
74
    if ((s->control & (TIMER_CTRL_PERIODIC | TIMER_CTRL_ONESHOT)) == 0) {
P
pbrook 已提交
75 76 77 78 79 80 81 82 83 84 85 86
        /* 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
Anthony Liguori 已提交
87
static void arm_timer_write(void *opaque, target_phys_addr_t offset,
88 89 90
                            uint32_t value)
{
    arm_timer_state *s = (arm_timer_state *)opaque;
P
pbrook 已提交
91
    int freq;
92 93 94 95

    switch (offset >> 2) {
    case 0: /* TimerLoad */
        s->limit = value;
P
pbrook 已提交
96
        arm_timer_recalibrate(s, 1);
97 98 99 100 101 102 103 104 105 106
        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 已提交
107
            ptimer_stop(s->timer);
108 109
        }
        s->control = value;
P
pbrook 已提交
110
        freq = s->freq;
111 112
        /* ??? Need to recalculate expiry time after changing divisor.  */
        switch ((value >> 2) & 3) {
P
pbrook 已提交
113 114
        case 1: freq >>= 4; break;
        case 2: freq >>= 8; break;
115
        }
116
        arm_timer_recalibrate(s, s->control & TIMER_CTRL_ENABLE);
P
pbrook 已提交
117
        ptimer_set_freq(s->timer, freq);
118 119
        if (s->control & TIMER_CTRL_ENABLE) {
            /* Restart the timer if still enabled.  */
P
pbrook 已提交
120
            ptimer_run(s->timer, (s->control & TIMER_CTRL_ONESHOT) != 0);
121 122 123 124 125 126 127
        }
        break;
    case 3: /* TimerIntClr */
        s->int_level = 0;
        break;
    case 6: /* TimerBGLoad */
        s->limit = value;
P
pbrook 已提交
128
        arm_timer_recalibrate(s, 0);
129 130
        break;
    default:
P
Paul Brook 已提交
131
        hw_error("arm_timer_write: Bad offset %x\n", (int)offset);
132
    }
P
pbrook 已提交
133
    arm_timer_update(s);
134 135 136 137
}

static void arm_timer_tick(void *opaque)
{
P
pbrook 已提交
138 139 140
    arm_timer_state *s = (arm_timer_state *)opaque;
    s->int_level = 1;
    arm_timer_update(s);
141 142
}

J
Juan Quintela 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155
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 已提交
156

P
Paul Brook 已提交
157
static arm_timer_state *arm_timer_init(uint32_t freq)
158 159
{
    arm_timer_state *s;
P
pbrook 已提交
160
    QEMUBH *bh;
161

162
    s = (arm_timer_state *)g_malloc0(sizeof(arm_timer_state));
P
pbrook 已提交
163
    s->freq = freq;
164 165
    s->control = TIMER_CTRL_IE;

P
pbrook 已提交
166 167
    bh = qemu_bh_new(arm_timer_tick, s);
    s->timer = ptimer_init(bh);
J
Juan Quintela 已提交
168
    vmstate_register(NULL, -1, &vmstate_arm_timer, s);
169 170 171 172 173
    return s;
}

/* ARM PrimeCell SP804 dual timer module.
   Docs for this device don't seem to be publicly available.  This
P
pbrook 已提交
174
   implementation is based on guesswork, the linux kernel sources and the
175 176 177
   Integrator/CP timer modules.  */

typedef struct {
P
Paul Brook 已提交
178
    SysBusDevice busdev;
A
Avi Kivity 已提交
179
    MemoryRegion iomem;
P
Paul Brook 已提交
180
    arm_timer_state *timer[2];
181
    int level[2];
P
pbrook 已提交
182
    qemu_irq irq;
183 184
} sp804_state;

P
pbrook 已提交
185
/* Merge the IRQs from the two component devices.  */
186 187 188 189 190
static void sp804_set_irq(void *opaque, int irq, int level)
{
    sp804_state *s = (sp804_state *)opaque;

    s->level[irq] = level;
P
pbrook 已提交
191
    qemu_set_irq(s->irq, s->level[0] || s->level[1]);
192 193
}

A
Avi Kivity 已提交
194 195
static uint64_t sp804_read(void *opaque, target_phys_addr_t offset,
                           unsigned size)
196 197 198 199 200 201 202 203 204 205 206
{
    sp804_state *s = (sp804_state *)opaque;

    /* ??? Don't know the PrimeCell ID for this device.  */
    if (offset < 0x20) {
        return arm_timer_read(s->timer[0], offset);
    } else {
        return arm_timer_read(s->timer[1], offset - 0x20);
    }
}

A
Anthony Liguori 已提交
207
static void sp804_write(void *opaque, target_phys_addr_t offset,
A
Avi Kivity 已提交
208
                        uint64_t value, unsigned size)
209 210 211 212 213 214 215 216 217 218
{
    sp804_state *s = (sp804_state *)opaque;

    if (offset < 0x20) {
        arm_timer_write(s->timer[0], offset, value);
    } else {
        arm_timer_write(s->timer[1], offset - 0x20, value);
    }
}

A
Avi Kivity 已提交
219 220 221 222
static const MemoryRegionOps sp804_ops = {
    .read = sp804_read,
    .write = sp804_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
223 224
};

J
Juan Quintela 已提交
225 226 227 228 229 230 231 232 233 234
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 已提交
235

236
static int sp804_init(SysBusDevice *dev)
237
{
P
Paul Brook 已提交
238
    sp804_state *s = FROM_SYSBUS(sp804_state, dev);
P
pbrook 已提交
239
    qemu_irq *qi;
240

P
pbrook 已提交
241
    qi = qemu_allocate_irqs(sp804_set_irq, s, 2);
P
Paul Brook 已提交
242
    sysbus_init_irq(dev, &s->irq);
243 244
    /* ??? The timers are actually configurable between 32kHz and 1MHz, but
       we don't implement that.  */
P
Paul Brook 已提交
245 246 247 248
    s->timer[0] = arm_timer_init(1000000);
    s->timer[1] = arm_timer_init(1000000);
    s->timer[0]->irq = qi[0];
    s->timer[1]->irq = qi[1];
A
Avi Kivity 已提交
249 250
    memory_region_init_io(&s->iomem, &sp804_ops, s, "sp804", 0x1000);
    sysbus_init_mmio_region(dev, &s->iomem);
J
Juan Quintela 已提交
251
    vmstate_register(&dev->qdev, -1, &vmstate_sp804, s);
252
    return 0;
253 254 255 256 257 258
}


/* Integrator/CP timer module.  */

typedef struct {
P
Paul Brook 已提交
259
    SysBusDevice busdev;
A
Avi Kivity 已提交
260
    MemoryRegion iomem;
P
Paul Brook 已提交
261
    arm_timer_state *timer[3];
262 263
} icp_pit_state;

A
Avi Kivity 已提交
264 265
static uint64_t icp_pit_read(void *opaque, target_phys_addr_t offset,
                             unsigned size)
266 267 268 269 270 271
{
    icp_pit_state *s = (icp_pit_state *)opaque;
    int n;

    /* ??? Don't know the PrimeCell ID for this device.  */
    n = offset >> 8;
P
Paul Brook 已提交
272 273 274
    if (n > 3) {
        hw_error("sp804_read: Bad timer %d\n", n);
    }
275 276 277 278

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

A
Anthony Liguori 已提交
279
static void icp_pit_write(void *opaque, target_phys_addr_t offset,
A
Avi Kivity 已提交
280
                          uint64_t value, unsigned size)
281 282 283 284 285
{
    icp_pit_state *s = (icp_pit_state *)opaque;
    int n;

    n = offset >> 8;
P
Paul Brook 已提交
286 287 288
    if (n > 3) {
        hw_error("sp804_write: Bad timer %d\n", n);
    }
289 290 291 292

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

A
Avi Kivity 已提交
293 294 295 296
static const MemoryRegionOps icp_pit_ops = {
    .read = icp_pit_read,
    .write = icp_pit_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
297 298
};

299
static int icp_pit_init(SysBusDevice *dev)
300
{
P
Paul Brook 已提交
301
    icp_pit_state *s = FROM_SYSBUS(icp_pit_state, dev);
302 303

    /* Timer 0 runs at the system clock speed (40MHz).  */
P
Paul Brook 已提交
304
    s->timer[0] = arm_timer_init(40000000);
305
    /* The other two timers run at 1MHz.  */
P
Paul Brook 已提交
306 307 308 309 310 311
    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);
312

A
Avi Kivity 已提交
313 314
    memory_region_init_io(&s->iomem, &icp_pit_ops, s, "icp_pit", 0x1000);
    sysbus_init_mmio_region(dev, &s->iomem);
P
pbrook 已提交
315 316
    /* This device has no state to save/restore.  The component timers will
       save themselves.  */
317
    return 0;
318
}
P
Paul Brook 已提交
319 320 321 322 323 324 325 326

static void arm_timer_register_devices(void)
{
    sysbus_register_dev("integrator_pit", sizeof(icp_pit_state), icp_pit_init);
    sysbus_register_dev("sp804", sizeof(sp804_state), sp804_init);
}

device_init(arm_timer_register_devices)