sh_timer.c 8.9 KB
Newer Older
1 2 3 4 5 6 7
/*
 * SuperH Timer modules.
 *
 * Copyright (c) 2007 Magnus Damm
 * Based on arm_timer.c by Paul Brook
 * Copyright (c) 2005-2006 CodeSourcery.
 *
M
Matthew Fernandez 已提交
8
 * This code is licensed under the GPL.
9 10
 */

P
Peter Maydell 已提交
11
#include "qemu/osdep.h"
12
#include "hw/hw.h"
P
Paolo Bonzini 已提交
13
#include "hw/sh4/sh.h"
14
#include "qemu/timer.h"
15
#include "qemu/main-loop.h"
16
#include "exec/address-spaces.h"
17
#include "hw/ptimer.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31

//#define DEBUG_TIMER

#define TIMER_TCR_TPSC          (7 << 0)
#define TIMER_TCR_CKEG          (3 << 3)
#define TIMER_TCR_UNIE          (1 << 5)
#define TIMER_TCR_ICPE          (3 << 6)
#define TIMER_TCR_UNF           (1 << 8)
#define TIMER_TCR_ICPF          (1 << 9)
#define TIMER_TCR_RESERVED      (0x3f << 10)

#define TIMER_FEAT_CAPT   (1 << 0)
#define TIMER_FEAT_EXTCLK (1 << 1)

A
aurel32 已提交
32 33 34 35 36
#define OFFSET_TCOR   0
#define OFFSET_TCNT   1
#define OFFSET_TCR    2
#define OFFSET_TCPR   3

37 38 39 40 41 42 43 44
typedef struct {
    ptimer_state *timer;
    uint32_t tcnt;
    uint32_t tcor;
    uint32_t tcr;
    uint32_t tcpr;
    int freq;
    int int_level;
45
    int old_level;
46 47
    int feat;
    int enabled;
A
aurel32 已提交
48
    qemu_irq irq;
49 50 51 52 53 54
} sh_timer_state;

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

static void sh_timer_update(sh_timer_state *s)
{
55 56 57
    int new_level = s->int_level && (s->tcr & TIMER_TCR_UNIE);

    if (new_level != s->old_level)
A
aurel32 已提交
58
      qemu_set_irq (s->irq, new_level);
59 60 61

    s->old_level = s->int_level;
    s->int_level = new_level;
62 63
}

A
Avi Kivity 已提交
64
static uint32_t sh_timer_read(void *opaque, hwaddr offset)
65 66 67 68
{
    sh_timer_state *s = (sh_timer_state *)opaque;

    switch (offset >> 2) {
A
aurel32 已提交
69
    case OFFSET_TCOR:
70
        return s->tcor;
A
aurel32 已提交
71
    case OFFSET_TCNT:
72
        return ptimer_get_count(s->timer);
A
aurel32 已提交
73
    case OFFSET_TCR:
74
        return s->tcr | (s->int_level ? TIMER_TCR_UNF : 0);
A
aurel32 已提交
75
    case OFFSET_TCPR:
76 77 78
        if (s->feat & TIMER_FEAT_CAPT)
            return s->tcpr;
    default:
P
Paul Brook 已提交
79
        hw_error("sh_timer_read: Bad offset %x\n", (int)offset);
80 81 82 83
        return 0;
    }
}

