arm_timer.c 8.9 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9
 * ARM PrimeCell Timer modules.
 *
 * Copyright (c) 2005-2006 CodeSourcery.
 * Written by Paul Brook
 *
 * This code is licenced under the GPL.
 */

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

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

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

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

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

P
pbrook 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/* Reset the timer limit after settings have changed.  */
static void arm_timer_recalibrate(arm_timer_state *s, int reload)
{
    uint32_t limit;

    if ((s->control & TIMER_CTRL_PERIODIC) == 0) {
        /* 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);
}

89 90 91 92
static void arm_timer_write(void *opaque, target_phys_addr_t offset,
                            uint32_t value)
{
    arm_timer_state *s = (arm_timer_state *)opaque;
P
pbrook 已提交
93
    int freq;
94 95 96 97

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

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

P
pbrook 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
static void arm_timer_save(QEMUFile *f, void *opaque)
{
    arm_timer_state *s = (arm_timer_state *)opaque;
    qemu_put_be32(f, s->control);
    qemu_put_be32(f, s->limit);
    qemu_put_be32(f, s->int_level);
    qemu_put_ptimer(f, s->timer);
}

static int arm_timer_load(QEMUFile *f, void *opaque, int version_id)
{
    arm_timer_state *s = (arm_timer_state *)opaque;

    if (version_id != 1)
        return -EINVAL;

    s->control = qemu_get_be32(f);
    s->limit = qemu_get_be32(f);
    s->int_level = qemu_get_be32(f);
    qemu_get_ptimer(f, s->timer);
    return 0;
}

P
pbrook 已提交
169
static void *arm_timer_init(uint32_t freq, qemu_irq irq)
170 171
{
    arm_timer_state *s;
P
pbrook 已提交
172
    QEMUBH *bh;
173 174 175

    s = (arm_timer_state *)qemu_mallocz(sizeof(arm_timer_state));
    s->irq = irq;
P
pbrook 已提交
176
    s->freq = freq;
177 178
    s->control = TIMER_CTRL_IE;

P
pbrook 已提交
179 180
    bh = qemu_bh_new(arm_timer_tick, s);
    s->timer = ptimer_init(bh);
P
pbrook 已提交
181
    register_savevm("arm_timer", -1, 1, arm_timer_save, arm_timer_load, s);
182 183 184 185 186
    return s;
}

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

typedef struct {
    void *timer[2];
    int level[2];
P
pbrook 已提交
193
    qemu_irq irq;
194 195
} sp804_state;

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

    s->level[irq] = level;
P
pbrook 已提交
202
    qemu_set_irq(s->irq, s->level[0] || s->level[1]);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
}

static uint32_t sp804_read(void *opaque, target_phys_addr_t offset)
{
    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);
    }
}

static void sp804_write(void *opaque, target_phys_addr_t offset,
                        uint32_t value)
{
    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);
    }
}

static CPUReadMemoryFunc *sp804_readfn[] = {
   sp804_read,
   sp804_read,
   sp804_read
};

static CPUWriteMemoryFunc *sp804_writefn[] = {
   sp804_write,
   sp804_write,
   sp804_write
};

P
pbrook 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
static void sp804_save(QEMUFile *f, void *opaque)
{
    sp804_state *s = (sp804_state *)opaque;
    qemu_put_be32(f, s->level[0]);
    qemu_put_be32(f, s->level[1]);
}

static int sp804_load(QEMUFile *f, void *opaque, int version_id)
{
    sp804_state *s = (sp804_state *)opaque;

    if (version_id != 1)
        return -EINVAL;

    s->level[0] = qemu_get_be32(f);
    s->level[1] = qemu_get_be32(f);
    return 0;
}

P
pbrook 已提交
260
void sp804_init(uint32_t base, qemu_irq irq)
261 262 263
{
    int iomemtype;
    sp804_state *s;
P
pbrook 已提交
264
    qemu_irq *qi;
265 266

    s = (sp804_state *)qemu_mallocz(sizeof(sp804_state));
P
pbrook 已提交
267
    qi = qemu_allocate_irqs(sp804_set_irq, s, 2);
268 269 270
    s->irq = irq;
    /* ??? The timers are actually configurable between 32kHz and 1MHz, but
       we don't implement that.  */
P
pbrook 已提交
271 272
    s->timer[0] = arm_timer_init(1000000, qi[0]);
    s->timer[1] = arm_timer_init(1000000, qi[1]);
273 274
    iomemtype = cpu_register_io_memory(0, sp804_readfn,
                                       sp804_writefn, s);
P
pbrook 已提交
275
    cpu_register_physical_memory(base, 0x00001000, iomemtype);
P
pbrook 已提交
276
    register_savevm("sp804", -1, 1, sp804_save, sp804_load, s);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
}


/* Integrator/CP timer module.  */

typedef struct {
    void *timer[3];
} icp_pit_state;

static uint32_t icp_pit_read(void *opaque, target_phys_addr_t offset)
{
    icp_pit_state *s = (icp_pit_state *)opaque;
    int n;

    /* ??? Don't know the PrimeCell ID for this device.  */
    n = offset >> 8;
    if (n > 3)
        cpu_abort(cpu_single_env, "sp804_read: Bad timer %d\n", n);

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

static void icp_pit_write(void *opaque, target_phys_addr_t offset,
                          uint32_t value)
{
    icp_pit_state *s = (icp_pit_state *)opaque;
    int n;

    n = offset >> 8;
    if (n > 3)
        cpu_abort(cpu_single_env, "sp804_write: Bad timer %d\n", n);

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


static CPUReadMemoryFunc *icp_pit_readfn[] = {
   icp_pit_read,
   icp_pit_read,
   icp_pit_read
};

static CPUWriteMemoryFunc *icp_pit_writefn[] = {
   icp_pit_write,
   icp_pit_write,
   icp_pit_write
};

P
pbrook 已提交
325
void icp_pit_init(uint32_t base, qemu_irq *pic, int irq)
326 327 328 329 330 331
{
    int iomemtype;
    icp_pit_state *s;

    s = (icp_pit_state *)qemu_mallocz(sizeof(icp_pit_state));
    /* Timer 0 runs at the system clock speed (40MHz).  */
P
pbrook 已提交
332
    s->timer[0] = arm_timer_init(40000000, pic[irq]);
333
    /* The other two timers run at 1MHz.  */
P
pbrook 已提交
334 335
    s->timer[1] = arm_timer_init(1000000, pic[irq + 1]);
    s->timer[2] = arm_timer_init(1000000, pic[irq + 2]);
336 337 338

    iomemtype = cpu_register_io_memory(0, icp_pit_readfn,
                                       icp_pit_writefn, s);
P
pbrook 已提交
339
    cpu_register_physical_memory(base, 0x00001000, iomemtype);
P
pbrook 已提交
340 341
    /* This device has no state to save/restore.  The component timers will
       save themselves.  */
342
}