A
Avi Kivity 已提交
84
static void sh_timer_write(void *opaque, hwaddr offset,
85 86 87 88 89 90
                            uint32_t value)
{
    sh_timer_state *s = (sh_timer_state *)opaque;
    int freq;

    switch (offset >> 2) {
A
aurel32 已提交
91
    case OFFSET_TCOR:
92 93 94
        s->tcor = value;
        ptimer_set_limit(s->timer, s->tcor, 0);
        break;
A
aurel32 已提交
95
    case OFFSET_TCNT:
96 97 98
        s->tcnt = value;
        ptimer_set_count(s->timer, s->tcnt);
        break;
A
aurel32 已提交
99
    case OFFSET_TCR:
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        if (s->enabled) {
            /* Pause the timer if it is running.  This may cause some
               inaccuracy dure to rounding, but avoids a whole lot of other
               messyness.  */
            ptimer_stop(s->timer);
        }
        freq = s->freq;
        /* ??? Need to recalculate expiry time after changing divisor.  */
        switch (value & TIMER_TCR_TPSC) {
        case 0: freq >>= 2; break;
        case 1: freq >>= 4; break;
        case 2: freq >>= 6; break;
        case 3: freq >>= 8; break;
        case 4: freq >>= 10; break;
	case 6:
	case 7: if (s->feat & TIMER_FEAT_EXTCLK) break;
P
Paul Brook 已提交
116
	default: hw_error("sh_timer_write: Reserved TPSC value\n"); break;
117 118 119 120 121 122
        }
        switch ((value & TIMER_TCR_CKEG) >> 3) {
	case 0: break;
        case 1:
        case 2:
        case 3: if (s->feat & TIMER_FEAT_EXTCLK) break;
P
Paul Brook 已提交
123
	default: hw_error("sh_timer_write: Reserved CKEG value\n"); break;
124 125 126 127 128
        }
        switch ((value & TIMER_TCR_ICPE) >> 6) {
	case 0: break;
        case 2:
        case 3: if (s->feat & TIMER_FEAT_CAPT) break;
P
Paul Brook 已提交
129
	default: hw_error("sh_timer_write: Reserved ICPE value\n"); break;
130 131 132 133 134 135 136
        }
	if ((value & TIMER_TCR_UNF) == 0)
            s->int_level = 0;

	value &= ~TIMER_TCR_UNF;

	if ((value & TIMER_TCR_ICPF) && (!(s->feat & TIMER_FEAT_CAPT)))
P
Paul Brook 已提交
137
            hw_error("sh_timer_write: Reserved ICPF value\n");
138 139 140 141

	value &= ~TIMER_TCR_ICPF; /* capture not supported */

	if (value & TIMER_TCR_RESERVED)
P
Paul Brook 已提交
142
            hw_error("sh_timer_write: Reserved TCR bits set\n");
143 144 145 146 147 148 149 150
        s->tcr = value;
        ptimer_set_limit(s->timer, s->tcor, 0);
        ptimer_set_freq(s->timer, freq);
        if (s->enabled) {
            /* Restart the timer if still enabled.  */
            ptimer_run(s->timer, 0);
        }
        break;
A
aurel32 已提交
151
    case OFFSET_TCPR:
152 153 154 155 156
        if (s->feat & TIMER_FEAT_CAPT) {
            s->tcpr = value;
	    break;
	}
    default:
P
Paul Brook 已提交
157
        hw_error("sh_timer_write: Bad offset %x\n", (int)offset);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    }
    sh_timer_update(s);
}

static void sh_timer_start_stop(void *opaque, int enable)
{
    sh_timer_state *s = (sh_timer_state *)opaque;

#ifdef DEBUG_TIMER
    printf("sh_timer_start_stop %d (%d)\n", enable, s->enabled);
#endif

    if (s->enabled && !enable) {
        ptimer_stop(s->timer);
    }
    if (!s->enabled && enable) {
        ptimer_run(s->timer, 0);
    }
    s->enabled = !!enable;

#ifdef DEBUG_TIMER
    printf("sh_timer_start_stop done %d\n", s->enabled);
#endif
}

static void sh_timer_tick(void *opaque)
{
    sh_timer_state *s = (sh_timer_state *)opaque;
    s->int_level = s->enabled;
    sh_timer_update(s);
}

A
aurel32 已提交
190
static void *sh_timer_init(uint32_t freq, int feat, qemu_irq irq)
191 192 193 194
{
    sh_timer_state *s;
    QEMUBH *bh;

195
    s = (sh_timer_state *)g_malloc0(sizeof(sh_timer_state));
196 197 198 199 200
    s->freq = freq;
    s->feat = feat;
    s->tcor = 0xffffffff;
    s->tcnt = 0xffffffff;
    s->tcpr = 0xdeadbeef;
A
aurel32 已提交
201
    s->tcr = 0;
202
    s->enabled = 0;
203
    s->irq = irq;
204 205

    bh = qemu_bh_new(sh_timer_tick, s);
206
    s->timer = ptimer_init(bh, PTIMER_POLICY_DEFAULT);
A
aurel32 已提交
207 208 209 210 211

    sh_timer_write(s, OFFSET_TCOR >> 2, s->tcor);
    sh_timer_write(s, OFFSET_TCNT >> 2, s->tcnt);
    sh_timer_write(s, OFFSET_TCPR >> 2, s->tcpr);
    sh_timer_write(s, OFFSET_TCR  >> 2, s->tcpr);
212 213 214 215 216
    /* ??? Save/restore.  */
    return s;
}

typedef struct {
B
Benoît Canet 已提交
217 218 219
    MemoryRegion iomem;
    MemoryRegion iomem_p4;
    MemoryRegion iomem_a7;
220 221 222 223 224 225 226
    void *timer[3];
    int level[3];
    uint32_t tocr;
    uint32_t tstr;
    int feat;
} tmu012_state;

A
Avi Kivity 已提交
227
static uint64_t tmu012_read(void *opaque, hwaddr offset,
B
Benoît Canet 已提交
228
                            unsigned size)
229 230 231 232 233 234 235 236 237
{
    tmu012_state *s = (tmu012_state *)opaque;

#ifdef DEBUG_TIMER
    printf("tmu012_read 0x%lx\n", (unsigned long) offset);
#endif

    if (offset >= 0x20) {
        if (!(s->feat & TMU012_FEAT_3CHAN))
P
Paul Brook 已提交
238
	    hw_error("tmu012_write: Bad channel offset %x\n", (int)offset);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        return sh_timer_read(s->timer[2], offset - 0x20);
    }

    if (offset >= 0x14)
        return sh_timer_read(s->timer[1], offset - 0x14);

    if (offset >= 0x08)
        return sh_timer_read(s->timer[0], offset - 0x08);

    if (offset == 4)
        return s->tstr;

    if ((s->feat & TMU012_FEAT_TOCR) && offset == 0)
        return s->tocr;

P
Paul Brook 已提交
254
    hw_error("tmu012_write: Bad offset %x\n", (int)offset);
255 256 257
    return 0;
}

A
Avi Kivity 已提交
258
static void tmu012_write(void *opaque, hwaddr offset,
B
Benoît Canet 已提交
259
                        uint64_t value, unsigned size)
260 261 262 263 264 265 266 267 268
{
    tmu012_state *s = (tmu012_state *)opaque;

#ifdef DEBUG_TIMER
    printf("tmu012_write 0x%lx 0x%08x\n", (unsigned long) offset, value);
#endif

    if (offset >= 0x20) {
        if (!(s->feat & TMU012_FEAT_3CHAN))
P
Paul Brook 已提交
269
	    hw_error("tmu012_write: Bad channel offset %x\n", (int)offset);
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        sh_timer_write(s->timer[2], offset - 0x20, value);
	return;
    }

    if (offset >= 0x14) {
        sh_timer_write(s->timer[1], offset - 0x14, value);
	return;
    }

    if (offset >= 0x08) {
        sh_timer_write(s->timer[0], offset - 0x08, value);
	return;
    }

    if (offset == 4) {
        sh_timer_start_stop(s->timer[0], value & (1 << 0));
        sh_timer_start_stop(s->timer[1], value & (1 << 1));
        if (s->feat & TMU012_FEAT_3CHAN)
            sh_timer_start_stop(s->timer[2], value & (1 << 2));
	else
            if (value & (1 << 2))
P
Paul Brook 已提交
291
                hw_error("tmu012_write: Bad channel\n");
292 293 294 295 296 297 298 299 300 301

	s->tstr = value;
	return;
    }

    if ((s->feat & TMU012_FEAT_TOCR) && offset == 0) {
        s->tocr = value & (1 << 0);
    }
}

B
Benoît Canet 已提交
302 303 304 305
static const MemoryRegionOps tmu012_ops = {
    .read = tmu012_read,
    .write = tmu012_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
306 307
};

A
Avi Kivity 已提交
308
void tmu012_init(MemoryRegion *sysmem, hwaddr base,
B
Benoît Canet 已提交
309
                 int feat, uint32_t freq,
A
aurel32 已提交
310 311
		 qemu_irq ch0_irq, qemu_irq ch1_irq,
		 qemu_irq ch2_irq0, qemu_irq ch2_irq1)
312 313 314 315
{
    tmu012_state *s;
    int timer_feat = (feat & TMU012_FEAT_EXTCLK) ? TIMER_FEAT_EXTCLK : 0;

316
    s = (tmu012_state *)g_malloc0(sizeof(tmu012_state));
317
    s->feat = feat;
318 319
    s->timer[0] = sh_timer_init(freq, timer_feat, ch0_irq);
    s->timer[1] = sh_timer_init(freq, timer_feat, ch1_irq);
320
    if (feat & TMU012_FEAT_3CHAN)
321 322
        s->timer[2] = sh_timer_init(freq, timer_feat | TIMER_FEAT_CAPT,
				    ch2_irq0); /* ch2_irq1 not supported */
B
Benoît Canet 已提交
323

324
    memory_region_init_io(&s->iomem, NULL, &tmu012_ops, s,
B
Benoît Canet 已提交
325 326
                          "timer", 0x100000000ULL);

327
    memory_region_init_alias(&s->iomem_p4, NULL, "timer-p4",
B
Benoît Canet 已提交
328 329 330
                             &s->iomem, 0, 0x1000);
    memory_region_add_subregion(sysmem, P4ADDR(base), &s->iomem_p4);

331
    memory_region_init_alias(&s->iomem_a7, NULL, "timer-a7",
B
Benoît Canet 已提交
332 333
                             &s->iomem, 0, 0x1000);
    memory_region_add_subregion(sysmem, A7ADDR(base), &s->iomem_a7);
334 335
    /* ??? Save/restore.  */
}